📄 stafcommandparser.cpp
字号:
result.errorBuffer = "Invalid length delimited data specifier, '"; result.errorBuffer += clcLengthString + "'"; *errorBuffer = result.errorBuffer.getImpl(); return kSTAFInvalidRequestString; } unsigned int clcLength = clcLengthString.asUInt(); i = endColonPos + 1; if ((i + clcLength) > aString.length()) { result.errorBuffer = "Invalid length delimited data specifier, '"; result.errorBuffer += clcLengthString + "', because " "specifies a length that is longer than the data"; *errorBuffer = result.errorBuffer.getImpl(); return kSTAFInvalidRequestString; } // Skip data for (unsigned int j = clcLength; j != 0; --j) i += aString.sizeOfChar(i); optionValue.value += aString.subString(endColonPos + 1, i - endColonPos - 1); optionValue.valueSpecified = true; } else if (currentChar == sUTF8_DoubleQuote) { if (hadWhitespace && (optionValue.value.length() != 0)) optionValue.value += sUTF8_Space; ++i; // Advance past the double quote unsigned int done = 0; unsigned int next = 0; do { next = aString.findFirstOf(sUTF8_QuoteSpecial, i); if (next == STAFString::kNPos) { // We didn't find the closing double quote, but since // we're at the end of the string, we're done done = 1; // Advance past the end of the string i = next; } else if (aString.subString(next, 1) == sUTF8_BackSlash) { // Add the data from the last search position optionValue.value += aString.subString(i, next - i); ++next; // Advance past the backslash // Add the character after the backslash optionValue.value += aString.subString(next, aString.sizeOfChar(next)); // Advance past the character after the backslash i = next + aString.sizeOfChar(next); } else { // We found the closing double quote, so we're done done = 1; // Add the data from the last search position optionValue.value += aString.subString(i, next - i); // Advance past the closing double quote i = ++next; } } while (!done); optionValue.valueSpecified = true; } else { // This is just regular data, its not double quoted, or // colon-length-colon format unsigned int endPos = aString.findFirstOf(sUTF8_WordSpecial, i); STAFString data = aString.subString(i, endPos - i); i = endPos; if (stafCommandParser.fOptionList.find( (stafCommandParser.fCaseSensitive) ? data : data.toLowerCase()) != stafCommandParser.fOptionList.end()) { rc = handleOptionValue(stafCommandParser, result, optionValue, errorBuffer); if (rc != kSTAFOk) return rc; optionValue = STAFCommandParserImpl::OptionValue(); optionValue.option = stafCommandParser.fCaseSensitive ? data : data.toLowerCase(); optionValue.optionSpecified = true; } else { if (hadWhitespace && (optionValue.value.length() != 0)) optionValue.value += sUTF8_Space; optionValue.value += data; optionValue.valueSpecified = true; } } } // Deal with any lingering data rc = handleOptionValue(stafCommandParser, result, optionValue, errorBuffer); if (rc != kSTAFOk) return rc; // Debug: // printParseResultInfo2(result); // Now check the restriction on number of arguments RECORD_TIME("Checking args"); if (result.fArgList.size() > stafCommandParser.fMaxArgs) { result.errorBuffer += STAFString("You may have no more than "); result.errorBuffer += STAFString(stafCommandParser.fMaxArgs); result.errorBuffer += STAFString(" argument(s). You specified "); result.errorBuffer += STAFString(result.fArgList.size()); result.errorBuffer += STAFString(" argument(s). "); result.errorBuffer += STAFString("The first excess argument "); result.errorBuffer += STAFString("is, "); result.errorBuffer += STAFString(result.fArgList[stafCommandParser.fMaxArgs]); result.errorBuffer += STAFString("."); *errorBuffer = result.errorBuffer.getImpl(); return kSTAFInvalidRequestString; } // Now check all the group requirements RECORD_TIME("Checking groups"); for(i = 0; i < stafCommandParser.fGroupList.size(); ++i) { STAFCommandParserImpl::OptionGroup group = stafCommandParser.fGroupList[i]; unsigned int groupCount = 0; unsigned int groupWordCount = group.group.numWords(); for (int j = 0; j < groupWordCount; ++j) { if (myOptionTimes(&result, group.group.subWord(j, 1)) != 0) { ++groupCount; } } if ((groupCount < group.minimum) || (groupCount > group.maximum)) { result.errorBuffer += STAFString("You must have at least "); result.errorBuffer += STAFString(group.minimum); result.errorBuffer += STAFString(", but no more than "); result.errorBuffer += STAFString(group.maximum); result.errorBuffer += STAFString(" of the option(s), "); result.errorBuffer += STAFString(group.group); *errorBuffer = result.errorBuffer.getImpl(); return kSTAFInvalidRequestString; } } // Now check the need requirements RECORD_TIME("Checking needs"); for(i = 0; i < stafCommandParser.fNeedList.size(); ++i) { STAFCommandParserImpl::OptionNeed need = stafCommandParser.fNeedList[i]; unsigned int foundNeeder = 0; unsigned int foundNeedee = 0; for(int j = 0; (j < need.needer.numWords()) && !foundNeeder; ++j) { if (myOptionTimes(&result, need.needer.subWord(j, 1)) != 0) { foundNeeder = 1; } } for(int k = 0; (k < need.needee.numWords()) && !foundNeedee; ++k) { if (myOptionTimes(&result, need.needee.subWord(k, 1)) != 0) { foundNeedee = 1; } } if (foundNeeder && !foundNeedee) { result.errorBuffer += STAFString("When specifying one of the options "); result.errorBuffer += STAFString(need.needer); result.errorBuffer += STAFString(", you must also specify one of the "); result.errorBuffer += STAFString("options "); result.errorBuffer += STAFString(need.needee); *errorBuffer = result.errorBuffer.getImpl(); return kSTAFInvalidRequestString; } } RECORD_TIME("Leaving try block"); } catch (...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} RECORD_TIME("Finished parsing"); OUTPUT_TIMES(); return rc;}STAFRC_t STAFCommandParserDestruct(STAFCommandParser_t *pParser, unsigned int *osRC){ if (pParser == 0) return kSTAFInvalidObject; delete *pParser; *pParser = 0; return kSTAFOk;}STAFRC_t STAFCommandParseResultGetErrorBuffer( STAFCommandParseResult_t result, STAFString_t *theBuffer, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { *theBuffer = result->errorBuffer.getImpl(); } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetNumInstances( STAFCommandParseResult_t result, unsigned int *numInstances, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { STAFCommandParseResultImpl &res = *result; *numInstances = res.fInstanceMap.size(); } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetInstanceName( STAFCommandParseResult_t result, unsigned int instanceNum, STAFString_t *instanceName, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { STAFCommandParseResultImpl &res = *result; STAFCommandParseResultImpl::OptionInstanceList::iterator iter; for (iter = res.fInstanceList.begin(); (--instanceNum > 0) && (iter != res.fInstanceList.end()); ++iter) { /* Do Nothing */ } *instanceName = iter->option.getImpl(); } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetInstanceValue( STAFCommandParseResult_t result, unsigned int instanceNum, STAFString_t *instanceValue, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { STAFCommandParseResultImpl &res = *result; STAFCommandParseResultImpl::OptionInstanceList::iterator iter; for (iter = res.fInstanceList.begin(); (--instanceNum > 0) && (iter != res.fInstanceList.end()); ++iter) { /* Do Nothing */ } *instanceValue = iter->value.getImpl(); } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetOptionTimes( STAFCommandParseResult_t result, const STAFString_t optionName, unsigned int *numTimes, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { STAFCommandParseResultImpl &res = *result; STAFString theOptionName(optionName); if (!res.fCaseSensitive) theOptionName.lowerCase(); std::pair<STAFCommandParseResultImpl::OptionInstanceMap::iterator, STAFCommandParseResultImpl::OptionInstanceMap::iterator> iterPair = res.fInstanceMap.equal_range(theOptionName); if (iterPair.first == res.fInstanceMap.end()) { *numTimes = 0; } else { #ifdef STAF_OS_NAME_HPUX std::distance(iterPair.first, iterPair.second, *numTimes); #else *numTimes = std::distance(iterPair.first, iterPair.second); #endif } } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetOptionValue( STAFCommandParseResult_t result, const STAFString_t optionName, unsigned int optionIndex, STAFString_t *optionValue, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; if ((optionIndex == 0) || (optionName == 0) || (optionValue == 0)) return kSTAFInvalidParm; try { STAFCommandParseResultImpl &res = *result; STAFString theOptionName(optionName); if (!res.fCaseSensitive) theOptionName.lowerCase(); std::pair<STAFCommandParseResultImpl::OptionInstanceMap::iterator, STAFCommandParseResultImpl::OptionInstanceMap::iterator> iterPair = res.fInstanceMap.equal_range(theOptionName); while ((--optionIndex != 0) && (iterPair.first != iterPair.second) && (iterPair.first != res.fInstanceMap.end())) { ++iterPair.first; } if ((iterPair.first == res.fInstanceMap.end()) || (iterPair.first == iterPair.second)) { *optionValue = 0; } else { *optionValue = iterPair.first->second.value.getImpl(); } } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetNumArgs(STAFCommandParseResult_t result, unsigned int *numArgs, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { STAFCommandParseResultImpl &res = *result; *numArgs = res.fArgList.size(); } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultGetArgValue( STAFCommandParseResult_t result, unsigned int argNum, STAFString_t *argValue, unsigned int *osRC){ STAFRC_t rc = kSTAFOk; if (result == 0) return kSTAFInvalidObject; try { STAFCommandParseResultImpl &res = *result; *argValue = res.fArgList[argNum].getImpl(); } catch(...) { rc = kSTAFUnknownError; if (osRC) *osRC = 0xFFFFFFFF;} return rc;}STAFRC_t STAFCommandParseResultDestruct(STAFCommandParseResult_t *pResult, unsigned int *osRC){ if (pResult == 0) return kSTAFInvalidObject; delete *pResult; *pResult = 0; return kSTAFOk;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -