csutil/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 "util.h" 00026 #include "array.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, int 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 strcasecmp (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 int FindSortedKey (csArrayCmpDecl(char const*, char const*) comparekey, 00100 int* candidate = 0) const 00101 { 00102 return superclass::FindSortedKey(comparekey, candidate); 00103 } 00104 00109 int FindSortedKey (char const* key, bool case_sensitive = true, 00110 int* 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 00122 char* Pop () 00123 { 00124 CS_ASSERT (Length () > 0); 00125 int l = Length () - 1; 00126 char* ret = (char*)Get (l); 00127 InitRegion (l, 1); 00128 SetLength (l); 00129 return ret; 00130 } 00131 00136 int Find (const char* what) const 00137 { 00138 for (int i = 0; i < Length (); i++) 00139 if (! strcmp (Get (i), what)) 00140 return i; 00141 return -1; 00142 } 00143 00148 int FindCaseInsensitive (const char* what) const 00149 { 00150 for (int i = 0; i < Length (); i++) 00151 if (!strcasecmp (Get (i), what)) 00152 return i; 00153 return -1; 00154 } 00155 }; 00156 00157 #endif // __CS_STRINGARRAY_H__
Generated for Crystal Space by doxygen 1.2.18