From ab840f41154f01d85fec769da693035149689c39 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Sun, 20 Apr 2014 20:37:39 +0200 Subject: Replaced c-style file reading with std::ifstream * there is no reason for performing system-calls instead of using the features offered by the standard library in this situation --- CMakeLists.txt | 1 - src/function/read_file.cc | 13 ++++++++---- src/utility.cc | 54 ----------------------------------------------- src/utility.h | 15 ------------- 4 files changed, 9 insertions(+), 74 deletions(-) delete mode 100644 src/utility.cc delete mode 100644 src/utility.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4421841..c87ca68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,6 @@ include_directories( add_executable( test test.cc - src/utility.cc src/transformer_facade.cc src/function/read_file.cc src/function/read_xml_file.cc diff --git a/src/function/read_file.cc b/src/function/read_file.cc index dcbff25..ea7a758 100644 --- a/src/function/read_file.cc +++ b/src/function/read_file.cc @@ -1,6 +1,6 @@ #include "read_file.h" -#include "utility.h" +#include namespace InputXSLT { @@ -33,10 +33,15 @@ xalan::XObjectPtr FunctionReadFile::execute( fileName.end() ); + std::ifstream file(fileName); + + std::string content( + (std::istreambuf_iterator(file)), + (std::istreambuf_iterator()) + ); + return executionContext.getXObjectFactory().createString( - xalan::XalanDOMString( - InputXSLT::readFile(fileName).data() - ) + xalan::XalanDOMString(content.data()) ); } diff --git a/src/utility.cc b/src/utility.cc deleted file mode 100644 index fb6fe61..0000000 --- a/src/utility.cc +++ /dev/null @@ -1,54 +0,0 @@ -#include "utility.h" - -#include -#include -#include - -#include -#include - -namespace { - -const int OpenFlags = O_RDONLY; -const mode_t OpenMode = S_IRUSR | S_IWUSR; - -} - -namespace InputXSLT { - -std::string readFile(const std::string& path) { - int descriptor( - open(path.data(), OpenFlags, OpenMode) - ); - - if ( descriptor == -1 ) { - close(descriptor); - - return "io error"; - } else { - struct stat info; - fstat(descriptor, &info); - const std::size_t size(info.st_size); - - char* const buffer(new char[size]); - - ssize_t readSize(read( - descriptor, - static_cast(buffer), - size - )); - - close(descriptor); - - std::string content( - buffer, - readSize - ); - - delete[] buffer; - - return content; - } -} - -} diff --git a/src/utility.h b/src/utility.h deleted file mode 100644 index ee39a25..0000000 --- a/src/utility.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef INPUTXSLT_SRC_UTILITY_H_ -#define INPUTXSLT_SRC_UTILITY_H_ - -#include - -namespace xalanc_1_11 { } -namespace xalan = xalanc_1_11; - -namespace InputXSLT { - -std::string readFile(const std::string&); - -} - -#endif // INPUTXSLT_SRC_UTILITY_H_ -- cgit v1.2.3