Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

utils.cpp

00001 //FILE:         utils.cpp
00002 //AUTHOR:       Nathan Cournia <nathan@cournia.com>
00003 
00004 #include <algorithm>
00005 #include <cctype>
00006 #include <unistd.h>
00007 #include <sys/stat.h>
00008 #include "utils.h"
00009 
00011 //some utils:: functions set this
00012 namespace utils { std::string error; }
00013 
00015 std::string
00016 utils::add_slash( const std::string& filename )
00017 {
00018         if( filename.length( ) > 0 ) {
00019                 //is last char a slash
00020                 if( filename[ filename.length( ) - 1 ] != '/' ) {
00021                         return filename + '/';
00022                 }
00023                 return filename;
00024         } else {
00025                 //empty string
00026                 return "/";
00027         }
00028 }
00029 
00031 void
00032 utils::to_upper( std::string& str )
00033 {
00034         std::transform( str.begin( ), str.end( ), str.begin( ),
00035                 static_cast<int(*)(int)>(std::toupper) );
00036 }
00037 
00039 std::string
00040 utils::to_upper( const std::string& in )
00041 {
00042         std::string str = in;
00043         std::transform( str.begin( ), str.end( ), str.begin( ),
00044                 static_cast<int(*)(int)>(std::toupper) );
00045         return str;
00046 }
00047 
00049 bool
00050 utils::stricmp( const std::string& a, const std::string& b )
00051 {
00052         return (utils::to_upper( a ) == utils::to_upper( b ));
00053 }
00054 
00056 //so slow . . .
00057 bool
00058 utils::check_extension( const std::string& file, const std::string& ext )
00059 {
00060         //look for .
00061         std::string::size_type pos = file.find_last_of( "." );
00062         if( pos == std::string::npos ) {
00063                 //file has no extension
00064                 return false;
00065         }
00066 
00067         std::string s1 = file.substr( pos );
00068         std::string s2 = "." + ext;
00069         to_upper( s1 );
00070         to_upper( s2 );
00071         return (s1 == s2);
00072 }
00073 
00075 std::string
00076 utils::trim( const std::string& str )
00077 {
00078         std::string::size_type pos = str.find_first_not_of( " \r\t\n", 0 );
00079         if( pos != std::string::npos ) {
00080                 return str.substr( pos );
00081         } else {
00082                 return "";
00083         }
00084 }
00085 
00087 std::string
00088 utils::rtrim( const std::string& str )
00089 {
00090         std::string::size_type pos = str.find_last_not_of( " \r\t\n", 
00091                 str.length( ) );
00092         if( pos != std::string::npos ) {
00093                 return str.substr( 0, pos + 1 );
00094         } else {
00095                 return "";
00096         }
00097 }

Generated on Tue Feb 11 18:49:42 2003 for uber by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002