Crystal Space: csutil/stringarray.h Source File (Crystal Space Public API Reference)

| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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 #include "csutil/csstring.h"
    00032
    00033 class csStringArrayElementHandler : public csArrayElementHandler<const char*>
    00034 {
    00035 public:
    00036   static void Construct (const char** address, const char* const& src)
    00037   {
    00038     *address = csStrNew (src);
    00039   }
    00040
    00041   static void Destroy (const char** address)
    00042   {
    00043     delete[] (char*)*address;
    00044   }
    00045
    00046   static void InitRegion (const char** address, size_t count)
    00047   {
    00048     memset (address, 0, count*sizeof (const char*));
    00049   }
    00050 };
    00051
    00056 class csStringArray : public csArray<const char*, csStringArrayElementHandler>
    00057 {
    00058 private:
    00059   typedef csArray<const char*, csStringArrayElementHandler> superclass;
    00060
    00061 public:
    00066   csStringArray (size_t limit = 0, size_t threshold = 0)
    00067         : superclass(limit, threshold)
    00068   {
    00069   }
    00070
    00072   static int CaseSensitiveCompare (const char* const &item1,
    00073                                    const char* const &item2)
    00074   {
    00075     return strcmp (item1, item2);
    00076   }
    00077
    00079   static int CaseInsensitiveCompare (const char* const &item1,
    00080                                      const char* const &item2)
    00081   {
    00082     return csStrCaseCmp (item1, item2);
    00083   }
    00084
    00088   void Sort (int(*compare)(char const* const&, char const* const&))
    00089   {
    00090     superclass::Sort (compare);
    00091   }
    00092
    00098   void Sort (bool case_sensitive = true)
    00099   {
    00100     if (case_sensitive)
    00101       Sort (CaseSensitiveCompare);
    00102     else
    00103       Sort (CaseInsensitiveCompare);
    00104   }
    00105
    00111   size_t FindSortedKey (csArrayCmp<char const*, char const*> comparekey,
    00112     size_t* candidate = 0) const
    00113   {
    00114     return superclass::FindSortedKey(comparekey, candidate);
    00115   }
    00116
    00122   size_t FindSortedKey (char const* key, bool case_sensitive = true,
    00123     size_t* candidate = 0) const
    00124   {
    00125     int(*cf)(char const* const&, char const* const&) =
    00126       case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare;
    00127     return FindSortedKey(csArrayCmp<char const*, char const*>(key, cf),
    00128       candidate);
    00129   }
    00130
    00135   size_t InsertSorted (const char* item, bool case_sensitive = true,
    00136     size_t* equal_index = 0)
    00137   {
    00138     int(*cf)(char const* const&, char const* const&) =
    00139       case_sensitive ? CaseSensitiveCompare : CaseInsensitiveCompare;
    00140     return superclass::InsertSorted (item, cf, equal_index);
    00141   }
    00142
    00148   char* Pop ()
    00149   {
    00150     CS_ASSERT (GetSize () > 0);
    00151     size_t l = GetSize () - 1;
    00152     char* ret = (char*)Get (l);
    00153     InitRegion (l, 1);
    00154     SetSize (l);
    00155     return ret;
    00156   }
    00157
    00164   size_t Find (const char* str) const
    00165   {
    00166     for (size_t i = 0; i < GetSize (); i++)
    00167       if (! strcmp (Get (i), str))
    00168         return i;
    00169     return (size_t)-1;
    00170   }
    00171
    00178   size_t FindCaseInsensitive (const char* str) const
    00179   {
    00180     for (size_t i = 0; i < GetSize (); i++)
    00181       if (!csStrCaseCmp (Get (i), str))
    00182         return i;
    00183     return (size_t)-1;
    00184   }
    00185
    00197   size_t Contains(const char* str, bool case_sensitive = true) const
    00198   {
    00199     return case_sensitive ? Find(str) : FindCaseInsensitive(str);
    00200   }
    00201
    00205   enum ConsecutiveDelimiterMode
    00206   {
    00208     delimSplitEach,
    00214     delimIgnore,
    00218     delimIgnoreDifferent
    00219   };
    00224   size_t SplitString (const char* str, const char* delimiters,
    00225     ConsecutiveDelimiterMode delimMode = delimSplitEach)
    00226   {
    00227     size_t num = 0;
    00228     csString currentString;
    00229     int lastDelim = -1;
    00230
    00231     const char* p = str;
    00232     while (*p != 0)
    00233     {
    00234       if (strchr (delimiters, *p))
    00235       {
    00236         bool newString = true;
    00237         switch (delimMode)
    00238         {
    00239           case delimSplitEach:
    00240             break;
    00241           case delimIgnore:
    00242             newString = lastDelim == -1;
    00243             break;
    00244           case delimIgnoreDifferent:
    00245             newString = (lastDelim == -1) || (lastDelim == *p);
    00246             break;
    00247         }
    00248         if (newString)
    00249         {
    00250           Push (currentString);
    00251           currentString.Empty();
    00252           num++;
    00253           lastDelim = *p;
    00254         }
    00255       }
    00256       else
    00257       {
    00258         currentString.Append (*p);
    00259         lastDelim = -1;
    00260       }
    00261       p++;
    00262     }
    00263
    00264     Push (currentString);
    00265     return num + 1;
    00266   }
    00267 };
    00268
    00269 #endif // __CS_STRINGARRAY_H__