suplibxx  1.3.9
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 #ifdef sgi
29 #include <stddef.h>
30 #include <stdio.h>
31 #else
32 #include <cstddef>
33 #include <cstdio>
34 #endif
35 #include <string>
36 
37 #include <Exception/Exception.h>
38 
39 using namespace std;
40 
41 namespace suplib {
42 
43  string& trim( string& str );
44 
45  string& prune( string& str );
46 
47  bool iscomment( const string& str, const string& ignore=" \t", const string& comment="#" );
48 
49  float str2f( const char* txt ) throw( Exception );
50 
51  double str2d( const char* txt ) throw( Exception );
52 
53  int str2i( const char* txt, int base=10 ) throw( Exception );
54 
55  long str2l( const char* txt, int base=10 ) throw( Exception );
56 
57  unsigned long str2ul( const char* txt, int base=10 ) throw( Exception );
58 
59 template <typename Container>
60 void tok (Container &container, string const &in,
61  const char * const delimiters = " \t\n",
62  bool skip=true
63  )
64 {
65  const string::size_type len = in.length();
66  string::size_type i = 0;
67  string::size_type j = 0;
68 
69  if ( 0 == len )
70  return;
71 
72  while ( len > i ) {
73 
74  if ( skip )
75  i = in.find_first_not_of( delimiters, i );
76 
77  j = in.find_first_of( delimiters, i );
78 
79  if ( string::npos == i ) {
80 
81  return;
82 
83  } else if ( string::npos == j ) {
84 
85  container.push_back( in.substr( i, j ) );
86  return;
87 
88  } else {
89 
90  container.push_back( in.substr( i, j - i ) );
91 
92  }
93 
94  i = skip ? j : j + 1;
95 
96  }
97 
98  container.push_back( string() );
99  /*
100  const string::size_type len = in.length();
101  string::size_type i = 0;
102 
103  while ( i < len )
104  {
105  if ( skip ) {
106  // eat leading whitespace
107  i = in.find_first_not_of (delimiters, i);
108  if (i == string::npos)
109  return; // nothing left but white space
110  }
111 
112  // find the end of the token
113  string::size_type j = in.find_first_of (delimiters, i);
114 
115  // push token
116  if (j == string::npos) {
117  container.push_back (in.substr(i));
118  return;
119  } else
120  container.push_back (in.substr(i, j-i));
121 
122  // set up for next loop
123  i = j + 1;
124  }
125  */
126  }
127 }
128 
129 #endif // ! SUPLIB_PP_STR_H
The suplib namespace encompasses all of the functions in the suplib++ library.
string & trim(string &str)
remove leading white space from a string
Definition: trim.cc:49
float str2f(const char *txt)
convert string to floating-point number
Definition: str2f.cc:39
long str2l(const char *txt, int base=10)
convert string to long number
Definition: str2l.cc:44
double str2d(const char *txt)
convert string to double-precision number
Definition: str2d.cc:44
int str2i(const char *txt, int base=10)
convert string to integer number
Definition: str2i.cc:40
string & prune(string &str)
remove leading and trailing white space from a string
Definition: prune.cc:48
unsigned long str2ul(const char *txt, int base=10)
convert string to long number
Definition: str2ul.cc:44
void tok(Container &container, string const &in, const char *const delimiters=" \t\n", bool skip=true)
split a string into tokens
Definition: str.h:60
bool iscomment(const string &str, const string &ignore=" \t", const string &comment="#")
determine if the string is a comment.
Definition: iscomment.cc:48