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 "util.h" 00025 #include "array.h" 00026 00027 class csStringArrayElementHandler 00028 { 00029 public: 00030 static void Construct (const char** address, const char* const& src) 00031 { 00032 *address = csStrNew (src); 00033 } 00034 00035 static void Destroy (const char** address) 00036 { 00037 delete[] (char*)*address; 00038 } 00039 00040 static void InitRegion (const char** address, int count) 00041 { 00042 memset (address, 0, count*sizeof (const char*)); 00043 } 00044 }; 00045 00050 class csStringArray : public csArray<const char*, csStringArrayElementHandler> 00051 { 00052 typedef csArray<const char*, csStringArrayElementHandler> superclass; 00053 public: 00058 csStringArray (int ilimit = 0, int ithreshold = 0) 00059 : superclass(ilimit, ithreshold) 00060 { 00061 } 00062 00063 static int CaseSensitiveCompareKey (char const* const& item1, void* p) 00064 { 00065 char const* item2 = (char const*)p; 00066 return strcmp (item1, item2); 00067 } 00068 00069 static int CaseInsensitiveCompareKey (char const* const& item1, void* p) 00070 { 00071 char const* item2 = (char const*)p; 00072 return strcasecmp (item1, item2); 00073 } 00074 00075 static int CaseSensitiveCompare (const char* const &item1, const char* const &item2) 00076 { 00077 return strcmp (item1, item2); 00078 } 00079 00080 static int CaseInsensitiveCompare (const char* const &item1, const char* const &item2) 00081 { 00082 return strcasecmp (item1, item2); 00083 } 00084 00085 static int CaseSensitiveCompareSort (void const* item1, void const* item2) 00086 { 00087 return strcmp (*(char const**)item1, *(char const**)item2); 00088 } 00089 00090 static int CaseInsensitiveCompareSort (void const* item1, void const* item2) 00091 { 00092 return strcasecmp (*(char const**)item1, *(char const**)item2); 00093 } 00094 00098 void Sort (ArraySortCompareFunction* compare) 00099 { 00100 superclass::Sort (compare); 00101 } 00102 00106 void Sort () 00107 { 00108 superclass::Sort (CaseSensitiveCompareSort); 00109 } 00110 00115 int FindSortedKey (void* key, ArrayCompareKeyFunction* comparekey 00116 = CaseSensitiveCompareKey, int* candidate = 0) const 00117 { 00118 return superclass::FindSortedKey(key, comparekey, candidate); 00119 } 00120 00125 char* Pop () 00126 { 00127 CS_ASSERT (Length () > 0); 00128 int l = Length () - 1; 00129 char* ret = (char*)Get (l); 00130 InitRegion (l, 1); 00131 SetLength (l); 00132 return ret; 00133 } 00134 00139 int Find (const char* what) const 00140 { 00141 for (int i = 0; i < Length (); i++) 00142 if (! strcmp (Get (i), what)) 00143 return i; 00144 return -1; 00145 } 00146 00151 int FindCaseInsensitive (const char* what) const 00152 { 00153 for (int i = 0; i < Length (); i++) 00154 if (!strcasecmp (Get (i), what)) 00155 return i; 00156 return -1; 00157 } 00158 00163 int InsertSorted (const char* const &newstr, ArrayCompareFunction* compare 00164 = CaseSensitiveCompare, int* equal_index = 0) 00165 { 00166 return superclass::InsertSorted(newstr, compare, equal_index); 00167 } 00168 }; 00169 00170 #endif // __CS_STRINGARRAY_H__
Generated for Crystal Space by doxygen 1.2.18