str.h
00001 #ifndef SUPLIB_PP_STR_H
00002 #define SUPLIB_PP_STR_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 }
00127 }
00128
00129 #endif // ! SUPLIB_PP_STR_H