0027232: Configuration - fix mblen missing building issue on Android
[occt.git] / src / ViewerTest / ViewerTest_CmdParser.cxx
CommitLineData
189f85a3 1// Created on: 2015-03-15
2// Created by: Danila ULYANOV
3// Copyright (c) 2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#include <ViewerTest_CmdParser.hxx>
17
18#include <Draw.hxx>
19
20#include <iostream>
21#include <sstream>
22#include <string>
23
24
25//===============================================================================================
26//function : ViewerTest_CmdParser
27//purpose :
28//===============================================================================================
29ViewerTest_CmdParser::ViewerTest_CmdParser()
30{
31 ViewerTest_CmdOption aDefaultOption;
32 myArgumentStorage.push_back (aDefaultOption);
33 myArgumentLists[""] = 0;
34 myArgumentLists["help"] = 0;
35}
36
37//===============================================================================================
38//function : AddOption
39//purpose :
40//===============================================================================================
41void ViewerTest_CmdParser::AddOption (const std::string& theOptionNames, const std::string& theOptionDescription)
42{
43 ViewerTest_CmdOption aNewOption;
44
45 // extract option names
46 std::vector<std::string> aNames;
47 std::stringstream aStream (theOptionNames);
48 std::string anItem;
49 while (std::getline (aStream, anItem, '|'))
50 {
51 std::transform (anItem.begin(), anItem.end(), anItem.begin(), ::tolower);
52 if (!anItem.empty())
53 {
54 aNames.push_back (anItem);
55 }
56 }
57
58 aNewOption.Name = aNames.front();
59 aNewOption.Description = theOptionDescription;
60 aNewOption.IsSet = Standard_False;
61
62 myArgumentStorage.push_back (aNewOption);
63
64 std::vector<std::string>::const_iterator anIt = aNames.begin();
65 for (; anIt != aNames.end(); ++anIt)
66 {
67 myArgumentLists[*anIt] = (Standard_Integer) myArgumentStorage.size() - 1;
68 }
69}
70
71//===============================================================================================
72//function : Help
73//purpose :
74//===============================================================================================
75void ViewerTest_CmdParser::Help()
76{
77 std::cout << myDescription << std::endl;
78
79 std::vector<ViewerTest_CmdOption>::const_iterator anIt = myArgumentStorage.begin();
80 for (++anIt; anIt != myArgumentStorage.end(); ++anIt)
81 {
82 std::cout << "\n -" << (*anIt).Name << " : " << (*anIt).Description;
83 }
84
85 std::cout << std::endl;
86}
87
88//===============================================================================================
89//function : Parse
90//purpose :
91//===============================================================================================
92void ViewerTest_CmdParser::Parse (Standard_Integer theArgsNb, const char** theArgVec)
93{
94 Standard_Integer aCurrentOption = 0;
95
96 for (Standard_Integer anIter = 1; anIter < theArgsNb; ++anIter)
97 {
98 if (theArgVec[anIter][0] == '-')
99 {
100 std::string anOptionName (&theArgVec[anIter][1]);
101 std::transform (anOptionName.begin(), anOptionName.end(), anOptionName.begin(), ::tolower);
102
103 std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (anOptionName);
104 if (aMapIter != myArgumentLists.end())
105 {
106 aCurrentOption = aMapIter->second;
107 myArgumentStorage[aCurrentOption].IsSet = true;
108 myArgumentStorage[aCurrentOption].Arguments.clear();
109 }
110 else
111 {
112 std::cerr << "Error: unknown argument '" << theArgVec[anIter] << "'\n";
113 }
114 }
115 else
116 {
117 myArgumentStorage[aCurrentOption].Arguments.push_back (theArgVec[anIter]);
118 }
119 }
120}
121
122//===============================================================================================
123//function : HasOption
124//purpose :
125//===============================================================================================
126Standard_Boolean ViewerTest_CmdParser::HasOption (const std::string& theOptionName, Standard_Integer theMandatoryArgsNb /*= 0*/, Standard_Boolean isFatal /*= Standard_False*/)
127{
128 std::string aLowerName = theOptionName;
129 std::transform (aLowerName.begin(), aLowerName.end(), aLowerName.begin(), ::tolower);
130
131 Standard_Boolean aResult = Standard_False;
132 std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (aLowerName);
133 if (aMapIter != myArgumentLists.end())
134 {
135 Standard_Integer anOption = aMapIter->second;
136 aResult = myArgumentStorage[anOption].Arguments.size() >= static_cast<size_t> (theMandatoryArgsNb);
137 if (isFatal && !aResult && myArgumentStorage[anOption].IsSet)
138 {
139 std::cerr << "Error: wrong syntax at argument '" << theOptionName << "'\n";
140 }
141
142 aResult &= myArgumentStorage[anOption].IsSet;
143 }
144
145 return aResult;
146}
147
148//===============================================================================================
149//function : Arg
150//purpose :
151//===============================================================================================
152std::string ViewerTest_CmdParser::Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex)
153{
154 std::string aLowerName = theOptionName;
155 std::transform (aLowerName.begin(), aLowerName.end(), aLowerName.begin(), ::tolower);
156
157 std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (aLowerName);
158 if (aMapIter != myArgumentLists.end())
159 {
160 Standard_Integer anOption = aMapIter->second;
161 if (myArgumentStorage[anOption].Arguments.size() > static_cast<size_t> (theArgumentIndex))
162 {
163 return myArgumentStorage[anOption].Arguments[theArgumentIndex];
164 }
165 else
166 {
167 std::cerr << "Error: wrong syntax at argument '" << aLowerName << "'\n";
168 }
169 }
170
171 return "";
172}
173
174//===============================================================================================
175//function : ArgVec3f
176//purpose :
177//===============================================================================================
178Graphic3d_Vec3 ViewerTest_CmdParser::ArgVec3f (const std::string& theOptionName)
179{
180 return Graphic3d_Vec3 (static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, 0).c_str())),
181 static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, 1).c_str())),
182 static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, 2).c_str())));
183}
184
185//===============================================================================================
186//function : ArgVec3d
187//purpose :
188//===============================================================================================
189Graphic3d_Vec3d ViewerTest_CmdParser::ArgVec3d (const std::string& theOptionName)
190{
191 return Graphic3d_Vec3d ( Draw::Atof (Arg (theOptionName, 0).c_str()),
192 Draw::Atof (Arg (theOptionName, 1).c_str()),
193 Draw::Atof (Arg (theOptionName, 2).c_str()));
194}
195
196//===============================================================================================
197//function : ArgDouble
198//purpose :
199//===============================================================================================
200Standard_Real ViewerTest_CmdParser::ArgDouble (const std::string& theOptionName)
201{
202 return Draw::Atof (Arg (theOptionName, 0).c_str());
203}
204
205//===============================================================================================
206//function : ArgFloat
207//purpose :
208//===============================================================================================
209Standard_ShortReal ViewerTest_CmdParser::ArgFloat (const std::string& theOptionName)
210{
211 return static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, 0).c_str()));
212}