13#include <boost/algorithm/string.hpp>
28std::string
loadFile(
const std::string &filename) {
32 in.open(filename.c_str());
54std::string
shorten(
const std::string &input,
const size_t max_length) {
55 const std::string ellipsis =
" ... ";
56 const size_t ellipsisSize = ellipsis.size();
58 if ((max_length == 0) || (input.size() < ellipsisSize + 2) || (input.size() <= max_length))
61 const size_t end_length = (max_length - ellipsisSize) / 2;
62 std::string retVal = input.substr(0, end_length) + ellipsis + input.substr(input.size() - end_length, end_length);
74std::string
replace(
const std::string &input,
const std::string &find_what,
const std::string &replace_with) {
75 std::string output = input;
76 std::string::size_type pos = 0;
77 while ((pos = output.find(find_what, pos)) != std::string::npos) {
78 output.erase(pos, find_what.length());
79 output.insert(pos, replace_with);
80 pos += replace_with.length();
94MANTID_KERNEL_DLL std::string
replaceAll(
const std::string &input,
const std::string &charStr,
95 const std::string &substitute) {
97 replaced.reserve(input.size());
98 std::string::const_iterator iend = input.end();
99 for (std::string::const_iterator itr = input.begin(); itr != iend; ++itr) {
100 char inputChar = (*itr);
101 if (charStr.find_first_of(inputChar) == std::string::npos)
106 replaced.push_back(inputChar);
108 replaced.append(substitute);
116MANTID_KERNEL_DLL std::string
toLower(
const std::string &input) {
117 std::string output(input);
118 std::transform(output.begin(), output.end(), output.begin(), ::tolower);
124MANTID_KERNEL_DLL std::string
toUpper(
const std::string &input) {
125 std::string output(input);
126 std::transform(output.begin(), output.end(), output.begin(), ::toupper);
139 std::ios_base::fmtflags PrevFlags = OFS.flags();
145 OFS.flags(PrevFlags);
158 for (
unsigned int i = 0; i <
Line.length(); i++) {
159 if (
Line[i] !=
' ' &&
Line[i] !=
'\t' &&
Line[i] !=
'\r' &&
Line[i] !=
'\n') {
169 if (lastReal <
static_cast<int>(Out.length()))
190 size_t minSize(cnt >
static_cast<int>(Word.size()) ? Word.size() : cnt);
191 std::string::size_type pos =
Line.find(Word.substr(0, minSize));
192 if (pos == std::string::npos)
195 size_t LinePt = minSize + pos;
196 for (; minSize < Word.size() && LinePt <
Line.size() && Word[minSize] ==
Line[LinePt]; LinePt++, minSize++) {
199 Line.erase(pos, LinePt - (pos - 1));
215 auto firstNumber = int(word.size());
216 for (
int i =
int(word.size()) - 1; i >= 0; i--) {
218 if ((c >
'9') || (c <
'0'))
223 if (firstNumber <
int(word.size())) {
224 std::string part = word.substr(firstNumber, word.size() - firstNumber);
239int confirmStr(
const std::string &S,
const std::string &fullPhrase) {
240 const size_t nS(S.length());
241 const size_t nC(fullPhrase.length());
242 if (nS > nC || nS == 0)
244 for (
size_t i = 0; i < nS; i++)
245 if (S[i] != fullPhrase[i])
262int getPartLine(std::istream &fh, std::string &Out, std::string &Excess,
const int spc) {
265 auto ss =
new char[spc + 1];
266 const auto clen =
static_cast<int>(spc - Out.length());
267 fh.getline(ss, clen,
'\n');
269 Out +=
static_cast<std::string
>(ss);
272 std::string::size_type pos = Out.find_first_of(
"#!");
273 if (pos != std::string::npos) {
277 if (fh.gcount() == clen - 1)
279 pos = Out.find_last_of(
"\t ");
280 if (pos != std::string::npos) {
281 Excess = Out.substr(pos, std::string::npos);
284 Excess.erase(0, std::string::npos);
303 for (
char character : CLine) {
304 if (!isspace(character) || prev ==
'\\') {
333 if (std::getline(fh,
Line)) {
335 auto pos =
Line.find_first_of(
"#!");
336 if (pos != std::string::npos)
346 std::streampos pos = fh.tellg();
360 std::string::size_type pos = A.find_first_not_of(
" \t");
361 return (pos != std::string::npos) ? 0 : 1;
371 std::string::size_type posA = A.find(
"$ ");
372 std::string::size_type posB = A.find(
"# ");
373 std::string::size_type posC = A.find(
'!');
378 if (posA != std::string::npos)
379 A.erase(posA, std::string::npos);
397std::string
strip(
const std::string &A) {
398 std::string result(A);
410 return (line.empty() || boost::starts_with(line,
"#"));
422 const int MaxLine(72);
423 std::string::size_type pos(0);
424 std::string
X =
Line.substr(0, MaxLine);
425 std::string::size_type posB =
X.find_last_of(
" ,");
427 while (posB != std::string::npos &&
static_cast<int>(
X.length()) >= MaxLine - spc) {
429 if (!isspace(
X[posB]))
431 const std::string Out =
X.substr(0, posB);
434 OX << std::string(spc,
' ');
435 OX <<
X.substr(0, posB) <<
'\n';
438 X =
Line.substr(pos, MaxLine - spc);
439 posB =
X.find_last_of(
" ,");
443 OX << std::string(spc,
' ');
454std::vector<std::string>
StrParts(
const std::string &Ln) {
469std::map<std::string, std::string>
splitToKeyValues(
const std::string &input,
const std::string &keyValSep,
470 const std::string &listSep) {
471 std::map<std::string, std::string> keyValues;
472 const int splitOptions =
475 for (
const auto &iter : listSplitter) {
477 if (keyValSplitter.
count() == 2) {
478 keyValues[keyValSplitter[0]] = keyValSplitter[1];
498 int sign, expt, fmask;
503 sign = (Bd.ival & 0x8000) ? -1 : 1;
504 expt = ((Bd.ival & 0x7f80) >> 7);
508 fmask = ((Bd.ival & 0x7f) << 16) | ((Bd.ival & 0xffff0000) >> 16);
511 frac =
static_cast<float>(fmask) / 0x1000000;
512 onum = frac *
static_cast<float>(sign) * pow(2.0, expt);
513 return static_cast<float>(onum);
531 std::istringstream cx;
536 const std::streamoff xpt = cx.tellg();
539 A.erase(0,
static_cast<unsigned int>(xpt));
553template <
typename T>
int section(
char *cA, T &out) {
557 const int item(
section(sA, out));
559 strcpy(cA, sA.c_str());
573template <
typename T>
int section(std::string &A, T &out) {
576 std::istringstream cx;
583 const std::streamoff xpt = cx.tellg();
584 const auto xc =
static_cast<char>(cx.get());
585 if (!cx.fail() && !isspace(xc))
587 A.erase(0,
static_cast<unsigned int>(xpt));
606 std::istringstream cx;
612 const std::streamoff xpt = cx.tellg();
616 const auto xc =
static_cast<char>(cx.get());
617 if (!cx.fail() && !isspace(xc) && (xc !=
'-' || xpt < 5)) {
620 A.erase(0,
static_cast<unsigned int>(xpt));
639template <
typename T>
int convPartNum(
const std::string &A, T &out) {
642 std::istringstream cx;
651 const std::streamoff xpt = cx.tellg();
655 return static_cast<int>(xpt);
665template <
typename T>
int convert(
const std::string &A, T &out) {
668 std::istringstream cx;
675 const auto clast =
static_cast<char>(cx.get());
676 if (!cx.fail() && !isspace(clast))
689template <
typename T>
int convert(
const char *A, T &out) {
704 std::ostringstream mess;
716 std::ostringstream mess;
717 auto it =
value.begin();
718 auto last =
value.end();
721 for (; it != last; ++it) {
724 for (; it != last; ++it) {
727 else if ((stop +
static_cast<T
>(1)) == *(it + 1))
750template <> MANTID_KERNEL_DLL std::string
toString(
const std::vector<std::string> &
value) {
762template <
template <
typename T,
typename A>
class V,
typename T,
typename A>
763int writeFile(
const std::string &Fname,
const T &step,
const V<T, A> &
Y) {
766 for (
unsigned int i = 0; i <
Y.size(); i++)
767 X.emplace_back(i * step);
780template <
template <
typename T,
typename A>
class V,
typename T,
typename A>
781int writeFile(
const std::string &Fname,
const V<T, A> &
X,
const V<T, A> &
Y) {
798template <
template <
typename T,
typename A>
class V,
typename T,
typename A>
799int writeFile(
const std::string &Fname,
const V<T, A> &
X,
const V<T, A> &
Y,
const V<T, A> &Err) {
800 const size_t Npts(
X.size() >
Y.size() ?
Y.size() :
X.size());
801 const size_t Epts(Npts > Err.size() ? Err.size() : Npts);
805 FX.open(Fname.c_str());
809 FX <<
"# " << Npts <<
" " << Epts <<
'\n';
811 FX.setf(std::ios::scientific, std::ios::floatfield);
812 auto xPt =
X.cbegin();
813 auto yPt =
Y.cbegin();
814 auto ePt = (Epts ? Err.cbegin() :
Y.cbegin());
818 for (; eCount < Epts; eCount++) {
819 FX << (*xPt) <<
" " << (*yPt) <<
" " << (*ePt) <<
'\n';
824 for (; eCount < Npts; eCount++) {
825 FX << (*xPt) <<
" " << (*yPt) <<
" 0.0\n";
844template <
typename T>
int setValues(
const std::string &
Line,
const std::vector<int> &Index, std::vector<T> &Out) {
848 if (Out.size() != Index.size())
853 std::string modLine =
Line;
854 std::vector<int> sIndex(Index);
855 std::vector<int> OPt(Index.size());
856 for (
unsigned int i = 0; i < Index.size(); i++)
861 using iVecIter = std::vector<int>::const_iterator;
862 std::vector<int>::const_iterator sc = sIndex.begin();
863 std::vector<int>::const_iterator oc = OPt.begin();
867 while (sc != sIndex.end() && *sc < 0) {
872 while (sc != sIndex.end()) {
875 return static_cast<int>(-1 - distance(
static_cast<iVecIter
>(sIndex.begin()), sc));
881 }
while (sc != sIndex.end() && *sc == cnt);
884 return static_cast<int>(-1 - distance(
static_cast<iVecIter
>(sIndex.begin()), sc));
900std::string
getWord(std::istream &in,
bool consumeEOL) {
906 nextch =
static_cast<char>(in.get());
907 }
while (nextch ==
' ');
910 if (nextch ==
'\n' || nextch ==
'\r') {
913 }
else if ((nextch ==
'\n' && in.peek() ==
'\r') || (nextch ==
'\r' && in.peek() ==
'\n')) {
929 nextch =
static_cast<char>(in.get());
932 if (nextch ==
'\n' || nextch ==
'\r') {
933 if ((nextch ==
'\n' && in.peek() ==
'\r') || (nextch ==
'\r' && in.peek() ==
'\n')) {
952 while (in.good() &&
getWord(in,
false).length() > 0)
970size_t split_path(
const std::string &path, std::vector<std::string> &path_components) {
972 path_components.resize(0);
976 std::string working_path(path);
977 for (
size_t i = 0; i < path.size(); i++) {
978 if (working_path[i] < 0x20 || working_path[i] > 0x7E)
979 working_path[i] =
'_';
980 if (working_path[i] ==
'\\')
981 working_path[i] =
'/';
982 if (working_path[i] ==
' ')
983 working_path[i] =
'_';
988 if (path[0] ==
'.') {
991 absol = absol.absolute();
992 working_path = absol.toString(Poco::Path::PATH_UNIX) + working_path;
998 std::list<int64_t> split_pos;
999 split_pos.emplace_back(-1);
1000 size_t path_size = working_path.size();
1001 for (
size_t i = 0; i < path_size; i++) {
1002 if (working_path[i] ==
'/') {
1003 split_pos.emplace_back(i);
1006 split_pos.emplace_back(path_size);
1008 size_t n_folders = split_pos.size() - 1;
1009 path_components.resize(n_folders);
1010 auto it1 = split_pos.begin();
1015 for (; it2 != split_pos.end(); ++it2) {
1016 std::string folder = working_path.substr(*it1 + 1, *it2 - *it1 - 1);
1017 if (folder.empty() || (folder.size() == 1 && folder ==
".")) {
1022 if (folder ==
"..") {
1025 throw(std::invalid_argument(
"path contains relative references to a "
1026 "folder outside of the seach tree"));
1030 path_components[ic] = folder;
1035 n_folders = size_t(ic);
1036 path_components.resize(n_folders);
1050int isMember(
const std::vector<std::string> &group,
const std::string &candidate) {
1052 for (
size_t i = 0; i < group.size(); i++) {
1053 if (candidate == group[i]) {
1071std::vector<int>
parseRange(
const std::string &str,
const std::string &elemSep,
const std::string &rangeSep) {
1076 if (elemSep.find(
' ') != std::string::npos) {
1081 Tokenizer ranges(str, rangeSep, Tokenizer::TOK_TRIM);
1082 std::string new_str =
join(ranges.begin(), ranges.end(), rangeSep.substr(0, 1));
1083 elements = Tokenizer(new_str, elemSep, Tokenizer::TOK_IGNORE_EMPTY | Tokenizer::TOK_TRIM);
1085 elements = Tokenizer(str, elemSep, Tokenizer::TOK_IGNORE_EMPTY | Tokenizer::TOK_TRIM);
1088 std::vector<int> result;
1091 result.reserve(elements.count());
1093 for (
const auto &elementString : elements) {
1095 Tokenizer rangeElements(elementString, rangeSep, Tokenizer::TOK_TRIM);
1097 size_t noOfRangeElements = rangeElements.count();
1100 if (noOfRangeElements == 1) {
1102 if (
convert(rangeElements[0], element) != 1)
1103 throw std::invalid_argument(
"Invalid element: " + elementString);
1104 result.emplace_back(element);
1107 else if (noOfRangeElements == 2) {
1110 if (
convert(rangeElements[0], start) != 1 ||
convert(rangeElements[1], end) != 1)
1111 throw std::invalid_argument(
"Invalid range: " + elementString);
1114 throw std::invalid_argument(
"Range boundaries are reversed: " + elementString);
1116 for (
int i = start; i <= end; i++)
1117 result.emplace_back(i);
1121 throw std::invalid_argument(
"Multiple range separators: " + elementString);
1143 c =
static_cast<char>(is.peek());
1149 }
else if (c ==
'\n') {
1160template MANTID_KERNEL_DLL
int section(std::string &,
double &);
1161template MANTID_KERNEL_DLL
int section(std::string &,
float &);
1162template MANTID_KERNEL_DLL
int section(std::string &,
int &);
1163template MANTID_KERNEL_DLL
int section(std::string &, std::string &);
1165template MANTID_KERNEL_DLL
int sectPartNum(std::string &,
double &);
1166template MANTID_KERNEL_DLL
int sectPartNum(std::string &,
int &);
1167template MANTID_KERNEL_DLL
int sectionMCNPX(std::string &,
double &);
1169template MANTID_KERNEL_DLL
int convert(
const std::string &,
double &);
1170template MANTID_KERNEL_DLL
int convert(
const std::string &,
float &);
1171template MANTID_KERNEL_DLL
int convert(
const std::string &, std::string &);
1172template MANTID_KERNEL_DLL
int convert(
const std::string &,
int &);
1173template MANTID_KERNEL_DLL
int convert(
const std::string &, std::size_t &);
1174template MANTID_KERNEL_DLL
int convert(
const std::string &,
bool &);
1175template MANTID_KERNEL_DLL
int convert(
const char *, std::string &);
1176template MANTID_KERNEL_DLL
int convert(
const char *,
double &);
1177template MANTID_KERNEL_DLL
int convert(
const char *,
int &);
1178template MANTID_KERNEL_DLL
int convert(
const char *, std::size_t &);
1179template MANTID_KERNEL_DLL
int convert(
const char *,
bool &);
1181template MANTID_KERNEL_DLL std::string toString(
const double &
value);
1182template MANTID_KERNEL_DLL std::string toString(
const float &
value);
1183template MANTID_KERNEL_DLL std::string toString(
const int &
value);
1184template MANTID_KERNEL_DLL std::string toString(
const uint16_t &
value);
1185template MANTID_KERNEL_DLL std::string toString(
const size_t &
value);
1186#if defined(__APPLE__) || (defined(_WIN32) && !defined(_WIN64)) || \
1187 (defined(__GNUC__) && !defined(__LP64__))
1188template MANTID_KERNEL_DLL std::string toString(
const uint64_t &
value);
1190template MANTID_KERNEL_DLL std::string toString(
const std::string &
value);
1192template MANTID_KERNEL_DLL std::string toString(
const std::vector<int> &
value);
1193template MANTID_KERNEL_DLL std::string toString(
const std::vector<size_t> &
value);
1196template MANTID_KERNEL_DLL std::string toString(
const std::set<int> &
value);
1197template MANTID_KERNEL_DLL std::string toString(
const std::set<int16_t> &
value);
1198template MANTID_KERNEL_DLL std::string toString(
const std::set<size_t> &
value);
1199#if defined(__APPLE__) || (defined(_WIN32) && !defined(_WIN64)) || \
1200 (defined(__GNUC__) && !defined(__LP64__))
1201template MANTID_KERNEL_DLL std::string toString(
const std::set<uint64_t> &
value);
1204template MANTID_KERNEL_DLL
int convPartNum(
const std::string &,
double &);
1205template MANTID_KERNEL_DLL
int convPartNum(
const std::string &,
int &);
1207template MANTID_KERNEL_DLL
int setValues(
const std::string &,
const std::vector<int> &, std::vector<double> &);
1209template MANTID_KERNEL_DLL
int writeFile(
const std::string &,
const double &,
const std::vector<double> &);
1210template MANTID_KERNEL_DLL
int writeFile(
const std::string &,
const std::vector<double> &,
const std::vector<double> &,
1211 const std::vector<double> &);
1212template MANTID_KERNEL_DLL
int writeFile(
const std::string &,
const std::vector<double> &,
const std::vector<double> &);
1213template MANTID_KERNEL_DLL
int writeFile(
const std::string &,
const std::vector<float> &,
const std::vector<float> &);
1214template MANTID_KERNEL_DLL
int writeFile(
const std::string &,
const std::vector<float> &,
const std::vector<float> &,
1215 const std::vector<float> &);
double value
The value of the point.
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
const TokenVec & asVector()
Returns a vector of tokenized strings.
std::size_t count() const
Get the total number of tokens.
A base-class for the a class that is able to return unit labels in different representations.
Holds support functions for strings.
MANTID_KERNEL_DLL std::string removeSpace(const std::string &CLine)
strip all spaces
MANTID_KERNEL_DLL std::string toLower(const std::string &input)
Converts string to all lowercase.
MANTID_KERNEL_DLL std::istream & extractToEOL(std::istream &is, std::string &str)
Extract a line from input stream, discarding any EOL characters encountered.
int sectPartNum(std::string &A, T &out)
Convert and cut a string.
MANTID_KERNEL_DLL size_t split_path(const std::string &path, std::vector< std::string > &path_components)
function parses a path, found in input string "path" and returns vector of the folders contributed in...
MANTID_KERNEL_DLL int confirmStr(const std::string &S, const std::string &fullPhrase)
determine if a character group exists in a string
float getVAXnum(const float A)
Convert a VAX number to x86 little eindien.
MANTID_KERNEL_DLL std::vector< int > parseRange(const std::string &str, const std::string &elemSep=",", const std::string &rangeSep="-")
Parses a number range, e.g.
MANTID_KERNEL_DLL std::string strip(const std::string &A)
strip pre/post spaces
MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length)
Converts long strings into "start ... end".
int convPartNum(const std::string &A, T &out)
Takes a character string and evaluates the first [typename T] object.
MANTID_KERNEL_DLL int isMember(const std::vector< std::string > &group, const std::string &candidate)
checks if the candidate is the member of the group
DLLExport std::string join(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator, typename std::enable_if<!(std::is_same< typename std::iterator_traits< ITERATOR_TYPE >::iterator_category, std::random_access_iterator_tag >::value)>::type *=nullptr)
Join a set or vector of (something that turns into a string) together into one string,...
MANTID_KERNEL_DLL std::string replace(const std::string &input, const std::string &find_what, const std::string &replace_with)
Return a string with all matching occurence-strings.
int section(std::string &A, T &out)
Convert and cut a string.
MANTID_KERNEL_DLL std::string peekLine(std::istream &fh)
Peek at a line without extracting it from the stream.
void printHex(std::ostream &OFS, const int n)
Function to convert a number into hex output (and leave the stream un-changed)
MANTID_KERNEL_DLL std::string replaceAll(const std::string &input, const std::string &charStr, const std::string &substitute)
Return a string with all occurrences of the characters in the input replaced by the replace string.
MANTID_KERNEL_DLL void readToEndOfLine(std::istream &in, bool ConsumeEOL)
Eat everything from the stream until the next EOL.
MANTID_KERNEL_DLL std::string fullBlock(const std::string &A)
strip pre/post spaces
MANTID_KERNEL_DLL bool skipLine(const std::string &line)
Determines if a string starts with a #.
std::string stripMultSpc(const std::string &Line)
Removes the multiple spaces in the line.
int sectionMCNPX(std::string &A, T &out)
Convert and cut a string for MCNPX.
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
MANTID_KERNEL_DLL std::vector< std::string > StrParts(std::string, const boost::regex &)
Split a line into component parts.
MANTID_KERNEL_DLL std::string toUpper(const std::string &input)
Converts string to all uppercase.
MANTID_KERNEL_DLL int endsWithInt(const std::string &word)
Get an int from the end of a word.
MANTID_KERNEL_DLL void writeMCNPX(const std::string &Line, std::ostream &OX)
Write file in standard MCNPX input form.
MANTID_KERNEL_DLL std::string getLine(std::istream &fh)
Get a line and strip comments Use only for a single call.
MANTID_KERNEL_DLL void stripComment(std::string &A)
strip trailling comments
MANTID_KERNEL_DLL int extractWord(std::string &Line, const std::string &Word, const int cnt=4)
Get a word from a string.
int convert(const std::string &A, T &out)
Convert a string into a number.
MANTID_KERNEL_DLL std::map< std::string, std::string > splitToKeyValues(const std::string &input, const std::string &keyValSep="=", const std::string &listSep=",")
Splits a string into key value pairs.
std::string toString(const T &value)
Convert a number to a string.
int setValues(const std::string &Line, const std::vector< int > &Index, std::vector< T > &Out)
Call to read in various values in position x1,x2,x3 from the line.
MANTID_KERNEL_DLL std::string getWord(std::istream &in, bool consumeEOL)
Returns the next word in the stream.
MANTID_KERNEL_DLL int isEmpty(const std::string &A)
Determines if a string is only spaces.
int writeFile(const std::string &Fname, const T &step, const V< T, A > &Y)
Write a set of containers to a file.
MANTID_KERNEL_DLL int getPartLine(std::istream &fh, std::string &Out, std::string &Excess, const int spc=256)
get a part of a long line