wxputils.cpp

00001 
00002 // Name:        wxputils.cpp
00003 // Purpose:     wxPackage API utilities
00004 // Author:      Julian Smart, Francesco Montorsi, Daniel J. Lauk
00005 // Modified by:
00006 // Created:     2004-11-07
00007 // RCS-ID:      $Id: wxputils.cpp,v 1.10 2007/01/28 14:35:29 frm Exp $
00008 // Copyright:   (c) Julian Smart
00009 // Licence:     wxWidgets licence
00011 
00012 
00013 // For compilers that support precompilation, includes "wx/wx.h".
00014 #include "wx/wxprec.h"
00015 
00016 #ifdef __BORLANDC__
00017 #pragma hdrstop
00018 #endif
00019 
00020 #ifndef WX_PRECOMP
00021     #include "wx/wx.h"
00022 #endif
00023 
00024 // includes
00025 #include <wx/fileconf.h>
00026 #include <wx/filename.h>
00027 #include <wx/zipstrm.h>
00028 #include <wx/wfstream.h>
00029 #include <wx/ptr_scpd.h>
00030 #include <wx/dir.h>
00031 
00032 #include "wxp/package.h"
00033 #include "wxp/wxp.h"
00034 
00035 
00036 // ----------------------------------------------------------------------------
00037 // misc
00038 // ----------------------------------------------------------------------------
00039 
00040 bool wxIsPlainWord(const wxString &tocheck)
00041 {
00042 #define ALLOWEDCHARS   "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-"
00043 
00044     wxString str(wxT(ALLOWEDCHARS));
00045     for (size_t i=0; i<tocheck.Len(); i++)
00046         if (!str.Contains(tocheck[i]))
00047             return false;
00048     return true;
00049 }
00050 
00051 wxString wxGetPlainWordAllowedCharacters()
00052 {
00053     return wxString(wxT(ALLOWEDCHARS));
00054 }
00055 
00056 wxString wxStripOutHTML(const wxString &str)
00057 {
00058     bool inside_tag = false;
00059     wxString ret;
00060     const wxChar *source = str;
00061 
00062     for (size_t i=0, max=str.length(); i<max; i++)
00063     {
00064         switch (str[i])
00065         {
00066             case wxT('<'):
00067                 if (inside_tag)
00068                 {
00069                     wxLogDebug(wxT("Invalid HTML !"));
00070                     return wxEmptyString;
00071                 }
00072                 inside_tag = true;
00073                 break;
00074 
00075             case wxT('>'):
00076                 if (!inside_tag)
00077                 {
00078                     wxLogDebug(wxT("Invalid HTML !"));
00079                     return wxEmptyString;
00080                 }
00081                 inside_tag = false;
00082                 break;
00083 
00084             default:
00085                 if (!inside_tag)
00086                     ret << str[i];
00087         }
00088     }
00089 
00090     return ret;
00091 }
00092 
00093 
00094 // ----------------------------------------------------------------------------
00095 // wxArrayString <-> wxString
00096 // ----------------------------------------------------------------------------
00097 
00098 wxString wxArray2String(const wxArrayString &arr, const wxString &sep)
00099 {
00100     wxString str;
00101     size_t max=arr.GetCount();
00102 
00103     if (max == 0)
00104         return wxEmptyString;
00105 
00106     str.Alloc(max * 10);        // 10 is a very arbitrary number
00107     str = arr[0];
00108 
00109     for (size_t i=1; i<max; i++)
00110         str += sep + arr[i];
00111 
00112     return str;
00113 }
00114 
00115 wxArrayString wxString2Array(const wxString &str, const wxString &sep)
00116 {
00117     return wxStringTokenize(str, sep);
00118 }
00119 
00120 
00121 
00122 // ----------------------------------------------------------------------------
00123 // global substitution helpers
00124 // ----------------------------------------------------------------------------
00125 
00126 wxString wxDoStringSubstitution(const wxString &str,
00127                                 const wxStringHashMap &hash)
00128 {
00129     wxString ret;
00130     for (const wxChar *p = str.c_str(); *p != wxT('\0'); p++)
00131     {
00132         switch (*p)
00133         {
00134         case wxT('$'):
00135             // is this a substitution marker?
00136             if (*(p+1) == wxT('('))
00137             {
00138                 p += 2;     // skip the $( part
00139 
00140                 // seems so...
00141                 const wxChar *pstart = p;
00142                 for (; *p != wxT('\0'); p++)
00143                     if (*p == wxT(')'))
00144                         break;
00145 
00146                 if (*p != wxT(')'))
00147                     break;      // not valid however
00148 
00149                 // does this key exists?
00150                 wxString keyName(pstart, (size_t)(p - pstart)/sizeof(wxChar));
00151                 wxStringHashMap::const_iterator key = hash.find(keyName);
00152                 if (key == hash.end())
00153                 {
00154                     // not a known key... skip it
00155                     ret << wxWPM_SUBSTITUTION_START_MARKER 
00156                         << keyName 
00157                         << wxWPM_SUBSTITUTION_END_MARKER;
00158                     break;
00159                 }
00160 
00161                 // do the replacement
00162                 ret << key->second;
00163             }
00164             break;
00165 
00166         default:
00167             ret << *p;
00168         }
00169     }
00170 
00171     return ret;
00172 }
00173 
00174 void wxMergeHashMap(wxStringHashMap &out, const wxStringHashMap &hash)
00175 {
00176     for (wxStringHashMap::const_iterator it = hash.begin(); it != hash.end(); ++it)
00177     {
00178         wxString key = it->first, value = it->second;
00179         out[key] = value;
00180     }
00181 }
00182 
00183 bool wxNormalizeSubstitutionHashMap(wxStringHashMap &hash)
00184 {
00185     // having a max substitution depth saves us from being trapped
00186     // in an endless loop when trying to evaluate recursively-defined variables!
00187 #define MAX_SUBSTITUTION_DEPTH  3
00188 
00189     bool success = true;
00190     for ( wxStringHashMap::const_iterator it = hash.begin(); it != hash.end(); ++it )
00191     {
00192         wxString key = it->first, value = it->second;
00193 
00194         size_t substCount = 0;
00195         while (value.Contains(wxWPM_SUBSTITUTION_START_MARKER) && 
00196                substCount < MAX_SUBSTITUTION_DEPTH)
00197         {
00198             // try substitute this same hashmap into this value
00199             value = wxDoStringSubstitution(value, hash);
00200             substCount++;
00201         }
00202 
00203         if (substCount > 0)
00204             hash[key] = value;      // update key value...
00205 
00206         success &= !value.Contains(wxWPM_SUBSTITUTION_START_MARKER);
00207     }
00208 
00209 #ifdef __WXDEBUG__
00210     if (!success)
00211         wxLogDebug(wxT("Cannot normalize a substitution hashmap..."));
00212 #endif
00213 
00214     return success;
00215 }
00216 
00217 
00218 
00219 // ----------------------------------------------------------------------------
00220 // wxPortId list helpers
00221 // ----------------------------------------------------------------------------
00222 
00223 long wxGetFullPortIdList()
00224 {
00225     return (1 << wxPORT_MAX) - 1;
00226 }
00227 
00228 wxString wxPortIdList2String(long n)
00229 {
00230     wxString ret;
00231     for (size_t i=0; i < wxPORT_MAX; i++)
00232     {
00233         wxPortId cur = (wxPortId)(1 << i);
00234         if ((n & cur) != 0)
00235             ret += wxPlatformInfo::GetPortIdName(cur, false) + wxT(",");
00236     }
00237     if (ret.EndsWith(wxT(",")))
00238         ret.RemoveLast();
00239     return ret;
00240 }
00241 
00242 long wxString2PortIdList(const wxString &str)
00243 {
00244     wxArrayString temp = wxString2Array(str);
00245     long ret = 0;
00246     for (size_t i=0; i<temp.GetCount(); i++)
00247     {
00248         wxPortId t = wxPlatformInfo::GetPortId(temp[i]);
00249         if (t != (wxPortId)-1)
00250             ret |= t;
00251     }
00252     return ret;
00253 }

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