xrootd
PathProcessor.hh
Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
00003 // Author: Lukasz Janyst <ljanyst@cern.ch>
00004 //------------------------------------------------------------------------------
00005 // XRootD is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // XRootD is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with XRootD.  If not, see <http://www.gnu.org/licenses/>.
00017 //------------------------------------------------------------------------------
00018 
00019 #ifndef EOS_NS_PATH_PROCESSOR_HH
00020 #define EOS_NS_PATH_PROCESSOR_HH
00021 
00022 #include <cstring>
00023 #include <vector>
00024 #include <string>
00025 
00026 namespace eos
00027 {
00028   //----------------------------------------------------------------------------
00030   //----------------------------------------------------------------------------
00031   class PathProcessor
00032   {
00033     public:
00034       //------------------------------------------------------------------------
00037       //------------------------------------------------------------------------
00038       static void splitPath( std::vector<std::string> &elements,
00039                              const std::string &path )
00040       {
00041         elements.clear();
00042         std::vector<char*> elems;
00043         char buffer[path.length()+1];
00044         strcpy( buffer, path.c_str() );
00045         splitPath( elems, buffer );
00046         for( size_t i = 0; i < elems.size(); ++i )
00047           elements.push_back( elems[i] );
00048       }
00049 
00050       //------------------------------------------------------------------------
00053       //------------------------------------------------------------------------
00054       static void splitPath( std::vector<char*> &elements, char *buffer )
00055       {
00056         elements.clear();
00057         elements.reserve( 10 );
00058 
00059         char *cursor = buffer;
00060         char *beg    = buffer;
00061 
00062         //----------------------------------------------------------------------
00063         // Go by the characters one by one
00064         //----------------------------------------------------------------------
00065         while( *cursor )
00066         {
00067           if( *cursor == '/' )
00068           {
00069             *cursor = 0;
00070             if( beg != cursor )
00071               elements.push_back( beg );
00072             beg = cursor+1;
00073           }
00074           ++cursor;
00075         }
00076 
00077         if( beg != cursor )
00078           elements.push_back( beg );
00079       }
00080   };
00081 }
00082 
00083 #endif // EOS_NS_PATH_PROCESSOR_HH