Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGStringTokenizer.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 #ifdef OSG_DOC_FILES_IN_MODULE
00040 
00043 #endif
00044 
00045 //---------------------------------------------------------------------------
00046 //  Includes
00047 //---------------------------------------------------------------------------
00048 
00049 #include <stdlib.h>
00050 #include <stdio.h>
00051 
00052 #include "OSGConfig.h"
00053 
00054 #include "OSGStringTokenizer.h"
00055 
00056 OSG_USING_NAMESPACE
00057 
00058 /*-------------------------------------------------------------------------*/
00059 /*                            Constructors                                 */
00060 
00061 StringTokenizer::StringTokenizer(std::string &toTokens) : 
00062     _hasNext    (false   ),
00063     _tokenString(toTokens),
00064     _nextToken  (        ),
00065     _currPos    (       0)
00066 {
00067     std::string::size_type start;
00068     std::string::size_type end;
00069 
00070     nextTokenPos(start, end);
00071 
00072     if((start == std::string::npos) ||
00073        (end   == std::string::npos)  )
00074     {
00075         _hasNext = false;
00076     }
00077     else
00078     {
00079         _hasNext   = true;
00080         _nextToken = _tokenString.substr(start, end - start);
00081         _currPos   = end;
00082     }  
00083 }
00084 
00085 /*-------------------------------------------------------------------------*/
00086 /*                             Destructor                                  */
00087 
00088 StringTokenizer::~StringTokenizer(void)
00089 {
00090 }
00091 
00092 /*-------------------------------------------------------------------------*/
00093 /*                              Tokenize                                   */
00094 
00095 bool StringTokenizer::hasNext(void)
00096 {
00097     return _hasNext;
00098 }
00099 
00100 std::string StringTokenizer::getNext(void)
00101 {
00102     std::string retString = _nextToken;
00103     if(!_hasNext)
00104     {
00105         return retString;
00106     }
00107     std::string::size_type start;
00108     std::string::size_type end;
00109     nextTokenPos(start, end);
00110     if((start == std::string::npos) ||
00111        (end   == std::string::npos)   )
00112     {
00113         _hasNext = false;
00114         return retString;
00115     }
00116     _nextToken = _tokenString.substr(start, end-start);
00117     _currPos   = end;
00118     return retString; 
00119 } 
00120 
00121 
00122 void StringTokenizer::setString(std::string &toTokens)
00123 {
00124     _currPos     = 0;
00125     _tokenString = toTokens;
00126 
00127     std::string::size_type start, end;
00128 
00129     nextTokenPos(start, end);
00130 
00131     if((start == std::string::npos) ||
00132        (end   == std::string::npos)  )
00133     {
00134         _hasNext = false;
00135     }
00136     else
00137     {
00138         _hasNext   = true;
00139         _nextToken = _tokenString.substr(start, end - start);
00140         _currPos   = end;
00141     }   
00142 }
00143 
00144 UInt32 StringTokenizer::countTokens(void)
00145 {
00146     std::string::size_type storePos     = _currPos;
00147     bool                   storeHasNext = _hasNext;
00148 
00149     UInt32 count = 0;
00150 
00151     std::string::size_type start;
00152     std::string::size_type end;
00153 
00154     _currPos = 0;
00155 
00156     nextTokenPos(start, end);
00157 
00158     if((start == std::string::npos) ||
00159        (end   == std::string::npos)  )
00160     {
00161         _hasNext = false;
00162     }
00163     else
00164     {
00165         _hasNext = true;
00166         _currPos = end;
00167     }
00168     
00169     while(_hasNext == true)
00170     {
00171         count++;
00172 
00173         nextTokenPos(start, end);
00174 
00175         if((start == std::string::npos) ||
00176            (end   == std::string::npos)  )
00177         {
00178             _hasNext = false;
00179         }
00180         else
00181         {
00182             _hasNext = true;
00183             _currPos = end;
00184         }
00185     }
00186 
00187     _currPos = storePos;
00188     _hasNext = storeHasNext;
00189 
00190     return count;
00191 }        
00192 
00193 void StringTokenizer::nextTokenPos(std::string::size_type &start,
00194                                    std::string::size_type &end  )
00195 {
00196     start = _tokenString.find_first_not_of(" \t\n", _currPos);
00197 
00198     if(start == std::string::npos)
00199     {
00200         end = std::string::npos;
00201 
00202         return;
00203     }
00204 
00205     end = _tokenString.find_first_of(" \t\n", start);
00206 
00207     if(end == std::string::npos)
00208     {
00209         end = _tokenString.length();
00210     }
00211 }
00212     
00213 /*-------------------------------------------------------------------------*/
00214 /*                              cvs id's                                   */
00215 
00216 #ifdef __sgi
00217 #pragma set woff 1174
00218 #endif
00219 
00220 #ifdef OSG_LINUX_ICC
00221 #pragma warning( disable : 177 )
00222 #endif
00223 
00224 namespace
00225 {
00226     static Char8 cvsid_cpp[] = "@(#)$Id: $";
00227     static Char8 cvsid_hpp[] = OSGSTRINGTOKENIZER_HEADER_CVSID;
00228     static Char8 cvsid_inl[] = OSGSTRINGTOKENIZER_INLINE_CVSID;
00229 }
00230 
00231 
00232 
00233 
00234 
00235 
00236 

Generated on Thu Aug 25 04:11:05 2005 for OpenSG by  doxygen 1.4.3