S3FC project page S3FC home page

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

s3_conversion.cc

Go to the documentation of this file.
00001 /*
00002  * Stone Three Foundation Class (s3fc) provides a number of utility classes.
00003  * Copyright (C) 2001 by Stone Three Signal Processing (Pty) Ltd.
00004  *
00005  * Authored by Stone Three Signal Processing (Pty) Ltd.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  * 
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  * 
00021  * Please see the file 'COPYING' in the source root directory.
00022  */
00023 
00033 #include <s3fc/s3_conversion.h>
00034 
00035 // specialisation for from_string<std::string> required in order to retain
00036 // entire string
00037 template<>
00038 std::string s3_conversion::from_string<std::string>(const std::string& s)
00039 {
00040    return s;
00041 }
00042 
00043 //
00044 s3_conversion::exception::exception(const std::string& where,
00045                 const std::string& what) :
00046    s3_generic_exception(where, what)
00047 {
00048 }
00049 
00050 // string to vector of string
00051 std::vector<std::string> s3_conversion::string_to_vec_string(
00052    const std::string& str)
00053 {
00054    std::vector<std::string> vs;
00055    // current position - at start
00056    size_t pos = 0;
00057 
00058    while (true)
00059    {
00060       // find first token starting at pos
00061       unsigned int strt_i = str.find_first_not_of(" \t\n\r", pos);
00062       // find ws after token
00063       unsigned int end_i = str.find_first_of(" \t\n\r", strt_i);
00064       if ( strt_i != std::string::npos )
00065       {
00066          vs.push_back(str.substr(strt_i, end_i - strt_i));
00067          pos = end_i;
00068       } else
00069       {
00070          break;
00071       }
00072    }
00073    return vs;
00074 }
00075 
00076 // string -> vector<string> -> vector<int>
00077 std::vector<int> s3_conversion::string_to_vec_int(const std::string& str)
00078 {
00079    std::vector<int> vi;
00080    
00081    std::vector<std::string> vs = string_to_vec_string(str);
00082    for ( std::vector<std::string>::const_iterator ptr = vs.begin();
00083          ptr != vs.end();
00084          ptr++)
00085    {
00086       vi.push_back(atoi(ptr->c_str()));
00087    }
00088    return vi;
00089 }
00090 
00091 // string -> vector<string> -> vector<int>
00092 std::vector<float> s3_conversion::string_to_vec_float(const std::string& str)
00093 {
00094    std::vector<float> vf;
00095    
00096    std::vector<std::string> vs = string_to_vec_string(str);
00097    for ( std::vector<std::string>::const_iterator ptr = vs.begin();
00098          ptr != vs.end();
00099          ptr++)
00100    {
00101       vf.push_back(atof(ptr->c_str()));
00102    }
00103    return vf;
00104 }
00105 
00106 //
00107 std::vector<std::string> s3_conversion::split_string(const std::string& str,
00108                        const char* delim)
00109 {
00110    std::vector<std::string> vs;
00111    // current position - at start
00112    size_t pos = 0;
00113 
00114    while ( true )
00115    {
00116       // whitespace
00117       if (delim == 0)
00118       {
00119     // find first token starting at pos
00120     const unsigned int strt_i = str.find_first_not_of(" \t\n\r", pos);
00121     // find ws after token
00122     const unsigned int end_i = str.find_first_of(" \t\n\r", strt_i);
00123     if ( strt_i != std::string::npos )
00124     {
00125        vs.push_back(str.substr(strt_i, end_i - strt_i));
00126        pos = end_i;
00127     } 
00128          else
00129     {
00130        break;
00131     }
00132       }
00133       // other delimiters: s0 d s1 d ... sn [d]
00134       else
00135       {
00136     // stop if we are past the end
00137     if ( pos >= str.length() ) 
00138          {
00139             break;
00140          }
00141     // find next delimiter, starting from pos
00142     const unsigned int delim_i = str.find_first_of(delim, pos);
00143     if ( delim_i != std::string::npos )    // delimeter found
00144     {
00145        vs.push_back( str.substr( pos, delim_i-pos ) );
00146        pos = delim_i + 1;
00147     }
00148     else // no delimeter in remaining substring
00149     {
00150        vs.push_back( str.substr(pos, str.length()-pos) );
00151        break;
00152     }
00153       }
00154    }
00155    return vs;
00156 }

Send comments to: s3fc@stonethree.com SourceForge Logo