csutil/stringarray.h
Go to the documentation of this file.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 00027 #include <stdarg.h> 00028 #include "csextern.h" 00029 #include "csutil/array.h" 00030 #include "csutil/util.h" 00031 00032 class csStringArrayElementHandler 00033 { 00034 public: 00035 static void Construct (const char** address, const char* const& src) 00036 { 00037 *address = csStrNew (src); 00038 } 00039 00040 static void Destroy (const char** address) 00041 { 00042 delete[] (char*)*address; 00043 } 00044 00045 static void InitRegion (const char** address, size_t count) 00046 { 00047 memset (address, 0, count*sizeof (const char*)); 00048 } 00049 }; 00050 00055 class csStringArray : public csArray<const char*, csStringArrayElementHandler> 00056 { 00057 private: 00058 typedef csArray<const char*, csStringArrayElementHandler> superclass; 00059 00060 public: 00065 csStringArray (size_t limit = 0, size_t threshold = 0) 00066 : superclass(limit, threshold) 00067 { 00068 } 00069 00071 static int CaseSensitiveCompare (const char* const &item1, 00072 const char* const &item2) 00073 { 00074 return strcmp (item1, item2); 00075 } 00076 00078 static int CaseInsensitiveCompare (const char* const &item1, 00079 const char* const &item2) 00080 { 00081 return csStrCaseCmp (item1, item2); 00082 } 00083 00087 void Sort (int(*compare)(char const* const&, char const* const&)) 00088 { 00089 superclass::Sort (compare); 00090 } 00091 00097 void Sort (bool case_sensitive = true) 00098 { 00099 if (case_sensitive) 00100 Sort (CaseSensitiveCompare); 00101 else 00102 Sort (CaseInsensitiveCompare); 00103 } 00104 00110 size_t FindSortedKey (csArrayCmpDecl(char const*, char const*) comparekey, 00111 size_t* candidate = 0) const 00112 { 00113 return superclass::FindSortedKey(comparekey, candidate); 00114 } 00115 00121 size_t FindSortedKey (char const* key, bool case_sensitive = true, 00122 size_t* candidate = 0) const 00123 { 00124 int(*cf)(char const* const&, char const* const&) = 00125 case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare; 00126 return FindSortedKey(csArrayCmp<char const*, char const*>(key, cf), 00127 candidate); 00128 } 00129 00134 size_t InsertSorted (const char* item, bool case_sensitive = true, 00135 size_t* equal_index = 0) 00136 { 00137 int(*cf)(char const* const&, char const* const&) = 00138 case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare; 00139 return superclass::InsertSorted (item, cf, equal_index); 00140 } 00141 00147 char* Pop () 00148 { 00149 CS_ASSERT (GetSize () > 0); 00150 size_t l = GetSize () - 1; 00151 char* ret = (char*)Get (l); 00152 InitRegion (l, 1); 00153 SetSize (l); 00154 return ret; 00155 } 00156 00163 size_t Find (const char* str) const 00164 { 00165 for (size_t i = 0; i < GetSize (); i++) 00166 if (! strcmp (Get (i), str)) 00167 return i; 00168 return (size_t)-1; 00169 } 00170 00177 size_t FindCaseInsensitive (const char* str) const 00178 { 00179 for (size_t i = 0; i < GetSize (); i++) 00180 if (!csStrCaseCmp (Get (i), str)) 00181 return i; 00182 return (size_t)-1; 00183 } 00184 00196 size_t Contains(const char* str, bool case_sensitive = true) const 00197 { 00198 return case_sensitive ? Find(str) : FindCaseInsensitive(str); 00199 } 00200 }; 00201 00202 #endif // __CS_STRINGARRAY_H__
Generated for Crystal Space by doxygen 1.4.6