FairRoot/PandaRoot
PndStringSeparator.cxx
Go to the documentation of this file.
1 #include "PndStringSeparator.h"
2 #include <iostream>
3 #include <sstream>
4 
5 
6 PndStringSeparator::PndStringSeparator (std::string AInput, std::string ADelimiter)
7  :fStartPos(0), fStrings(), fInput(AInput), fDelimiter(ADelimiter), fOutput(), fFirstDel(false),fLastDel(false)
8 {}
9 
10 
12 {
13  std::string::size_type pos;
14  std::string aString;
15  pos = fInput.find_first_of(fDelimiter.c_str(), fStartPos);
16  if (pos-fStartPos == 0) { //first value at fStartPos is a fDelimiter
17  fStartPos += 1;
18  return "";
19  }
20 
21  else if (pos != std::string::npos){ // a delimiter was found after fStartPos
22  aString = fInput.substr(fStartPos, pos-fStartPos);
23  fStartPos = pos + 1;
24  return aString;
25  }
26 
27  else { //no delimiter was found after fStartPos
28  aString = fInput.substr(fStartPos, pos-fStartPos);
29  fStartPos = pos;
30  return aString;
31  }
32 }
33 
34 
35 std::vector<std::string> PndStringSeparator::GetStringVector(void)
36 {
37  fStartPos = 0;
38  std::string value;
39 
40  ResetVector();
41  TestFirst();
42  TestLast();
43  while (fStartPos != std::string::npos){
44  value = GetString();
45  if (value.length() > 0)
46  fStrings.push_back(value);
47  }
48  return fStrings;
49 }
50 
51 std::string PndStringSeparator::Replace(std::string from, std::string to)
52 {
53  std::string olddel = fDelimiter;
54  SetDelimiter(from);
55  std::stringstream result;
56  std::vector<std::string> strVector = GetStringVector();
57  if (GetIfFirst()) result << to;
58  result << strVector[0];
59  for (unsigned int i = 1; i < strVector.size(); i++){
60  result << to << strVector[i];
61  }
62  if(GetIfLast()) result << to;
63  fDelimiter = olddel;
64  //std::cout << "New String: " << result.str() << std::endl;
65  return result.str();
66 }
67 
69 {
70  std::cout << "PndStringSeparator for: " << fInput << std::endl;
71  for (UInt_t i = 0; i < fStrings.size(); i++)
72  std::cout << i << ": " << fStrings[i] << std::endl;
73 }
TVector3 pos
std::vector< std::string > GetStringVector(void)
Int_t i
Definition: run_full.C:25
std::string Replace(std::string from, std::string to)
std::vector< std::string > fStrings
std::string::size_type fStartPos
void SetDelimiter(std::string ADelimiter)
std::string GetString(void)