sprint 1-alpha
sprint/container/const_string.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::const_string
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00027 #ifndef _SPRINT_CONST_STRING_H
00028 #define _SPRINT_CONST_STRING_H
00029 
00030 #include <sprint/sysdef.h> // strcasecmp
00031 
00032 namespace sprint { 
00033           
00034  namespace detail {
00035 
00041 template<bool case_sensitive> 
00042 class const_string {
00044   const char *m_str;
00045   
00046   public:
00047     const_string(const char *_str) : m_str(_str) { }
00048     
00050     inline operator bool () const 
00051                 { 
00052                         return m_str!=NULL; 
00053                 }
00054 
00056     inline operator const char * () const 
00057                 { 
00058                         return m_str; 
00059                 }
00060 
00062     inline const char *c_str() const 
00063       { 
00064         return m_str; 
00065        }
00066 
00068     static int compare(const char *a, const char *b)
00069       {
00070       // null case
00071       if(a==NULL && b==NULL) return 0;
00072       if(a==NULL && b!=NULL) return 1;
00073       if(a!=NULL && b==NULL) return -1;
00074       
00075       return (case_sensitive) ? ( ::strcmp(a,b) ) : ( strcasecmp(a,b) );
00076       }  
00077 
00078     inline 
00079     bool operator == (const char *c) const
00080       {
00081       return compare(m_str, c)==0;
00082       }
00083     
00084     inline
00085     bool operator == (const const_string<case_sensitive> & s) const
00086       {
00087       return compare(m_str, s.m_str)==0;
00088       }
00089       
00090     inline
00091     bool operator < (const char *string) const
00092     {
00093       return compare(m_str, string)<0;
00094     }
00095     inline
00096     bool operator < (const const_string<case_sensitive> & string) const
00097     {
00098       return compare(m_str, string.m_str)<0;
00099     }
00100     
00101     inline
00102     bool operator > (const char *string) const
00103     {
00104       return compare(m_str, string)>0;
00105     }
00106 
00107     inline
00108     bool operator > (const const_string<case_sensitive> & string) const
00109     {
00110       return compare(m_str, string.m_str)>0;
00111     }
00112     
00113 }; //
00114 
00115 } // detail
00116 
00118 typedef detail::const_string<true> const_string;
00119 
00121 typedef detail::const_string<false> const_istring;
00122 
00123 } // sprint
00124     
00125 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines