#include "__stream.h"
#include "Exception.h"
#include <unistd.h>
#include <fcntl.h>

namespace jet {
   
   __stream::__stream(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, this) {
      output = false;
      if(!global.cgi)
	throw coreutils::Exception("must enable cgi mode on jet tag to use stream tag.");
      if(!keywordDefined("name"))
	throw coreutils::Exception("stream tag must have a file name to stream.");
      global.outputHeaders();
      int len;
      char buffer[1024];
      int fd = open(keywords["name"].c_str(), O_RDONLY);
      do {
	 len = read(fd, &buffer, 1024);
	 std::cout << buffer;
      } while (len > 0);
   }
}

