Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StringTokenizer.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file StringTokenizer.h
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 // StringTokenizer
16 // General purpose string tokenizer (C++ string version)
17 // based on https://github.com/ViDA-NYU/birdvis/blob/master/Tokenizer.cpp
18 
19 #ifndef STRINGTOKENIZER_H
20 #define STRINGTOKENIZER_H
21 
22 #include <string>
23 #include <vector>
24 
25 namespace Jetscape {
26 
27 // default delimiter string (space, tab, newline, carriage return, form feed and =,>,[,].
28 const std::string DEFAULT_DELIMITER = " \t\v\n\r\f=>[]";
29 
31 public:
32  // ctor/dtor
37 
38  // set string and delimiter
39  void set(const std::string &str,
41  void setString(const std::string &str); // set source string only
42  void setDelimiter(const std::string &delimiter); // set delimiter string only
43 
44  std::string next(); // return the next token, return "" if it ends
45 
46  std::vector<std::string>
47  split(); // return array of tokens from current cursor
48 
49  bool done() const { return currPos == buffer.end(); }
50 
51  // Specific to potential JetScape Ascii format ...
52  bool isGraphEntry() const;
53  bool isNodeEntry() const;
54  bool isNodeZero() const;
55  bool isEdgeEntry() const;
56  bool isCommentEntry() const;
57  bool isEventEntry() const;
58  bool isHadronEntry() const;
59 
60 private:
61  void skipDelimiter(); // ignore leading delimiters
62  bool isDelimiter(char c); // check if the current char is delimiter
63 
64  std::string buffer; // input string
65  std::string token; // output string
66  std::string delimiter; // delimiter string
67  std::string::const_iterator
68  currPos; // string iterator pointing the current position
69 };
70 
71 } // end namespace Jetscape
72 
73 #endif // STRINGTOKENIZER_H
74 
76 // Usage of Tokenizer Class: Example program
78 
79 /*
80 // testing Tokenizer class
81 
82 #include "StringTokenizer.h"
83 #include <string>
84 #include <iostream>
85 
86 
87 using std::string;
88 using std::cout;
89 using std::endl;
90 
91 int main(int argc, char* argv[])
92 {
93  // instanciate Tokenizer class
94  StringTokenizer str;
95  string token;
96  int counter = 0;
97 
98  string m_str2="[0]-->[1] 100. 0 0 75.";
99  cout<<m_str2<<endl;
100  str.set(m_str2);
101  //str.setDelimiter(" ->[]");
102 
103  cout<<str.isGraphEntry()<<endl;
104  cout<<str.isEdgeEntry()<<endl;
105 
106  // Two ways ...
107  //while((token = str.next()) != "")
108  while (!str.done())
109  {
110  token = str.next();
111  ++counter;
112  cout << counter << ": " << token << endl;
113  }
114 
115  string m_str3="#[-->Just --> a Comment []..";
116  cout<<m_str3<<endl;
117  str.set(m_str3);
118 
119  cout<<str.isGraphEntry()<<endl;
120  cout<<str.isEdgeEntry()<<endl;
121  cout<<str.isCommentEntry()<<endl;
122 
123  return 0;
124 }
125 
126 */