str.h

00001 #ifndef SUPLIB_PP_STR_H
00002 #define SUPLIB_PP_STR_H
00003 
00004 // --8<--8<--8<--8<--
00005 //
00006 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of suplibxx
00009 //
00010 // suplibxx is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // suplibxx is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the 
00022 //       Free Software Foundation, Inc. 
00023 //       51 Franklin Street, Fifth Floor
00024 //       Boston, MA  02110-1301, USA
00025 //
00026 // -->8-->8-->8-->8--
00027 
00028 #ifdef sgi
00029 #include <stddef.h>
00030 #include <stdio.h>
00031 #else
00032 #include <cstddef>
00033 #include <cstdio>
00034 #endif
00035 #include <string>
00036 
00037 #include <Exception/Exception.h>
00038 
00039 using namespace std;
00040 
00041 namespace suplib { 
00042   
00043  string& trim( string& str );
00044 
00045  string& prune( string& str );
00046 
00047  bool iscomment( const string& str, const string& ignore=" \t", const string& comment="#" );
00048  
00049  float str2f( const char* txt ) throw( Exception );
00050 
00051  double str2d( const char* txt ) throw( Exception );
00052 
00053  int str2i( const char* txt, int base=10 ) throw( Exception );
00054 
00055  long str2l( const char* txt, int base=10 ) throw( Exception );
00056 
00057  unsigned long str2ul( const char* txt, int base=10 ) throw( Exception );
00058 
00059 template <typename Container>
00060 void tok (Container &container, string const &in,
00061           const char * const delimiters = " \t\n",
00062           bool skip=true
00063           )
00064 {
00065   const string::size_type len = in.length();
00066   string::size_type i = 0;
00067   string::size_type j = 0;
00068 
00069   if ( 0 == len )
00070     return;
00071 
00072   while ( len > i ) {
00073 
00074     if ( skip ) 
00075       i = in.find_first_not_of( delimiters, i );
00076 
00077     j = in.find_first_of( delimiters, i );
00078 
00079     if ( string::npos == i ) {
00080 
00081       return;
00082 
00083     } else if ( string::npos == j ) {
00084       
00085       container.push_back( in.substr( i,  j ) );
00086       return;
00087       
00088     } else {
00089 
00090       container.push_back( in.substr( i, j - i ) );
00091       
00092     }
00093     
00094     i = skip ? j : j + 1;
00095     
00096   }
00097 
00098   container.push_back( string() );
00099   /*
00100   const string::size_type len = in.length();
00101           string::size_type i = 0;
00102 
00103     while ( i < len )
00104     {
00105         if ( skip ) {
00106             // eat leading whitespace
00107             i = in.find_first_not_of (delimiters, i);
00108             if (i == string::npos)
00109                 return;   // nothing left but white space
00110         }
00111         
00112         // find the end of the token
00113         string::size_type j = in.find_first_of (delimiters, i);
00114 
00115         // push token
00116         if (j == string::npos) {
00117             container.push_back (in.substr(i));
00118             return;
00119         } else
00120             container.push_back (in.substr(i, j-i));
00121 
00122         // set up for next loop
00123         i = j + 1;
00124     }
00125   */
00126  }
00127 }
00128 
00129 #endif // ! SUPLIB_PP_STR_H

Generated on Thu Oct 2 20:49:06 2008 for suplibxx by  doxygen 1.5.6