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

OSGIDString.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 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include "OSGConfig.h"
00044 
00045 #include <string.h>
00046 #include <ctype.h>
00047 
00048 
00049 #include "OSGIDString.h"
00050 
00051 OSG_USING_NAMESPACE
00052 
00053 /*-------------------------- Constructor ----------------------------------*/
00054 
00055 IDString::IDString(UInt32 size) : 
00056     _str    (NULL), 
00057     _memType(COPY)
00058 {
00059     setLength(size);
00060 }
00061 
00062 IDString::IDString(const Char8 *str, MemType memType) : 
00063     _str    (NULL   ), 
00064     _memType(memType)
00065 {
00066     set(str, memType);
00067 }
00068 
00069 IDString::IDString(const IDString &obj, MemType memType) :
00070     _str    (NULL   ), 
00071     _memType(memType)
00072 {
00073     set(obj._str, memType);
00074 }
00075 
00076 
00077 /*-------------------------- Destructor -----------------------------------*/
00078 
00079 IDString::~IDString()
00080 {
00081     if(_memType == COPY)
00082         delete [] _str;
00083 }
00084 
00085 
00086 /*------------------------------ access -----------------------------------*/
00087 
00088 void IDString::set(const Char8 *str, MemType memType)
00089 {
00090     if(str == _str)
00091     {
00092         // !!! can you change _memType here? I think not. IMHO
00093         return;
00094     }
00095 
00096     if(_memType == COPY)
00097         delete [] _str;
00098 
00099     if(memType == COPY)
00100     {
00101         if(str != NULL)
00102         {
00103             _str = new Char8[strlen(str) + 1];
00104 
00105             strcpy(_str, str);
00106         }
00107         else
00108         {
00109             _str = NULL;
00110         }
00111     }
00112     else
00113     {
00114         _str = const_cast<Char8 *>(str);
00115     }
00116 
00117     _memType = memType;
00118 }
00119 
00120 #ifdef OSG_WIN32_ICL
00121 #pragma warning ( disable : 810 )
00122 #endif
00123 
00124 void IDString::toUpper(void)
00125 {
00126     UInt32 l = getLength();
00127 
00128     for(UInt32 i = 0; i < l; ++i)
00129     {
00130         _str[i] = ::toupper(_str[i]);
00131     }
00132 
00133 }
00134 
00135 void IDString::toLower(void)
00136 {
00137     UInt32 l = getLength();
00138 
00139     for(UInt32 i = 0; i < l; ++i)
00140     {
00141         _str[i] = ::tolower(_str[i]);
00142     }
00143 
00144 }
00145 
00146 #ifdef OSG_WIN32_ICL
00147 #pragma warning ( default : 810 )
00148 #endif
00149 
00150 UInt32 IDString::getLength(void) const
00151 {
00152     return (_str != NULL) ? ::strlen(_str) : 0;
00153 }
00154 
00155 void IDString::setLength(UInt32 length)
00156 {
00157     if(_memType == COPY)
00158         delete [] _str;
00159 
00160     if(length != 0)
00161     {
00162          _str = new Char8[length];
00163         *_str = 0;
00164     }
00165     else
00166     {
00167         _str = 0;
00168     }
00169 
00170     _memType = COPY;
00171 }
00172 
00173 void IDString::tokenize(StringVec &v)
00174 {
00175     UInt32 l        = getLength();
00176     UInt32 oldpos   = 0;
00177     UInt32 pos      = 0;
00178     bool   inQuotes = false;
00179     bool   inToken  = false;
00180 
00181     if(l > 0)
00182     {
00183         Char8 *buf = new Char8[l + 1];
00184 
00185         for(pos = 0; pos <= l; ++pos)
00186         {
00187             if(!inQuotes)
00188             {
00189                 if(!inToken)
00190                 {
00191                     if(_str[pos] == '"')
00192                     {
00193                         inQuotes = true;
00194                         oldpos   = pos + 1;
00195                     }
00196                     else if(_str[pos] != ' ')
00197                     {
00198                         inToken = true;
00199                         oldpos  = pos;
00200                     }
00201                 }
00202                 else if(inToken)
00203                 {
00204                     if(_str[pos] == '"')
00205                     {
00206                         inToken = false;
00207 
00208                         strncpy(buf, _str + oldpos, pos - oldpos);
00209 
00210                         buf[pos - oldpos] = '\0';
00211 
00212                         v.push_back(IDString(buf));
00213 
00214                         inQuotes = true;
00215                         oldpos   = pos;
00216                     }
00217                     else if(_str[pos] == ' ')
00218                     {
00219                         inToken = false;
00220 
00221                         strncpy(buf, _str + oldpos, pos - oldpos);
00222 
00223                         buf[pos - oldpos] = '\0';
00224 
00225                         v.push_back(IDString(buf));
00226                     }
00227                     else if(pos == l)
00228                     {
00229                         strncpy(buf, _str + oldpos, pos - oldpos);
00230 
00231                         buf[pos - oldpos] = '\0';
00232 
00233                         v.push_back(IDString(buf));
00234                     }
00235                 }
00236             }
00237             else if(inQuotes)
00238             {
00239                 if(_str[pos] == '"')
00240                 {
00241                     inQuotes = false;
00242 
00243                     if(pos > oldpos)
00244                     {
00245                         strncpy(buf, _str + oldpos, pos - oldpos);
00246 
00247                         buf[pos - oldpos] = '\0';
00248 
00249                         v.push_back(IDString(buf));
00250                     }
00251                 }
00252                 else if(pos == l)
00253                 {
00254                     if(pos > oldpos)
00255                     {
00256                         strncpy(buf, _str + oldpos, pos - oldpos);
00257 
00258                         buf[pos - oldpos] = '\0';
00259 
00260                         v.push_back(IDString(buf));
00261                     }
00262                 }
00263             }
00264         }
00265 
00266         delete [] buf;
00267     }
00268 }
00269 
00270 void IDString::tokenize(StringPVec &v)
00271 {
00272     UInt32 l        = getLength();
00273     UInt32 oldpos   = 0;
00274     UInt32 pos      = 0;
00275     bool   inQuotes = false;
00276     bool   inToken  = false;
00277 
00278     IDString *pString = NULL;
00279 
00280     if(l > 0)
00281     {
00282         Char8 *buf = new Char8[l + 1];
00283 
00284         for(pos = 0; pos <= l; ++pos)
00285         {
00286             if(!inQuotes)
00287             {
00288                 if(!inToken)
00289                 {
00290                     if(_str[pos] == '"')
00291                     {
00292                         inQuotes = true;
00293                         oldpos   = pos + 1;
00294                     }
00295                     else if(_str[pos] != ' ')
00296                     {
00297                         inToken = true;
00298                         oldpos  = pos;
00299                     }
00300                 }
00301                 else if(inToken)
00302                 {
00303                     if(_str[pos] == '"')
00304                     {
00305                         inToken = false;
00306 
00307                         strncpy(buf, _str + oldpos, pos - oldpos);
00308 
00309                         buf[pos - oldpos] = '\0';
00310 
00311                         pString =  new IDString(buf);
00312 
00313                         v.push_back(pString);
00314 
00315                         inQuotes = true;
00316                         oldpos   = pos;
00317 
00318                     }
00319                     else if(_str[pos] == ' ')
00320                     {
00321                         inToken = false;
00322 
00323                         strncpy(buf, _str + oldpos, pos - oldpos);
00324 
00325                         buf[pos - oldpos] = '\0';
00326 
00327                         pString =  new IDString(buf);
00328 
00329                         v.push_back(pString);
00330                     }
00331                     else if(pos == l)
00332                     {
00333                         strncpy(buf, _str + oldpos, pos - oldpos);
00334 
00335                         buf[pos - oldpos] = '\0';
00336 
00337                         pString =  new IDString(buf);
00338 
00339                         v.push_back(pString);
00340                     }
00341                 }
00342             }
00343             else if(inQuotes)
00344             {
00345                 if(_str[pos] == '"')
00346                 {
00347                     inQuotes = false;
00348 
00349                     if(pos > oldpos)
00350                     {
00351                         strncpy(buf, _str + oldpos, pos - oldpos);
00352 
00353                         buf[pos - oldpos] = '\0';
00354 
00355                         pString =  new IDString(buf);
00356 
00357                         v.push_back(pString);
00358                     }
00359                 }
00360                 else if(pos == l)
00361                 {
00362                     if(pos > oldpos)
00363                     {
00364                         strncpy(buf, _str + oldpos, pos - oldpos);
00365 
00366                         buf[pos - oldpos] = '\0';
00367 
00368                         pString =  new IDString(buf);
00369 
00370                         v.push_back(pString);
00371                     }
00372                 }
00373             }
00374         }
00375 
00376         delete [] buf;
00377     }
00378 }
00379 
00380 
00381 OSG_BEGIN_NAMESPACE
00382 
00383 OSG_BASE_DLLMAPPING std::ostream &operator <<(      std::ostream &os, 
00384                                               const IDString     &obj)
00385 {
00386     return os << (obj.str() ? obj.str() : "0 IDString");
00387 }
00388 
00389 OSG_END_NAMESPACE

Generated on Thu Aug 25 04:06:00 2005 for OpenSG by  doxygen 1.4.3