00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_STRINGARRAY_H__
00021 #define __CS_STRINGARRAY_H__
00022
00023 #include <stdarg.h>
00024 #include "csutil/util.h"
00025 #include "csutil/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 (char const* item1, char const* item2)
00076 {
00077 return strcmp (item1, item2);
00078 }
00079
00080 static int CaseInsensitiveCompare (char const* item1, char const* item2)
00081 {
00082 return strcasecmp (item1, item2);
00083 }
00084
00085 static int CaseSensitiveCompare (void const* item1, void const* item2)
00086 {
00087 return strcmp (*(char const**)item1, *(char const**)item2);
00088 }
00089
00090 static int CaseInsensitiveCompare (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 (CaseSensitiveCompare);
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 };
00159
00160 #endif // __CS_STRINGARRAY_H__