1 // Created on: 2015-03-15
2 // Created by: Danila ULYANOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <ViewerTest_CmdParser.hxx>
25 //===============================================================================================
26 //function : ViewerTest_CmdParser
28 //===============================================================================================
29 ViewerTest_CmdParser::ViewerTest_CmdParser()
31 ViewerTest_CmdOption aDefaultOption;
32 myArgumentStorage.push_back (aDefaultOption);
33 myArgumentLists[""] = 0;
34 myArgumentLists["help"] = 0;
37 //===============================================================================================
38 //function : AddOption
40 //===============================================================================================
41 void ViewerTest_CmdParser::AddOption (const std::string& theOptionNames, const std::string& theOptionDescription)
43 ViewerTest_CmdOption aNewOption;
45 // extract option names
46 std::vector<std::string> aNames;
47 std::stringstream aStream (theOptionNames);
49 while (std::getline (aStream, anItem, '|'))
51 std::transform (anItem.begin(), anItem.end(), anItem.begin(), ::tolower);
54 aNames.push_back (anItem);
58 aNewOption.Name = aNames.front();
59 aNewOption.Description = theOptionDescription;
60 aNewOption.IsSet = Standard_False;
62 myArgumentStorage.push_back (aNewOption);
64 std::vector<std::string>::const_iterator anIt = aNames.begin();
65 for (; anIt != aNames.end(); ++anIt)
67 myArgumentLists[*anIt] = (Standard_Integer) myArgumentStorage.size() - 1;
71 //===============================================================================================
74 //===============================================================================================
75 void ViewerTest_CmdParser::Help()
77 std::cout << myDescription << std::endl;
79 std::vector<ViewerTest_CmdOption>::const_iterator anIt = myArgumentStorage.begin();
80 for (++anIt; anIt != myArgumentStorage.end(); ++anIt)
82 std::cout << "\n -" << (*anIt).Name << " : " << (*anIt).Description;
85 std::cout << std::endl;
88 //===============================================================================================
91 //===============================================================================================
92 void ViewerTest_CmdParser::Parse (Standard_Integer theArgsNb, const char** theArgVec)
94 Standard_Integer aCurrentOption = 0;
96 for (Standard_Integer anIter = 1; anIter < theArgsNb; ++anIter)
98 if (theArgVec[anIter][0] == '-')
100 std::string anOptionName (&theArgVec[anIter][1]);
101 std::transform (anOptionName.begin(), anOptionName.end(), anOptionName.begin(), ::tolower);
103 std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (anOptionName);
104 if (aMapIter != myArgumentLists.end())
106 aCurrentOption = aMapIter->second;
107 myArgumentStorage[aCurrentOption].IsSet = true;
108 myArgumentStorage[aCurrentOption].Arguments.clear();
112 std::cerr << "Error: unknown argument '" << theArgVec[anIter] << "'\n";
117 myArgumentStorage[aCurrentOption].Arguments.push_back (theArgVec[anIter]);
122 //===============================================================================================
123 //function : HasOption
125 //===============================================================================================
126 Standard_Boolean ViewerTest_CmdParser::HasOption (const std::string& theOptionName, Standard_Integer theMandatoryArgsNb /*= 0*/, Standard_Boolean isFatal /*= Standard_False*/)
128 std::string aLowerName = theOptionName;
129 std::transform (aLowerName.begin(), aLowerName.end(), aLowerName.begin(), ::tolower);
131 Standard_Boolean aResult = Standard_False;
132 std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (aLowerName);
133 if (aMapIter != myArgumentLists.end())
135 Standard_Integer anOption = aMapIter->second;
136 aResult = myArgumentStorage[anOption].Arguments.size() >= static_cast<size_t> (theMandatoryArgsNb);
137 if (isFatal && !aResult && myArgumentStorage[anOption].IsSet)
139 std::cerr << "Error: wrong syntax at argument '" << theOptionName << "'\n";
142 aResult &= myArgumentStorage[anOption].IsSet;
148 //===============================================================================================
151 //===============================================================================================
152 std::string ViewerTest_CmdParser::Arg (const std::string& theOptionName, Standard_Integer theArgumentIndex)
154 std::string aLowerName = theOptionName;
155 std::transform (aLowerName.begin(), aLowerName.end(), aLowerName.begin(), ::tolower);
157 std::map<std::string, Standard_Integer>::iterator aMapIter = myArgumentLists.find (aLowerName);
158 if (aMapIter != myArgumentLists.end())
160 Standard_Integer anOption = aMapIter->second;
161 if (myArgumentStorage[anOption].Arguments.size() > static_cast<size_t> (theArgumentIndex))
163 return myArgumentStorage[anOption].Arguments[theArgumentIndex];
167 std::cerr << "Error: wrong syntax at argument '" << aLowerName << "'\n";
174 //===============================================================================================
175 //function : ArgVec3f
177 //===============================================================================================
178 Graphic3d_Vec3 ViewerTest_CmdParser::ArgVec3f (const std::string& theOptionName, Standard_Integer theArgumentIndex)
180 return Graphic3d_Vec3 (static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str())),
181 static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str())),
182 static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str())));
185 //===============================================================================================
186 //function : ArgVec3d
188 //===============================================================================================
189 Graphic3d_Vec3d ViewerTest_CmdParser::ArgVec3d (const std::string& theOptionName, Standard_Integer theArgumentIndex)
191 return Graphic3d_Vec3d ( Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str()),
192 Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
193 Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
196 //===============================================================================================
199 //===============================================================================================
200 gp_Vec ViewerTest_CmdParser::ArgVec (const std::string& theOptionName, Standard_Integer theArgumentIndex)
202 return gp_Vec ( Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str()),
203 Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
204 Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
207 //===============================================================================================
210 //===============================================================================================
211 gp_Pnt ViewerTest_CmdParser::ArgPnt (const std::string& theOptionName, Standard_Integer theArgumentIndex)
213 return gp_Pnt ( Draw::Atof (Arg (theOptionName, theArgumentIndex ).c_str()),
214 Draw::Atof (Arg (theOptionName, theArgumentIndex + 1).c_str()),
215 Draw::Atof (Arg (theOptionName, theArgumentIndex + 2).c_str()));
218 //===============================================================================================
219 //function : ArgDouble
221 //===============================================================================================
222 Standard_Real ViewerTest_CmdParser::ArgDouble (const std::string& theOptionName, Standard_Integer theArgumentIndex)
224 return Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str());
227 //===============================================================================================
228 //function : ArgFloat
230 //===============================================================================================
231 Standard_ShortReal ViewerTest_CmdParser::ArgFloat (const std::string& theOptionName, Standard_Integer theArgumentIndex)
233 return static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str()));
236 //===============================================================================================
239 //===============================================================================================
240 Standard_Integer ViewerTest_CmdParser::ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex)
242 return static_cast<Standard_Integer> (Draw::Atoi (Arg (theOptionName, theArgumentIndex).c_str()));
245 //===============================================================================================
248 //===============================================================================================
249 Standard_Boolean ViewerTest_CmdParser::ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex)
251 return static_cast<Standard_Boolean> (Draw::Atoi (Arg (theOptionName, theArgumentIndex).c_str()));