22#if !defined(LOOS_UTILS_HPP)
36#include <boost/algorithm/string.hpp>
37#include <boost/any.hpp>
38#include <boost/lexical_cast.hpp>
39#include <boost/program_options.hpp>
43#include <loos_defs.hpp>
44#include <exceptions.hpp>
46#include <pdb_remarks.hpp>
47#include <LineReader.hpp>
56 boost::tuple<std::string, std::string> splitFilename(
const std::string &filename);
59 std::string
getNextLine(std::istream &is,
int *lineno);
76 std::istringstream iss(reader.
line());
79 data.push_back(datum);
90 return (readVector<T>(lr));
97 std::ifstream ifs(fname.c_str());
99 return (readVector<T>(lr));
108 template <
typename T>
111 std::vector<std::vector<T>> table;
115 if (reader.
line().empty())
118 std::istringstream iss(reader.
line());
122 row.push_back(datum);
123 table.push_back(row);
129 template <
typename T>
133 return (readTable<T>(lr));
137 template <
typename T>
138 std::vector<std::vector<T>>
readTable(
const std::string &fname)
140 std::ifstream ifs(fname.c_str());
142 return (readTable<T>(lr));
167 template <
typename T>
168 std::vector<T>
parseRange(
const std::string &text,
const T endpoint = 0)
174 std::vector<T> indices;
175 std::istringstream is(text);
180 indices.push_back(a);
185 bool is_negative =
false;
187 if (is.peek() ==
'*')
194 if (is.peek() ==
'-')
200 if (is.fail() || sep !=
':')
201 throw(
ParseError(
"Could not parse range (1) " + text));
216 throw(
ParseError(
"Could not parse range (2) " + text));
218 if (is.peek() ==
'*')
227 throw(
ParseError(
"Could not parse range (3) " + text));
232 if (a > b && !is_negative)
233 throw(
ParseError(
"You must use a negative step to count down: " + text));
234 if (a < b && is_negative)
235 throw(
ParseError(
"You must use a postive step to count up: " + text));
237 throw(
ParseError(
"Thou shalt only use non-zero step sizes: " + text));
243 for (i = a; i > b; i -= c)
244 indices.push_back(i);
245 if (a < -a && b == 0)
246 indices.push_back(0);
248 indices.push_back(i);
251 for (T i = a; i <= b; i += c)
252 indices.push_back(i);
265 template <
typename T>
268 std::vector<std::string> terms;
270 std::insert_iterator<std::set<T>> ii(indices, indices.begin());
272 boost::split(terms, text, boost::is_any_of(
","), boost::token_compress_on);
273 std::vector<std::string>::const_iterator ci;
274 for (ci = terms.begin(); ci != terms.end(); ci++)
278 std::vector<T> result = parseRange<T>(*ci, endpoint);
279 std::copy(result.begin(), result.end(), ii);
281 std::vector<T> results(indices.size());
282 std::copy(indices.begin(), indices.end(), results.begin());
287 std::vector<int>
parseRangeList(
const std::string &,
const int endpoint = 0);
290 template <
typename T>
291 std::vector<T>
parseRangeList(
const std::vector<std::string> &ranges,
const T endpoint = 0)
293 std::ostringstream os;
294 std::copy(ranges.begin(), ranges.end(), std::ostream_iterator<std::string>(os,
","));
295 return (parseRangeList<T>(os.str(), endpoint));
299 AtomicGroup
selectAtoms(
const AtomicGroup &,
const std::string);
305 template <
typename T>
308 uint size =
sizeof(T);
309 const unsigned char *p =
reinterpret_cast<const unsigned char *
>(&datum);
311 unsigned char *q =
reinterpret_cast<unsigned char *
>(&swabbed);
314 for (i = 0, j = size - 1; i < size; ++i, --j)
321 std::string
timeAsString(
const double t,
const uint precision = 0);
324 template <
typename T>
325 T
parseStringAs(
const std::string &source,
const uint pos = 0,
const uint nelem = 0)
329 if (pos >= source.size())
331 std::stringstream msg;
332 msg <<
"Missing Field at position " << pos << std::endl;
333 msg <<
"> " << source << std::endl;
337 uint n = !nelem ? source.size() - pos : nelem;
338 if (pos + n > source.size())
339 n = source.size() - pos + 1;
341 std::string element(source.substr(pos, n));
342 std::istringstream iss(element);
345 std::stringstream msg;
346 msg <<
"PARSE ERROR\n"
347 << source << std::endl;
348 for (uint i = 0; i < pos; ++i)
352 for (uint i = 1; i < n; ++i)
362 std::string parseStringAs<std::string>(
const std::string &source,
const uint pos,
const uint nelem);
364 template <
typename T>
365 std::string fixedSizeFormat(
const T t,
const uint n)
367 std::stringstream ss;
369 std::string s(ss.str());
372 return (s.substr(m - n, n));
377 std::string fixedSizeFormat(
const std::string &s,
const uint n);
388 template <
typename T>
391 std::ostringstream oss;
393 for (
typename T::const_iterator i = x.begin(); i != x.end(); ++i)
394 oss << *i << ((i == x.end() - 1) ?
"" :
",");
409 template <
typename T>
412 std::ostringstream oss;
413 for (
typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
416 if (i != v.end() - 1)
426 template <
typename T>
430 std::insert_iterator<std::set<T>> ii(indices, indices.begin());
431 std::copy(list.begin(), list.end(), ii);
433 std::vector<T> uniques(indices.size());
434 std::copy(indices.begin(), indices.end(), uniques.begin());
438 long availableMemory();
Class for reading line-by-line from a file while tracking line numbers and stripping comments.
Definition LineReader.hpp:39
virtual bool getNext()
Get the next line from the file, returning true if successful.
Definition LineReader.cpp:38
virtual std::string line() const
The currently read line.
Definition LineReader.cpp:62
Exception when parsing input data.
Definition exceptions.hpp:69
Namespace for most things not already encapsulated within a class.
Definition version.cpp:3
AtomicGroup selectAtoms(const AtomicGroup &source, const std::string selection)
Applies a string-based selection to an atomic group...
Definition utils.cpp:183
std::string findBaseName(const std::string &s)
Pull off the file name extension (if present)
Definition utils.cpp:61
std::vector< T > readVector(LineReader &reader)
Read a list of items using a LineReader object.
Definition utils.hpp:71
std::string stringsAsString(const std::vector< std::string > &v)
Converts a vector of strings into a single string with newlines.
Definition utils.cpp:403
std::string getNextLine(std::istream &is, int *lineno=0)
Get the next line of input, skipping blanks and stripping comments.
Definition utils.cpp:88
std::vector< T > uniquifyVector(const std::vector< T > &list)
Return a vector containing only the unique elements of the input vector.
Definition utils.hpp:427
std::vector< int > parseRangeList(const std::string &text, const int endpoint)
Parses a list of Octave-style range specifiers (for compatability)
Definition utils.cpp:166
int parseStringAsHybrid36(const std::string &source, const uint pos, const uint nelem)
Convert a hybrid-36 encoded string into an int.
Definition utils.cpp:259
std::string stringsAsComments(const std::vector< std::string > &v)
Converts a vector of strings into a standard log format.
Definition utils.cpp:393
std::string vectorAsStringWithCommas(const std::vector< std::string > &v)
Specialization for strings that sanitizes the contained strings.
Definition utils.cpp:418
T parseStringAs(const std::string &source, const uint pos=0, const uint nelem=0)
Extracts a field from a string.
Definition utils.hpp:325
std::string sanitizeString(const std::string &s)
Removes internal newlines from string.
Definition utils.cpp:380
std::vector< std::vector< T > > readTable(LineReader &reader)
Read in a table of items using a LineReader object.
Definition utils.hpp:109
std::vector< T > parseRange(const std::string &text, const T endpoint=0)
Parse an Octave/Matlab-style range.
Definition utils.hpp:168
std::string invocationHeader(int argc, char *argv[])
Create an invocation header.
Definition utils.cpp:119
std::string timeAsString(const double t, const uint precision)
Convert t (seconds) into a string, converting to hours and minutes as necessary.
Definition utils.cpp:203
std::string hybrid36AsString(int d, uint n)
Convert an int into a hybrid-36 encoded string.
Definition utils.cpp:320
std::string vToString(const T &x)
Convert something that can iterate into a string...
Definition utils.hpp:389
T swab(const T &datum)
Returns a byte-swapped copy of an arbitrary type.
Definition utils.hpp:306