util.cpp

Go to the documentation of this file.
00001 //======================================================================
00002 // File:        util.cpp
00003 // Author:      Matthias Toussaint
00004 // Created:     Sun Dec  3 16:27:25 CET 2006
00005 // Project:     QtDMM
00006 // Description: Utility class
00007 //----------------------------------------------------------------------
00008 // This file  may  be used under  the terms of  the GNU  General Public
00009 // License  version 2.0 as published   by the Free Software  Foundation
00010 // and appearing  in the file LICENSE.GPL included  in the packaging of
00011 // this file.
00012 // 
00013 // This file is provided AS IS with  NO WARRANTY OF ANY KIND, INCLUDING 
00014 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00015 // PURPOSE.
00016 //----------------------------------------------------------------------
00017 // Copyright 2006 Matthias Toussaint
00018 //======================================================================
00019 
00020 #include <util.h>
00021 
00022 TokenList Util::tokenize( const std::string & line, 
00023                           const std::string & separators )
00024                                               
00025 {
00026   TokenList tokenList;
00027   std::string token;
00028       
00029   for (unsigned i=0; i<line.size(); ++i)
00030   {
00031     unsigned s;
00032     for (s=0; s<separators.size(); ++s)
00033     {
00034       if (line[i] == separators[s])
00035       {
00036         tokenList.push_back( token );
00037         token = "";
00038         break;
00039       }
00040     }
00041     
00042     if (s>=separators.size()) token += line[i];
00043   }
00044   if (token.size()) tokenList.push_back( token );
00045   
00046   return tokenList;
00047 }
00048 
00049 std::string 
00050 Util::strip_whitespace( const std::string & str )
00051 {
00052   std::string retVal;
00053   
00054   unsigned start;
00055   for (start=0; start<str.size(); ++start)
00056   {
00057     if (str[start] != ' '  && str[start] != '\t' &&
00058         str[start] != '\r' && str[start] != '\n' &&
00059         str[start] != '\"')
00060     {
00061       break;
00062     }
00063   }
00064   
00065   unsigned stop;
00066   for (stop=str.size()-1; stop>0; --stop)
00067   {
00068     if (str[stop] != ' '  && str[stop] != '\t' &&
00069         str[stop] != '\r' && str[stop] != '\n' &&
00070         str[stop] != '\"')
00071     {
00072       break;
00073     }
00074   }
00075   
00076   return str.substr( start, stop-start+1 );
00077 }
00078 

Generated on Mon Jan 22 23:24:18 2007 for cdmm by  doxygen 1.4.6