Commit 4941a556 authored by kulvait's avatar kulvait

New function to append files into single file

void concatenateTextFiles(const std::string& outputFile, bool overwrite, std::initializer_list<std::string> inputFiles);
This function concatenates the files from the inputFiles to the outputFile.
parent 74c81af7
......@@ -41,7 +41,7 @@ else()
set(COR_LIB "libmkl_rt.so")
endif()
find_path(MKL_INCLUDE_DIR NAMES mkl_lapacke.h HINTS /opt/intel/compilers_and_libraries_2019.0.117/linux/mkl/include /opt/intel/compilers_and_libraries_2018.2.199/linux/mkl/include)
find_path(MKL_INCLUDE_DIR NAMES mkl_lapacke.h HINTS /opt/intel/compilers_and_libraries_2019.4.243/linux/mkl/include /opt/intel/compilers_and_libraries_2019.0.117/linux/mkl/include)
set(MKL_INCLUDE_DIRS ${MKL_INCLUDE_DIR})
message("Found MKL_INCLUDE_DIRS ${MKL_INCLUDE_DIR}")
get_filename_component(MKL_BASE_DIR ${MKL_INCLUDE_DIR} DIRECTORY)
......
......@@ -31,5 +31,24 @@ namespace io {
std::string getParent(const std::string& path);
std::string getBasename(const std::string& path);
std::string fileToString(const std::string& fileName);
/**
* Concatenate text files given by inputFiles into the file given by outputFile.
* Intended to create compilation of cl files into the file allsources.cl
* Example:
* @code
* std::string xpath = "/path/to/files"
* clFile = io::xprintf("%s/opencl/allsources.cl", xpath.c_str());
* io::concatenateTextFiles(clFile, true,
* { io::xprintf("%s/opencl/utils.cl", xpath.c_str()),
* io::xprintf("%s/opencl/projector.cl", xpath.c_str()),
* io::xprintf("%s/opencl/backprojector.cl", xpath.c_str()) });
*
* @endcode
*
* @param outputFile Output file string.
* @param overwrite Should overwrite output file.
* @param inputFiles Input files as a std::initializer_list should be strings put into {} brackets.
*/
void concatenateTextFiles(const std::string& outputFile, bool overwrite, std::initializer_list<std::string> inputFiles);
} // namespace io
} // namespace CTL
......@@ -106,7 +106,8 @@ namespace io {
}
}
void writeBytesFrom(std::string fileName, uint64_t fromPosition, uint8_t* buffer, uint64_t numBytes)
void
writeBytesFrom(std::string fileName, uint64_t fromPosition, uint8_t* buffer, uint64_t numBytes)
{
if(CHAR_BIT != 8)
{
......@@ -263,5 +264,32 @@ namespace io {
return std::string(bytes.data(), fileSize);
}
void concatenateTextFiles(const std::string& outputFile,
bool overwrite,
std::initializer_list<std::string> inputFiles)
{
if(fileExists(outputFile) && !overwrite)
{
std::string ERR = io::xprintf("Ouptut file %s exists and overwrite is not set.",
outputFile.c_str());
LOGE << ERR;
throw std::ios_base::failure(ERR);
}
std::ofstream ofs(outputFile, std::ios_base::binary | std::ios::trunc);
for(std::string f : inputFiles)
{
if(!fileExists(f))
{
std::string ERR
= io::xprintf("One of the files to concatenate %s does not exist!", f.c_str());
LOGE << ERR;
throw std::ios_base::failure(ERR);
}
std::ifstream ifs(f, std::ios_base::binary);
ofs << ifs.rdbuf();
ifs.close();
}
ofs.close();
}
} // namespace io
} // namespace CTL
// Logging, testing
#include "catch.hpp"
#include <plog/Log.h>
// Standard libs
#include <cmath>
#include <iostream>
// Internal libs
#include "CTIOL.h"
using namespace CTL;
TEST_CASE("TEST: concatenateTextFiles function.", "rawop.h")
{
std::string fileA = "../tests/testFiles/A";
std::string fileB = "../tests/testFiles/B";
std::string outputFile = "/tmp/AB";
io::concatenateTextFiles(outputFile, true, { fileA, fileB });
std::string content = io::fileToString(outputFile);
REQUIRE(content == "A\nB\n");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment