1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/***********************************************/
/**
* @file fileStringTable.cpp
*
* @brief Read/write table of strings.
*
* @author Torsten Mayer-Guerr
* @date 2017-12-03
*
*/
/***********************************************/
#define DOCSTRING_FILEFORMAT_StringList
#define DOCSTRING_FILEFORMAT_StringTable
#include "base/import.h"
#include "inputOutput/fileArchive.h"
#include "files/fileFormatRegister.h"
#include "fileStringTable.h"
GROOPS_REGISTER_FILEFORMAT(StringList, "stringList")
GROOPS_REGISTER_FILEFORMAT(StringTable, "stringTable")
/***********************************************/
static Bool stripComments(std::istream &stream)
{
try
{
char c;
if(!(stream>>c))
return FALSE;
stream.putback(c);
if(c != '#')
return TRUE;
std::string line;
std::getline(stream, line); // skip rest of line
return stripComments(stream);
}
catch(std::exception &e)
{
GROOPS_RETHROW(e)
}
}
/***********************************************/
void readFileStringTable(const FileName &fileName, std::vector<std::vector<std::string>> &x)
{
try
{
InFile file(fileName);
while(stripComments(file))
{
std::string line;
std::getline(file, line);
std::stringstream ss(line);
std::vector<std::string> dataLine;
while(stripComments(ss))
{
std::string data;
ss>>data;
if(data.at(0) == '"') // string in quotes?
{
if(data.size() == 1)
data += ss.get();
while(data.back() != '"')
data += ss.get();
data = data.substr(1, data.size()-2);
}
dataLine.push_back(data);
}
if(dataLine.size())
x.push_back(dataLine);
}
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("filename=<"+fileName.str()+">", e)
}
}
/***********************************************/
void writeFileStringTable(const FileName &fileName, const std::vector<std::vector<std::string>> &x)
{
try
{
OutFile file(fileName);
for(const auto &line : x)
{
for(UInt i=0; i<line.size(); i++)
{
if((!line.at(i).empty()) && (line.at(i).find_first_of(" \t\n#") == std::string::npos))
file<<line.at(i); // string without special characters
else
file<<"\""<<line.at(i)<<"\""; // string in quotes
if(i < line.size()-1) // no space at the end of a line
file<<" ";
}
file<<std::endl;
}
}
catch(std::exception &e)
{
GROOPS_RETHROW_EXTRA("filename=<"+fileName.str()+">", e)
}
}
/***********************************************/
|