gnStringTools.cpp Source File

Main Page Namespace List Class Hierarchy Alphabetical List Compound List File List Compound Members File Members

# gnStringTools.cpp

Go to the documentation of this file.

    00001
    00002 // File:            gnStringTools.cpp
    00003 // Purpose:         Random string manipulation tools
    00004 // Description:     Random string manipulation tools
    00005 // Changes:
    00006 // Version:         libGenome 0.1.0
    00007 // Author:          Aaron Darling
    00008 // Last Edited:     April 15, 2001, 10:34:50pm
    00009 // Modified by:
    00010 // Copyright:       (c) Aaron Darling
    00011 // Licenses:        Proprietary
    00013 #include "gn/gnStringTools.h"
    00014
    00015 void BaseCount(const string& bases, gnSeqI& a_count, gnSeqI& c_count, gnSeqI& g_count, gnSeqI& t_count, gnSeqI& other_count){
    00016         a_count = 0;
    00017         c_count = 0;
    00018         g_count = 0;
    00019         t_count = 0;
    00020         other_count = 0;
    00021         for(uint32 i=0; i < bases.length(); i++){
    00022                 if((bases[i] == 'a')||(bases[i] == 'A'))
    00023                         a_count++;
    00024                 else if((bases[i] == 'c')||(bases[i] == 'C'))
    00025                         c_count++;
    00026                 else if((bases[i] == 'g')||(bases[i] == 'G'))
    00027                         g_count++;
    00028                 else if((bases[i] == 't')||(bases[i] == 'T'))
    00029                         t_count++;
    00030                 else
    00031                         other_count++;
    00032         }
    00033 }
    00034
    00035 // removes white space, keeps only one return for multiple returns
    00036 unsigned int removeSpace(string &str)
    00037 {
    00038         bool onSpace = true;
    00039         unsigned int nbrSpace = 0;
    00040         bool containsReturn = false;
    00041         unsigned int i;
    00042         for( i = str.length(); i > 0 ; i--)
    00043         {
    00044                 if( isspace(str[i-1]) )
    00045                 {
    00046                         nbrSpace++;
    00047                         if( (str[i-1] == '\n') || (str[i-1] == '\r') )
    00048                                 containsReturn = true;
    00049                         onSpace = true;
    00050                 }
    00051                 else
    00052                 {
    00053                         onSpace = false;
    00054                         if( nbrSpace > 0 )
    00055                         {
    00056                                 str.erase( i, nbrSpace-1);
    00057                                 str[i] = (containsReturn?'\n':' ');
    00058                         }
    00059                         containsReturn = false;
    00060                         nbrSpace = 0;
    00061                 }
    00062         }
    00063         if( nbrSpace > 0 )
    00064         {
    00065                 str.erase( i, nbrSpace);
    00066         }
    00067         if( str.length() > 0 )
    00068         {
    00069                 if( isspace(str[str.length()-1]) ) str.erase(str.length()-1, 1);
    00070         }
    00071         return nbrSpace;
    00072 }
    00073
    00074 void removeEndSpace(string &str)
    00075 {
    00076         unsigned int nbrSpace = 0;
    00077         unsigned int i;
    00078         for( i = str.length()-1; i > 0 ; i--)
    00079         {
    00080                 if( !isSpace(str[i]) )
    00081                         break;
    00082                 nbrSpace++;
    00083         }
    00084         if( i != str.length() )
    00085         {
    00086                 str.erase(i+1, nbrSpace);
    00087         }
    00088 }
    00089
    00090
    00091 bool isNewLine(char ch)
    00092 {
    00093         if( (ch == '\n') || (ch == '\r') )
    00094         {
    00095                 return true;
    00096         }
    00097         return false;
    00098 }
    00099
    00100 bool isWhiteSpace(char ch)
    00101 {
    00102         if( (ch == ' ') || (ch == '\t') )
    00103         {
    00104                 return true;
    00105         }
    00106         return false;
    00107 }
    00108
    00109 bool isSpace(char ch)
    00110 {
    00111         if( isWhiteSpace(ch) || isNewLine(ch) )
    00112         {
    00113                 return true;
    00114         }
    00115         return false;
    00116 }
    00117
    00118 string uintToString(unsigned int value)
    00119 {
    00120         string str = "";
    00121         char ch = '\0';
    00122         unsigned int b = 0;
    00123         if( value == 0 )
    00124                 str = "0";
    00125         while( value != 0 )
    00126         {
    00127                 b = value % 10;
    00128                 value /= 10;
    00129                 ch = b + 48;
    00130                 str = ch + str;
    00131         }
    00132         return str;
    00133 }
    00134 string ulongToString(unsigned long value)
    00135 {
    00136         string str = "";
    00137         char ch = '\0';
    00138         unsigned long b = 0;
    00139         if( value == 0 )
    00140                 str = "0";
    00141         while( value != 0 )
    00142         {
    00143                 b = value % 10;
    00144                 value /= 10;
    00145                 ch = b + 48;
    00146                 str = ch + str;
    00147         }
    00148         return str;
    00149 }
    00150
    00151 unsigned int parseValue(string &valueString)
    00152 {
    00153         unsigned int retValue = 0;
    00154         unsigned int length = valueString.length();
    00155         for( unsigned int i=0; i < length;  i++)
    00156         {
    00157                 retValue = (retValue * 10) + (valueString[i] - '0');
    00158         }
    00159         return retValue;
    00160 }
    00161
    00162 int parseUintValue(string &valueString)
    00163 {
    00164         int retValue = 0;
    00165         unsigned int length = valueString.length();
    00166         for( unsigned int i=0; i < length;  i++)
    00167         {
    00168                 if( isdigit( valueString[i] ) )
    00169                 {
    00170                         retValue = (retValue * 10) + (valueString[i] - '0');
    00171                 }
    00172                 else
    00173                         break;
    00174         }
    00175         return retValue;
    00176 }
    00177
    00178 int parseIntValue(string &valueString)
    00179 {
    00180         int sign = 1;
    00181         int retValue = 0;
    00182         unsigned int length = valueString.length();
    00183         unsigned int i=0;
    00184         for( ; i < length;  i++)
    00185         {
    00186                 if( valueString[i] == '-' )
    00187                 {
    00188                         sign = -1;
    00189                         break;
    00190                 }
    00191                 else if( isdigit( valueString[i] ) )
    00192                 {
    00193                         retValue = (retValue * 10) + sign * (valueString[i] - '0');
    00194                         break;
    00195                 }
    00196         }
    00197         i++;
    00198         for( ; i < length;  i++)
    00199         {
    00200                 if( isdigit( valueString[i] ) )
    00201                 {
    00202                         retValue = (retValue * 10) + sign * (valueString[i] - '0');
    00203                 }
    00204                 else
    00205                         break;
    00206         }
    00207         return retValue;
    00208 }
    00209
    00210 vector< string > tokenizeString( const string &str, char delimiter )
    00211 {
    00212         return tokenizeString( str.c_str(), str.length(), delimiter );
    00213 }
    00214
    00215 vector< string > tokenizeString( const char* str, unsigned int len, char delimiter )
    00216 {
    00217         unsigned int lastIndex = 0 ;
    00218         vector< string > tokenizeVector;
    00219         unsigned int i=0;
    00220         for( i = 0; i < len ; ++i )
    00221         {
    00222                 if( str[i] == delimiter )
    00223                 {
    00224                         if( i > (lastIndex + 1) )
    00225                         {
    00226                                 tokenizeVector.push_back( string( str + lastIndex, i - lastIndex ) );
    00227                         }
    00228                         lastIndex = i + 1;
    00229                 }
    00230         }
    00231         if( i > (lastIndex + 1) )
    00232         {
    00233                 tokenizeVector.push_back( string( str + lastIndex, i - lastIndex ) );
    00234         }
    00235         return tokenizeVector;
    00236 }
    00237
    00238
    00239 void standarizePathString( string &oFileName )
    00240 {
    00241         unsigned int len = oFileName.size();
    00242         for( unsigned int i=0; i < len ; ++i )
    00243         {
    00244                 if( oFileName[i] == '\\' )
    00245                         oFileName[i] = '/';
    00246         }
    00247 }
    00248 string getPathString( string oFileName )
    00249 {
    00250         string::size_type i = oFileName.rfind('/');
    00251         if( i != string::npos )
    00252                 oFileName.erase(i+1, oFileName.length() - (i+1));
    00253 //      else
    00254 //              oFileName.clear();
    00255         return oFileName;
    00256 }
    00257 string getFileString( string oFileName )
    00258 {
    00259         string::size_type i = oFileName.rfind('/');
    00260         if( i != string::npos )
    00261                 oFileName.erase(0, i + 1);
    00262         return oFileName;
    00263 }
    00264 string getExtString( string oFileName )
    00265 {
    00266         string::size_type i = oFileName.rfind('.');
    00267         if( i != string::npos )
    00268                 oFileName.erase( 0, i+1);
    00269 //      else
    00270 //              oFileName.clear();
    00271         return oFileName;
    00272 }
    00273
    00274 string getFileNoExtString( string oFileName )
    00275 {
    00276         string::size_type i = oFileName.rfind('/');
    00277         if( i != string::npos )
    00278                 oFileName.erase(0, i + 1);
    00279         i = oFileName.rfind('.');
    00280         if( i != string::npos )
    00281                 oFileName.erase( i, string::npos);
    00282         return oFileName;
    00283 }