Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
StdString.cxx
1/*
2 * Hal::StdString.cxx
3 *
4 * Created on: 25 lut 2019
5 * Author: Daniel Wielanek
6 * E-mail: daniel.wielanek@gmail.com
7 * Warsaw University of Technology, Faculty of Physics
8 */
9#include "StdString.h"
10
11#include "Cout.h"
12
13#include <TMath.h>
14#include <TRegexp.h>
15#include <TSystem.h>
16#include <fstream>
17#include <iostream>
18
19NamespaceImp(Hal::Std);
20namespace Hal {
21 namespace Std {
22
23 TString RemoveUnits(TString tString) {
24 TString res;
25 for (int i = 0; i < tString.First('['); i++) {
26 res = res + tString[i];
27 }
28 // if (res[res.Length() - 1] == "") { res = res(0, res.Length() - 1); }
29 return res;
30 }
31 TString GetUnits(TString string) {
32 TString res;
33 Int_t first = string.First('[');
34 Int_t last = string.Last(']');
35 if (first < 0) return "";
36 for (int i = first + 1; i < last; i++) {
37 res = res + string[i];
38 }
39 return res;
40 }
41
42 std::vector<TString> ExplodeString(TString string, Char_t delimiter, Bool_t keepEmpty) {
43 std::vector<TString> array;
44 if (string.Length() == 0) return array;
45 if (string[0] == delimiter) string = string(1, string.Length() - 1);
46 if (string.Length() == 0) return array;
47 if (string[string.Length() - 1] != delimiter) string = string + delimiter;
48 Int_t no = string.CountChar(delimiter);
49 for (int count = 0; count < no; count++) {
50 Size_t pos = string.First(delimiter);
51 TString element = string(0, pos);
52 if (keepEmpty) {
53 if (element.Length() != 0) array.push_back(element);
54 } else {
55 array.push_back(element);
56 }
57 string = string(pos + 1, string.Length() - pos - 1);
58 }
59 return array;
60 }
61
62 std::vector<std::pair<TString, TString>> ConvertMainArgs(int argc, char* argv[]) {
63 std::vector<std::pair<TString, TString>> res;
64 for (int i = 1; i < argc; i++) {
65 TString fullArg = argv[i];
66 if (fullArg.BeginsWith("--")) {
67 std::vector<TString> vec = ExplodeString(fullArg, '=');
68 if (vec.size() < 1) {
69 Hal::Cout::PrintInfo(Form("Problem with extraction of argument %s", fullArg.Data()), Hal::EInfo::kWarning);
70 } else if (vec.size() != 2) {
71 vec[0] = vec[0](2, vec[0].Length() - 2);
72 res.push_back(std::pair<TString, TString>(vec[0], ""));
73 } else {
74 vec[0] = vec[0](2, vec[0].Length() - 2);
75 res.push_back(std::pair<TString, TString>(vec[0], vec[1]));
76 }
77 } else {
78 res.push_back(std::pair<TString, TString>("", fullArg));
79 }
80 }
81 return res;
82 }
83
84 TString RoundToString(Double_t value, Int_t precission, Option_t* opt) {
85 TString option = opt;
86 if (option == "prefix") {
87 Double_t lognum = TMath::Log10(TMath::Abs(value));
88 Int_t full_lognum = lognum / 3;
89 if (lognum < 0) { full_lognum--; }
90 TString answer;
91 Double_t number = value / TMath::Power(10, full_lognum * 3);
92 if (precission >= 0) {
93 answer = RoundToString(number, precission, "");
94 } else {
95 precission = TMath::Abs(precission);
96 Int_t digits = 0;
97 Double_t abs_value = TMath::Abs(number);
98 if (abs_value < 10) {
99 digits = precission - 1;
100 } else if (abs_value < 100) {
101 digits = precission - 2;
102 } else {
103 digits = precission - 3;
104 }
105 if (digits < 0) digits = 0;
106 answer = RoundToString(number, digits, "");
107 }
108 TString prefix = "x";
109 switch (full_lognum * 3) {
110 case -24: prefix = "y"; break;
111 case -21: prefix = "z"; break;
112 case -18: prefix = "a"; break;
113 case -15: prefix = "f"; break;
114 case -12: prefix = "p"; break;
115 case -9: prefix = "n"; break;
116 case -6: prefix = "#mu"; break;
117 case -3: prefix = "m"; break;
118 case 0: prefix = ""; break;
119 case 3: prefix = "k"; break;
120 case 6: prefix = "M"; break;
121 case 9: prefix = "G"; break;
122 case 12: prefix = "T"; break;
123 case 15: prefix = "P"; break;
124 case 18: prefix = "E"; break;
125 case 21: prefix = "Z"; break;
126 case 24: prefix = "Y"; break;
127 default: prefix = Form("x10^%i", full_lognum * 3); break;
128 }
129 if (value == 0) prefix = "";
130 return answer + prefix;
131 } else if (option == "separators") {
132 ULong64_t integer = TMath::Abs(value);
133 Double_t fractional = TMath::Abs(value) - ((Double_t) integer);
134 fractional = TMath::Abs(fractional);
135 TString first_cout = RoundToString(integer, 0, "separators");
136 TString sec_cout = RoundToString(fractional, precission, "");
137 TString other = "";
138 Int_t pos = sec_cout.First('.');
139 for (int i = 1; i < sec_cout.Length() - 1; i++) {
140 if (i % 3 != 0)
141 other = other + sec_cout[pos + i];
142 else
143 other = other + sec_cout[pos + i] + " ";
144 }
145 if (value >= 0)
146 return Form("%s.%s", first_cout.Data(), other.Data());
147 else
148 return Form("-%s.%s", first_cout.Data(), other.Data());
149 } else {
150 TString flag = Form("%%4.%if", precission);
151 return Form(flag, value);
152 }
153 }
154
155 TString RoundToString(Int_t value, Int_t precission, Option_t* opt) {
156 TString option = opt;
157 if (option == "separators") {
158 TString val = Form("%i", TMath::Abs(value));
159 if (val.Length() < 4) {
160 if (value < 0) {
161 return "-" + val;
162 } else {
163 return val;
164 }
165 }
166 TString newstr = "";
167 Int_t mod = val.Length() % 3;
168 for (int i = 0; i < val.Length(); i++) {
169 if (mod == 0) {
170 if (i != 0) newstr = newstr + " ";
171 newstr = newstr + val[i];
172 mod = 2;
173 } else {
174 mod--;
175 newstr = newstr + val[i];
176 }
177 }
178 if (value < 0) {
179 return ("-" + newstr);
180 } else {
181 return newstr;
182 }
183 } else if (option == "prefix") {
184 return RoundToString((Double_t) value, precission, opt);
185 } else {
186 return Form("%i", value);
187 }
188 }
189
190 TString RoundToString(ULong_t value, Int_t precission, Option_t* opt) {
191 // FIXME large numbers and round issue
192 TString option = opt;
193 if (option == "separators") {
194 TString val = Form("%lu", TMath::Abs((long int) value));
195 if (val.Length() < 4) { return val; }
196 TString newstr = "";
197 Int_t mod = val.Length() % 3;
198 for (int i = 0; i < val.Length(); i++) {
199 if (mod == 0) {
200 if (i != 0) newstr = newstr + " ";
201 newstr = newstr + val[i];
202 mod = 2;
203 } else {
204 mod--;
205 newstr = newstr + val[i];
206 }
207 }
208 return newstr;
209 } else if (option == "prefix") {
210 Double_t newval = (Double_t) value;
211 return RoundToString(newval, precission, "prefix");
212 } else {
213 return Form("%lu", value);
214 }
215 }
216
217 TString RoundToString(ULong64_t value, Int_t precission, Option_t* opt) {
218 // FIXME large numbers and round issue
219 TString option = opt;
220 if (option == "separators") {
221 TString val = Form("%llu", TMath::Abs((long long int) value));
222 if (val.Length() < 4) { return val; }
223 TString newstr = "";
224 Int_t mod = val.Length() % 3;
225 for (int i = 0; i < val.Length(); i++) {
226 if (mod == 0) {
227 if (i != 0) newstr = newstr + " ";
228 newstr = newstr + val[i];
229 mod = 2;
230 } else {
231 mod--;
232 newstr = newstr + val[i];
233 }
234 }
235 return newstr;
236 } else if (option == "prefix") {
237 Double_t newval = (Double_t) value;
238 return RoundToString(newval, precission, "prefix");
239 } else {
240 return Form("%llu", value);
241 }
242 }
243
244 TString RoundToString(UInt_t value, Int_t precission, Option_t* opt) {
245 // FIXME large numbers and round issue
246 TString option = opt;
247 if (option == "separators") {
248 TString val = Form("%u", value);
249 if (val.Length() < 4) { return val; }
250 TString newstr = "";
251 Int_t mod = val.Length() % 3;
252 for (int i = 0; i < val.Length(); i++) {
253 if (mod == 0) {
254 if (i != 0) newstr = newstr + " ";
255 newstr = newstr + val[i];
256 mod = 2;
257 } else {
258 mod--;
259 newstr = newstr + val[i];
260 }
261 }
262 return newstr;
263 } else if (option == "prefix") {
264 Double_t newval = (Double_t) value;
265 return RoundToString(newval, precission, "prefix");
266 } else {
267 return Form("%u", value);
268 }
269 }
270
271 TString RemoveString(TString text, TString pattern) {
272 TRegexp match(pattern);
273 TString newword = text;
274 TString regstring = text(match);
275 if (regstring > 0) newword.Remove(text.Index(match), regstring.Length());
276 return newword;
277 }
278
279 TString RemoveNChars(TString str, Int_t n, Char_t opt) {
280 Int_t lenght = str.Length();
281 if (opt == 'b') {
282 return TString(str(n, lenght - n));
283 } else
284 return TString(str(0, lenght - n));
285 }
286
287 std::vector<TString> GetLinesFromFile(TString file, Bool_t skip) {
288 std::ifstream txtfile(file);
289 std::string instr;
290 std::vector<TString> res;
291 while (std::getline(txtfile, instr)) {
292 TString line = instr;
293 if (line.Length() == 0 && skip) continue;
294 res.push_back(instr);
295 }
296 txtfile.close();
297 return res;
298 }
299
300 std::vector<TString> FindBrackets(TString& option, Bool_t remove, Bool_t skipEmpty) {
301 TString expr = "";
302 TString copy = option;
303 TRegexp regexp("\\{[^}]*\\}");
304 std::vector<TString> res;
305 std::vector<TString> resRaw;
306 do {
307 expr = copy(regexp);
308 if (expr.Length()) { // we have something!
309 resRaw.push_back(expr);
310 expr = expr(1, expr.Length() - 2);
311 if (skipEmpty) {
312 if (expr.Length() > 1) res.push_back(expr);
313 } else
314 res.push_back(expr);
315 copy.Remove(copy.First('{'), 1);
316 }
317
318 } while (expr.Length());
319 if (remove) {
320 for (auto i : resRaw)
321 option.ReplaceAll(i, "");
322 }
323 return res;
324 }
325
326 void ReplaceInFile(TString path, TString newPath, TString oldPattern, TString newPattern) {
327 if (!FileExists(path)) return;
328 if (path.EqualTo(".temp.txt")) {
329 Cout::PrintInfo("File .temp.txt cannot be modified by HAL", EInfo::kLowWarning);
330 return;
331 }
332 if (path == newPath) { // replace in file
333 ReplaceInFile(path, ".temp.txt", oldPattern, newPattern); // replace file -> .temp.txt
334 gSystem->Exec(Form("mv .temp.txt %s", path.Data())); // overwrite old file
335 } else {
336 std::ifstream inFile;
337 inFile.open(path);
338 std::ofstream outFile;
339 outFile.open(newPath);
340 for (std::string line; std::getline(inFile, line);) {
341 TString temp = line;
342 temp.ReplaceAll(oldPattern, newPattern);
343 outFile << temp << std::endl;
344 }
345 outFile.close();
346 inFile.close();
347 }
348 }
349
350 void ReplaceInFile(TString path,
351 TString newPath,
352 std::initializer_list<TString> oldPattern,
353 std::initializer_list<TString> newPattern) {
354 if (!FileExists(path)) return;
355 std::vector<TString> list1 = Std::GetVector(oldPattern);
356 std::vector<TString> list2 = Std::GetVector(newPattern);
357 if (list1.size() != list2.size()) {
358 Cout::PrintInfo(Form("Not compatible lists %s %i", __FILE__, __LINE__), EInfo::kLowWarning);
359 return;
360 }
361 if (list1.size() == 0) return;
362 ReplaceInFile(path, newPath, list1[0], list2[0]);
363 for (unsigned int i = 1; i < list1.size(); i++) {
364 ReplaceInFile(newPath, newPath, list1[i], list2[i]);
365 }
366 }
367
368 Bool_t FindParam(TString& option, TString pattern, Bool_t remove) {
369 if (remove) {
370 option.ReplaceAll(" ", "");
371 option.ReplaceAll("++", "");
372 }
373 TString lower_opt = option;
374 TString lower_pattern = pattern;
375 const Int_t lenght = lower_pattern.Length();
376 lower_opt.ToLower();
377 lower_pattern.ToLower();
381 if (lower_opt == lower_pattern) {
382 if (remove) option = "";
383 return kTRUE;
384 } else if (lower_opt.Length() == lower_pattern.Length()) {
385 return kFALSE;
386 }
387 lower_pattern.ReplaceAll("+", "\\+");
391 Int_t index = lower_opt.Index(TRegexp(Form("^%s\\+", lower_pattern.Data())));
392 if (index >= 0) {
393 if (remove) option.Remove(index, lenght + 1);
394 return kTRUE;
395 }
399 index = lower_opt.Index(TRegexp(Form("\\+%s", lower_pattern.Data())));
400 if (index >= 0 && index + lenght + 1 == lower_opt.Length()) {
401 if (remove) option.Remove(index, lenght + 1);
402 return kTRUE;
403 }
407 index = lower_opt.Index(TRegexp(Form("\\+%s\\+", lower_pattern.Data())));
408 if (index >= 0) {
409 if (remove) option.Remove(index, lenght + 1);
410 return kTRUE;
411 }
412 return kFALSE;
413 }
414
415 Int_t FindParam2(TString& option, TString pattern, Bool_t remove) {
416 TString negPar = Form("!%s", pattern.Data());
417 if (FindParam(option, negPar, remove)) {
418 return -1;
419 } else if (FindParam(option, pattern, remove)) {
420 return 1;
421 }
422 return 0;
423 }
424
425 Bool_t FindExpressionSingleValue(TString& expression, Int_t& val, Bool_t remove) {
426 TString option = expression;
427 TRegexp regexp("{[0-9]+}");
428 TString expr = option(regexp);
429 if (expr.Length() <= 0) { return kFALSE; }
430 // found regular exprestion like {number}
431 option.Remove(option.Index(regexp),
432 expr.Length()); // remove exprestion from string
433 TRegexp number_expr("{[0-9]+}");
434 TString first = expr(number_expr);
435 TString number_str = first(TRegexp("[0-9]+"));
436 val = number_str.Atoi();
437 if (remove) { expression = option; }
438 return kTRUE;
439 }
440
441 Bool_t FindExpressionTwoValues(TString& expression, Int_t& val1, Int_t& val2, Bool_t remove) {
442 TString option = expression;
443 TRegexp regexp("{[0-9]+x[0-9]+}");
444 TString expr = option(regexp);
445 if (expr.Length() <= 0) { return kFALSE; }
446 // found regular exprestion like {number x number}
447 option.Remove(option.Index(regexp),
448 expr.Length()); // remove exprestion from string
449 TRegexp number_expr("{[0-9]+x");
450 TRegexp jump_expr("x[0-9]+}");
451 TString first = expr(number_expr);
452 TString sec = expr(jump_expr);
453 TString number_str = first(TRegexp("[0-9]+"));
454 TString jump_str = sec(TRegexp("[0-9]+"));
455 val1 = number_str.Atoi();
456 val2 = jump_str.Atoi();
457 if (remove) { expression = option; }
458 return kTRUE;
459 }
463 Bool_t FindExpressionTwoFloats(TString& expression, Double_t& val1, Double_t& val2, Bool_t remove) {
464 TRegexp regexp("{[-+]?[0-9]*\\.?[0-9]*,[-+]?[0-9]*\\.?[0-9]*}");
465 TString expr = expression(regexp);
466 if (expr.Length() > 1) {
467 TRegexp low_expr("{[-+]?[0-9]*\\.?[0-9]*,");
468 TRegexp high_expr(",[-+]?[0-9]*\\.?[0-9]*}");
469 TString first = expr(low_expr);
470 first.ReplaceAll(",", "");
471 first.ReplaceAll("{", "");
472 TString last = expr(high_expr);
473 last.ReplaceAll(",", "");
474 last.ReplaceAll("}", "");
475 val1 = first.Atof();
476 val2 = last.Atof();
477 if (remove) expression.ReplaceAll(expr, "");
478 return kTRUE;
479 } else {
480 val1 = 0;
481 val2 = 0;
482 return kFALSE;
483 }
484 }
485 } // namespace Std
486} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370