| 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 | //=============================================================================================== |
| 29 | ViewerTest_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 | //=============================================================================================== |
| 41 | void 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 | //=============================================================================================== |
| 75 | void 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 | //=============================================================================================== |
| 92 | void 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] == '-' && !std::isdigit (theArgVec[anIter][1])) |
| 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 | //=============================================================================================== |
| 126 | Standard_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 | //=============================================================================================== |
| 152 | std::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 | //=============================================================================================== |
| 178 | Graphic3d_Vec3 ViewerTest_CmdParser::ArgVec3f (const std::string& theOptionName, Standard_Integer theArgumentIndex) |
| 179 | { |
| 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()))); |
| 183 | } |
| 184 | |
| 185 | //=============================================================================================== |
| 186 | //function : ArgVec3d |
| 187 | //purpose : |
| 188 | //=============================================================================================== |
| 189 | Graphic3d_Vec3d ViewerTest_CmdParser::ArgVec3d (const std::string& theOptionName, Standard_Integer theArgumentIndex) |
| 190 | { |
| 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())); |
| 194 | } |
| 195 | |
| 196 | //=============================================================================================== |
| 197 | //function : ArgVec |
| 198 | //purpose : |
| 199 | //=============================================================================================== |
| 200 | gp_Vec ViewerTest_CmdParser::ArgVec (const std::string& theOptionName, Standard_Integer theArgumentIndex) |
| 201 | { |
| 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())); |
| 205 | } |
| 206 | |
| 207 | //=============================================================================================== |
| 208 | //function : ArgPnt |
| 209 | //purpose : |
| 210 | //=============================================================================================== |
| 211 | gp_Pnt ViewerTest_CmdParser::ArgPnt (const std::string& theOptionName, Standard_Integer theArgumentIndex) |
| 212 | { |
| 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())); |
| 216 | } |
| 217 | |
| 218 | //=============================================================================================== |
| 219 | //function : ArgDouble |
| 220 | //purpose : |
| 221 | //=============================================================================================== |
| 222 | Standard_Real ViewerTest_CmdParser::ArgDouble (const std::string& theOptionName, Standard_Integer theArgumentIndex) |
| 223 | { |
| 224 | return Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str()); |
| 225 | } |
| 226 | |
| 227 | //=============================================================================================== |
| 228 | //function : ArgFloat |
| 229 | //purpose : |
| 230 | //=============================================================================================== |
| 231 | Standard_ShortReal ViewerTest_CmdParser::ArgFloat (const std::string& theOptionName, Standard_Integer theArgumentIndex) |
| 232 | { |
| 233 | return static_cast<Standard_ShortReal> (Draw::Atof (Arg (theOptionName, theArgumentIndex).c_str())); |
| 234 | } |
| 235 | |
| 236 | //=============================================================================================== |
| 237 | //function : ArgInt |
| 238 | //purpose : |
| 239 | //=============================================================================================== |
| 240 | Standard_Integer ViewerTest_CmdParser::ArgInt (const std::string& theOptionName, const Standard_Integer theArgumentIndex) |
| 241 | { |
| 242 | return static_cast<Standard_Integer> (Draw::Atoi (Arg (theOptionName, theArgumentIndex).c_str())); |
| 243 | } |
| 244 | |
| 245 | //=============================================================================================== |
| 246 | //function : ArgBool |
| 247 | //purpose : |
| 248 | //=============================================================================================== |
| 249 | Standard_Boolean ViewerTest_CmdParser::ArgBool (const std::string& theOptionName, const Standard_Integer theArgumentIndex) |
| 250 | { |
| 251 | return Draw::Atoi (Arg (theOptionName, theArgumentIndex).c_str()) != 0; |
| 252 | } |