#include "IMFBoundary.h"
#include "Exception.h"
#include <unistd.h>

namespace coreutils {

   IMFBoundary(StreamReader &reader, ZString boundary) : boundary(boundary) {
      
   }
      
   int IMFBoundary::parse() {
      switch(state) {
       case PREFIX:
	 if(buffer.remaining() > 3)          
	   if(reader.nextIf("\r\n--")) 
	     state = BOUNDARY;
       case BOUNDARY:
	 while(buffer.getTokenInclude(""));
	 if(buffer.eod())
	   return 2;
	 else if(buffer.remaining() > 1)
	   if(buffer.ifNext("--"))
	     state = LAST_BOUNDARY;
       case LAST_BOUNDARY:
	 return 0;	 
      }
      	
   }
            
}