48 lines
2.1 KiB
C++
48 lines
2.1 KiB
C++
#include "__write.h"
|
|
#include "Exception.h"
|
|
#include "Operand.h"
|
|
#include <iostream>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
namespace jet {
|
|
|
|
__write::__write(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) {
|
|
output = false;
|
|
int mode = 0;
|
|
int len;
|
|
processContainer(container);
|
|
if(!variableDefined("file"))
|
|
throw coreutils::Exception("write tag must have file defined.");
|
|
resolveKeyword("file");
|
|
if(!variableDefined("expr") && variableDefined("value") && hasContainer)
|
|
throw coreutils::Exception("write tag cannot have both value and a container.");
|
|
if(variableDefined("expr") && !variableDefined("value") && hasContainer)
|
|
throw coreutils::Exception("write tag cannot have both expr and a container.");
|
|
if(variableDefined("expr") && variableDefined("value") && !hasContainer)
|
|
throw coreutils::Exception("write tag cannot have both expr and value.");
|
|
if(!variableDefined("expr") && !variableDefined("value") && !hasContainer)
|
|
throw coreutils::Exception("write tag must have a value, expr or a container.");
|
|
if(!variableDefined("mode"))
|
|
throw coreutils::Exception("write tag must have a mode keyword.");
|
|
resolveKeyword("mode");
|
|
if(variables["mode"] == "append")
|
|
mode = O_APPEND;
|
|
else if(variables["mode"] == "overwrite")
|
|
mode = O_TRUNC;
|
|
else
|
|
throw coreutils::Exception("mode keyword must be 'overwrite' or 'append'.");
|
|
int fd = open(variables["file"].c_str(), mode, 0644); // TODO: Need to add O_CREAT and AUTH flags.
|
|
if(hasContainer && !evaluate)
|
|
len = write(fd, container.getData(), container.getLength());
|
|
else if(hasContainer && evaluate)
|
|
len = write(fd, out.getData(), out.getLength());
|
|
else if(!hasContainer && variableDefined("value"))
|
|
len = write(fd, variables["value"].getData(), variables["value"].getLength());
|
|
else if(!hasContainer && variableDefined("expr"))
|
|
len = write(fd, variables["expr"].getData(), variables["expr"].getLength());
|
|
close(fd);
|
|
}
|
|
|
|
}
|