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);
106 word[0] = toupper(word[0]);
112 vector<size_t>
const& colSize,
113 vector<string>
const& colName,
114 vector<string>
const& colInfo,
115 char const& commentChar)
117 size_t const stdWidth = 80;
119 if (!(colSize.size() == colName.size() &&
120 colName.size() == colInfo.size()))
122 throw runtime_error(
"ERROR: Could not create file header, column sizes"
123 " are not equal.\n");
125 size_t numCols = colSize.size();
128 string sepLine(stdWidth, commentChar);
131 string startStr(1, commentChar);
134 h.push_back(sepLine);
135 for (vector<string>::const_iterator it = title.begin();
136 it != title.end(); ++it)
138 h.push_back(startStr +
' ' + (*it) +
'\n');
140 h.push_back(sepLine);
143 size_t widthNames = 0;
144 for (vector<string>::const_iterator it = colName.begin();
145 it != colName.end(); ++it)
147 widthNames = max(widthNames, it->size());
153 << left << setw(4) <<
"Col" <<
' '
154 << left << setw(widthNames) <<
"Name" <<
' '
155 << left <<
"Description\n";
156 h.push_back(s.str());
157 h.push_back(sepLine);
158 for (
size_t i = 0; i < numCols; ++i)
163 << left << setw(4) << i + 1 <<
' '
164 << left << setw(widthNames) << colName.at(i) <<
' '
165 << colInfo.at(i) <<
'\n';
166 h.push_back(s.str());
170 size_t widthData = 0;
171 for (vector<size_t>::const_iterator it = colSize.begin();
172 it != colSize.end(); ++it)
174 widthData += (*it) + 1;
178 if (widthData > stdWidth)
181 h.push_back(
string(widthData, commentChar) +
"\n");
185 h.push_back(sepLine);
192 for (
size_t i = 0; i < numCols; ++i)
194 size_t n = colSize.at(i);
196 s <<
' ' << right << setw(n) << i + 1;
199 h.push_back(s.str());
205 for (
size_t i = 0; i < numCols; ++i)
207 size_t n = colSize.at(i);
209 string name = colName.at(i);
212 name.erase(n, string::npos);
214 s <<
' ' << right << setw(n) << name;
217 h.push_back(s.str());
220 h.push_back(
string(widthData, commentChar) +
"\n");
227 for (vector<string>::const_iterator it = lines.begin();
228 it != lines.end(); ++it)
238 for (vector<string>::const_iterator it = lines.begin();
239 it != lines.end(); ++it)
241 fprintf(file,
"%s", it->c_str());
248 vector<size_t> columns,
251 map<size_t, vector<double>> result;
253 sort(columns.begin(), columns.end());
254 for (
auto col : columns)
256 result[col] = vector<double>();
260 file.open(fileName.c_str());
263 throw runtime_error(
"ERROR: Could not open file: \"" + fileName
267 while (getline(file, line))
269 if (line.size() == 0)
continue;
270 if (line.at(0) != comment)
273 for (
auto col : columns)
275 if (col >= splitLine.size())
277 result[col].push_back(
278 std::numeric_limits<double>::quiet_NaN());
282 result[col].push_back(atof(splitLine.at(col).c_str()));
309 if (m & 1) result *= x;
string pad(string const &input, size_t num, char fill, bool right)
string cap(string word)
Capitalize first letter of word.
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.