26#define STRPR_MAXBUF 1024
33vector<string>
split(
string const& input,
char delimiter)
40 while (getline(ss, item, delimiter)) {
41 parts.push_back(item);
47string trim(
string const& line,
string const& whitespace)
49 size_t const begin = line.find_first_not_of(whitespace);
50 if (begin == string::npos)
54 size_t const end = line.find_last_not_of(whitespace);
55 size_t const range = end - begin + 1;
57 return line.substr(begin, range);
61 string const& whitespace,
64 string result =
trim(line, whitespace);
66 size_t begin = result.find_first_of(whitespace);
67 while (begin != string::npos)
69 size_t const end = result.find_first_not_of(whitespace, begin);
70 size_t const range = end - begin;
71 result.replace(begin, range, fill);
72 size_t const newBegin = begin + fill.length();
73 begin = result.find_first_of(whitespace, newBegin);
79string pad(
string const& input,
size_t num,
char fill,
bool right)
81 string result = input;
83 if (input.size() >= num)
return result;
85 string pad(num - input.size(), fill);
86 if (right)
return result +
pad;
87 else return pad + result;
90string strpr(
const char* format, ...)
95 va_start(args, format);
96 vsprintf(buffer, format, args);
105 vector<size_t>
const& colSize,
106 vector<string>
const& colName,
107 vector<string>
const& colInfo,
108 char const& commentChar)
110 size_t const stdWidth = 80;
112 if (!(colSize.size() == colName.size() &&
113 colName.size() == colInfo.size()))
115 throw runtime_error(
"ERROR: Could not create file header, column sizes"
116 " are not equal.\n");
118 size_t numCols = colSize.size();
121 string sepLine(stdWidth, commentChar);
124 string startStr(1, commentChar);
127 h.push_back(sepLine);
128 for (vector<string>::const_iterator it = title.begin();
129 it != title.end(); ++it)
131 h.push_back(startStr +
' ' + (*it) +
'\n');
133 h.push_back(sepLine);
136 size_t widthNames = 0;
137 for (vector<string>::const_iterator it = colName.begin();
138 it != colName.end(); ++it)
140 widthNames = max(widthNames, it->size());
146 << left << setw(4) <<
"Col" <<
' '
147 << left << setw(widthNames) <<
"Name" <<
' '
148 << left <<
"Description\n";
149 h.push_back(s.str());
150 h.push_back(sepLine);
151 for (
size_t i = 0; i < numCols; ++i)
156 << left << setw(4) << i + 1 <<
' '
157 << left << setw(widthNames) << colName.at(i) <<
' '
158 << colInfo.at(i) <<
'\n';
159 h.push_back(s.str());
163 size_t widthData = 0;
164 for (vector<size_t>::const_iterator it = colSize.begin();
165 it != colSize.end(); ++it)
167 widthData += (*it) + 1;
171 if (widthData > stdWidth)
174 h.push_back(
string(widthData, commentChar) +
"\n");
178 h.push_back(sepLine);
185 for (
size_t i = 0; i < numCols; ++i)
187 size_t n = colSize.at(i);
189 s <<
' ' << right << setw(n) << i + 1;
192 h.push_back(s.str());
198 for (
size_t i = 0; i < numCols; ++i)
200 size_t n = colSize.at(i);
202 string name = colName.at(i);
205 name.erase(n, string::npos);
207 s <<
' ' << right << setw(n) << name;
210 h.push_back(s.str());
213 h.push_back(
string(widthData, commentChar) +
"\n");
220 for (vector<string>::const_iterator it = lines.begin();
221 it != lines.end(); ++it)
231 for (vector<string>::const_iterator it = lines.begin();
232 it != lines.end(); ++it)
234 fprintf(file,
"%s", it->c_str());
241 vector<size_t> columns,
244 map<size_t, vector<double>> result;
246 sort(columns.begin(), columns.end());
247 for (
auto col : columns)
249 result[col] = vector<double>();
253 file.open(fileName.c_str());
256 throw runtime_error(
"ERROR: Could not open file: \"" + fileName
260 while (getline(file, line))
262 if (line.size() == 0)
continue;
263 if (line.at(0) != comment)
266 for (
auto col : columns)
268 if (col >= splitLine.size())
270 result[col].push_back(
271 std::numeric_limits<double>::quiet_NaN());
275 result[col].push_back(atof(splitLine.at(col).c_str()));
302 if (m & 1) result *= x;
string pad(string const &input, size_t num, char fill, bool right)
string strpr(const char *format,...)
String version of printf function.
vector< string > createFileHeader(vector< string > const &title, vector< size_t > const &colSize, vector< string > const &colName, vector< string > const &colInfo, char const &commentChar)
string trim(string const &line, string const &whitespace)
Remove leading and trailing whitespaces from string.
vector< string > split(string const &input, char delimiter)
Split string at each delimiter.
map< size_t, vector< double > > readColumnsFromFile(string fileName, vector< size_t > columns, char comment)
string reduce(string const &line, string const &whitespace, string const &fill)
Replace multiple whitespaces with fill.
double pow_int(double x, int n)
Integer version of power function, "fast exponentiation algorithm".
void appendLinesToFile(ofstream &file, vector< string > const lines)
Append multiple lines of strings to open file stream.