wxputils.h

00001 
00002 // Name:        wxputils.h
00003 // Purpose:     wxPackage API utilities
00004 // Author:      Francesco Montorsi
00005 // Modified by:
00006 // Created:     2006-11-07
00007 // RCS-ID:      $Id: wxputils.h,v 1.6 2007/01/23 18:25:31 frm Exp $
00008 // Copyright:   (c) Francesco Montorsi
00009 // Licence:     wxWidgets licence
00011 
00012 #ifndef _WXPUTILS_H_
00013 #define _WXPUTILS_H_
00014 
00015 // Includes
00016 #include <wx/tokenzr.h>
00017 #include <wx/hashmap.h>
00018 
00020 #define wxPACKAGE_DELIMITERS                wxT(", ")
00021 
00022 
00023 // these are used by wxIMPLEMENT_STRING2COMBINEABLE_ENUM
00024 extern double log(double);
00025 
00026 
00027 // A macro which helps defines the wxString2* functions for wxPackage API.
00028 // VERY IMPORTANT: this assumes that the vaid values of the enum start at
00029 //                 value zero and ends at the given 'max' value and that
00030 //                 all values in zero-max range are valid
00031 #define wxDEFINE_STRING2ENUM(enumname)                       \
00032     extern wxString wx##enumname##Name[];                    \
00033     wx##enumname wxString2##enumname(const wxString &str);   \
00034     wxString wx##enumname##2String(wx##enumname value);
00035 
00036 // A macro which helps defines the wxString2* functions for wxPackage API.
00037 // VERY IMPORTANT: this assumes that the vaid values of the enum start at
00038 //                 value zero and ends at the 2^'max' value and that
00039 //                 all values are powers of two.
00040 #define wxDEFINE_STRING2COMBINEABLE_ENUM(enumname)           \
00041     wxDEFINE_STRING2ENUM(enumname);                          \
00042     wx##enumname wxIdx2##enumname(size_t n);                 \
00043     size_t wx##enumname##2Idx(wx##enumname value);           \
00044     wxString wx##enumname##List2String(long n);              \
00045     long wxString2##enumname##List(const wxString &str);     \
00046     long wxGetFull##enumname##List();
00047 
00048 
00049 // A macro which implements the wxString2* functions for wxPackage API.
00050 // VERY IMPORTANT: this assumes that the vaid values of the enum start at
00051 //                 value zero and ends at the given 'max' value and that
00052 //                 all values in zero-max range are valid
00053 #define wxIMPLEMENT_STRING2ENUM(enumname, max)                  \
00054     wx##enumname wxString2##enumname (const wxString &str)      \
00055     {                                                           \
00056         for (size_t i=0; i < max; i++)                          \
00057             if (wx##enumname##Name[i].IsSameAs(str, false))     \
00058                 return (wx##enumname)i;                         \
00059         return (wx##enumname)wxNOT_FOUND;                       \
00060     }                                                           \
00061                                                                 \
00062     wxString wx##enumname##2String(wx##enumname value)          \
00063     {                                                           \
00064         wxASSERT(value >= 0 && value < max);                    \
00065         return wx##enumname##Name[(size_t)value];               \
00066     }
00067 
00068 // Like wxIMPLEMENT_STRING2ENUM except that this works for enumerations
00069 // which have values which are power of 2.
00070 // The first valid value is supposed to be 1 and the last is 2^(max-1).
00071 #define wxIMPLEMENT_STRING2COMBINEABLE_ENUM(enumname, max)      \
00072                                                                 \
00073     /* linear index <-> enum value conversions */               \
00074     wx##enumname wxIdx2##enumname(size_t n)                     \
00075     {                                                           \
00076         wxASSERT(n >= 0 && n < max);                            \
00077         return (wx##enumname)(1 << n);                          \
00078     }                                                           \
00079                                                                 \
00080     size_t wx##enumname##2Idx(wx##enumname value)               \
00081     {                                                           \
00082         wxASSERT(value >= 0 && value <= (1 << (max-1)));        \
00083         return (int)wxRound(log((double)value)/log((double)2.));\
00084     }                                                           \
00085                                                                 \
00086     /* string <-> enum value conversions */                     \
00087     wx##enumname wxString2##enumname (const wxString &str)      \
00088     {                                                           \
00089         for (size_t i=0; i < max; i++)                          \
00090             if (wx##enumname##Name[i].CmpNoCase(str) == 0)      \
00091                 return (wx##enumname)(1 << i);                  \
00092         return (wx##enumname)wxNOT_FOUND;                       \
00093     }                                                           \
00094                                                                 \
00095     wxString wx##enumname##2String(wx##enumname value)          \
00096     {                                                           \
00097         size_t n = wx##enumname##2Idx(value);                   \
00098         return wx##enumname##Name[n];                           \
00099     }                                                           \
00100                                                                 \
00101     /* combined list <-> string conversions */                  \
00102     wxString wx##enumname##List2String(long n)                  \
00103     {                                                           \
00104         wxString ret;                                           \
00105         for (size_t i=0; i < max; i++)                          \
00106         {                                                       \
00107             wx##enumname cur = wxIdx2##enumname(i);             \
00108             if ((n & cur) != 0)                                 \
00109                 ret += wx##enumname##2String(cur) + wxT(",");   \
00110         }                                                       \
00111         if (ret.EndsWith(wxT(",")))                             \
00112             ret.RemoveLast();                                   \
00113         return ret;                                             \
00114     }                                                           \
00115                                                                 \
00116     long wxString2##enumname##List(const wxString &str)         \
00117     {                                                           \
00118         wxArrayString temp = wxString2Array(str);               \
00119         long ret = 0;                                           \
00120         for (size_t i=0; i<temp.GetCount(); i++)                \
00121         {                                                       \
00122             wx##enumname t = wxString2##enumname(temp[i]);      \
00123             if (t != (wx##enumname)-1)                          \
00124                 ret |= t;                                       \
00125         }                                                       \
00126         return ret;                                             \
00127     }                                                           \
00128                                                                 \
00129     long wxGetFull##enumname##List()                            \
00130         { return (1 << max) - 1; }
00131 
00132 
00133 #define WX_DECLARE_STD_OBJARRAY_HELPERS(T, emptyT)              \
00134     int IndexByName(const wxString &name) const                 \
00135     {                                                           \
00136         for (size_t i=0; i<GetCount(); i++)                     \
00137             if (Item(i).GetName().CmpNoCase(name) == 0)         \
00138                 return i;                                       \
00139         return wxNOT_FOUND;                                     \
00140     }                                                           \
00141                                                                 \
00142     T &ItemByName(const wxString &name)                         \
00143     {                                                           \
00144         int n = IndexByName(name);                              \
00145         if (n != wxNOT_FOUND)                                   \
00146             return Item(n);                                     \
00147         return emptyT;                                          \
00148     }                                                           \
00149                                                                 \
00150     bool IsOk() const                                           \
00151     {                                                           \
00152         for (size_t i=0; i<GetCount(); i++)                     \
00153             if (!Item(i).IsOk())                                \
00154                 return false;                                   \
00155         return true;                                            \
00156     }                                                           \
00157                                                                 \
00158     bool Contains(const T &p) const                             \
00159         { return Index(p) != wxNOT_FOUND; }                     \
00160     bool ContainsItemNamed(const wxString &name) const          \
00161         { return IndexByName(name) != wxNOT_FOUND; }            \
00162                                                                 \
00163     /* need to redefine Index() so that it uses the */          \
00164     /* operator== of our items as wxObjArray never uses */      \
00165     /* the item's comparison operator! */                       \
00166     int Index(const T &item) const                              \
00167     {                                                           \
00168         for (size_t i=0; i<GetCount(); i++)                     \
00169             if (Item(i) == item)                                \
00170                 return i;                                       \
00171         return wxNOT_FOUND;                                     \
00172     }
00173 
00174 
00175 // a string hashmap
00176 // FIXME: maybe this could be moved in wx directly ??
00177 WX_DECLARE_STRING_HASH_MAP( wxString,
00178                             wxStringHashMap );
00179 
00180 // this helps checking that the user input is valid
00181 bool wxIsPlainWord(const wxString &tocheck);
00182 wxString wxGetPlainWordAllowedCharacters();
00183 
00184 
00185 // substitution helpers
00186 
00187 #define wxWPM_SUBSTITUTION_START_MARKER     wxT("$(")
00188 #define wxWPM_SUBSTITUTION_END_MARKER       wxT(")")
00189 
00190 wxString wxDoStringSubstitution(const wxString &str,
00191                                 const wxStringHashMap &hash);
00192 
00194 void wxMergeHashMap(wxStringHashMap &out, 
00195                     const wxStringHashMap &tomerge);
00196 
00200 bool wxNormalizeSubstitutionHashMap(wxStringHashMap &out);
00201 
00202 
00203 
00204 // string lists <-> wxArrayString
00205 
00206 wxString wxArray2String(const wxArrayString &, const wxString &sep = wxT(","));
00207 wxArrayString wxString2Array(const wxString &, const wxString &sep = wxPACKAGE_DELIMITERS);
00208 
00209 
00210 // HTML -> plain text
00211 
00212 wxString wxStripOutHTML(const wxString &str);
00213 
00214 
00215 // helpers for wxPORT_* flags
00216 #define wxPORT_MAX      11
00217 long wxGetFullPortIdList();
00218 wxString wxPortIdList2String(long n);
00219 long wxString2PortIdList(const wxString &str);
00220 
00221 //wxDEFINE_STRING2COMBINEABLE_ENUM(PortId);
00222 
00223 
00224 #endif      // _WXPUTILS_H_
00225 

Generated on Thu Feb 1 22:14:31 2007 for wxWidgets Package Manager by  doxygen 1.5.1-p1