Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
it1 = it2;
continue;
}
path_components[ic] = folder;
ic++;
it1 = it2;
}
n_folders = size_t(ic);
path_components.resize(n_folders);
return n_folders;
}
/**
* Function checks if the candidate is the member of the group
* @param group :: vector of string to check
* @param candidate :: the string which has to be checked against the group
* @returns :: number of the candidate in the input vector of strings if the
candidate belongs to the group
or -1 if it does not.
Returns the number of the first maching entry in the group if
there are duplicated entries in the group
*/
int isMember(const std::vector<std::string> &group,
const std::string &candidate) {
int num(-1);
for (size_t i = 0; i < group.size(); i++) {
if (candidate.compare(group[i]) == 0) {
num = int(i);
return num;
}
}
return num;
}
/**
* Parses a number range, e.g. "1,4-9,54-111,3,10", to the vector containing all
* the elements
* within the range.
* @param str String to parse
* @param elemSep String with characters used to separate elements (',')
* @param rangeSep String with characters used to separate range start and end
* ('-')
* @return A vector with all the elements from the range
*/
std::vector<int> parseRange(const std::string &str, const std::string &elemSep,
const std::string &rangeSep) {
typedef Poco::StringTokenizer Tokenizer;
boost::shared_ptr<Tokenizer> elements;
if (elemSep.find(' ') != std::string::npos) {
// If element separator contains space character it's a special case,
// because in that case
// it is allowed to have element separator inside a range, e.g. "4 - 5", but
// not "4,-5"
// Space is added so that last empty element of the "1,2,3-" is not ignored
// and we can
// spot the error. Behaviour is changed in Poco 1.5 and this will not be
// needed.
Tokenizer ranges(str + " ", rangeSep, Tokenizer::TOK_TRIM);
std::string new_str =
join(ranges.begin(), ranges.end(), rangeSep.substr(0, 1));
elements = boost::make_shared<Tokenizer>(
new_str, elemSep, Tokenizer::TOK_IGNORE_EMPTY | Tokenizer::TOK_TRIM);
} else {
elements = boost::make_shared<Tokenizer>(
str, elemSep, Tokenizer::TOK_IGNORE_EMPTY | Tokenizer::TOK_TRIM);
}
std::vector<int> result;
// Estimation of the resulting number of elements
result.reserve(elements->count());
for (Tokenizer::Iterator it = elements->begin(); it != elements->end();
it++) {
// See above for the reason space is added
Tokenizer rangeElements(*it + " ", rangeSep, Tokenizer::TOK_TRIM);
size_t noOfRangeElements = rangeElements.count();
// A single element
if (noOfRangeElements == 1) {
int element;
if (convert(rangeElements[0], element) != 1)
throw std::invalid_argument("Invalid element: " + *it);
result.push_back(element);
}
// A pair
else if (noOfRangeElements == 2) {
int start, end;
if (convert(rangeElements[0], start) != 1 ||
convert(rangeElements[1], end) != 1)
throw std::invalid_argument("Invalid range: " + *it);
if (start >= end)
throw std::invalid_argument("Range boundaries are reversed: " + *it);
for (int i = start; i <= end; i++)
result.push_back(i);
}
// Error - e.g. "--""
else {
throw std::invalid_argument("Multiple range separators: " + *it);
}
}
return result;
}
/**
* Extract a string until an EOL character is reached. There are 3 scenarios that
* we need to deal with
* 1) Windows-style - CRLF ('\\r\\n');
* 2) Unix-style - LF ('\\n');
* 3) Old MAC style - CR ('\\r').
* This function will give the string preceding any of these sequences
* @param is :: The input stream to read from
* @param str :: The output string to use to accumulate the line
* @returns A reference to the input stream
*/
std::istream &extractToEOL(std::istream &is, std::string &str) {
// Empty the string
str = "";
char c('\0');
while (is.get(c)) {
if (c == '\r') {
c = static_cast<char>(is.peek());
if (c == '\n') {
// Extract this as well
is.get();
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
break;
} else if (c == '\n') {
break;
} else {
// Accumulate the string
str += c;
}
}
return is;
}
/// \cond TEMPLATE
template MANTID_KERNEL_DLL int section(std::string &, double &);
template MANTID_KERNEL_DLL int section(std::string &, float &);
template MANTID_KERNEL_DLL int section(std::string &, int &);
template MANTID_KERNEL_DLL int section(std::string &, std::string &);
template MANTID_KERNEL_DLL int sectPartNum(std::string &, double &);
template MANTID_KERNEL_DLL int sectPartNum(std::string &, int &);
template MANTID_KERNEL_DLL int sectionMCNPX(std::string &, double &);
template MANTID_KERNEL_DLL int convert(const std::string &, double &);
template MANTID_KERNEL_DLL int convert(const std::string &, float &);
template MANTID_KERNEL_DLL int convert(const std::string &, std::string &);
template MANTID_KERNEL_DLL int convert(const std::string &, int &);
template MANTID_KERNEL_DLL int convert(const std::string &, std::size_t &);
template MANTID_KERNEL_DLL int convert(const std::string &, bool &);
template MANTID_KERNEL_DLL int convert(const char *, std::string &);
template MANTID_KERNEL_DLL int convert(const char *, double &);
template MANTID_KERNEL_DLL int convert(const char *, int &);
template MANTID_KERNEL_DLL int convert(const char *, std::size_t &);
template MANTID_KERNEL_DLL int convert(const char *, bool &);
template MANTID_KERNEL_DLL std::string toString(const double &value);
template MANTID_KERNEL_DLL std::string toString(const float &value);
template MANTID_KERNEL_DLL std::string toString(const int &value);
template MANTID_KERNEL_DLL std::string toString(const uint16_t &value);
template MANTID_KERNEL_DLL std::string
toString(const size_t &value); // Matches uint64_t on Linux 64 & Win 64
#if defined(__APPLE__) || (defined(_WIN32) && !defined(_WIN64)) || \
(defined(__GNUC__) && !defined(__LP64__)) // Mac or 32-bit compiler
template MANTID_KERNEL_DLL std::string toString(const uint64_t &value);
template MANTID_KERNEL_DLL std::string toString(const std::string &value);
template MANTID_KERNEL_DLL std::string toString(const std::vector<int> &value);
// this block should generate the vector ones as well
template MANTID_KERNEL_DLL std::string toString(const std::set<int> &value);
template MANTID_KERNEL_DLL std::string toString(const std::set<int16_t> &value);
template MANTID_KERNEL_DLL std::string toString(
const std::set<size_t> &value); // Matches uint64_t on Linux 64 & Win 64
#if defined(__APPLE__) || (defined(_WIN32) && !defined(_WIN64)) || \
(defined(__GNUC__) && !defined(__LP64__)) // Mac or 32-bit compiler
template MANTID_KERNEL_DLL std::string
toString(const std::set<uint64_t> &value);
#endif
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
template MANTID_KERNEL_DLL int convPartNum(const std::string &, double &);
template MANTID_KERNEL_DLL int convPartNum(const std::string &, int &);
template MANTID_KERNEL_DLL int
setValues(const std::string &, const std::vector<int> &, std::vector<double> &);
template MANTID_KERNEL_DLL int writeFile(const std::string &, const double &,
const std::vector<double> &);
template MANTID_KERNEL_DLL int writeFile(const std::string &,
const std::vector<double> &,
const std::vector<double> &,
const std::vector<double> &);
template MANTID_KERNEL_DLL int writeFile(const std::string &,
const std::vector<double> &,
const std::vector<double> &);
template MANTID_KERNEL_DLL int writeFile(const std::string &,
const std::vector<float> &,
const std::vector<float> &);
template MANTID_KERNEL_DLL int writeFile(const std::string &,
const std::vector<float> &,
const std::vector<float> &,
const std::vector<float> &);
/// \endcond TEMPLATE
} // NAMESPACE Strings
} // NAMESPACE Kernel
} // NAMESPACE Mantid