| # Gaudi Framework, version v21r8 |

Home |  | Generated: 17 Mar 2010

# Tokenizer.cpp

Go to the documentation of this file.

    00001 // $Header: /tmp/svngaudi/tmp.jEpFh25751/Gaudi/GaudiKernel/src/Lib/Tokenizer.cpp,v 1.4 2008/10/27 16:41:33 marcocle Exp $
    00002 //==============================================================================
    00003 // Tokenizer.h
    00004 //------------------------------------------------------------------------------
    00005 //
    00006 // Package : Kernel
    00007 //             The LHCb definition package
    00008 //
    00009 // Author  : M.Frank
    00010 //
    00011 // Changes : M.Frank ,  01/10/00: Initiali version
    00012 //
    00013 //==============================================================================
    00014 #define KERNEL_TOKENIZER_CPP
    00015 #include "GaudiKernel/Tokenizer.h"
    00016 #include "GaudiKernel/Environment.h"
    00017
    00018 #include <cstring>
    00019
    00020 // Assignment operator
    00021 Tokenizer::Token& Tokenizer::Token::operator=(const Token& copy)     {
    00022   m_length=copy.m_length;
    00023   m_tag=copy.m_tag;
    00024   m_value=copy.m_value;
    00025   return *this;
    00026 }
    00027
    00028 // Resolve value from environment
    00029 void Tokenizer::Token::resolveValue()  {
    00030   std::string res;
    00031   if ( System::resolveEnv(m_value, res).isSuccess() )  {
    00032     m_value = res;
    00033   }
    00034 }
    00035
    00036 // Create tokens from string
    00037 void Tokenizer::analyse(const std::string& s, const char* delim, const char* tagBegin, const char* tagEnd, const char* eq, const char* valBegin, const char* valEnd)    {
    00038   m_tokens.clear();
    00039   if ( valEnd == 0 ) valEnd = valBegin;
    00040   long start = 0;
    00041   Token tok;
    00042   do {
    00043     tok.make(s, start, delim, tagBegin, tagEnd, eq, valBegin, valEnd);
    00044     if ( tok.length() > 0 )   {
    00045       start += tok.length();
    00046       m_tokens.push_back(tok);
    00047     }
    00048     else  {
    00049       start += s.length();
    00050     }
    00051   } while ( start < long(s.length()) );
    00052   if ( m_resolve )  {
    00053     for(Items::iterator i=m_tokens.begin(); i != m_tokens.end(); ++i)  {
    00054       (*i).resolveValue();
    00055     }
    00056   }
    00057 }
    00058
    00059 // Create token from string
    00060 void Tokenizer::Token::make(const std::string& s, long st, const char* delim, const char* tagBeg, const char* tagEnd, const char* eq, const char* valBeg, const char* valEnd)    {
    00061   long lenTagBeg   = ::strlen(tagBeg);
    00062   long lenTagEnd   = ::strlen(tagEnd);
    00063   long lenValBeg   = ::strlen(valBeg);
    00064   long lenValEnd   = ::strlen(valEnd);
    00065   long lenDelim    = ::strlen(delim);
    00066   long lenEq       = ::strlen(eq);
    00067   long slen        = s.length();
    00068   long start       = st;
    00069   while(::strncmp(s.c_str()+start,delim,lenDelim)==0) start += lenDelim;
    00070   long posTag      = (lenTagBeg>0) ? s.find(tagBeg, start) + lenTagBeg : start;
    00071   long posDelim    = s.find(delim,posTag) > 0 ? s.find(delim,posTag) : slen;
    00072   long posEq       = s.find(eq, posTag)   > 0 ? s.find(eq, posTag) : posDelim-posTag;
    00073   long lenTag      = (lenTagEnd>0)   ? s.find(tagEnd, posTag)-posTag : (posEq>0) ? (posEq>posTag) ? posEq-posTag : posDelim-posTag : posDelim;
    00074   posEq            = s.find(eq, posTag+lenTag+lenTagEnd)+lenEq;
    00075   long posVal      = (lenValBeg>0)   ? s.find(valBeg, posEq)+lenValBeg : posEq;
    00076   posDelim         = (lenDelim>0)    ? s.find(delim, posVal+lenValBeg) : slen;
    00077   long lenVal      = (lenValEnd>0)   ? s.find(valEnd, posVal)-posVal : ((posDelim>=0) ? posDelim : slen)-posVal;
    00078
    00079   m_tag = m_value  = "";
    00080   // Ooops: Valid tag found:
    00081   if ( start  >= 0 && posTag >= 0 )   {
    00082     m_tag    = s.substr(posTag, lenTag);
    00083     m_length = posTag+m_tag.length()+lenTagEnd-st;
    00084   }
    00085   // Ooops: there is also a value:
    00086   if ( posVal >= 0 && lenVal >= 0 )   {
    00087     m_value  = s.substr(posVal, lenVal);
    00088     m_length = posVal+m_value.length()+lenValEnd-st;
    00089   }
    00090   while(::strncmp(s.c_str()+start+m_length,delim,lenDelim)==0) m_length += lenDelim;
    00091 }
    00092