// Copyright (c) 2007, Monju System Architects
//
// Released under the Monju-Public copyright license:
//
// Permission is hereby granted, free of charge, to any person or entity
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies and modifications
// of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of published, sold, or
// ownership-transferred versions of the Software. The notices are not
// required for binary-only releases or binary-only derivatives of the
// Software, or documentation provided with binary-only releases and
// binary-only derivatives.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// INCLUDES.
#include "public/parseArgument.h"
// parseArgument()
//
// Given a null terminated command line, this parses arguments off the
// command line. It should handle quoting properly, but it does not
// handle some of the trickier command lines that Windows might have
// (e.g. 'dumb.exe "foo bar.dll",42').
void parseArgument(const char* pBuffer,
char* pResult,
int* pInOutPosition,
int bufferSize) {
pBuffer = pBuffer + *pInOutPosition;
int onChar = 0;
int onResultChar = 0;
// SKIP LEFT WHITE SPACE.
while (true) {
if (pBuffer[onChar] == '\0') {
break;
}
if (pBuffer[onChar] == ' ') {
onChar++;
continue;
}
if (pBuffer[onChar] == '\t') {
onChar++;
continue;
}
break;
}
// LOOK FOR END OF ARGUMENT.
bool start = true;
bool quoted = false;
bool inEscape = false;
char character;
while (true) {
// CHECK FOR BUFFER OVERRUN.
if ((onChar >= bufferSize) && (bufferSize >= 0)) {
throw("parseArgument(): Buffer overflow.");
}
// CHECK FOR END OF STRING.
character = pBuffer[onChar];
onChar++;
if (character == '\0') {
break;
}
// DETERMINE IF WE ARE QUOTED.
if (start) {
if (character == '\"') {
quoted = true;
start = false;
continue;
}
start = false;
}
// HANDLE ESCAPED CHARACTER.
if (inEscape) {
pResult[onResultChar] = character;
onResultChar++;
}
// HANDLE NON-ESCAPED CHARACTER.
else {
if (character == '\\') {
inEscape = true;
continue;
}
if (quoted && (character == '\"')) {
break;
}
if (!quoted && (character == ' ')) {
break;
}
pResult[onResultChar] = character;
onResultChar++;
}
}
// UPDATE THE RESULT.
pResult[onResultChar] = '\0';
*pInOutPosition += onChar;
}
|