Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | 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 "csutil/array.h" 00026 #include "csutil/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 private: 00054 typedef csArray<const char*, csStringArrayElementHandler> superclass; 00055 00056 public: 00061 csStringArray (size_t limit = 0, size_t threshold = 0) 00062 : superclass(limit, threshold) 00063 { 00064 } 00065 00067 static int CaseSensitiveCompare (const char* const &item1, 00068 const char* const &item2) 00069 { 00070 return strcmp (item1, item2); 00071 } 00072 00074 static int CaseInsensitiveCompare (const char* const &item1, 00075 const char* const &item2) 00076 { 00077 return csStrCaseCmp (item1, item2); 00078 } 00079 00083 void Sort (int(*compare)(char const* const&, char const* const&)) 00084 { 00085 superclass::Sort (compare); 00086 } 00087 00093 void Sort (bool case_sensitive = true) 00094 { 00095 if (case_sensitive) 00096 Sort (CaseSensitiveCompare); 00097 else 00098 Sort (CaseInsensitiveCompare); 00099 } 00100 00106 size_t FindSortedKey (csArrayCmpDecl(char const*, char const*) comparekey, 00107 size_t* candidate = 0) const 00108 { 00109 return superclass::FindSortedKey(comparekey, candidate); 00110 } 00111 00117 size_t FindSortedKey (char const* key, bool case_sensitive = true, 00118 size_t* candidate = 0) const 00119 { 00120 int(*cf)(char const* const&, char const* const&) = 00121 case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare; 00122 return FindSortedKey(csArrayCmp<char const*, char const*>(key, cf), 00123 candidate); 00124 } 00125 00130 size_t InsertSorted (const char* item, bool case_sensitive = true, 00131 size_t* equal_index = 0) 00132 { 00133 int(*cf)(char const* const&, char const* const&) = 00134 case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare; 00135 return superclass::InsertSorted (item, cf, equal_index); 00136 } 00137 00143 char* Pop () 00144 { 00145 CS_ASSERT (GetSize () > 0); 00146 size_t l = GetSize () - 1; 00147 char* ret = (char*)Get (l); 00148 InitRegion (l, 1); 00149 SetSize (l); 00150 return ret; 00151 } 00152 00159 size_t Find (const char* str) const 00160 { 00161 for (size_t i = 0; i < GetSize (); i++) 00162 if (! strcmp (Get (i), str)) 00163 return i; 00164 return (size_t)-1; 00165 } 00166 00173 size_t FindCaseInsensitive (const char* str) const 00174 { 00175 for (size_t i = 0; i < GetSize (); i++) 00176 if (!csStrCaseCmp (Get (i), str)) 00177 return i; 00178 return (size_t)-1; 00179 } 00180 00192 size_t Contains(const char* str, bool case_sensitive = true) const 00193 { 00194 return case_sensitive ? Find(str) : FindCaseInsensitive(str); 00195 } 00196 }; 00197 00198 #endif // __CS_STRINGARRAY_H__
Generated for Crystal Space by doxygen 1.4.4