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

utils.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //----------------------------------------------------------------------------- 
00006 //  UTILS.CPP
00007 //----------------------------------------------------------------------------- 
00013 //_____________________________________________________________________________
00014 //
00015 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00016 //
00017 //  This file is part of the XDFLengine project.
00018 //
00019 //  The XDFLengine is free software; you can redistribute it and/or modify
00020 //  it under the terms of the GNU General Public License as published by
00021 //  the Free Software Foundation; either version 2 of the License, or
00022 //  (at your option) any later version.
00023 //
00024 //  This program is distributed in the hope that it will be useful,
00025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 //  GNU General Public License for more details.
00028 //
00029 //  You should have received a copy of the GNU General Public License
00030 //  along with this program; if not, write to the Free Software
00031 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00032 //  USA.
00033 //
00034 //  For more information, 
00035 //      contact  : guillaume@baurand.net 
00036 //      or visit : http://xdflengine.sourceforge.net
00037 //
00038 //============================================================================= 
00039 
00040 #   include <ctype.h>
00041 // Xerces
00042 #   include "xercesc/sax2/XMLReaderFactory.hpp"
00043 #   include "xercesc/sax2/SAX2XMLReader.hpp"
00044 
00045 
00046 
00047 #   include "utils.hpp"
00048 #   include <time.h>
00049  
00050 BEGIN_XDFLENGINE_NS
00051 
00052 // XML STRING
00053 
00054     //_____________________________________________________________________
00055     //  XSTR
00056     //---------------------------------------------------------------------
00057     char* XSTR(const XMLCh* totrans,char* p_pszBuffer)
00058     {
00059         //DEBUG_FUNC(XSTR)
00060         unsigned int    l_uiLen;
00061         
00062         l_uiLen = XMLString::stringLen( totrans);
00063         if(!p_pszBuffer) p_pszBuffer = allocCharBuffer( l_uiLen);
00064         XMLString::transcode( totrans, p_pszBuffer, l_uiLen);
00065 
00066         return p_pszBuffer;
00067     }
00068 
00069 
00070     //_____________________________________________________________________
00071     //  XSTR
00072     //---------------------------------------------------------------------
00073     char* XSTR(XMLTranscoder* fXCoder, const XMLCh* const toFormat, const unsigned int count, char* p_pszBuffer)
00074     {
00075 
00076         unsigned int    charsEaten;
00077 
00078         if(!p_pszBuffer) p_pszBuffer = allocCharBuffer( count*2+2);
00079         const unsigned int outBytes = fXCoder->transcodeTo
00080         (
00081             toFormat
00082             , count
00083             , (unsigned char*)p_pszBuffer
00084             , count*2
00085             , charsEaten
00086             , XMLTranscoder::UnRep_RepChar
00087         );
00088         p_pszBuffer[ outBytes]='\0'; 
00089         return p_pszBuffer;
00090 
00091     }
00092 
00093     //_____________________________________________________________________
00094     //  XSTR
00095     //---------------------------------------------------------------------
00096     char* XSTR(XMLTranscoder* fXCoder, const XMLCh* const toFormat, char* p_pszBuffer)
00097     {
00098 
00099         return XSTR(fXCoder, toFormat,XMLString::stringLen(toFormat), p_pszBuffer);
00100     }
00101 
00102     //_____________________________________________________________________
00103     //  STRX
00104     //---------------------------------------------------------------------
00105     XMLCh* STRX(const char* totrans)
00106     {
00107         //DEBUG_FUNC(XSTR)
00108         return XMLString::transcode(totrans);
00109     }
00110 
00111     //_____________________________________________________________________
00112     //  CORRECTATTRVALUE
00113     //---------------------------------------------------------------------
00114     char* correctAttrValue(const char* src)
00115     {
00116         char* l_pszTmp;
00117         char* l_pszTmp2;
00118 
00119         l_pszTmp = replaceInCharBuffer( src,"<","&lt;");
00120         l_pszTmp2 = replaceInCharBuffer( l_pszTmp,">","&gt;");
00121         l_pszTmp = releaseCharBuffer( l_pszTmp);
00122         return l_pszTmp2;
00123     }
00124 
00125 
00126 // SAX 
00127 
00128     //_____________________________________________________________________
00129     //  SAX_PARSEXML
00130     //---------------------------------------------------------------------
00131     void SAX_parseXML(InputSource* p_pXMLDOM_inputSrc ,ContentHandler* p_pXMLSAX_handlers, ErrorHandler* p_pErrHandler )
00132     {
00133         //---------------------------------------------------------------------
00134         SAX2XMLReader*          l_pXMLSAX_parser=0;
00135         //---------------------------------------------------------------------
00136 
00137         DEBUG_IN(SAX_parseXML)
00138         
00139         //  Create a SAX parser object. 
00140         l_pXMLSAX_parser = XMLReaderFactory::createXMLReader();  
00141 
00142         l_pXMLSAX_parser->setFeature( XMLUni::fgXercesContinueAfterFatalError, false);
00143         l_pXMLSAX_parser->setFeature( XMLUni::fgXercesDynamic, true);
00144         l_pXMLSAX_parser->setFeature( XMLUni::fgXercesSchema, false);
00145         l_pXMLSAX_parser->setFeature( XMLUni::fgXercesSchemaFullChecking, false);
00146          
00147         l_pXMLSAX_parser->setFeature( XMLUni::fgSAX2CoreValidation, true);
00148         l_pXMLSAX_parser->setFeature( XMLUni::fgSAX2CoreNameSpaces, true);
00149         l_pXMLSAX_parser->setFeature( XMLUni::fgSAX2CoreNameSpacePrefixes, true);
00150          
00151 
00152         // Give handler to the parser
00153         l_pXMLSAX_parser->setContentHandler( p_pXMLSAX_handlers);
00154         l_pXMLSAX_parser->setErrorHandler  ( p_pErrHandler);
00155 
00156         // Parse !
00157         l_pXMLSAX_parser->parse( *p_pXMLDOM_inputSrc);
00158 
00159         // Release objects
00160         delete l_pXMLSAX_parser;
00161               
00162         DEBUG_OUT(SAX_parseXML)
00163     };
00164 
00165 
00166     //_____________________________________________________________________
00167     //  SAX_GETATTRVALUE
00168     //---------------------------------------------------------------------
00169     char* SAX_getAttrValue(const Attributes& p_attrs,const char * p_pzName, const char* p_pszDefValue,char* p_pzBuffer)
00170     {
00171         int l_iIndex;
00172         XMLCh*  l_pxmlchTmp;
00173 
00174         l_pxmlchTmp = STRX( p_pzName);
00175         l_iIndex = p_attrs.getIndex( l_pxmlchTmp);
00176         XMLString::release( &l_pxmlchTmp);
00177 
00178         if( l_iIndex>-1 )
00179         {
00180             return XSTR(p_attrs.getValue( l_iIndex), p_pzBuffer);
00181         }
00182         else
00183         {
00184             if(! p_pzBuffer) return copyCharBuffer( p_pszDefValue);
00185             else
00186             {
00187                 strcpy( p_pzBuffer, p_pszDefValue);
00188                 return p_pzBuffer;
00189             }
00190         }
00191     }
00192 
00193 // STRING PROCESSING
00194 
00195     //_____________________________________________________________________
00196     //  UPPERIZE
00197     //---------------------------------------------------------------------
00198     void upperize(char* cTxt)
00199     {
00200         for(unsigned int i=0; i<strlen(cTxt); i++) cTxt[i]=toupper(cTxt[i]);
00201     }
00202     void lowerize(char* cTxt)
00203     {
00204         for(unsigned int i=0; i<strlen(cTxt); i++) cTxt[i]=tolower(cTxt[i]);
00205     }
00206 
00207     
00208     //_____________________________________________________________________
00209     //  GETTIMESTRING
00210     //--------------------------------------------------------------------- 
00211     char* getTimeString(const char* p_cExpression)
00212     {
00213 
00214         time_t          start_time;
00215         struct tm       tm_start;
00216         char *          psDate;
00217         int             l_iSec;
00218         int             l_iMin;
00219         int             l_iHour;
00220         int             l_iDay;
00221         int             l_iMonth;
00222         int             l_iYear;        
00223 
00224         start_time = time(0);
00225         tm_start   = *localtime(&start_time);
00226 
00227         l_iYear =   1900+tm_start.tm_year ;
00228         l_iMonth =  tm_start.tm_mon+1 ;
00229         l_iDay =    tm_start.tm_mday ;
00230         l_iHour =   tm_start.tm_hour ;
00231         l_iMin =    tm_start.tm_min ;
00232         l_iSec =    tm_start.tm_sec ;
00233 
00234 
00235         psDate=(char*)  allocCharBuffer(40);
00236 
00237         if( ! strcmp(p_cExpression, DATEFORMAT_USER))
00238         {   // jj/mm/aaaa hh:mm:ss
00239             sprintf(psDate,     "%02d/%02d/%04d",       l_iDay, l_iMonth, l_iYear);
00240             sprintf(psDate+10,  " %02d:%02d:%02d",      l_iHour, l_iMin, l_iSec);
00241         }
00242 
00243         if( ! strcmp(p_cExpression, DATEFORMAT_FILE))
00244         {   // aaaammjj_hhmmss
00245             sprintf( psDate,    "%04d%02d%02d",         l_iYear, l_iMonth, l_iDay);
00246             sprintf( psDate+8,  "_%02d%02d%02d",        l_iHour, l_iMin, l_iSec);
00247         }
00248 
00249         if( ! strcmp(p_cExpression, DATEFORMAT_SQL))
00250         {   // aaaa-mm-ss hh:mm:ss
00251             sprintf( psDate,    "%04d-%02d-%02d",       l_iYear, l_iMonth, l_iDay);
00252             sprintf( psDate+10, " %02d:%02d:%02d",      l_iHour, l_iMin, l_iSec);
00253         }
00254 
00255         return psDate;
00256     }   
00257 
00258 
00259 
00260     //_____________________________________________________________________
00261     //  ALLOCCHARBUFFER
00262     //--------------------------------------------------------------------- 
00263     char* allocCharBuffer(unsigned int p_uiSize)
00264     {
00265         //if(p_uiSize<50) p_uiSize=50;
00266         //DEBUG_CREATE(charBuffer)
00267         return (char*) malloc( p_uiSize+1);
00268     }
00269 
00270     //_____________________________________________________________________
00271     //  RELEASECHARBUFFER
00272     //--------------------------------------------------------------------- 
00273     char* releaseCharBuffer(char* p_pszBuffer)
00274     {
00275         free( p_pszBuffer);
00276         //DEBUG_DEL(charBuffer)
00277         return (char*) 0;
00278     }
00279 
00280     //_____________________________________________________________________
00281     //  COPYCHARBUFFER
00282     //--------------------------------------------------------------------- 
00283     char* copyCharBuffer(const char* p_pszBuffer)
00284     {
00285         //------------------------------------------------------------------
00286         char*   l_pszNewBuffer = 0;
00287         //------------------------------------------------------------------
00288 
00289         if( p_pszBuffer)
00290         {
00291             l_pszNewBuffer = allocCharBuffer( strlen( p_pszBuffer));
00292             strcpy( l_pszNewBuffer, p_pszBuffer);
00293         }
00294         return l_pszNewBuffer;
00295     }
00296 
00297     //---------------------------------------------------------------------
00298     //  TRIMCHARBUFFER
00299     //---------------------------------------------------------------------
00300     char* trimCharBuffer( const char* p_pszToTrim)
00301     {
00302         //-----------------------------------------------------------------
00303         char*           p_pszTrimmed;
00304         char            l_chChar;
00305         bool            l_fOK;
00306         unsigned int    i;
00307         unsigned int    l_uiOutIndex;
00308         unsigned int    l_uiToTrimLen;
00309         //-----------------------------------------------------------------
00310         
00311         l_uiToTrimLen = strlen( p_pszToTrim);
00312         p_pszTrimmed = allocCharBuffer( l_uiToTrimLen);
00313         l_fOK = false;
00314         l_uiOutIndex = 0;
00315         
00316         // Trim begin
00317         for (i=0 ; i < l_uiToTrimLen; i++)
00318         {
00319             l_chChar = p_pszToTrim[i];
00320             if ( ((unsigned char)l_chChar > 32) || (l_fOK) )
00321             {
00322                 p_pszTrimmed[ l_uiOutIndex] = l_chChar;
00323                 l_uiOutIndex++;
00324                 l_fOK=true;
00325             }       
00326         }
00327         
00328         // Trim end         
00329         for (i=l_uiOutIndex ; i>0 ; i--)
00330         {
00331             l_chChar = p_pszTrimmed[i-1];
00332             if ( ((unsigned char)l_chChar) > 32) break;
00333         }
00334         p_pszTrimmed[i]='\0';
00335         
00336         return p_pszTrimmed;        
00337     }
00338 
00339 
00340     //_____________________________________________________________________
00341     //  IMPORTCHARBUFFER
00342     //--------------------------------------------------------------------- 
00343     char* importCharBuffer( char* p_pszOldBuffer,const char* p_pszNewBuffer)
00344     {
00345         char* l_pszBuffer;
00346 
00347         if( p_pszOldBuffer) 
00348         {
00349             if( strlen( p_pszOldBuffer) >= strlen( p_pszNewBuffer) )
00350             {
00351                 strcpy( p_pszOldBuffer, p_pszNewBuffer);
00352                 l_pszBuffer = p_pszOldBuffer;
00353             }
00354             else
00355             {
00356                 l_pszBuffer = (char*) realloc( p_pszOldBuffer, strlen(p_pszNewBuffer)+1);
00357                 strcpy( l_pszBuffer, p_pszNewBuffer);
00358             }
00359         }
00360         else
00361         {
00362             l_pszBuffer = copyCharBuffer( p_pszNewBuffer);  
00363         }
00364 
00365         return l_pszBuffer;
00366     }
00367 
00368     //_____________________________________________________________________
00369     //  EXPORTCHARBUFFER
00370     //--------------------------------------------------------------------- 
00371     const char* exportCharBuffer(char* l_pszBuffer)
00372     {
00373         return l_pszBuffer;
00374     }
00375 
00376     //_____________________________________________________________________
00377     //  CONCATCHARBUFFER
00378     //--------------------------------------------------------------------- 
00379     char* concatCharBuffer( char* p_pszBaseBuffer, const char* p_pszToAdd, unsigned int p_uiGrowSize)
00380     {
00381         unsigned int    l_uiBufSize;
00382         unsigned int    l_uiStrLen;
00383         unsigned int    l_uiAddLen;
00384         unsigned int    l_uiTotalLen;
00385         unsigned int    l_uiConcatSize;
00386         //unsigned int  l_uiNumGrows;
00387         char*           l_pszConcatBuf;
00388 
00389         if(p_pszToAdd)
00390         {
00391             l_uiAddLen = strlen( p_pszToAdd);
00392 
00393             if( p_pszBaseBuffer)
00394             {
00395                 l_uiStrLen = strlen( p_pszBaseBuffer);
00396                 //p_pszBaseBuffer[l_uiStrLen]=' ';
00397                 l_uiBufSize = strlen(p_pszBaseBuffer);
00398             }
00399             else
00400             {
00401                 l_uiBufSize = 0;
00402                 l_uiStrLen = 0;
00403             }
00404 
00405             l_uiTotalLen = l_uiStrLen + l_uiAddLen;
00406 
00407             if( l_uiBufSize < l_uiTotalLen)
00408             {
00409                 //l_uiNumGrows = (l_uiTotalLen - l_uiBufSize) / p_uiGrowSize + 1;
00410                 //l_uiConcatSize = l_uiBufSize + p_uiGrowSize * l_uiNumGrows;
00411                 l_uiConcatSize = l_uiTotalLen;
00412                 l_pszConcatBuf = (char*) realloc( p_pszBaseBuffer, l_uiConcatSize+1);
00413                 //memset( l_pszConcatBuf + l_uiStrLen,' ',l_uiTotalLen-l_uiStrLen);
00414                 //l_pszConcatBuf[l_uiConcatSize]='\0';
00415             }
00416             else
00417             {
00418                 if(p_pszBaseBuffer) l_pszConcatBuf = p_pszBaseBuffer;
00419                 else l_pszConcatBuf = allocCharBuffer(1);
00420             }
00421 
00422             memcpy( l_pszConcatBuf + l_uiStrLen, p_pszToAdd, l_uiAddLen);   
00423             l_pszConcatBuf[l_uiStrLen+l_uiAddLen]='\0';
00424             return l_pszConcatBuf;
00425         }
00426         else return p_pszBaseBuffer;
00427     }
00428 
00429 
00430     //_____________________________________________________________________
00431     //  REPLACEINCHARBUFFER
00432     //---------------------------------------------------------------------
00433     char* replaceInCharBuffer(const char* src, const char *srch, const char *rpl)
00434     {
00435         const char *    p = NULL;
00436         char            c[2];
00437         size_t          srclen = 0;
00438         size_t          srchlen = 0;
00439         size_t          rpllen = 0;
00440         size_t          x = 0;
00441         char*           ret = 0;
00442         unsigned int    i=0;
00443             
00444         /* Initialize */
00445 
00446         ret=copyCharBuffer("");
00447         srclen= strlen( src);
00448         srchlen = strlen(srch);
00449         rpllen = strlen(rpl);
00450         c[1]='\0';
00451 
00452         for (i=0; i < srclen ; i++) 
00453         {
00454         
00455             p= (const char*) &(src[i]);
00456             if ( strncmp(p, srch, srchlen) == 0)
00457             {
00458                 /* Match found, add replacement in */
00459                 ret = concatCharBuffer( ret, rpl);          
00460                 i += (srchlen - 1);
00461             } 
00462             else
00463             {
00464                 /* No match, add single character back in */
00465                 c[0]=(char)p[0];
00466                 ret = concatCharBuffer( ret, &c[0]);
00467             }
00468         }
00469 
00470         return ret;
00471     }
00472 
00473 END_XDFLENGINE_NS
00474 
00475 

Generated on Sat Oct 4 13:20:02 2003 for XDFLengine by doxygen1.3-rc2