diff options
Diffstat (limited to 'src/uscxml/plugins/element/file/FileElement.cpp')
-rw-r--r-- | src/uscxml/plugins/element/file/FileElement.cpp | 215 |
1 files changed, 117 insertions, 98 deletions
diff --git a/src/uscxml/plugins/element/file/FileElement.cpp b/src/uscxml/plugins/element/file/FileElement.cpp index 2287eed..a630a8a 100644 --- a/src/uscxml/plugins/element/file/FileElement.cpp +++ b/src/uscxml/plugins/element/file/FileElement.cpp @@ -1,3 +1,22 @@ +/** + * @file + * @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + #include "FileElement.h" #include <glog/logging.h> #include <stdio.h> @@ -13,7 +32,7 @@ namespace uscxml { #ifdef BUILD_AS_PLUGINS PLUMA_CONNECTOR -bool connect(pluma::Host& host) { +bool pluginConnect(pluma::Host& host) { host.add( new FileElementProvider() ); return true; } @@ -34,11 +53,11 @@ void FileElement::enterElement(const Arabica::DOM::Node<std::string>& node) { } _givenUrl = (HAS_ATTR(node, "url") ? ATTR(node, "url") : _interpreter->getDataModel().evalAsString(ATTR(node, "urlexpr"))); - std::string sandBoxStr = (HAS_ATTR(node, "sandbox") ? ATTR(node, "sandbox") : "on"); - if (boost::iequals(sandBoxStr, "off") || boost::iequals(sandBoxStr, "false") || boost::iequals(sandBoxStr, "no")) { - _sandBoxed = false; - } - + std::string sandBoxStr = (HAS_ATTR(node, "sandbox") ? ATTR(node, "sandbox") : "on"); + if (boost::iequals(sandBoxStr, "off") || boost::iequals(sandBoxStr, "false") || boost::iequals(sandBoxStr, "no")) { + _sandBoxed = false; + } + if (HAS_ATTR(node, "operation")) { std::string operation = ATTR(node, "operation"); if (boost::iequals(operation, "read") || boost::iequals(operation, "load")) { @@ -54,7 +73,7 @@ void FileElement::enterElement(const Arabica::DOM::Node<std::string>& node) { } else { _operation = READ; } - + // callback is only needed for reading std::string callback; if (_operation == READ) { @@ -64,7 +83,7 @@ void FileElement::enterElement(const Arabica::DOM::Node<std::string>& node) { } callback = (HAS_ATTR(node, "callback") ? ATTR(node, "callback") : _interpreter->getDataModel().evalAsString(ATTR(node, "callbackexpr"))); } - + std::string contentStr; char* content = NULL; size_t contentSize = 0; @@ -110,111 +129,111 @@ void FileElement::enterElement(const Arabica::DOM::Node<std::string>& node) { LOG(ERROR) << "Given URL is absolute with sandboxing enabled."; return; } - + if (_sandBoxed) _actualUrl.toAbsolute(URL::getResourceDir()); - + _filename = _actualUrl.path(); - + std::string writeMode; switch (_operation) { - case APPEND: - writeMode = "a+"; - case WRITE: { - if (writeMode.length() == 0) - writeMode = "w+"; - - FILE *fp; - fp = fopen(_filename.c_str(), writeMode.c_str()); - if (fp == NULL) { - LOG(ERROR) << "Error opening '" << _filename << "' for writing: " << strerror(errno); + case APPEND: + writeMode = "a+"; + case WRITE: { + if (writeMode.length() == 0) + writeMode = "w+"; + + FILE *fp; + fp = fopen(_filename.c_str(), writeMode.c_str()); + if (fp == NULL) { + LOG(ERROR) << "Error opening '" << _filename << "' for writing: " << strerror(errno); + } + + if (content && contentSize > 0) { + size_t written = fwrite(content, 1, contentSize, fp); + if (written != contentSize) { + LOG(ERROR) << "Error writing to '" << _filename << "': " << strerror(errno); + return; } - - if (content && contentSize > 0) { - size_t written = fwrite(content, 1, contentSize, fp); - if (written != contentSize) { - LOG(ERROR) << "Error writing to '" << _filename << "': " << strerror(errno); - return; - } - } else if (contentStr.length() > 0) { - size_t written = fwrite(contentStr.c_str(), contentStr.length(), 1, fp); - if (written < 1) { - LOG(ERROR) << "Error writing to '" << _filename << "': " << strerror(errno); - } - } else { - LOG(WARNING) << "Nothing to write to '" << _filename; + } else if (contentStr.length() > 0) { + size_t written = fwrite(contentStr.c_str(), contentStr.length(), 1, fp); + if (written < 1) { + LOG(ERROR) << "Error writing to '" << _filename << "': " << strerror(errno); } - fclose(fp); - break; + } else { + LOG(WARNING) << "Nothing to write to '" << _filename; } - case READ: { - struct stat fileStat; - int err = stat(_filename.c_str(), &fileStat); - if (err < 0) { - LOG(ERROR) << "Cannot stat file '" << _filename << "': " << strerror(errno); + fclose(fp); + break; + } + case READ: { + struct stat fileStat; + int err = stat(_filename.c_str(), &fileStat); + if (err < 0) { + LOG(ERROR) << "Cannot stat file '" << _filename << "': " << strerror(errno); + return; + } + + Event event; + event.name = callback; + event.data.compound["file"].compound["name"] = Data(_filename, Data::VERBATIM); + event.data.compound["file"].compound["mtime"] = toStr(fileStat.st_mtime); + event.data.compound["file"].compound["ctime"] = toStr(fileStat.st_ctime); + event.data.compound["file"].compound["atime"] = toStr(fileStat.st_atime); + event.data.compound["file"].compound["size"] = toStr(fileStat.st_size); + + + FILE *fp; + fp = fopen(_filename.c_str(), "r"); + + fseek (fp, 0, SEEK_END); + size_t filesize = ftell(fp); + rewind (fp); + + char* fileContents = (char*)malloc(filesize); + size_t read = fread(fileContents, 1, filesize, fp); + fclose(fp); + if (read != filesize) { + LOG(ERROR) << "Error reading from '" << _filename << "': " << strerror(errno); + return; + } + + switch (_type) { + case BINARY: + event.data.compound["content"] = Data(fileContents, fileStat.st_size, 1); + break; + case TEXT: + event.data.compound["content"] = Data(fileContents, Data::VERBATIM); + free(fileContents); + break; + case JSON: { + Data json = Data::fromJSON(fileContents); + free(fileContents); + if (!json) { + LOG(ERROR) << "Cannot parse contents of " << _filename << " as JSON"; return; } - - Event event; - event.name = callback; - event.data.compound["file"].compound["name"] = Data(_filename, Data::VERBATIM); - event.data.compound["file"].compound["mtime"] = toStr(fileStat.st_mtime); - event.data.compound["file"].compound["ctime"] = toStr(fileStat.st_ctime); - event.data.compound["file"].compound["atime"] = toStr(fileStat.st_atime); - event.data.compound["file"].compound["size"] = toStr(fileStat.st_size); - - - FILE *fp; - fp = fopen(_filename.c_str(), "r"); - - fseek (fp, 0, SEEK_END); - size_t filesize = ftell(fp); - rewind (fp); - - char* fileContents = (char*)malloc(filesize); - size_t read = fread(fileContents, 1, filesize, fp); - fclose(fp); - if (read != filesize) { - LOG(ERROR) << "Error reading from '" << _filename << "': " << strerror(errno); + event.data.compound["content"] = json; + break; + } + case XML: { + NameSpacingParser parser = NameSpacingParser::fromXML(fileContents); + if (parser.errorsReported()) { + LOG(ERROR) << "Cannot parse contents of " << _filename << " as XML"; return; } - - switch (_type) { - case BINARY: - event.data.compound["content"] = Data(fileContents, fileStat.st_size, 1); - break; - case TEXT: - event.data.compound["content"] = Data(fileContents, Data::VERBATIM); - free(fileContents); - break; - case JSON: { - Data json = Data::fromJSON(fileContents); - free(fileContents); - if (!json) { - LOG(ERROR) << "Cannot parse contents of " << _filename << " as JSON"; - return; - } - event.data.compound["content"] = json; - break; - } - case XML: { - NameSpacingParser parser = NameSpacingParser::fromXML(fileContents); - if (parser.errorsReported()) { - LOG(ERROR) << "Cannot parse contents of " << _filename << " as XML"; - return; - } - event.dom = parser.getDocument().getDocumentElement(); - break; - } - } - _interpreter->receive(event); + event.dom = parser.getDocument().getDocumentElement(); break; } + } + _interpreter->receive(event); + break; + } } - - - - + + + + } void FileElement::exitElement(const Arabica::DOM::Node<std::string>& node) { |