#include "IMFMultipart.h"
#include "Log.h"
#include "Exception.h"

#include <unistd.h>

namespace coreutils {

   IMFMultipart::IMFMultipart(int fd, MString boundary) {
      char ch = 0;
      int rc = 0;
      while((ch != '\r') && (ch != '\n')) {
	 rc = ::read(fd, &ch, 1);
	 if((ch != '\r') && (ch != '\n'))
	   boundary.write(ch);
      }
      if(ch == '\r') {
	 rc = ::read(fd, &ch, 1);
      }	   
      while(1) {
	 IMFFormData *formData = new IMFFormData(fd, boundary);
	 
	 rc = ::read(fd, &ch, 1);
	 if(ch == '-') {
	    rc = ::read(fd, &ch, 1);
	    if(ch == '-') {
	       break;
	    }	    	    
	 } else if(ch == '\r') {
	    rc = ::read(fd, &ch, 1);
	    if(ch != '\n') 
	      coreutils::Exception("expecting '\n' in data stream.");	     
	 }
      }
   }
   
}
