summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Utils/CmdLineParser.cpp
blob: 2c63c16f10c9d58f924992c611a9b4ef21d74601 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================

#include "stdafx.h"
#include "Strings.h"
#include "StringLoader.h"

//=============================================================================
//	Includes
//==============================================================================

#include "CmdLineParser.h"
#include "StudioObjectTypes.h"
#include "UICFile.h"
#include "UICString.h"

// using namespace Q3DStudio;  <-- Do not do this here because it will conflict with CList and make
// the template generator go blah

//=============================================================================
/**
 * Create a new Command Line Parser object.
 */
CCmdLineParser::CCmdLineParser()
    : m_Silent(false)
    , m_RunUnitTests(false)
{
}

CCmdLineParser::~CCmdLineParser()
{
}

//=============================================================================
/**
 * If the execution mode is to open a file or execute XML tests strings then
 * this will return the filename used for them.
 * @return filename for file open or XML tests
 */
Q3DStudio::CString CCmdLineParser::GetFilename() const
{
    return m_Filename;
}

//=============================================================================
/**
 * Call to parse the specified parameter from the command line format into
 * a format useful by the app.
 * @param inParameter the name of the parameter to be parsed.
 * @param inIsFlag true if the param is a flag, false if it is an argument.
 */
void CCmdLineParser::ParseParam(const Q3DStudio::CString &inParameter, bool inIsFlag)
{
    if (inIsFlag) {
        if (inParameter == "t" || inParameter == "test") {
            m_ExecutionQueue.push_back(TEST_CMD_LINE);
            m_RunUnitTests = true;
        } else if (inParameter == "silent") {
            m_Silent = true;
        }
    } else {
        m_Params.push_back(inParameter);
    }
}

//=============================================================================
/**
 * Call this to parse all the command line arguments into useful info.
 * This is most often called with the args from main( ).
 * @param inArgc the number of argments being given.
 * @param inArgv the arguments.
 */
void CCmdLineParser::ParseArguments(int inArgc, wchar_t **inArgv)
{
    m_Filename.Clear();
    m_Params.clear();

    for (int i = 0; i < inArgc; ++i) {
        bool isFlag = false;
        Q3DStudio::CString theArg(inArgv[i]);
        if (theArg[(long)0] == '-') {
            isFlag = true;
            // take off the dash
            theArg = theArg.Extract(1);
        }
        ParseParam(theArg, isFlag);
    }

    // m_Params[1]is m_Filename
    // m_Params[0] is the path to the executable
    if (m_Params.size() > 1) {
        ConvertToLongFilename(m_Params[1], m_Filename);
    }

    // Post-processing
    // If there were no switches modifying m_ExecutionMode, default execution mode
    // according to the file extension found on m_Filename.
    if (0 == m_ExecutionQueue.size()) {
        CUICFile theFile(m_Filename);
        Q3DStudio::CString theExtension = "." + theFile.GetExtension();
        if (theExtension.CompareNoCase(::LoadResourceString(IDS_FILE_EXT_UIP)))
            m_ExecutionQueue.push_back(OPEN_FILE);
    }
}

//=============================================================================
/**
 * Get the next main mode of execution that the application should be in.
 * @return EExecutionMode execution mode; END_OF_CMDS if end of queue.
 */
CCmdLineParser::EExecutionMode CCmdLineParser::PopExecutionMode()
{
    EExecutionMode theMode = END_OF_CMDS;
    if (m_ExecutionQueue.size() > 0) {
        theMode = m_ExecutionQueue.front();
        m_ExecutionQueue.pop_front();
    }
    return theMode;
}

//=============================================================================
/**
 * Return true if the silent flag is specified.
 * @return true if running in muted mode. ie. no dialogs.
 */
bool CCmdLineParser::IsSilent() const
{
    return m_Silent;
}

//=============================================================================
/**
 * Return true if running unit tests.
 * @return true if running unit tests.
 */
bool CCmdLineParser::IsRunUnitTests() const
{
    return m_RunUnitTests;
}

//=============================================================================
/**
 * Converts the specified path to its long form. If no long path is found,
 * this function simply returns the specified name.
 * @param inSource			Source filename
 * @param outLongFilename	Output filename
 */
void CCmdLineParser::ConvertToLongFilename(const Q3DStudio::CString &inSource,
                                           Q3DStudio::CString &outLongFilename) const
{
#ifdef _WIN32
    TCHAR thePathBuffer[_MAX_PATH * 2 + 1] = {
        0
    }; // make sure this char buffer is big enough for 2-byte chars
    long theRet = GetLongPathName(inSource, thePathBuffer, _MAX_PATH * 2);
    if (theRet != 0 && theRet <= _MAX_PATH * 2)
        outLongFilename = Q3DStudio::CString(thePathBuffer);
    else
        outLongFilename = inSource; // No conversion
#else
    outLongFilename = inSource;
#endif
}