CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

stringarray.h

00001 /*
00002   Crystal Space String Array
00003   Copyright (C) 2003 by Jorrit Tyberghein
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Library General Public
00007   License as published by the Free Software Foundation; either
00008   version 2 of the License, or (at your option) any later version.
00009 
00010   This library 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 GNU
00013   Library General Public License for more details.
00014 
00015   You should have received a copy of the GNU Library General Public
00016   License along with this library; if not, write to the Free
00017   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_STRINGARRAY_H__
00021 #define __CS_STRINGARRAY_H__
00022 
00023 #include <stdarg.h>
00024 #include "csextern.h"
00025 #include "array.h"
00026 #include "util.h"
00027 
00028 class csStringArrayElementHandler
00029 {
00030 public:
00031   static void Construct (const char** address, const char* const& src)
00032   {
00033     *address = csStrNew (src);
00034   }
00035 
00036   static void Destroy (const char** address)
00037   {
00038     delete[] (char*)*address;
00039   }
00040 
00041   static void InitRegion (const char** address, size_t count)
00042   {
00043     memset (address, 0, count*sizeof (const char*));
00044   }
00045 };
00046 
00051 class csStringArray : public csArray<const char*, csStringArrayElementHandler>
00052 {
00053   typedef csArray<const char*, csStringArrayElementHandler> superclass;
00054 public:
00059   csStringArray (int ilimit = 0, int ithreshold = 0)
00060         : superclass(ilimit, ithreshold)
00061   {
00062   }
00063 
00064   static int CaseSensitiveCompare (const char* const &item1,
00065                                    const char* const &item2)
00066   {
00067     return strcmp (item1, item2);
00068   }
00069 
00070   static int CaseInsensitiveCompare (const char* const &item1,
00071                                      const char* const &item2)
00072   {
00073     return csStrCaseCmp (item1, item2);
00074   }
00075 
00079   void Sort (int(*compare)(char const* const&, char const* const&))
00080   {
00081     superclass::Sort (compare);
00082   }
00083 
00087   void Sort (bool case_sensitive = true)
00088   {
00089     if (case_sensitive)
00090       Sort (CaseSensitiveCompare);
00091     else
00092       Sort (CaseInsensitiveCompare);
00093   }
00094 
00099   size_t FindSortedKey (csArrayCmpDecl(char const*, char const*) comparekey,
00100     size_t* candidate = 0) const
00101   {
00102     return superclass::FindSortedKey(comparekey, candidate);
00103   }
00104 
00109   size_t FindSortedKey (char const* key, bool case_sensitive = true,
00110     size_t* candidate = 0) const
00111   {
00112     int(*cf)(char const* const&, char const* const&) =
00113       case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare;
00114     return FindSortedKey(csArrayCmp<char const*, char const*>(key, cf),
00115       candidate);
00116   }
00117 
00118 
00123   size_t InsertSorted (const char* item, bool case_sensitive = true,
00124     size_t* equal_index = 0)
00125   {
00126     int(*cf)(char const* const&, char const* const&) =
00127       case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare;
00128     return superclass::InsertSorted (item, cf, equal_index);
00129   }
00130 
00131 
00136   char* Pop ()
00137   {
00138     CS_ASSERT (Length () > 0);
00139     size_t l = Length () - 1;
00140     char* ret = (char*)Get (l);
00141     InitRegion (l, 1);
00142     SetLength (l);
00143     return ret;
00144   }
00145 
00150   size_t Find (const char* what) const
00151   {
00152     for (size_t i = 0; i < Length (); i++)
00153       if (! strcmp (Get (i), what))
00154         return i;
00155     return (size_t)-1;
00156   }
00157 
00162   size_t FindCaseInsensitive (const char* what) const
00163   {
00164     for (size_t i = 0; i < Length (); i++)
00165       if (!csStrCaseCmp (Get (i), what))
00166         return i;
00167     return (size_t)-1;
00168   }
00169 };
00170 
00171 #endif // __CS_STRINGARRAY_H__

Generated for Crystal Space by doxygen 1.3.9.1