| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Lasse Tassing |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | ----------------------------------------------------------------------------- |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #include "StdAfx.h" |
|---|
| 27 | #include <vector> |
|---|
| 28 | |
|---|
| 29 | CDDObject *SharedUtilities::GetCommandLineParms(void) |
|---|
| 30 | { |
|---|
| 31 | // Get command line parameters for current process |
|---|
| 32 | LPSTR lpStr=GetCommandLine(); |
|---|
| 33 | |
|---|
| 34 | // Create new object |
|---|
| 35 | CDDObject *pParms=new CDDObject; |
|---|
| 36 | |
|---|
| 37 | // Break command line into seperate strings |
|---|
| 38 | std::vector< std::string > lCmdParms; |
|---|
| 39 | bool bActiveParm=false; |
|---|
| 40 | char cParmDelimiter=' '; |
|---|
| 41 | LPSTR pszParmStart=lpStr; |
|---|
| 42 | while(*lpStr!=0) |
|---|
| 43 | { |
|---|
| 44 | // Check if we hit end-of-parameter delimiter (' ' or '"') |
|---|
| 45 | if(*lpStr==cParmDelimiter) |
|---|
| 46 | { |
|---|
| 47 | std::string sNew; |
|---|
| 48 | |
|---|
| 49 | // Length of parameter |
|---|
| 50 | int iLen=((int)(lpStr-pszParmStart)); |
|---|
| 51 | if(iLen>0) |
|---|
| 52 | { |
|---|
| 53 | // Yep, we have a valid parameter string - add it to the list of RAW command strings |
|---|
| 54 | sNew.assign(pszParmStart, iLen); |
|---|
| 55 | lCmdParms.push_back(sNew); |
|---|
| 56 | } |
|---|
| 57 | // Skip '"' and all white spaces |
|---|
| 58 | if(*lpStr=='"') *lpStr++; |
|---|
| 59 | while(isspace(*lpStr) && *lpStr!=0) lpStr++; |
|---|
| 60 | |
|---|
| 61 | // We assume next command is delimited by a ' ' (this will change, if we encounter a '"') |
|---|
| 62 | cParmDelimiter=' '; |
|---|
| 63 | pszParmStart=lpStr; |
|---|
| 64 | bActiveParm=false; |
|---|
| 65 | } else if(*lpStr=='"') |
|---|
| 66 | { |
|---|
| 67 | // We hit a '"' - then the parameter will be delimited by another '"' |
|---|
| 68 | cParmDelimiter='"'; |
|---|
| 69 | ++lpStr; |
|---|
| 70 | if(!bActiveParm) |
|---|
| 71 | { |
|---|
| 72 | // It was during the beginning of a parameter, skip the '"' |
|---|
| 73 | pszParmStart=lpStr; |
|---|
| 74 | bActiveParm=true; |
|---|
| 75 | } |
|---|
| 76 | } else |
|---|
| 77 | { |
|---|
| 78 | // Skip to next character |
|---|
| 79 | ++lpStr; |
|---|
| 80 | bActiveParm=true; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | // Check if there was one last parameter |
|---|
| 84 | std::string sNew; |
|---|
| 85 | int iLen=((int)(lpStr-pszParmStart)); |
|---|
| 86 | if(iLen>0) |
|---|
| 87 | { |
|---|
| 88 | sNew.assign(pszParmStart, iLen); |
|---|
| 89 | lCmdParms.push_back(sNew); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | // OK - check if we got any parameters at all? |
|---|
| 93 | if(!lCmdParms.size()) return pParms; |
|---|
| 94 | |
|---|
| 95 | // Setup base information |
|---|
| 96 | pParms->SetString("AppPath", lCmdParms[0].c_str()); |
|---|
| 97 | // pParms->SetStringList("RAWParms", lCmdParms); |
|---|
| 98 | |
|---|
| 99 | // Parse parameters (note that first argument is skipped - assumed to be application path) |
|---|
| 100 | for(unsigned p=1;p<lCmdParms.size();p++) |
|---|
| 101 | { |
|---|
| 102 | // Setup pointers |
|---|
| 103 | const char *pArg=lCmdParms[p].c_str(); |
|---|
| 104 | |
|---|
| 105 | // Try to find '=' or ':' - they delimit a value pair |
|---|
| 106 | const char *pSep=strchr(pArg, '='); |
|---|
| 107 | if(!pSep) pSep=strchr(pArg, ':'); |
|---|
| 108 | |
|---|
| 109 | // If the current parameter string was delimited, we parse it! |
|---|
| 110 | if(pSep) |
|---|
| 111 | { |
|---|
| 112 | // Skip tokens, spaces and '"' |
|---|
| 113 | while((*pArg=='/' || *pArg=='+' || *pArg=='-' || isspace(*pArg) || *pArg=='"') && *pArg!=0) pArg++; |
|---|
| 114 | if(!*pArg) continue; // broken argument |
|---|
| 115 | |
|---|
| 116 | // Extract parameter name |
|---|
| 117 | std::string sParmName; |
|---|
| 118 | sParmName.assign(pArg, (int)(pSep-pArg)); |
|---|
| 119 | // for(unsigned ic=0;ic<sParmName.size();ic++) |
|---|
| 120 | // tolower(sParmName[ic]); |
|---|
| 121 | |
|---|
| 122 | // Skip tokens, spaces and '"' |
|---|
| 123 | while((*pSep=='=' || *pSep==':' || isspace(*pSep) || *pSep=='"') && *pSep!=0) pSep++; |
|---|
| 124 | if(!pSep) continue; // broken argument |
|---|
| 125 | |
|---|
| 126 | // Try to convert the value to a long |
|---|
| 127 | char *pStop; |
|---|
| 128 | long iValue=strtol(pSep, &pStop, 0); |
|---|
| 129 | if(!iValue && pStop==pSep) |
|---|
| 130 | { |
|---|
| 131 | // Conversion failed - we assume it is a string |
|---|
| 132 | pParms->SetString(sParmName.c_str(), pSep); |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | // A comma seperated list of integers? |
|---|
| 137 | if(*pStop==',') |
|---|
| 138 | { |
|---|
| 139 | // Yep, create list |
|---|
| 140 | fastvector<int> lIntList; |
|---|
| 141 | |
|---|
| 142 | // Add the current value to the list |
|---|
| 143 | lIntList.push_back(iValue); |
|---|
| 144 | |
|---|
| 145 | // Enter loop.. |
|---|
| 146 | do { |
|---|
| 147 | // Skip past the ',' and all whitespaces |
|---|
| 148 | pSep=pStop+1; |
|---|
| 149 | while(isspace(*pSep) && *pSep!=0) pSep++; |
|---|
| 150 | |
|---|
| 151 | // Check if we hit last character |
|---|
| 152 | if(*pSep==0) break; |
|---|
| 153 | |
|---|
| 154 | // Try to convert current string |
|---|
| 155 | long iValue=strtol(pSep, &pStop, 0); |
|---|
| 156 | |
|---|
| 157 | // If it went well, we add the value to the list |
|---|
| 158 | if(iValue || pStop!=pSep) lIntList.push_back(iValue); |
|---|
| 159 | |
|---|
| 160 | // Continue as long as the argument string is valid |
|---|
| 161 | } while(pStop!=pSep && *pStop!=0); |
|---|
| 162 | |
|---|
| 163 | // Put the assembled integer list into the parameter object |
|---|
| 164 | pParms->SetIntList(sParmName.c_str(), lIntList); |
|---|
| 165 | } else |
|---|
| 166 | { |
|---|
| 167 | // Put the integer value into the parameter object |
|---|
| 168 | pParms->SetInt(sParmName.c_str(), iValue); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } else |
|---|
| 172 | { |
|---|
| 173 | // Skip any token charaters and add the parameter as an integer to the parameter object |
|---|
| 174 | if(*pArg=='/' || *pArg=='+' || *pArg=='-') |
|---|
| 175 | pParms->SetInt(pArg+1, *pArg); |
|---|
| 176 | else |
|---|
| 177 | pParms->SetInt(pArg, *pArg); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | // Return object |
|---|
| 182 | return pParms; |
|---|
| 183 | } |
|---|