]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/cmdline.cpp
Smooth Aqua buttons in wxTreeCtrl.
[wxWidgets.git] / src / common / cmdline.cpp
index 965d1143deb36f346267b1b27deb3a67f82a6e49..efb4a3e9664c50ead898e5fdd547c3093c6c659d 100644 (file)
     #pragma hdrstop
 #endif
 
+#include "wx/cmdline.h"
+
+#if wxUSE_CMDLINE_PARSER
+
 #ifndef WX_PRECOMP
     #include "wx/string.h"
     #include "wx/log.h"
@@ -40,7 +44,6 @@
 #include <ctype.h>
 
 #include "wx/datetime.h"
-#include "wx/cmdline.h"
 
 // ----------------------------------------------------------------------------
 // private functions
@@ -62,6 +65,9 @@ struct wxCmdLineOption
                     wxCmdLineParamType typ,
                     int fl)
     {
+        wxASSERT_MSG( !shrt.empty() || !lng.empty(),
+                      _T("option should have at least one name") );
+
         kind = k;
 
         shortName = shrt;
@@ -98,7 +104,7 @@ struct wxCmdLineOption
     void SetDateVal(const wxDateTime val)
         { Check(wxCMD_LINE_VAL_DATE); m_dateVal = val; m_hasVal = TRUE; }
 
-    void SetHasValue() { m_hasVal = TRUE; }
+    void SetHasValue(bool hasValue = TRUE) { m_hasVal = hasValue; }
     bool HasValue() const { return m_hasVal; }
 
 public:
@@ -155,7 +161,7 @@ struct wxCmdLineParserData
 
     // methods
     wxCmdLineParserData();
-    void SetArguments(int argc, char **argv);
+    void SetArguments(int argc, wxChar **argv);
     void SetArguments(const wxString& cmdline);
 
     int FindOption(const wxString& name);
@@ -180,7 +186,7 @@ wxCmdLineParserData::wxCmdLineParserData()
 #endif
 }
 
-void wxCmdLineParserData::SetArguments(int argc, char **argv)
+void wxCmdLineParserData::SetArguments(int argc, wxChar **argv)
 {
     m_arguments.Empty();
 
@@ -190,23 +196,29 @@ void wxCmdLineParserData::SetArguments(int argc, char **argv)
     }
 }
 
-void wxCmdLineParserData::SetArguments(const wxString& WXUNUSED(cmdline))
+void wxCmdLineParserData::SetArguments(const wxString& cmdLine)
 {
-    // either use wxMSW wxApp::ConvertToStandardCommandArgs() or move its logic
-    // here and use this method from it - but don't duplicate the code
+    m_arguments.Empty();
+
+    m_arguments.Add(wxTheApp->GetAppName());
+
+    wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdLine);
 
-    wxFAIL_MSG(_T("TODO"));
+    WX_APPEND_ARRAY(m_arguments, args);
 }
 
 int wxCmdLineParserData::FindOption(const wxString& name)
 {
-    size_t count = m_options.GetCount();
-    for ( size_t n = 0; n < count; n++ )
+    if ( !name.empty() )
     {
-        if ( m_options[n].shortName == name )
+        size_t count = m_options.GetCount();
+        for ( size_t n = 0; n < count; n++ )
         {
-            // found
-            return n;
+            if ( m_options[n].shortName == name )
+            {
+                // found
+                return n;
+            }
         }
     }
 
@@ -237,7 +249,7 @@ void wxCmdLineParser::Init()
     m_data = new wxCmdLineParserData;
 }
 
-void wxCmdLineParser::SetCmdLine(int argc, char **argv)
+void wxCmdLineParser::SetCmdLine(int argc, wxChar **argv)
 {
     m_data->SetArguments(argc, argv);
 }
@@ -370,6 +382,9 @@ void wxCmdLineParser::AddParam(const wxString& desc,
 bool wxCmdLineParser::Found(const wxString& name) const
 {
     int i = m_data->FindOption(name);
+    if ( i == wxNOT_FOUND )
+        i = m_data->FindOptionByLongName(name);
+
     wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown switch") );
 
     wxCmdLineOption& opt = m_data->m_options[(size_t)i];
@@ -382,6 +397,9 @@ bool wxCmdLineParser::Found(const wxString& name) const
 bool wxCmdLineParser::Found(const wxString& name, wxString *value) const
 {
     int i = m_data->FindOption(name);
+    if ( i == wxNOT_FOUND )
+        i = m_data->FindOptionByLongName(name);
+
     wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown option") );
 
     wxCmdLineOption& opt = m_data->m_options[(size_t)i];
@@ -398,6 +416,9 @@ bool wxCmdLineParser::Found(const wxString& name, wxString *value) const
 bool wxCmdLineParser::Found(const wxString& name, long *value) const
 {
     int i = m_data->FindOption(name);
+    if ( i == wxNOT_FOUND )
+        i = m_data->FindOptionByLongName(name);
+
     wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown option") );
 
     wxCmdLineOption& opt = m_data->m_options[(size_t)i];
@@ -414,6 +435,9 @@ bool wxCmdLineParser::Found(const wxString& name, long *value) const
 bool wxCmdLineParser::Found(const wxString& name, wxDateTime *value) const
 {
     int i = m_data->FindOption(name);
+    if ( i == wxNOT_FOUND )
+        i = m_data->FindOptionByLongName(name);
+
     wxCHECK_MSG( i != wxNOT_FOUND, FALSE, _T("unknown option") );
 
     wxCmdLineOption& opt = m_data->m_options[(size_t)i];
@@ -437,6 +461,17 @@ wxString wxCmdLineParser::GetParam(size_t n) const
     return m_data->m_parameters[n];
 }
 
+// Resets switches and options
+void wxCmdLineParser::Reset()
+{
+    for ( size_t i = 0; i < m_data->m_options.Count(); i++ )
+    {
+        wxCmdLineOption& opt = m_data->m_options[i];
+        opt.SetHasValue(FALSE);
+    }
+}
+
+
 // ----------------------------------------------------------------------------
 // the real work is done here
 // ----------------------------------------------------------------------------
@@ -452,6 +487,8 @@ int wxCmdLineParser::Parse()
 
     size_t countParam = m_data->m_paramDesc.GetCount();
 
+    Reset();
+
     // parse everything
     wxString arg;
     size_t count = m_data->m_arguments.GetCount();
@@ -893,7 +930,7 @@ void wxCmdLineParser::Usage()
 }
 
 // ----------------------------------------------------------------------------
-// global functions
+// private functions
 // ----------------------------------------------------------------------------
 
 static wxString GetTypeName(wxCmdLineParamType type)
@@ -912,3 +949,107 @@ static wxString GetTypeName(wxCmdLineParamType type)
 
     return s;
 }
+
+#endif // wxUSE_CMDLINE_PARSER
+
+// ----------------------------------------------------------------------------
+// global functions
+// ----------------------------------------------------------------------------
+
+/* static */
+wxArrayString wxCmdLineParser::ConvertStringToArgs(const wxChar *p)
+{
+    wxArrayString args;
+
+    wxString arg;
+    arg.reserve(1024);
+
+    bool isInsideQuotes = FALSE;
+    for ( ;; )
+    {
+        // skip white space
+        while ( *p == _T(' ') || *p == _T('\t') )
+            p++;
+
+        // anything left?
+        if ( *p == _T('\0') )
+            break;
+
+        // parse this parameter
+        arg.clear();
+        for ( ;; p++ )
+        {
+            // do we have a (lone) backslash?
+            bool isQuotedByBS = FALSE;
+            while ( *p == _T('\\') )
+            {
+                p++;
+
+                // if we have 2 backslashes in a row, output one
+                if ( isQuotedByBS )
+                {
+                    arg += _T('\\');
+                    isQuotedByBS = FALSE;
+                }
+                else // the next char is quoted
+                {
+                    isQuotedByBS = TRUE;
+                }
+            }
+
+            bool skipChar = FALSE,
+                 endParam = FALSE;
+            switch ( *p )
+            {
+                case _T('"'):
+                    if ( !isQuotedByBS )
+                    {
+                        // don't put the quote itself in the arg
+                        skipChar = TRUE;
+
+                        isInsideQuotes = !isInsideQuotes;
+                    }
+                    //else: insert a literal quote
+
+                    break;
+
+                case _T(' '):
+                case _T('\t'):
+                    if ( isInsideQuotes || isQuotedByBS )
+                    {
+                        // preserve it, skip endParam below
+                        break;
+                    }
+                    //else: fall through
+
+                case _T('\0'):
+                    endParam = TRUE;
+                    break;
+
+                default:
+                    if ( isQuotedByBS )
+                    {
+                        // ignore backslash before an ordinary character - this
+                        // is needed to properly handle the file names under
+                        // Windows appearing in the command line
+                        arg += _T('\\');
+                    }
+            }
+
+            // end of argument?
+            if ( endParam )
+                break;
+
+            // otherwise copy this char to arg
+            if ( !skipChar )
+            {
+                arg += *p;
+            }
+        }
+
+        args.Add(arg);
+    }
+
+    return args;
+}
+