packageopt.cpp

00001 
00002 // Name:        packageopt.cpp
00003 // Purpose:     wxPackageCommandOption, wxPackageCommandOptionArray
00004 // Author:      Julian Smart, Francesco Montorsi, Daniel J. Lauk
00005 // Modified by:
00006 // Created:     2004-11-07
00007 // RCS-ID:      $Id: packageopt.cpp,v 1.4 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 
00028 #include "wxp/packagecmd.h"
00029 #include "wxp/packageinfo.h"
00030 
00031 
00032 // definitions
00033 
00034 wxString wxPackageBuildSystemStageName[] =
00035 {
00036     _T("Build"),
00037     _T("Install"),
00038     _T("Uninstall"),
00039     _T("Clean")
00040 };
00041 
00042 wxString wxPackageCommandOptionCategoryName[] =
00043 {
00044     _T("listed-values"),
00045     _T("directory"),
00046     _T("file"),
00047     _T("string")
00048 };
00049 
00050 
00051 // RTTI
00052 IMPLEMENT_DYNAMIC_CLASS(wxPackageCondition, wxObject)
00053 IMPLEMENT_DYNAMIC_CLASS(wxPackageCommandOption, wxObject)
00054 
00055 
00056 // wx array implementations
00057 #include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
00058 WX_DEFINE_OBJARRAY(wxPackageCommandOptionArrayHelper);
00059 
00060 
00061 // global objects
00062 wxPackageCommandOption wxEmptyPackageCommandOption;
00063 wxPackageCommandOptionArray wxEmptyPackageCommandOptionArray;
00064 
00065 
00066 
00067 // ----------------------------------------------------------------------------
00068 // string2enum helpers
00069 // ----------------------------------------------------------------------------
00070 
00071 wxIMPLEMENT_STRING2COMBINEABLE_ENUM(PackageBuildSystemStage, wxPBSS_MAX)
00072 wxIMPLEMENT_STRING2ENUM(PackageCommandOptionCategory, wxPCOC_MAX)
00073 
00074 
00075 // ----------------------------------------------------------------------------
00076 // wxPackageCommandOption
00077 // ----------------------------------------------------------------------------
00078 
00079 bool wxPackageCommandOption::operator==(const wxPackageCommandOption &opt) const
00080 {
00081     // check string data
00082     if (m_strName != opt.m_strName ||
00083         m_strAllowedValues != opt.m_strAllowedValues ||
00084         m_strDefValue != opt.m_strDefValue ||
00085         m_strValue != opt.m_strValue ||
00086         m_strDesc != opt.m_strDesc ||
00087         m_strGroup != opt.m_strGroup ||
00088         m_strValuesDesc != opt.m_strValuesDesc)
00089         return false;
00090 
00091     if (m_nCat != opt.m_nCat ||
00092         m_cond != opt.m_cond)
00093         return false;
00094 
00095     // everything is identic
00096     return true;
00097 }
00098 
00099 void wxPackageCommandOption::AutodetectCategory()
00100 {
00101     if (!m_strAllowedValues.IsEmpty())
00102     {
00103         m_nCat = wxPCOC_LISTED_VALUES;
00104         return;
00105     }
00106 
00107     // FIXME: how to autodetect wxPCOC_DIRECTORY and wxPCOC_FILE ?
00108     //if (m_strName.EndsWith(wxT("DIR"))) m_nCat = wxPCOC_DIRECTORY;
00109 
00110     m_nCat = wxPCOC_STRING;
00111 }
00112 
00113 bool wxPackageCommandOption::IsOk() const
00114 {
00115     if (m_strName.IsEmpty() || m_nCat == wxPCOC_INVALID || m_strGroup.IsEmpty())
00116         return false;
00117     if (!GetCondition().IsOk())
00118         return false;
00119 
00120     switch (m_nCat)
00121     {
00122     case wxPCOC_LISTED_VALUES:
00123         if (m_strAllowedValues.IsEmpty())
00124             return false;
00125         if (!m_strValuesDesc.IsEmpty() &&
00126             GetAllowedValuesDescArray().GetCount() != GetAllowedValuesArray().GetCount())
00127             return false;
00128         if (!m_strValue.IsEmpty() &&
00129             GetAllowedValuesArray().Index(m_strValue) == wxNOT_FOUND)
00130             return false;
00131         if (!m_strDefValue.IsEmpty() &&
00132             GetAllowedValuesArray().Index(m_strDefValue) == wxNOT_FOUND)
00133             return false;
00134         break;
00135 
00136     default:
00137         // cannot do any other check for other categories
00138         // (we cannot do existence checks here for wxPCOC_FILE and wxPCOC_DIRECTORY!)
00139         break;
00140     }
00141 
00142     return true;
00143 }
00144 
00145 
00146 
00147 // ----------------------------------------------------------------------------
00148 // wxPackageCommandOptionArray
00149 // ----------------------------------------------------------------------------
00150 
00151 wxString wxJoinOptionWithValue(const wxString &optname, const wxString &value)
00152 {
00153 #if 0
00154     // use double quotes around the argument only if strictly necessary
00155     if (value.Contains(wxT(" ")))
00156         return wxString::Format(wxT("%s=\"%s\""), optname.c_str(), value.c_str());
00157     return wxString::Format(wxT("%s=%s"), optname.c_str(), value.c_str());
00158 #else
00159     // always use double quotes aroung the argument:
00160     // if the argument is a $(key) it may later be substituted with an empty
00161     // value and it's not nice to see lots of OPT_NAME= strings around
00162     return wxString::Format(wxT("%s=\"%s\""), optname.c_str(), value.c_str());
00163 #endif
00164 }
00165 
00166 wxString wxPackageCommandOptionArray::GetAsList() const
00167 {
00168     wxString ret;
00169 
00170     // micro optimization
00171     ret.Alloc(GetCount()*15);
00172 
00173     for (size_t j=0; j<GetCount(); j++)
00174         ret += wxJoinOptionWithValue(Item(j).GetName(),
00175                                      Item(j).GetValue()) + wxT(" ");
00176 
00177     // remove any eventually present space from the end
00178     ret.Trim();
00179     return ret;
00180 }
00181 
00182 wxStringHashMap wxPackageCommandOptionArray::GetSubstitutionOptionsHashMap(
00183                             long flags, const wxPackageCondition &cond) const
00184 {
00185     wxStringHashMap ret;
00186 
00187     wxMergeHashMap(ret, GetGroupNamesHashMap(flags, cond));
00188     wxMergeHashMap(ret, GetTypeNamesHashMap(flags, cond));
00189 
00190     return ret;
00191 }
00192 
00193 wxStringHashMap wxPackageCommandOptionArray::GetGroupNamesHashMap(
00194                             long flags, const wxPackageCondition &cond) const
00195 {
00196     wxStringHashMap ret;
00197 
00198     wxArrayString groups(GetAllOptionGroups());
00199     for (size_t i=0; i < groups.GetCount(); i++)
00200     {
00201         // add to 'value' only the options which belong to the i-th group
00202         wxString value;
00203         for (size_t j=0; j<GetCount(); j++)
00204         {
00205             if (Item(j).GetGroup() != groups[i])
00206                 continue;
00207 
00208             if ((flags & wxPKGCMD_ONLY_CHANGED) != 0 &&
00209                 !Item(j).IsChanged())
00210                 continue;
00211 
00212             if ((flags & wxPKGCMD_ONLY_MATCHING_CONDITION) != 0 &&
00213                 !Item(j).GetCondition().Matches(cond))
00214                 continue;
00215 
00216             // even if all previous tests passed, don't substitute options
00217             // with empty values as they would not be correctly interpreted
00218             // by make processes!
00219             if (Item(j).GetValue().IsEmpty())
00220                 continue;       // skip it!
00221 
00222             value += wxJoinOptionWithValue(Item(j).GetName(),
00223                                            Item(j).GetValue()) + wxT(" ");
00224         }
00225 
00226         // remove any eventually present space from the end
00227         ret[groups[i].Lower() + wxT("options")] = value.Trim();
00228     }
00229 
00230     return ret;
00231 }
00232 
00233 wxStringHashMap wxPackageCommandOptionArray::GetTypeNamesHashMap(
00234                             long flags, const wxPackageCondition &cond) const
00235 {
00236     wxStringHashMap ret;
00237 
00238     wxArrayString types(3, wxPackageBuildSystemStageName);
00239     for (size_t i=0; i < types.GetCount(); i++)
00240     {
00241         // add to 'value' only the options which belong to the i-th type
00242         wxString value;
00243         for (size_t j=0; j<GetCount(); j++)
00244         {
00245             if (!Item(j).GetCondition().IsValidForStage(wxIdx2PackageBuildSystemStage(i)))
00246                 continue;
00247 
00248             if ((flags & wxPKGCMD_ONLY_CHANGED) != 0 &&
00249                 !Item(j).IsChanged())
00250                 continue;
00251 
00252             if ((flags & wxPKGCMD_ONLY_MATCHING_CONDITION) != 0 &&
00253                 !Item(j).GetCondition().Matches(cond))
00254                 continue;
00255 
00256             // even if all previous tests passed, don't substitute options
00257             // with empty values as they would not be correctly interpreted
00258             // by make processes!
00259             if (Item(j).GetValue().IsEmpty())
00260                 continue;       // skip it!
00261 
00262             value += wxJoinOptionWithValue(Item(j).GetName(),
00263                                            Item(j).GetValue()) + wxT(" ");
00264         }
00265 
00266 
00267         // remove any eventually present space from the end
00268         ret[types[i].Lower() + wxT("options")] = value.Trim();
00269     }
00270 
00271     return ret;
00272 }
00273 
00274 wxArrayString wxPackageCommandOptionArray::GetOptionGroups(long stages) const
00275 {
00276     wxArrayString ret;
00277     for (size_t i=0; i < GetCount(); i++)
00278         if (Item(i).GetCondition().IsValidForStages(stages))
00279             if (ret.Index(Item(i).GetGroup()) == wxNOT_FOUND)
00280                 ret.Add(Item(i).GetGroup());
00281     return ret;
00282 }
00283 
00284 wxArrayString wxPackageCommandOptionArray::GetAllOptionGroups() const
00285 {
00286     wxArrayString ret;
00287     for (size_t i=0; i < GetCount(); i++)
00288         if (ret.Index(Item(i).GetGroup()) == wxNOT_FOUND)
00289             ret.Add(Item(i).GetGroup());
00290     return ret;
00291 }
00292 
00293 wxPackageCommandOptionArray wxPackageCommandOptionArray::GetChangedOptions() const
00294 {
00295     wxPackageCommandOptionArray ret;
00296     for (size_t i=0; i < GetCount(); i++)
00297         if (Item(i).IsChanged())
00298             ret.Add(Item(i));
00299     return ret;
00300 }
00301 
00302 bool wxPackageCommandOptionArray::UpdateOption(const wxPackageCommandOption &opt)
00303 {
00304     int n = IndexByName(opt.GetName());
00305     if (n == wxNOT_FOUND)
00306         return false;
00307 
00308     //ItemById(pkg) = pkg;
00309     RemoveAt(n);
00310     Insert(opt, n);
00311     return true;
00312 }
00313 
00314 bool wxPackageCommandOptionArray::Contains(const wxPackageCommandOptionArray &arr) const
00315 {
00316     size_t i, j;
00317     for (i=0; i<arr.GetCount(); i++)
00318     {
00319         for (j=0; j<GetCount(); j++)
00320             if (Item(j) == arr[i])
00321                 break;
00322 
00323         // if this package does not have the i-th option of the option preset
00324         // it's not using that preset!
00325         if (j == GetCount())
00326             return false;
00327     }
00328 
00329     return true;
00330 }
00331 
00332 bool wxPackageCommandOptionArray::Remove(const wxPackageCommandOptionArray &arr)
00333 {
00334     bool allfound = true;
00335     size_t i, j;
00336 
00337     for (i=0; i<arr.GetCount(); i++)
00338     {
00339         for (j=0; j<GetCount(); j++)
00340             if (Item(j) == arr[i])
00341                 break;
00342 
00343         if (j == GetCount())
00344             allfound = false;
00345         else
00346         {
00347             RemoveAt(j);
00348             j--;
00349         }
00350     }
00351 
00352     return allfound;
00353 }
00354 
00355 wxPackageCondition wxPackageCommandOptionArray::GetCommonCondition() const
00356 {
00357     wxPackageCondition ret;
00358     ret.SetAlwaysTrue();
00359     for (size_t i=0; i<GetCount(); i++)
00360         ret.IntersecateWith(Item(i).GetCondition());
00361 
00362     return ret;
00363 }
00364 

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