osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/StringUtils.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00020 #include <osgEarth/StringUtils>
00021 
00022 using namespace osgEarth;
00023 
00024 StringTokenizer::StringTokenizer( const std::string& delims, const std::string& quotes ) :
00025 _allowEmpties( true ),
00026 _trimTokens  ( true )
00027 {
00028     addDelims( delims );
00029     addQuotes( quotes );
00030 }
00031 
00032 StringTokenizer::StringTokenizer(const std::string& input, 
00033                                  StringVector&      output,
00034                                  const std::string& delims, 
00035                                  const std::string& quotes,
00036                                  bool               allowEmpties,
00037                                  bool               trimTokens ) :
00038 _allowEmpties( allowEmpties ),
00039 _trimTokens  ( trimTokens )
00040 {
00041     addDelims( delims );
00042     addQuotes( quotes );
00043     tokenize( input, output );
00044 }
00045 
00046 void
00047 StringTokenizer::addDelim( char delim, bool keep )
00048 {
00049     _delims[delim] = keep;
00050 }
00051 
00052 void
00053 StringTokenizer::addDelims( const std::string& delims, bool keep )
00054 {
00055     for( unsigned i=0; i<delims.size(); ++i )
00056         addDelim( delims.at(i), keep );
00057 }
00058 
00059 void
00060 StringTokenizer::addQuote( char quote, bool keep )
00061 {
00062     _quotes[quote] = keep;
00063 }
00064 
00065 void
00066 StringTokenizer::addQuotes( const std::string& quotes, bool keep )
00067 {
00068     for( unsigned i=0; i<quotes.size(); ++i )
00069         addQuote( quotes.at(i), keep );
00070 }
00071 
00072 void
00073 StringTokenizer::tokenize( const std::string& input, StringVector& output ) const
00074 {
00075     output.clear();
00076 
00077     std::stringstream buf;
00078     bool quoted = false;
00079     for( std::string::const_iterator i = input.begin(); i != input.end(); ++i )
00080     {
00081         char c = *i;    
00082 
00083         TokenMap::const_iterator q = _quotes.find( c );
00084 
00085         if ( quoted )
00086         {
00087             if ( q != _quotes.end() )
00088             {
00089                 quoted = false;
00090                 if ( q->second )
00091                     buf << c;
00092             }
00093             else
00094             {
00095                 buf << c;
00096             }
00097         }
00098         else
00099         {
00100             if ( q != _quotes.end() )
00101             {
00102                 quoted = true;
00103                 if ( q->second )
00104                     buf << c;
00105             }
00106             else
00107             {
00108                 TokenMap::const_iterator d = _delims.find( c );
00109                 if ( d == _delims.end() )
00110                 {
00111                     buf << c;
00112                 }
00113                 else
00114                 {
00115                     std::string token = _trimTokens ? trim(buf.str()) : buf.str();
00116 
00117                     if ( _allowEmpties || !token.empty() )
00118                         output.push_back( token );
00119 
00120                     if ( d->second == true )
00121                     {
00122                         output.push_back( std::string(1, c) );
00123                     }
00124 
00125                     buf.str("");
00126                 }
00127             }
00128         }       
00129     }
00130 
00131     std::string last = _trimTokens ? trim(buf.str()) : buf.str();
00132     if ( !last.empty() )
00133         output.push_back( last );
00134 }
00135 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines