suplibxx  1.3.13
str.h
1 #ifndef SUPLIB_PP_STR_H
2 #define SUPLIB_PP_STR_H
3 
4 // --8<--8<--8<--8<--
5 //
6 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
7 //
8 // This file is part of suplibxx
9 //
10 // suplibxx is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // suplibxx is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the
22 // Free Software Foundation, Inc.
23 // 51 Franklin Street, Fifth Floor
24 // Boston, MA 02110-1301, USA
25 //
26 // -->8-->8-->8-->8--
27 
28 #include <cstddef>
29 #include <cstdio>
30 #include <string>
31 
32 #include <Exception/Exception.h>
33 
34 namespace suplib {
35 
36  std::string& trim( std::string& str );
37 
38  std::string& prune( std::string& str );
39 
40  bool iscomment( const std::string& str, const std::string& ignore=" \t", const std::string& comment="#" );
41 
42  float str2f( const char* txt );
43 
44  double str2d( const char* txt );
45 
46  int str2i( const char* txt, int base=10 );
47 
48  long str2l( const char* txt, int base=10 );
49 
50  unsigned long str2ul( const char* txt, int base=10 );
51 
52 template <typename Container>
53 void tok (Container &container, std::string const &in,
54  const char * const delimiters = " \t\n",
55  bool skip=true
56  )
57 {
58  const std::string::size_type len = in.length();
59  std::string::size_type i = 0;
60  std::string::size_type j = 0;
61 
62  if ( 0 == len )
63  return;
64 
65  while ( len > i ) {
66 
67  if ( skip )
68  i = in.find_first_not_of( delimiters, i );
69 
70  j = in.find_first_of( delimiters, i );
71 
72  if ( std::string::npos == i ) {
73 
74  return;
75 
76  } else if ( std::string::npos == j ) {
77 
78  container.push_back( in.substr( i, j ) );
79  return;
80 
81  } else {
82 
83  container.push_back( in.substr( i, j - i ) );
84 
85  }
86 
87  i = skip ? j : j + 1;
88 
89  }
90 
91  container.push_back( std::string() );
92  /*
93  const std::string::size_type len = in.length();
94  std::string::size_type i = 0;
95 
96  while ( i < len )
97  {
98  if ( skip ) {
99  // eat leading whitespace
100  i = in.find_first_not_of (delimiters, i);
101  if (i == std::string::npos)
102  return; // nothing left but white space
103  }
104 
105  // find the end of the token
106  std::string::size_type j = in.find_first_of (delimiters, i);
107 
108  // push token
109  if (j == std::string::npos) {
110  container.push_back (in.substr(i));
111  return;
112  } else
113  container.push_back (in.substr(i, j-i));
114 
115  // set up for next loop
116  i = j + 1;
117  }
118  */
119  }
120 }
121 
122 #endif // ! SUPLIB_PP_STR_H
bool iscomment(const std::string &str, const std::string &ignore=" \t", const std::string &comment="#")
determine if the string is a comment.
Definition: iscomment.cc:45
The suplib namespace encompasses all of the functions in the suplib++ library.
float str2f(const char *txt)
convert string to floating-point number
Definition: str2f.cc:41
long str2l(const char *txt, int base=10)
convert string to long number
Definition: str2l.cc:46
std::string & prune(std::string &str)
remove leading and trailing white space from a string
Definition: prune.cc:46
double str2d(const char *txt)
convert string to double-precision number
Definition: str2d.cc:44
std::string & trim(std::string &str)
remove leading white space from a string
Definition: trim.cc:48
int str2i(const char *txt, int base=10)
convert string to integer number
Definition: str2i.cc:43
unsigned long str2ul(const char *txt, int base=10)
convert string to long number
Definition: str2ul.cc:46