summaryrefslogtreecommitdiffstats
path: root/Source/WXDialog/CommandLineInfo.cpp
blob: 5f6b14ee4fbfa530058ea305b724dfa18cfd1ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*=========================================================================

  Program:   WXDialog - wxWidgets X-platform GUI Front-End for CMake
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Author:    Jorgen Bodde

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "CommandLineInfo.h" 

#include "cmSystemTools.h"

///////////////////////////////////////////////////////////////
// cmCommandLineInfo 

cmCommandLineInfo::cmCommandLineInfo()
{
  this->m_WhereSource = "";
  this->m_WhereBuild = "";
  this->m_AdvancedValues = false;
  m_GeneratorChoiceString.Empty();
  this->m_LastUnknownParameter = "";
  this->m_ValidArguments = "";
  this->m_ExitAfterLoad = false;
} 

///////////////////////////////////////////////////////////////
cmCommandLineInfo::~cmCommandLineInfo()
{
}

///////////////////////////////////////////////////////////////
void cmCommandLineInfo::ParseCommandLine(int argc, char* argv[])
{
    for ( int cc = 1; cc < argc; cc ++ )
    {
        if ( strlen(argv[cc]) < 1 )
            continue;

        std::string argument = argv[cc];
        bool valid = ((argument.size() > 1) && (m_ValidArguments.find(argument[1]) == std::string::npos));
        
        ParseParam(argument, valid, (cc + 1 == argc));
    }
  
    m_ExecutablePath = cmSystemTools::GetFilenamePath(argv[0]);
}

///////////////////////////////////////////////////////////////
int cmCommandLineInfo::GetBoolValue(const std::string& v) {
  std::string value = cmSystemTools::LowerCase(v);
  if (value == "1" || 
      value == "on" || 
      value == "true" || 
      value == "yes")
    {
    return 1;
    }
  else if (value == "0" || 
           value == "off" || 
           value == "false" || 
           value == "no")
    {
    return -1;
    }
  return 0;
}

///////////////////////////////////////////////////////////////
// Parse param

void cmCommandLineInfo::ParseParam(const std::string& parameter, 
                                   bool know_about, bool /*last*/)
{
    // this is the last parameter we know, so we assign this to be
    // path to source or path to existing build
    if(!know_about)
        m_LastUnknownParameter = parameter;
    else
    {
        std::string sParam(parameter.c_str(), 1, parameter.npos);
        
        // Single letter valued flag like /B=value or /B:value
        std::string value;
        if (sParam[1] == '=' || sParam[1] == ':')
        {
            value = std::string(parameter.c_str()+3);
        }
        else
        {
            value = std::string(parameter.c_str()+2);
        }
        int res;
    
        switch (sParam[0])
        {
            case 'A':
                res = cmCommandLineInfo::GetBoolValue(value);
                if (res == 1)
                {
                    m_AdvancedValues = true;
                }
                else if (res == -1)
                {
                    m_AdvancedValues = false;
                }
                break;
      
            case 'B':
                m_WhereBuild = value;
                break;
      
            case 'G':
                m_GeneratorChoiceString = GetStringParam(value.c_str());
                break;
      
            case 'Q':
                m_ExitAfterLoad = true;
                break;
            
            case 'H':
                m_WhereSource = value;
                break;
        }
    }
}

// When the string param given has string quotes around it
// we remove them and we pass back the string. If not, we 
// simply pass back the string as-is
wxString cmCommandLineInfo::GetStringParam(const char *pString)
{
    wxCHECK(pString, wxEmptyString);

    wxString str(pString);
    str = str.Strip(wxString::both);

    // if we have only one (or no chars return the current string)
    if(str.Len() < 2)
        return str;

    // if we have quotes
    if(str.GetChar(0) == '\"' && str.Last() == '\"')
    {
        // when we only have 2 in size, return empty string
        if(str.Len() == 2)
            return wxEmptyString;

        // now remove the outer and inner, and return
        return str.Mid(1, str.Len()-1);
    }

    return str;
}