📄 cpstringutils.cpp
字号:
if (theSrcCStr[theCurrentLineOffset] == '\0') { SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Too few lines in source text, can't get line #",inWhichLine); } byteCount = 0; while (theSrcCStr[theCurrentLineOffset + byteCount] != '\r' && theSrcCStr[theCurrentLineOffset + byteCount] != '\0') { byteCount++; } *outNewHandle = NewHandle(byteCount + 1); if (*outNewHandle == nil) { SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Can't allocate Handle, MemError() = ",MemError()); } ::BlockMove(theSrcCStr + theCurrentLineOffset,**outNewHandle,byteCount); (**outNewHandle)[byteCount] = '\0'; errCode = noErr;EXITPOINT: return(errCode);}OSErr CountDigits(const char *inCStr,int *outNumIntegerDigits,int *outNumFractDigits){OSErr errCode = noErr;int numIntDigits = 0;int numFractDigits = 0;int digitIndex = 0; SetErrorMessageAndBailIfNil(inCStr,"CountDigits: Bad parameter, theSrcCStr == nil"); SetErrorMessageAndBailIfNil(outNumIntegerDigits,"CountDigits: Bad parameter, outNumIntegerDigits == nil"); SetErrorMessageAndBailIfNil(outNumFractDigits,"CountDigits: Bad parameter, outNumFractDigits == nil"); digitIndex = 0; while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9') { digitIndex++; numIntDigits++; } if (inCStr[digitIndex] == '.') { digitIndex++; while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9') { digitIndex++; numFractDigits++; } } *outNumIntegerDigits = numIntDigits; *outNumFractDigits = numFractDigits; errCode = noErr; EXITPOINT: return(errCode);}OSErr ExtractIntFromCStr(const char *theSrcCStr,int *outInt,Boolean skipLeadingSpaces){OSErr errCode;int theCharIndex; if (theSrcCStr == nil) { SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr == nil")); } if (outInt == nil) { SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, outInt == nil")); } *outInt = 0; theCharIndex = 0; if (skipLeadingSpaces == true) { while (theSrcCStr[theCharIndex] == ' ') { theCharIndex++; } } if (theSrcCStr[theCharIndex] < '0' || theSrcCStr[theCharIndex] > '9') { SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr contains a bogus numeric representation")); } while (theSrcCStr[theCharIndex] >= '0' && theSrcCStr[theCharIndex] <= '9') { *outInt = (*outInt * 10) + (theSrcCStr[theCharIndex] - '0'); theCharIndex++; } errCode = noErr;EXITPOINT: return(errCode);}OSErr ExtractIntFromPStr(const unsigned char *theSrcPStr,int *outInt,Boolean skipLeadingSpaces){OSErr errCode;char theCStr[256]; if (theSrcPStr == nil) { SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, theSrcPStr == nil")); } if (outInt == nil) { SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, outInt == nil")); } CopyPStrToCStr(theSrcPStr,theCStr,sizeof(theCStr)); errCode = ExtractIntFromCStr(theCStr,outInt,skipLeadingSpaces);EXITPOINT: return(errCode);}int CountOccurencesOfCharInCStr(const char inChar,const char *inSrcCStr){int theSrcCharIndex;int numOccurrences = -1; if (inSrcCStr != nil && inChar != '\0') { numOccurrences = 0; for (theSrcCharIndex = 0;inSrcCStr[theSrcCharIndex] != '\0';theSrcCharIndex++) { if (inSrcCStr[theSrcCharIndex] == inChar) { numOccurrences++; } } } return(numOccurrences);}int CountWordsInCStr(const char *inSrcCStr){int numWords = -1; if (inSrcCStr != nil) { numWords = 0; // Skip lead spaces while (*inSrcCStr == ' ') { inSrcCStr++; } while (*inSrcCStr != '\0') { numWords++; while (*inSrcCStr != ' ' && *inSrcCStr != '\0') { inSrcCStr++; } while (*inSrcCStr == ' ') { inSrcCStr++; } } } return(numWords);}void ConvertCStrToUpperCase(char *theSrcCStr){char *theCharPtr; if (theSrcCStr != nil) { theCharPtr = theSrcCStr; while (*theCharPtr != 0) { if (*theCharPtr >= 'a' && *theCharPtr <= 'z') { *theCharPtr = *theCharPtr - 'a' + 'A'; } theCharPtr++; } }}void ExtractCStrItemFromCStr(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,char *outDstCharPtr,const int inDstCharPtrMaxLength,const Boolean inTreatMultipleDelimsAsSingleDelim){int theItem;int theSrcCharIndex;int theDstCharIndex; if (foundItem != nil) { *foundItem = false; } if (outDstCharPtr != nil && inDstCharPtrMaxLength > 0 && inItemNumber >= 0 && inItemDelimiter != 0) { *outDstCharPtr = 0; theSrcCharIndex = 0; for (theItem = 0;theItem < inItemNumber;theItem++) { while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0') { theSrcCharIndex++; } if (inSrcCStr[theSrcCharIndex] == inItemDelimiter) { theSrcCharIndex++; if (inTreatMultipleDelimsAsSingleDelim) { while (inSrcCStr[theSrcCharIndex] == inItemDelimiter) { theSrcCharIndex++; } } } if (inSrcCStr[theSrcCharIndex] == '\0') { goto EXITPOINT; } } if (foundItem != nil) { *foundItem = true; } theDstCharIndex = 0; for (;;) { if (inSrcCStr[theSrcCharIndex] == 0 || inSrcCStr[theSrcCharIndex] == inItemDelimiter || theDstCharIndex >= inDstCharPtrMaxLength - 1) { outDstCharPtr[theDstCharIndex] = 0; break; } outDstCharPtr[theDstCharIndex++] = inSrcCStr[theSrcCharIndex++]; } } EXITPOINT: return;}OSErr ExtractCStrItemFromCStrIntoNewHandle(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,Handle *outNewHandle,const Boolean inTreatMultipleDelimsAsSingleDelim){OSErr errCode;int theItem;int theSrcCharIndex;int theItemLength; if (inSrcCStr == nil) { SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inSrcCStr == nil"); errCode = kGenericError; goto EXITPOINT; } if (outNewHandle == nil) { SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, outNewHandle == nil"); errCode = kGenericError; goto EXITPOINT; } if (foundItem == nil) { SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, foundItem == nil"); errCode = kGenericError; goto EXITPOINT; } if (inItemNumber < 0) { SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemNumber < 0"); errCode = kGenericError; goto EXITPOINT; } if (inItemDelimiter == 0) { SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemDelimiter == 0"); errCode = kGenericError; goto EXITPOINT; } *foundItem = false; theSrcCharIndex = 0; for (theItem = 0;theItem < inItemNumber;theItem++) { while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0') { theSrcCharIndex++; } if (inSrcCStr[theSrcCharIndex] == inItemDelimiter) { theSrcCharIndex++; if (inTreatMultipleDelimsAsSingleDelim) { while (inSrcCStr[theSrcCharIndex] == inItemDelimiter) { theSrcCharIndex++; } } } if (inSrcCStr[theSrcCharIndex] == '\0') { errCode = noErr; goto EXITPOINT; } } *foundItem = true; for (theItemLength = 0;;theItemLength++) { if (inSrcCStr[theSrcCharIndex + theItemLength] == 0 || inSrcCStr[theSrcCharIndex + theItemLength] == inItemDelimiter) { break; } } *outNewHandle = NewHandle(theItemLength + 1); if (*outNewHandle == nil) { SetErrorMessageAndLongIntAndBail("ExtractCStrItemFromCStrIntoNewHandle: Can't allocate Handle, MemError() = ",MemError()); } BlockMove(inSrcCStr + theSrcCharIndex,**outNewHandle,theItemLength); (**outNewHandle)[theItemLength] = 0; errCode = noErr; EXITPOINT: return(errCode);}OSErr ExtractFloatFromCStr(const char *inCString,extended80 *outFloat){OSErr errCode;Str255 theStr255;Handle theNumberPartsTableHandle = nil;long theNumberPartsOffset,theNumberPartsLength;FormatResultType theFormatResultType;NumberParts theNumberPartsTable;NumFormatStringRec theNumFormatStringRec; if (inCString == nil) { SetErrorMessage("ExtractFloatFromCStr: Bad parameter, inCString == nil"); errCode = kGenericError; goto EXITPOINT; } if (outFloat == nil) { SetErrorMessage("ExtractFloatFromCStr: Bad parameter, outFloat == nil"); errCode = kGenericError; goto EXITPOINT; } // GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); if (theNumberPartsTableHandle == nil) { SetErrorMessage("ExtractFloatFromCStr: Can't get number parts table for converting string representations to/from numeric representations"); errCode = kGenericError; goto EXITPOINT; } if (theNumberPartsLength > sizeof(theNumberPartsTable)) { SetErrorMessage("ExtractFloatFromCStr: Number parts table has bad length"); errCode = kGenericError; goto EXITPOINT; } BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength); theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec); if (theFormatResultType != fFormatOK) { SetErrorMessage("ExtractFloatFromCStr: StringToFormatRec() != fFormatOK"); errCode = kGenericError; goto EXITPOINT; } CopyCStrToPStr(inCString,theStr255,sizeof(theStr255)); theFormatResultType = (FormatResultType) StringToExtended(theStr255,&theNumFormatStringRec,&theNumberPartsTable,outFloat); if (theFormatResultType != fFormatOK && theFormatResultType != fBestGuess) { SetErrorMessageAndLongIntAndBail("ExtractFloatFromCStr: StringToExtended() = ",theFormatResultType); } errCode = noErr; EXITPOINT: return(errCode);}OSErr CopyFloatToCStr(const extended80 *theFloat,char *theCStr,const int maxCStrLength,const int inMaxNumIntDigits,const int inMaxNumFractDigits){OSErr errCode;Str255 theStr255;Handle theNumberPartsTableHandle = nil;long theNumberPartsOffset,theNumberPartsLength;FormatResultType theFormatResultType;NumberParts theNumberPartsTable;NumFormatStringRec theNumFormatStringRec; if (theCStr == nil) { SetErrorMessage("CopyFloatToCStr: Bad parameter, theCStr == nil"); errCode = kGenericError; goto EXITPOINT; } if (theFloat == nil) { SetErrorMessage("CopyFloatToCStr: Bad parameter, theFloat == nil"); errCode = kGenericError; goto EXITPOINT; } // GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); if (theNumberPartsTableHandle == nil) { SetErrorMessage("CopyFloatToCStr: Can't get number parts table for converting string representations to/from numeric representations"); errCode = kGenericError; goto EXITPOINT; } if (theNumberPartsLength > sizeof(theNumberPartsTable)) { SetErrorMessage("CopyFloatToCStr: Number parts table has bad length"); errCode = kGenericError; goto EXITPOINT; } BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength); if (inMaxNumIntDigits >= 0 || inMaxNumFractDigits >= 0) { char numberFormat[64]; int numberFormatLength = 0; for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) { numberFormat[numberFormatLength++] = '0'; } if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1) { numberFormat[numberFormatLength++] = '.'; for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) { numberFormat[numberFormatLength++] = '0'; } } if (numberFormatLength < sizeof(numberFormat) - 1) { numberFormat[numberFormatLength++] = ';'; } if (numberFormatLength < sizeof(numberFormat) - 1) { numberFormat[numberFormatLength++] = '-'; } for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) { numberFormat[numberFormatLength++] = '0'; } if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1) { numberFormat[numberFormatLength++] = '.'; for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) { numberFormat[numberFormatLength++] = '0'; } } numberFormat[numberFormatLength] = '\0'; Str255 tempStr255; CopyCStrToPStr(numberFormat,tempStr255,sizeof(tempStr255)); theFormatResultType = (FormatResultType) StringToFormatRec(tempStr255,&theNumberPartsTable,&theNumFormatStringRec); } else { theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec); } if (theFormatResultType != fFormatOK) { SetErrorMessage("CopyFloatToCStr: StringToFormatRec() != fFormatOK"); errCode = kGenericError; goto EXITPOINT; } theFormatResultType = (FormatResultType) ExtendedToString(theFloat,&theNumFormatStringRec,&theNumberPartsTable,theStr255); if (theFormatResultType != fFormatOK) { SetErrorMessage("CopyFloatToCStr: ExtendedToString() != fFormatOK"); errCode = kGenericError; goto EXITPOINT; } CopyPStrToCStr(theStr255,theCStr,maxCStrLength); errCode = noErr; EXITPOINT: return(errCode);}void SkipWhiteSpace(char **ioSrcCharPtr,const Boolean inStopAtEOL){ if (ioSrcCharPtr != nil && *ioSrcCharPtr != nil) { if (inStopAtEOL) { while ((**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t') && **ioSrcCharPtr != '\r' && **ioSrcCharPtr != '\n') { *ioSrcCharPtr++; } } else { while (**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t') { *ioSrcCharPtr++; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -