cmtkStrUtility.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2152 $
00026 //
00027 //  $LastChangedDate: 2010-08-04 10:19:33 -0700 (Wed, 04 Aug 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkStrUtility.h"
00034 
00035 #include <string.h>
00036 #include <stdlib.h>
00037 #include <limits.h>
00038 
00039 #ifdef _MSC_VER
00040 #  include <direct.h>
00041 #endif
00042 
00043 namespace
00044 cmtk
00045 {
00046 
00049 
00050 int
00051 StrCmp( const char* s1, const char* s2 )
00052 {
00053   if ( s1 == NULL ) 
00054     {
00055     if ( s2 == NULL ) return 0;
00056     else return -1;
00057     } 
00058   else
00059     {
00060     if ( s2 == NULL ) return 1;
00061     else return strcmp( s1, s2 );
00062     }
00063 }
00064 
00065 void
00066 StrReplace( char*& s1, const char* s2 )
00067 {
00068   StrFree( s1 );
00069   if ( s2 )
00070     s1 = strdup( s2 );
00071   else
00072     s1 = NULL;
00073 }
00074 
00075 void StrFree( char *const s )
00076 {
00077   free( s );
00078 }
00079 
00080 int StrPrefixCmp( const char *s, const char* prefix )
00081 {
00082   return (0 == strncmp( s, prefix, strlen( prefix ) ));
00083 }
00084 
00085 static char StrBuffer[PATH_MAX];
00086 
00087 const char*
00088 StrDir( const char *path )
00089 {
00090   const char *slash = strrchr( path, CMTK_PATH_SEPARATOR );
00091   if ( slash && (slash != path) ) 
00092     {
00093     int dirLen = (slash-path);
00094     strncpy( StrBuffer, path, dirLen );
00095     StrBuffer[dirLen] = 0;
00096     } 
00097   else
00098     {
00099     if ( slash )
00100       strcpy( StrBuffer, CMTK_PATH_SEPARATOR_STR );
00101     else
00102       strcpy( StrBuffer, path );
00103     }
00104   return StrBuffer;
00105 }
00106 
00107 const char*
00108 StrFName( const char *path )
00109 {
00110   const char *slash = strrchr( path, CMTK_PATH_SEPARATOR );
00111   if ( slash )
00112     {
00113     strcpy( StrBuffer, slash+1 );
00114     } 
00115   else
00116     {
00117     StrBuffer[0] = 0;
00118     }
00119   return StrBuffer;
00120 }
00121 
00122 std::string
00123 StrReplace
00124 ( const std::string& str, const std::map<std::string,std::string>& rules, const bool multiple )
00125 {
00126   std::string result = str;
00127   
00128   std::map<std::string,std::string>::const_iterator it = rules.begin();
00129   while ( it != rules.end() ) 
00130     {
00131     bool replaced = true;
00132     while ( replaced ) 
00133       {
00134       replaced = false;
00135       std::string::size_type pos = result.find( it->first );
00136       while ( pos != std::string::npos ) 
00137         {
00138         result.replace( pos, it->first.length(), it->second );
00139         replaced = true;
00140         pos = result.find( it->first );
00141         if ( ! multiple ) break;
00142         }
00143       
00144       if ( ! multiple ) break;
00145       }
00146     ++it;
00147     }
00148   return result;
00149 }
00150 
00151 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines