TF++, Miscellaneous classes and modules in C++:
rangestring.h
Go to the documentation of this file.
1 
37 // include guard
38 #ifndef TF_RANGESTRING_H_VERSION
39 
40 #define TF_RANGESTRING_H_VERSION \
41  "TF_RANGESTRING_H V1.2"
42 
43 #include<tfxx/range.h>
44 #include<tfxx/rangelist.h>
45 #include<tfxx/stringfunc.h>
46 #include<list>
47 #include<vector>
48 #include<sstream>
49 
50 namespace tfxx {
51 
52  namespace string {
53 
60  template<class T>
61  tfxx::Range<T> range(const std::string& s)
62  {
63  typedef tfxx::Range<T> Trange;
64  typename Trange::Tvalue v1, v2;
65  std::list<std::string> slist;
66  tfxx::string::gen_split(slist, s, "-", true);
67  std::list<std::string>::const_iterator i=slist.begin();
68  std::istringstream iss(*i);
69  iss >> v1;
70  if (slist.size()>1)
71  {
72  ++i;
73  iss.str(*i);
74  // clear status after reading first number
75  iss.clear();
76  iss >> v2;
77  }
78  else
79  { v2=v1; }
80  Trange retval(v1,v2);
81  return retval;
82  } // tfxx::Range<T> range(const std::string& s)
83 
84  /*----------------------------------------------------------------------*/
85 
97  template<class T>
98  tfxx::RangeList<T> rangelist(const std::string& s)
99  {
100  typedef tfxx::Range<T> Trange;
101  typedef tfxx::RangeList<T> Tlist;
102  Tlist retval;
103  std::list<std::string> slist;
104  tfxx::string::gen_split(slist, s, ",", true);
105  std::list<std::string>::const_iterator i=slist.begin();
106  while (i!=slist.end())
107  {
108  std::vector<std::string> svec;
109  tfxx::string::gen_split(svec, *i, "+", true);
110  if (svec.size()>1)
111  {
112  std::vector<std::string> svec2;
113  tfxx::string::gen_split(svec2, svec[1], "-", true);
114  TFXX_assert(svec2.size()>1, "range not properly defined");
115  typename Trange::Tvalue start(0), step(0), end(0);
116  { std::istringstream iss(svec[0]); iss >> start; }
117  { std::istringstream iss(svec2[0]); iss >> step; }
118  { std::istringstream iss(svec2[1]); iss >> end; }
119  /*
120  std::cerr << "start=" << svec[0]
121  << " step=" << svec2[0]
122  << " end=" << svec2[1] << std::endl;
123  std::cerr << "start=" << start
124  << " step=" << step
125  << " end=" << end << std::endl;
126  */
127  TFXX_assert(step>0, "range step size must be positive");
128  for (typename Trange::Tvalue j=start; j<=end; j=j+step)
129  {
130  retval.append(Trange(j,j));
131  }
132  }
133  else
134  {
135  // no '+' symbol, go ahead with simple range
136  retval.append(range<typename Tlist::Tvalue>(*i));
137  }
138  ++i;
139  }
140  return retval;
141  } // tfxx::RangeList<T> rangelist(const std::string& s)
142 
143  } // namespace string
144 
145 } // namespace tfxx
146 
147 #endif // TF_RANGESTRING_H_VERSION (includeguard)
148 
149 /* ----- END OF rangestring.h ----- */
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
tfxx::Range< T > range(const std::string &s)
Definition: rangestring.h:61
A class to deal with numerical ranges.
Definition: range.h:50
void gen_split(C &v, const std::string &s, const std::string &delimiter=" ", const bool &dropdelimiter=false)
Definition: stringfunc.h:112
tfxx::RangeList< T > rangelist(const std::string &s)
Definition: rangestring.h:98
Namespace containing all code of library libtfxx.