proj home

Files   Classes   Functions   Hierarchy  

stringconvert.cpp

Go to the documentation of this file.
00001 
00002 #include <iostream>
00003 #include <string>
00004 using namespace std;
00005 
00006 #include <stringconvert.h>
00007 
00008 
00009 // Integer greater than or equal to zero.
00010 boolc isstringdigits(stringc & str)
00011 {
00012   if (str.empty())
00013     return false;
00014 
00015   for (size_t i=0; i<str.length(); ++i)
00016   {
00017 //cout << i << ":" << str[i] << "  " << isdigit(str[i]) << endl;
00018     if (isdigit(str[i])==false)
00019       return false;
00020   }
00021 
00022   return true;
00023 }
00024 
00025 boolc isstringinteger(stringc & str)
00026 {
00027   if (str.empty())
00028     return false;
00029 
00030   if (str[0]=='-')
00031     return isstringinteger(str.substr(1));
00032 
00033   return isstringdigits(str);
00034 }
00035 
00036 boolc isstringzero_or_positive_real(stringc & str)
00037 {
00038   if (str.empty())
00039     return false;
00040 
00041   if (str[0]=='.')
00042     return isstringdigits(str.substr(1));
00043 
00044   size_t k;
00045   uint kcount=0;
00046   for (size_t i=0; i<str.length(); ++i)
00047   {
00048     if (str[i]=='.')
00049     {
00050       k=i;
00051       ++kcount;
00052       continue;
00053     }
00054 
00055     if (isdigit(str[i])==false)
00056       return false;
00057   }
00058 
00059   if (kcount>1)
00060     return false;
00061 
00062   return true;
00063 }
00064 
00065 boolc isstringreal(stringc & str)
00066 {
00067   if (str.empty())
00068     return false;
00069 
00070   if (str[0]=='-')
00071     return isstringzero_or_positive_real(str.substr(1));
00072 
00073   return isstringzero_or_positive_real(str);
00074 }
00075 
00076 boolc isstringnegative(stringc & str)
00077 {
00078   if (str.empty())
00079     return false;
00080 
00081   if (str[0] != '-')
00082     return false;
00083 
00084   return isstringreal(str);
00085 }
00086 
00087 void stringconvert::findandreplace
00088 ( 
00089   string& source, 
00090   stringc & find, 
00091   stringc & replace 
00092 ) 
00093 { 
00094   size_t pos = 0; 
00095   size_t findlen = find.length(); 
00096   size_t replacelen = replace.length();
00097 
00098   if( findlen == 0 )
00099     return;
00100 
00101   for( ; (pos = source.find( find, pos )) != string::npos; )
00102   {
00103     source.replace( pos, findlen, replace );
00104     pos += replacelen;
00105   }
00106 }
00107 
00108 stringc stringconvert::findandreplacefn
00109 ( 
00110   stringc & source, 
00111   stringc & find, 
00112   stringc & replace 
00113 ) 
00114 { 
00115   string str(source); 
00116   findandreplace(str,find,replace); 
00117   return str; 
00118 }
00119 

Generated on Fri Mar 4 00:49:29 2011 for Chelton Evans Source by  doxygen 1.5.8