Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / openssl / MacOS / GetHTTPS.src / CPStringUtils.cpp
blob617aae2c70680da3069675f3e1122cd69522a4b4
1 /* ====================================================================
2 * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
57 #include "CPStringUtils.hpp"
58 #include "ErrorHandling.hpp"
62 #define kNumberFormatString "\p########0.00#######;-########0.00#######"
66 // Useful utility functions which could be optimized a whole lot
69 void CopyPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength)
71 int i,numPChars;
74 if (thePStr != nil && theCStr != nil && maxCStrLength > 0)
76 numPChars = thePStr[0];
78 for (i = 0;;i++)
80 if (i >= numPChars || i >= maxCStrLength - 1)
82 theCStr[i] = 0;
84 break;
87 else
89 theCStr[i] = thePStr[i + 1];
96 void CopyPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength)
98 int theMaxDstStrLength;
101 theMaxDstStrLength = maxDstStrLength;
104 if (theDstPStr != nil && theSrcPStr != nil && theMaxDstStrLength > 0)
106 if (theMaxDstStrLength > 255)
108 theMaxDstStrLength = 255;
112 if (theMaxDstStrLength - 1 < theSrcPStr[0])
114 BlockMove(theSrcPStr + 1,theDstPStr + 1,theMaxDstStrLength - 1);
116 theDstPStr[0] = theMaxDstStrLength - 1;
119 else
121 BlockMove(theSrcPStr,theDstPStr,theSrcPStr[0] + 1);
127 void CopyCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxDstStrLength)
129 int i;
132 if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0)
134 for (i = 0;;i++)
136 if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1)
138 theDstCStr[i] = 0;
140 break;
143 else
145 theDstCStr[i] = theSrcCStr[i];
153 void CopyCSubstrToCStr(const char *theSrcCStr,const int maxCharsToCopy,char *theDstCStr,const int maxDstStrLength)
155 int i;
158 if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0)
160 for (i = 0;;i++)
162 if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1 || i >= maxCharsToCopy)
164 theDstCStr[i] = 0;
166 break;
169 else
171 theDstCStr[i] = theSrcCStr[i];
179 void CopyCSubstrToPStr(const char *theSrcCStr,const int maxCharsToCopy,unsigned char *theDstPStr,const int maxDstStrLength)
181 int i;
182 int theMaxDstStrLength;
185 theMaxDstStrLength = maxDstStrLength;
187 if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0)
189 if (theMaxDstStrLength > 255)
191 theMaxDstStrLength = 255;
195 for (i = 0;;i++)
197 if (theSrcCStr[i] == 0 || i >= theMaxDstStrLength - 1 || i >= maxCharsToCopy)
199 theDstPStr[0] = i;
201 break;
204 else
206 theDstPStr[i + 1] = theSrcCStr[i];
214 void CopyCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength)
216 int i;
217 int theMaxDstStrLength;
220 theMaxDstStrLength = maxDstStrLength;
222 if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0)
224 if (theMaxDstStrLength > 255)
226 theMaxDstStrLength = 255;
230 for (i = 0;;i++)
232 if (i >= theMaxDstStrLength - 1 || theSrcCStr[i] == 0)
234 theDstPStr[0] = i;
236 break;
239 else
241 theDstPStr[i + 1] = theSrcCStr[i];
248 void ConcatPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength)
250 int i,numPChars,cStrLength;
253 if (thePStr != nil && theCStr != nil && maxCStrLength > 0)
255 for (cStrLength = 0;theCStr[cStrLength] != 0;cStrLength++)
261 numPChars = thePStr[0];
264 for (i = 0;;i++)
266 if (i >= numPChars || cStrLength >= maxCStrLength - 1)
268 theCStr[cStrLength++] = 0;
270 break;
273 else
275 theCStr[cStrLength++] = thePStr[i + 1];
283 void ConcatPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength)
285 int theMaxDstStrLength;
288 theMaxDstStrLength = maxDstStrLength;
290 if (theSrcPStr != nil && theDstPStr != nil && theMaxDstStrLength > 0)
292 if (theMaxDstStrLength > 255)
294 theMaxDstStrLength = 255;
298 if (theMaxDstStrLength - theDstPStr[0] - 1 < theSrcPStr[0])
300 BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theMaxDstStrLength - 1 - theDstPStr[0]);
302 theDstPStr[0] = theMaxDstStrLength - 1;
305 else
307 BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theSrcPStr[0]);
309 theDstPStr[0] += theSrcPStr[0];
316 void ConcatCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength)
318 int i,thePStrLength;
319 int theMaxDstStrLength;
322 theMaxDstStrLength = maxDstStrLength;
324 if (theSrcCStr != nil && theDstPStr != nil && theMaxDstStrLength > 0)
326 if (theMaxDstStrLength > 255)
328 theMaxDstStrLength = 255;
332 thePStrLength = theDstPStr[0];
334 for (i = 0;;i++)
336 if (theSrcCStr[i] == 0 || thePStrLength >= theMaxDstStrLength - 1)
338 theDstPStr[0] = thePStrLength;
340 break;
343 else
345 theDstPStr[thePStrLength + 1] = theSrcCStr[i];
347 thePStrLength++;
355 void ConcatCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxCStrLength)
357 int cStrLength;
360 if (theSrcCStr != nil && theDstCStr != nil && maxCStrLength > 0)
362 for (cStrLength = 0;theDstCStr[cStrLength] != 0;cStrLength++)
368 for (;;)
370 if (*theSrcCStr == 0 || cStrLength >= maxCStrLength - 1)
372 theDstCStr[cStrLength++] = 0;
374 break;
377 else
379 theDstCStr[cStrLength++] = *theSrcCStr++;
387 void ConcatCharToCStr(const char theChar,char *theDstCStr,const int maxCStrLength)
389 int cStrLength;
392 if (theDstCStr != nil && maxCStrLength > 0)
394 cStrLength = CStrLength(theDstCStr);
396 if (cStrLength < maxCStrLength - 1)
398 theDstCStr[cStrLength++] = theChar;
399 theDstCStr[cStrLength++] = '\0';
406 void ConcatCharToPStr(const char theChar,unsigned char *theDstPStr,const int maxPStrLength)
408 int pStrLength;
411 if (theDstPStr != nil && maxPStrLength > 0)
413 pStrLength = PStrLength(theDstPStr);
415 if (pStrLength < maxPStrLength - 1 && pStrLength < 255)
417 theDstPStr[pStrLength + 1] = theChar;
418 theDstPStr[0] += 1;
426 int CompareCStrs(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase)
428 int returnValue;
429 char firstChar,secondChar;
432 returnValue = 0;
435 if (theFirstCStr != nil && theSecondCStr != nil)
437 for (;;)
439 firstChar = *theFirstCStr;
440 secondChar = *theSecondCStr;
442 if (ignoreCase == true)
444 if (firstChar >= 'A' && firstChar <= 'Z')
446 firstChar = 'a' + (firstChar - 'A');
449 if (secondChar >= 'A' && secondChar <= 'Z')
451 secondChar = 'a' + (secondChar - 'A');
456 if (firstChar == 0 && secondChar != 0)
458 returnValue = -1;
460 break;
463 else if (firstChar != 0 && secondChar == 0)
465 returnValue = 1;
467 break;
470 else if (firstChar == 0 && secondChar == 0)
472 returnValue = 0;
474 break;
477 else if (firstChar < secondChar)
479 returnValue = -1;
481 break;
484 else if (firstChar > secondChar)
486 returnValue = 1;
488 break;
491 theFirstCStr++;
492 theSecondCStr++;
497 return(returnValue);
502 Boolean CStrsAreEqual(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase)
504 if (CompareCStrs(theFirstCStr,theSecondCStr,ignoreCase) == 0)
506 return true;
509 else
511 return false;
516 Boolean PStrsAreEqual(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase)
518 if (ComparePStrs(theFirstPStr,theSecondPStr,ignoreCase) == 0)
520 return true;
523 else
525 return false;
531 int ComparePStrs(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase)
533 int i,returnValue;
534 char firstChar,secondChar;
537 returnValue = 0;
540 if (theFirstPStr != nil && theSecondPStr != nil)
542 for (i = 1;;i++)
544 firstChar = theFirstPStr[i];
545 secondChar = theSecondPStr[i];
547 if (ignoreCase == true)
549 if (firstChar >= 'A' && firstChar <= 'Z')
551 firstChar = 'a' + (firstChar - 'A');
554 if (secondChar >= 'A' && secondChar <= 'Z')
556 secondChar = 'a' + (secondChar - 'A');
561 if (theFirstPStr[0] < i && theSecondPStr[0] >= i)
563 returnValue = -1;
565 break;
568 else if (theFirstPStr[0] >= i && theSecondPStr[0] < i)
570 returnValue = 1;
572 break;
575 else if (theFirstPStr[0] < i && theSecondPStr[0] < i)
577 returnValue = 0;
579 break;
582 else if (firstChar < secondChar)
584 returnValue = -1;
586 break;
589 else if (firstChar > secondChar)
591 returnValue = 1;
593 break;
599 return(returnValue);
604 int CompareCStrToPStr(const char *theCStr,const unsigned char *thePStr,const Boolean ignoreCase)
606 int returnValue;
607 char tempString[256];
610 returnValue = 0;
612 if (theCStr != nil && thePStr != nil)
614 CopyPStrToCStr(thePStr,tempString,sizeof(tempString));
616 returnValue = CompareCStrs(theCStr,tempString,ignoreCase);
620 return(returnValue);
625 void ConcatLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits)
627 Str255 theStr255;
630 NumToString(theNum,theStr255);
633 if (numDigits > 0)
635 int charsToInsert;
638 charsToInsert = numDigits - PStrLength(theStr255);
640 if (charsToInsert > 0)
642 char tempString[256];
644 CopyCStrToCStr("",tempString,sizeof(tempString));
646 for (;charsToInsert > 0;charsToInsert--)
648 ConcatCStrToCStr("0",tempString,sizeof(tempString));
651 ConcatPStrToCStr(theStr255,tempString,sizeof(tempString));
653 CopyCStrToPStr(tempString,theStr255,sizeof(theStr255));
658 ConcatPStrToCStr(theStr255,theCStr,maxCStrLength);
664 void ConcatLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits)
666 Str255 theStr255;
669 NumToString(theNum,theStr255);
672 if (numDigits > 0)
674 int charsToInsert;
677 charsToInsert = numDigits - PStrLength(theStr255);
679 if (charsToInsert > 0)
681 char tempString[256];
683 CopyCStrToCStr("",tempString,sizeof(tempString));
685 for (;charsToInsert > 0;charsToInsert--)
687 ConcatCStrToCStr("0",tempString,sizeof(tempString));
690 ConcatPStrToCStr(theStr255,tempString,sizeof(tempString));
692 CopyCStrToPStr(tempString,theStr255,sizeof(theStr255));
697 ConcatPStrToPStr(theStr255,thePStr,maxPStrLength);
702 void CopyCStrAndConcatLongIntToCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength)
704 CopyCStrToCStr(theSrcCStr,theDstCStr,maxDstStrLength);
706 ConcatLongIntToCStr(theNum,theDstCStr,maxDstStrLength);
711 void CopyLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits)
713 Str255 theStr255;
716 NumToString(theNum,theStr255);
719 if (numDigits > 0)
721 int charsToInsert;
724 charsToInsert = numDigits - PStrLength(theStr255);
726 if (charsToInsert > 0)
728 char tempString[256];
730 CopyCStrToCStr("",tempString,sizeof(tempString));
732 for (;charsToInsert > 0;charsToInsert--)
734 ConcatCStrToCStr("0",tempString,sizeof(tempString));
737 ConcatPStrToCStr(theStr255,tempString,sizeof(tempString));
739 CopyCStrToPStr(tempString,theStr255,sizeof(theStr255));
744 CopyPStrToCStr(theStr255,theCStr,maxCStrLength);
751 void CopyUnsignedLongIntToCStr(const unsigned long theNum,char *theCStr,const int maxCStrLength)
753 char tempString[256];
754 int srcCharIndex,dstCharIndex;
755 unsigned long tempNum,quotient,remainder;
758 if (theNum == 0)
760 CopyCStrToCStr("0",theCStr,maxCStrLength);
763 else
765 srcCharIndex = 0;
767 tempNum = theNum;
769 for (;;)
771 if (srcCharIndex >= sizeof(tempString) - 1 || tempNum == 0)
773 for (dstCharIndex = 0;;)
775 if (dstCharIndex >= maxCStrLength - 1 || srcCharIndex <= 0)
777 theCStr[dstCharIndex] = 0;
779 break;
782 theCStr[dstCharIndex++] = tempString[--srcCharIndex];
785 break;
789 quotient = tempNum / 10;
791 remainder = tempNum - (quotient * 10);
793 tempString[srcCharIndex] = '0' + remainder;
795 srcCharIndex++;
797 tempNum = quotient;
805 void CopyLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits)
807 char tempString[256];
810 CopyLongIntToCStr(theNum,tempString,sizeof(tempString),numDigits);
812 CopyCStrToPStr(tempString,thePStr,maxPStrLength);
817 OSErr CopyLongIntToNewHandle(const long inTheLongInt,Handle *theHandle)
819 OSErr errCode = noErr;
820 char tempString[32];
823 CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString));
825 errCode = CopyCStrToNewHandle(tempString,theHandle);
827 return(errCode);
831 OSErr CopyLongIntToExistingHandle(const long inTheLongInt,Handle theHandle)
833 OSErr errCode = noErr;
834 char tempString[32];
837 CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString));
839 errCode = CopyCStrToExistingHandle(tempString,theHandle);
841 return(errCode);
847 OSErr CopyCStrToExistingHandle(const char *theCString,Handle theHandle)
849 OSErr errCode = noErr;
850 long stringLength;
853 if (theCString == nil)
855 SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, theCString == nil"));
858 if (theHandle == nil)
860 SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, theHandle == nil"));
863 if (*theHandle == nil)
865 SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, *theHandle == nil"));
870 stringLength = CStrLength(theCString) + 1;
872 SetHandleSize(theHandle,stringLength);
874 if (GetHandleSize(theHandle) < stringLength)
876 SetErrorMessageAndLongIntAndBail("CopyCStrToExistingHandle: Can't set Handle size, MemError() = ",MemError());
880 ::BlockMove(theCString,*theHandle,stringLength);
883 EXITPOINT:
885 return(errCode);
892 OSErr CopyCStrToNewHandle(const char *theCString,Handle *theHandle)
894 OSErr errCode = noErr;
895 long stringLength;
898 if (theCString == nil)
900 SetErrorMessageAndBail(("CopyCStrToNewHandle: Bad parameter, theCString == nil"));
903 if (theHandle == nil)
905 SetErrorMessageAndBail(("CopyCStrToNewHandle: Bad parameter, theHandle == nil"));
910 stringLength = CStrLength(theCString) + 1;
912 *theHandle = NewHandle(stringLength);
914 if (*theHandle == nil)
916 SetErrorMessageAndLongIntAndBail("CopyCStrToNewHandle: Can't allocate Handle, MemError() = ",MemError());
920 ::BlockMove(theCString,**theHandle,stringLength);
923 EXITPOINT:
925 return(errCode);
930 OSErr CopyPStrToNewHandle(const unsigned char *thePString,Handle *theHandle)
932 OSErr errCode = noErr;
933 long stringLength;
936 if (thePString == nil)
938 SetErrorMessageAndBail(("CopyPStrToNewHandle: Bad parameter, thePString == nil"));
941 if (theHandle == nil)
943 SetErrorMessageAndBail(("CopyPStrToNewHandle: Bad parameter, theHandle == nil"));
948 stringLength = PStrLength(thePString) + 1;
950 *theHandle = NewHandle(stringLength);
952 if (*theHandle == nil)
954 SetErrorMessageAndLongIntAndBail("CopyPStrToNewHandle: Can't allocate Handle, MemError() = ",MemError());
958 if (stringLength > 1)
960 BlockMove(thePString + 1,**theHandle,stringLength - 1);
963 (**theHandle)[stringLength - 1] = 0;
966 EXITPOINT:
968 return(errCode);
972 OSErr AppendPStrToHandle(const unsigned char *thePString,Handle theHandle,long *currentLength)
974 OSErr errCode = noErr;
975 char tempString[256];
978 CopyPStrToCStr(thePString,tempString,sizeof(tempString));
980 errCode = AppendCStrToHandle(tempString,theHandle,currentLength);
983 EXITPOINT:
985 return(errCode);
990 OSErr AppendCStrToHandle(const char *theCString,Handle theHandle,long *currentLength,long *maxLength)
992 OSErr errCode = noErr;
993 long handleMaxLength,handleCurrentLength,stringLength,byteCount;
996 if (theCString == nil)
998 SetErrorMessageAndBail(("AppendCStrToHandle: Bad parameter, theCString == nil"));
1001 if (theHandle == nil)
1003 SetErrorMessageAndBail(("AppendCStrToHandle: Bad parameter, theHandle == nil"));
1007 if (maxLength != nil)
1009 handleMaxLength = *maxLength;
1012 else
1014 handleMaxLength = GetHandleSize(theHandle);
1018 if (currentLength != nil && *currentLength >= 0)
1020 handleCurrentLength = *currentLength;
1023 else
1025 handleCurrentLength = CStrLength(*theHandle);
1029 stringLength = CStrLength(theCString);
1031 byteCount = handleCurrentLength + stringLength + 1;
1033 if (byteCount > handleMaxLength)
1035 SetHandleSize(theHandle,handleCurrentLength + stringLength + 1);
1037 if (maxLength != nil)
1039 *maxLength = GetHandleSize(theHandle);
1041 handleMaxLength = *maxLength;
1044 else
1046 handleMaxLength = GetHandleSize(theHandle);
1049 if (byteCount > handleMaxLength)
1051 SetErrorMessageAndLongIntAndBail("AppendCStrToHandle: Can't increase Handle allocation, MemError() = ",MemError());
1056 BlockMove(theCString,*theHandle + handleCurrentLength,stringLength + 1);
1059 if (currentLength != nil)
1061 *currentLength += stringLength;
1065 errCode = noErr;
1068 EXITPOINT:
1070 return(errCode);
1075 OSErr AppendCharsToHandle(const char *theChars,const int numChars,Handle theHandle,long *currentLength,long *maxLength)
1077 OSErr errCode = noErr;
1078 long handleMaxLength,handleCurrentLength,byteCount;
1081 if (theChars == nil)
1083 SetErrorMessageAndBail(("AppendCharsToHandle: Bad parameter, theChars == nil"));
1086 if (theHandle == nil)
1088 SetErrorMessageAndBail(("AppendCharsToHandle: Bad parameter, theHandle == nil"));
1092 if (maxLength != nil)
1094 handleMaxLength = *maxLength;
1097 else
1099 handleMaxLength = GetHandleSize(theHandle);
1103 if (currentLength != nil && *currentLength >= 0)
1105 handleCurrentLength = *currentLength;
1108 else
1110 handleCurrentLength = CStrLength(*theHandle);
1114 byteCount = handleCurrentLength + numChars + 1;
1116 if (byteCount > handleMaxLength)
1118 SetHandleSize(theHandle,handleCurrentLength + numChars + 1);
1120 if (maxLength != nil)
1122 *maxLength = GetHandleSize(theHandle);
1124 handleMaxLength = *maxLength;
1127 else
1129 handleMaxLength = GetHandleSize(theHandle);
1132 if (byteCount > handleMaxLength)
1134 SetErrorMessageAndLongIntAndBail("AppendCharsToHandle: Can't increase Handle allocation, MemError() = ",MemError());
1139 BlockMove(theChars,*theHandle + handleCurrentLength,numChars);
1141 (*theHandle)[handleCurrentLength + numChars] = '\0';
1143 if (currentLength != nil)
1145 *currentLength += numChars;
1149 errCode = noErr;
1152 EXITPOINT:
1154 return(errCode);
1159 OSErr AppendLongIntToHandle(const long inTheLongInt,Handle theHandle,long *currentLength)
1161 OSErr errCode = noErr;
1162 char tempString[32];
1165 CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString));
1167 errCode = AppendCStrToHandle(tempString,theHandle,currentLength);
1169 return(errCode);
1175 long CStrLength(const char *theCString)
1177 long cStrLength = 0;
1180 if (theCString != nil)
1182 for (cStrLength = 0;theCString[cStrLength] != 0;cStrLength++)
1189 return(cStrLength);
1194 long PStrLength(const unsigned char *thePString)
1196 long pStrLength = 0;
1199 if (thePString != nil)
1201 pStrLength = thePString[0];
1205 return(pStrLength);
1212 void ZeroMem(void *theMemPtr,const unsigned long numBytes)
1214 unsigned char *theBytePtr;
1215 unsigned long *theLongPtr;
1216 unsigned long numSingleBytes;
1217 unsigned long theNumBytes;
1220 theNumBytes = numBytes;
1222 if (theMemPtr != nil && theNumBytes > 0)
1224 theBytePtr = (unsigned char *) theMemPtr;
1226 numSingleBytes = (unsigned long) theBytePtr & 0x0003;
1228 while (numSingleBytes > 0)
1230 *theBytePtr++ = 0;
1232 theNumBytes--;
1233 numSingleBytes--;
1237 theLongPtr = (unsigned long *) theBytePtr;
1239 while (theNumBytes >= 4)
1241 *theLongPtr++ = 0;
1243 theNumBytes -= 4;
1247 theBytePtr = (unsigned char *) theLongPtr;
1249 while (theNumBytes > 0)
1251 *theBytePtr++ = 0;
1253 theNumBytes--;
1261 char *FindCharInCStr(const char theChar,const char *theCString)
1263 char *theStringSearchPtr;
1266 theStringSearchPtr = (char *) theCString;
1268 if (theStringSearchPtr != nil)
1270 while (*theStringSearchPtr != '\0' && *theStringSearchPtr != theChar)
1272 theStringSearchPtr++;
1275 if (*theStringSearchPtr == '\0')
1277 theStringSearchPtr = nil;
1281 return(theStringSearchPtr);
1286 long FindCharOffsetInCStr(const char theChar,const char *theCString,const Boolean inIgnoreCase)
1288 long theOffset = -1;
1291 if (theCString != nil)
1293 theOffset = 0;
1296 if (inIgnoreCase)
1298 char searchChar = theChar;
1300 if (searchChar >= 'a' && searchChar <= 'z')
1302 searchChar = searchChar - 'a' + 'A';
1306 while (*theCString != 0)
1308 char currentChar = *theCString;
1310 if (currentChar >= 'a' && currentChar <= 'z')
1312 currentChar = currentChar - 'a' + 'A';
1315 if (currentChar == searchChar)
1317 break;
1320 theCString++;
1321 theOffset++;
1325 else
1327 while (*theCString != 0 && *theCString != theChar)
1329 theCString++;
1330 theOffset++;
1334 if (*theCString == 0)
1336 theOffset = -1;
1340 return(theOffset);
1344 long FindCStrOffsetInCStr(const char *theCSubstring,const char *theCString,const Boolean inIgnoreCase)
1346 long theOffset = -1;
1349 if (theCSubstring != nil && theCString != nil)
1351 for (theOffset = 0;;theOffset++)
1353 if (theCString[theOffset] == 0)
1355 theOffset = -1;
1357 goto EXITPOINT;
1361 for (const char *tempSubstringPtr = theCSubstring,*tempCStringPtr = theCString + theOffset;;tempSubstringPtr++,tempCStringPtr++)
1363 if (*tempSubstringPtr == 0)
1365 goto EXITPOINT;
1368 else if (*tempCStringPtr == 0)
1370 break;
1373 char searchChar = *tempSubstringPtr;
1374 char currentChar = *tempCStringPtr;
1376 if (inIgnoreCase && searchChar >= 'a' && searchChar <= 'z')
1378 searchChar = searchChar - 'a' + 'A';
1381 if (inIgnoreCase && currentChar >= 'a' && currentChar <= 'z')
1383 currentChar = currentChar - 'a' + 'A';
1386 if (currentChar != searchChar)
1388 break;
1393 theOffset = -1;
1397 EXITPOINT:
1399 return(theOffset);
1404 void InsertCStrIntoCStr(const char *theSrcCStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength)
1406 int currentLength;
1407 int insertLength;
1408 int numCharsToInsert;
1409 int numCharsToShift;
1412 if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0 && theInsertionOffset < maxDstStrLength - 1)
1414 currentLength = CStrLength(theDstCStr);
1416 insertLength = CStrLength(theSrcCStr);
1419 if (theInsertionOffset + insertLength < maxDstStrLength - 1)
1421 numCharsToInsert = insertLength;
1424 else
1426 numCharsToInsert = maxDstStrLength - 1 - theInsertionOffset;
1430 if (numCharsToInsert + currentLength < maxDstStrLength - 1)
1432 numCharsToShift = currentLength - theInsertionOffset;
1435 else
1437 numCharsToShift = maxDstStrLength - 1 - theInsertionOffset - numCharsToInsert;
1441 if (numCharsToShift > 0)
1443 BlockMove(theDstCStr + theInsertionOffset,theDstCStr + theInsertionOffset + numCharsToInsert,numCharsToShift);
1446 if (numCharsToInsert > 0)
1448 BlockMove(theSrcCStr,theDstCStr + theInsertionOffset,numCharsToInsert);
1451 theDstCStr[theInsertionOffset + numCharsToInsert + numCharsToShift] = 0;
1457 void InsertPStrIntoCStr(const unsigned char *theSrcPStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength)
1459 int currentLength;
1460 int insertLength;
1461 int numCharsToInsert;
1462 int numCharsToShift;
1465 if (theDstCStr != nil && theSrcPStr != nil && maxDstStrLength > 0 && theInsertionOffset < maxDstStrLength - 1)
1467 currentLength = CStrLength(theDstCStr);
1469 insertLength = PStrLength(theSrcPStr);
1472 if (theInsertionOffset + insertLength < maxDstStrLength - 1)
1474 numCharsToInsert = insertLength;
1477 else
1479 numCharsToInsert = maxDstStrLength - 1 - theInsertionOffset;
1483 if (numCharsToInsert + currentLength < maxDstStrLength - 1)
1485 numCharsToShift = currentLength - theInsertionOffset;
1488 else
1490 numCharsToShift = maxDstStrLength - 1 - theInsertionOffset - numCharsToInsert;
1494 if (numCharsToShift > 0)
1496 BlockMove(theDstCStr + theInsertionOffset,theDstCStr + theInsertionOffset + numCharsToInsert,numCharsToShift);
1499 if (numCharsToInsert > 0)
1501 BlockMove(theSrcPStr + 1,theDstCStr + theInsertionOffset,numCharsToInsert);
1504 theDstCStr[theInsertionOffset + numCharsToInsert + numCharsToShift] = 0;
1510 OSErr InsertCStrIntoHandle(const char *theCString,Handle theHandle,const long inInsertOffset)
1512 OSErr errCode;
1513 int currentLength;
1514 int insertLength;
1517 SetErrorMessageAndBailIfNil(theCString,"InsertCStrIntoHandle: Bad parameter, theCString == nil");
1519 SetErrorMessageAndBailIfNil(theHandle,"InsertCStrIntoHandle: Bad parameter, theHandle == nil");
1521 currentLength = CStrLength(*theHandle);
1523 if (currentLength + 1 > ::GetHandleSize(theHandle))
1525 SetErrorMessageAndBail("InsertCStrIntoHandle: Handle has been overflowed");
1528 if (inInsertOffset > currentLength)
1530 SetErrorMessageAndBail("InsertCStrIntoHandle: Insertion offset is greater than string length");
1533 insertLength = CStrLength(theCString);
1535 ::SetHandleSize(theHandle,currentLength + 1 + insertLength);
1537 if (::GetHandleSize(theHandle) < currentLength + 1 + insertLength)
1539 SetErrorMessageAndLongIntAndBail("InsertCStrIntoHandle: Can't expand storage for Handle, MemError() = ",MemError());
1542 ::BlockMove(*theHandle + inInsertOffset,*theHandle + inInsertOffset + insertLength,currentLength - inInsertOffset + 1);
1544 ::BlockMove(theCString,*theHandle + inInsertOffset,insertLength);
1547 errCode = noErr;
1550 EXITPOINT:
1552 return(errCode);
1558 void CopyCStrAndInsert1LongIntIntoCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength)
1560 CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr,nil,theNum,theDstCStr,maxDstStrLength);
1564 void CopyCStrAndInsert2LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,char *theDstCStr,const int maxDstStrLength)
1566 const long theLongInts[] = { long1,long2 };
1568 CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,nil,theLongInts,theDstCStr,maxDstStrLength);
1572 void CopyCStrAndInsert3LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,const long long3,char *theDstCStr,const int maxDstStrLength)
1574 const long theLongInts[] = { long1,long2,long3 };
1576 CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,nil,theLongInts,theDstCStr,maxDstStrLength);
1580 void CopyCStrAndInsertCStrIntoCStr(const char *theSrcCStr,const char *theInsertCStr,char *theDstCStr,const int maxDstStrLength)
1582 const char *theCStrs[2] = { theInsertCStr,nil };
1584 CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,theCStrs,nil,theDstCStr,maxDstStrLength);
1589 void CopyCStrAndInsertCStrLongIntIntoCStr(const char *theSrcCStr,const char *theInsertCStr,const long theNum,char *theDstCStr,const int maxDstStrLength)
1591 const char *theCStrs[2] = { theInsertCStr,nil };
1592 const long theLongInts[1] = { theNum };
1594 CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,theCStrs,theLongInts,theDstCStr,maxDstStrLength);
1599 void CopyCStrAndInsertCStrsLongIntsIntoCStr(const char *theSrcCStr,const char **theInsertCStrs,const long *theLongInts,char *theDstCStr,const int maxDstStrLength)
1601 int dstCharIndex,srcCharIndex,theMaxDstStrLength;
1602 int theCStrIndex = 0;
1603 int theLongIntIndex = 0;
1606 theMaxDstStrLength = maxDstStrLength;
1608 if (theDstCStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0)
1610 dstCharIndex = 0;
1612 srcCharIndex = 0;
1615 // Allow room for NULL at end of string
1617 theMaxDstStrLength--;
1620 for (;;)
1622 // Hit end of buffer?
1624 if (dstCharIndex >= theMaxDstStrLength)
1626 theDstCStr[dstCharIndex++] = 0;
1628 goto EXITPOINT;
1631 // End of source string?
1633 else if (theSrcCStr[srcCharIndex] == 0)
1635 theDstCStr[dstCharIndex++] = 0;
1637 goto EXITPOINT;
1640 // Did we find a '%s'?
1642 else if (theInsertCStrs != nil && theInsertCStrs[theCStrIndex] != nil && theSrcCStr[srcCharIndex] == '%' && theSrcCStr[srcCharIndex + 1] == 's')
1644 // Skip over the '%s'
1646 srcCharIndex += 2;
1649 // Terminate the dest string and then concat the string
1651 theDstCStr[dstCharIndex] = 0;
1653 ConcatCStrToCStr(theInsertCStrs[theCStrIndex],theDstCStr,theMaxDstStrLength);
1655 dstCharIndex = CStrLength(theDstCStr);
1657 theCStrIndex++;
1660 // Did we find a '%ld'?
1662 else if (theLongInts != nil && theSrcCStr[srcCharIndex] == '%' && theSrcCStr[srcCharIndex + 1] == 'l' && theSrcCStr[srcCharIndex + 2] == 'd')
1664 // Skip over the '%ld'
1666 srcCharIndex += 3;
1669 // Terminate the dest string and then concat the number
1671 theDstCStr[dstCharIndex] = 0;
1673 ConcatLongIntToCStr(theLongInts[theLongIntIndex],theDstCStr,theMaxDstStrLength);
1675 theLongIntIndex++;
1677 dstCharIndex = CStrLength(theDstCStr);
1680 else
1682 theDstCStr[dstCharIndex++] = theSrcCStr[srcCharIndex++];
1689 EXITPOINT:
1691 return;
1698 OSErr CopyCStrAndInsertCStrLongIntIntoHandle(const char *theSrcCStr,const char *theInsertCStr,const long theNum,Handle *theHandle)
1700 OSErr errCode;
1701 long byteCount;
1704 if (theHandle != nil)
1706 byteCount = CStrLength(theSrcCStr) + CStrLength(theInsertCStr) + 32;
1708 *theHandle = NewHandle(byteCount);
1710 if (*theHandle == nil)
1712 SetErrorMessageAndLongIntAndBail("CopyCStrAndInsertCStrLongIntIntoHandle: Can't allocate Handle, MemError() = ",MemError());
1716 HLock(*theHandle);
1718 CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr,theInsertCStr,theNum,**theHandle,byteCount);
1720 HUnlock(*theHandle);
1723 errCode = noErr;
1726 EXITPOINT:
1728 return(errCode);
1735 OSErr CopyIndexedWordToCStr(char *theSrcCStr,int whichWord,char *theDstCStr,int maxDstCStrLength)
1737 OSErr errCode;
1738 char *srcCharPtr,*dstCharPtr;
1739 int wordCount;
1740 int byteCount;
1743 if (theSrcCStr == nil)
1745 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theSrcCStr == nil"));
1748 if (theDstCStr == nil)
1750 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theDstCStr == nil"));
1753 if (whichWord < 0)
1755 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, whichWord < 0"));
1758 if (maxDstCStrLength <= 0)
1760 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, maxDstCStrLength <= 0"));
1764 *theDstCStr = '\0';
1766 srcCharPtr = theSrcCStr;
1768 while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
1770 srcCharPtr++;
1774 for (wordCount = 0;wordCount < whichWord;wordCount++)
1776 while (*srcCharPtr != ' ' && *srcCharPtr != '\t' && *srcCharPtr != '\r' && *srcCharPtr != '\n' && *srcCharPtr != '\0')
1778 srcCharPtr++;
1781 if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
1783 errCode = noErr;
1785 goto EXITPOINT;
1788 while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
1790 srcCharPtr++;
1793 if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
1795 errCode = noErr;
1797 goto EXITPOINT;
1802 dstCharPtr = theDstCStr;
1803 byteCount = 0;
1806 for(;;)
1808 if (byteCount >= maxDstCStrLength - 1 || *srcCharPtr == '\0' || *srcCharPtr == ' ' || *srcCharPtr == '\t' || *srcCharPtr == '\r' || *srcCharPtr == '\n')
1810 *dstCharPtr = '\0';
1811 break;
1814 *dstCharPtr++ = *srcCharPtr++;
1816 byteCount++;
1820 errCode = noErr;
1823 EXITPOINT:
1825 return(errCode);
1832 OSErr CopyIndexedWordToNewHandle(char *theSrcCStr,int whichWord,Handle *outTheHandle)
1834 OSErr errCode;
1835 char *srcCharPtr;
1836 int wordCount;
1837 int byteCount;
1840 if (theSrcCStr == nil)
1842 SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, theSrcCStr == nil"));
1845 if (outTheHandle == nil)
1847 SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, outTheHandle == nil"));
1850 if (whichWord < 0)
1852 SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, whichWord < 0"));
1856 *outTheHandle = nil;
1859 srcCharPtr = theSrcCStr;
1861 while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
1863 srcCharPtr++;
1867 for (wordCount = 0;wordCount < whichWord;wordCount++)
1869 while (*srcCharPtr != ' ' && *srcCharPtr != '\t' && *srcCharPtr != '\r' && *srcCharPtr != '\n' && *srcCharPtr != '\0')
1871 srcCharPtr++;
1874 if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
1876 break;
1879 while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
1881 srcCharPtr++;
1884 if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
1886 break;
1891 for (byteCount = 0;;byteCount++)
1893 if (srcCharPtr[byteCount] == ' ' || srcCharPtr[byteCount] == '\t' || srcCharPtr[byteCount] == '\r' || srcCharPtr[byteCount] == '\n' || srcCharPtr[byteCount] == '\0')
1895 break;
1900 *outTheHandle = NewHandle(byteCount + 1);
1902 if (*outTheHandle == nil)
1904 SetErrorMessageAndLongIntAndBail("CopyIndexedWordToNewHandle: Can't allocate Handle, MemError() = ",MemError());
1908 ::BlockMove(srcCharPtr,**outTheHandle,byteCount);
1910 (**outTheHandle)[byteCount] = '\0';
1912 errCode = noErr;
1915 EXITPOINT:
1917 return(errCode);
1922 OSErr CopyIndexedLineToCStr(const char *theSrcCStr,int inWhichLine,int *lineEndIndex,Boolean *gotLastLine,char *theDstCStr,const int maxDstCStrLength)
1924 OSErr errCode;
1925 int theCurrentLine;
1926 int theCurrentLineOffset;
1927 int theEOSOffset;
1930 if (theSrcCStr == nil)
1932 SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, theSrcCStr == nil"));
1935 if (theDstCStr == nil)
1937 SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, theDstCStr == nil"));
1940 if (inWhichLine < 0)
1942 SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, inWhichLine < 0"));
1945 if (maxDstCStrLength <= 0)
1947 SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, maxDstCStrLength <= 0"));
1951 if (gotLastLine != nil)
1953 *gotLastLine = false;
1957 *theDstCStr = 0;
1959 theCurrentLineOffset = 0;
1961 theCurrentLine = 0;
1964 while (theCurrentLine < inWhichLine)
1966 while (theSrcCStr[theCurrentLineOffset] != '\r' && theSrcCStr[theCurrentLineOffset] != 0)
1968 theCurrentLineOffset++;
1971 if (theSrcCStr[theCurrentLineOffset] == 0)
1973 break;
1976 theCurrentLineOffset++;
1977 theCurrentLine++;
1980 if (theSrcCStr[theCurrentLineOffset] == 0)
1982 SetErrorMessageAndLongIntAndBail("CopyIndexedLineToCStr: Too few lines in source text, can't get line ",inWhichLine);
1986 theEOSOffset = FindCharOffsetInCStr('\r',theSrcCStr + theCurrentLineOffset);
1988 if (theEOSOffset >= 0)
1990 CopyCSubstrToCStr(theSrcCStr + theCurrentLineOffset,theEOSOffset,theDstCStr,maxDstCStrLength);
1992 if (gotLastLine != nil)
1994 *gotLastLine = false;
1997 if (lineEndIndex != nil)
1999 *lineEndIndex = theEOSOffset;
2003 else
2005 theEOSOffset = CStrLength(theSrcCStr + theCurrentLineOffset);
2007 CopyCSubstrToCStr(theSrcCStr + theCurrentLineOffset,theEOSOffset,theDstCStr,maxDstCStrLength);
2009 if (gotLastLine != nil)
2011 *gotLastLine = true;
2014 if (lineEndIndex != nil)
2016 *lineEndIndex = theEOSOffset;
2021 errCode = noErr;
2024 EXITPOINT:
2026 return(errCode);
2031 OSErr CopyIndexedLineToNewHandle(const char *theSrcCStr,int inWhichLine,Handle *outNewHandle)
2033 OSErr errCode;
2034 int theCurrentLine;
2035 int theCurrentLineOffset;
2036 int byteCount;
2039 SetErrorMessageAndBailIfNil(theSrcCStr,"CopyIndexedLineToNewHandle: Bad parameter, theSrcCStr == nil");
2040 SetErrorMessageAndBailIfNil(outNewHandle,"CopyIndexedLineToNewHandle: Bad parameter, outNewHandle == nil");
2042 if (inWhichLine < 0)
2044 SetErrorMessageAndBail(("CopyIndexedLineToNewHandle: Bad parameter, inWhichLine < 0"));
2048 theCurrentLineOffset = 0;
2050 theCurrentLine = 0;
2053 while (theCurrentLine < inWhichLine)
2055 while (theSrcCStr[theCurrentLineOffset] != '\r' && theSrcCStr[theCurrentLineOffset] != '\0')
2057 theCurrentLineOffset++;
2060 if (theSrcCStr[theCurrentLineOffset] == '\0')
2062 break;
2065 theCurrentLineOffset++;
2066 theCurrentLine++;
2069 if (theSrcCStr[theCurrentLineOffset] == '\0')
2071 SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Too few lines in source text, can't get line #",inWhichLine);
2075 byteCount = 0;
2077 while (theSrcCStr[theCurrentLineOffset + byteCount] != '\r' && theSrcCStr[theCurrentLineOffset + byteCount] != '\0')
2079 byteCount++;
2083 *outNewHandle = NewHandle(byteCount + 1);
2085 if (*outNewHandle == nil)
2087 SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Can't allocate Handle, MemError() = ",MemError());
2090 ::BlockMove(theSrcCStr + theCurrentLineOffset,**outNewHandle,byteCount);
2092 (**outNewHandle)[byteCount] = '\0';
2094 errCode = noErr;
2097 EXITPOINT:
2099 return(errCode);
2105 OSErr CountDigits(const char *inCStr,int *outNumIntegerDigits,int *outNumFractDigits)
2107 OSErr errCode = noErr;
2108 int numIntDigits = 0;
2109 int numFractDigits = 0;
2110 int digitIndex = 0;
2113 SetErrorMessageAndBailIfNil(inCStr,"CountDigits: Bad parameter, theSrcCStr == nil");
2114 SetErrorMessageAndBailIfNil(outNumIntegerDigits,"CountDigits: Bad parameter, outNumIntegerDigits == nil");
2115 SetErrorMessageAndBailIfNil(outNumFractDigits,"CountDigits: Bad parameter, outNumFractDigits == nil");
2117 digitIndex = 0;
2119 while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9')
2121 digitIndex++;
2122 numIntDigits++;
2125 if (inCStr[digitIndex] == '.')
2127 digitIndex++;
2129 while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9')
2131 digitIndex++;
2132 numFractDigits++;
2136 *outNumIntegerDigits = numIntDigits;
2138 *outNumFractDigits = numFractDigits;
2140 errCode = noErr;
2142 EXITPOINT:
2144 return(errCode);
2149 OSErr ExtractIntFromCStr(const char *theSrcCStr,int *outInt,Boolean skipLeadingSpaces)
2151 OSErr errCode;
2152 int theCharIndex;
2155 if (theSrcCStr == nil)
2157 SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr == nil"));
2160 if (outInt == nil)
2162 SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, outInt == nil"));
2166 *outInt = 0;
2168 theCharIndex = 0;
2170 if (skipLeadingSpaces == true)
2172 while (theSrcCStr[theCharIndex] == ' ')
2174 theCharIndex++;
2178 if (theSrcCStr[theCharIndex] < '0' || theSrcCStr[theCharIndex] > '9')
2180 SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr contains a bogus numeric representation"));
2184 while (theSrcCStr[theCharIndex] >= '0' && theSrcCStr[theCharIndex] <= '9')
2186 *outInt = (*outInt * 10) + (theSrcCStr[theCharIndex] - '0');
2188 theCharIndex++;
2192 errCode = noErr;
2195 EXITPOINT:
2197 return(errCode);
2202 OSErr ExtractIntFromPStr(const unsigned char *theSrcPStr,int *outInt,Boolean skipLeadingSpaces)
2204 OSErr errCode;
2205 char theCStr[256];
2208 if (theSrcPStr == nil)
2210 SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, theSrcPStr == nil"));
2213 if (outInt == nil)
2215 SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, outInt == nil"));
2219 CopyPStrToCStr(theSrcPStr,theCStr,sizeof(theCStr));
2222 errCode = ExtractIntFromCStr(theCStr,outInt,skipLeadingSpaces);
2225 EXITPOINT:
2227 return(errCode);
2232 int CountOccurencesOfCharInCStr(const char inChar,const char *inSrcCStr)
2234 int theSrcCharIndex;
2235 int numOccurrences = -1;
2238 if (inSrcCStr != nil && inChar != '\0')
2240 numOccurrences = 0;
2242 for (theSrcCharIndex = 0;inSrcCStr[theSrcCharIndex] != '\0';theSrcCharIndex++)
2244 if (inSrcCStr[theSrcCharIndex] == inChar)
2246 numOccurrences++;
2251 return(numOccurrences);
2255 int CountWordsInCStr(const char *inSrcCStr)
2257 int numWords = -1;
2260 if (inSrcCStr != nil)
2262 numWords = 0;
2264 // Skip lead spaces
2266 while (*inSrcCStr == ' ')
2268 inSrcCStr++;
2271 while (*inSrcCStr != '\0')
2273 numWords++;
2275 while (*inSrcCStr != ' ' && *inSrcCStr != '\0')
2277 inSrcCStr++;
2280 while (*inSrcCStr == ' ')
2282 inSrcCStr++;
2287 return(numWords);
2293 void ConvertCStrToUpperCase(char *theSrcCStr)
2295 char *theCharPtr;
2298 if (theSrcCStr != nil)
2300 theCharPtr = theSrcCStr;
2302 while (*theCharPtr != 0)
2304 if (*theCharPtr >= 'a' && *theCharPtr <= 'z')
2306 *theCharPtr = *theCharPtr - 'a' + 'A';
2309 theCharPtr++;
2320 void ExtractCStrItemFromCStr(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,char *outDstCharPtr,const int inDstCharPtrMaxLength,const Boolean inTreatMultipleDelimsAsSingleDelim)
2322 int theItem;
2323 int theSrcCharIndex;
2324 int theDstCharIndex;
2327 if (foundItem != nil)
2329 *foundItem = false;
2333 if (outDstCharPtr != nil && inDstCharPtrMaxLength > 0 && inItemNumber >= 0 && inItemDelimiter != 0)
2335 *outDstCharPtr = 0;
2338 theSrcCharIndex = 0;
2340 for (theItem = 0;theItem < inItemNumber;theItem++)
2342 while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0')
2344 theSrcCharIndex++;
2347 if (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
2349 theSrcCharIndex++;
2351 if (inTreatMultipleDelimsAsSingleDelim)
2353 while (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
2355 theSrcCharIndex++;
2361 if (inSrcCStr[theSrcCharIndex] == '\0')
2363 goto EXITPOINT;
2368 if (foundItem != nil)
2370 *foundItem = true;
2374 theDstCharIndex = 0;
2376 for (;;)
2378 if (inSrcCStr[theSrcCharIndex] == 0 || inSrcCStr[theSrcCharIndex] == inItemDelimiter || theDstCharIndex >= inDstCharPtrMaxLength - 1)
2380 outDstCharPtr[theDstCharIndex] = 0;
2382 break;
2385 outDstCharPtr[theDstCharIndex++] = inSrcCStr[theSrcCharIndex++];
2390 EXITPOINT:
2392 return;
2397 OSErr ExtractCStrItemFromCStrIntoNewHandle(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,Handle *outNewHandle,const Boolean inTreatMultipleDelimsAsSingleDelim)
2399 OSErr errCode;
2400 int theItem;
2401 int theSrcCharIndex;
2402 int theItemLength;
2405 if (inSrcCStr == nil)
2407 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inSrcCStr == nil");
2408 errCode = kGenericError;
2409 goto EXITPOINT;
2412 if (outNewHandle == nil)
2414 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, outNewHandle == nil");
2415 errCode = kGenericError;
2416 goto EXITPOINT;
2419 if (foundItem == nil)
2421 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, foundItem == nil");
2422 errCode = kGenericError;
2423 goto EXITPOINT;
2426 if (inItemNumber < 0)
2428 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemNumber < 0");
2429 errCode = kGenericError;
2430 goto EXITPOINT;
2433 if (inItemDelimiter == 0)
2435 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemDelimiter == 0");
2436 errCode = kGenericError;
2437 goto EXITPOINT;
2441 *foundItem = false;
2443 theSrcCharIndex = 0;
2445 for (theItem = 0;theItem < inItemNumber;theItem++)
2447 while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0')
2449 theSrcCharIndex++;
2452 if (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
2454 theSrcCharIndex++;
2456 if (inTreatMultipleDelimsAsSingleDelim)
2458 while (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
2460 theSrcCharIndex++;
2466 if (inSrcCStr[theSrcCharIndex] == '\0')
2468 errCode = noErr;
2470 goto EXITPOINT;
2475 *foundItem = true;
2478 for (theItemLength = 0;;theItemLength++)
2480 if (inSrcCStr[theSrcCharIndex + theItemLength] == 0 || inSrcCStr[theSrcCharIndex + theItemLength] == inItemDelimiter)
2482 break;
2487 *outNewHandle = NewHandle(theItemLength + 1);
2489 if (*outNewHandle == nil)
2491 SetErrorMessageAndLongIntAndBail("ExtractCStrItemFromCStrIntoNewHandle: Can't allocate Handle, MemError() = ",MemError());
2495 BlockMove(inSrcCStr + theSrcCharIndex,**outNewHandle,theItemLength);
2497 (**outNewHandle)[theItemLength] = 0;
2499 errCode = noErr;
2502 EXITPOINT:
2504 return(errCode);
2512 OSErr ExtractFloatFromCStr(const char *inCString,extended80 *outFloat)
2514 OSErr errCode;
2515 Str255 theStr255;
2516 Handle theNumberPartsTableHandle = nil;
2517 long theNumberPartsOffset,theNumberPartsLength;
2518 FormatResultType theFormatResultType;
2519 NumberParts theNumberPartsTable;
2520 NumFormatStringRec theNumFormatStringRec;
2523 if (inCString == nil)
2525 SetErrorMessage("ExtractFloatFromCStr: Bad parameter, inCString == nil");
2526 errCode = kGenericError;
2527 goto EXITPOINT;
2530 if (outFloat == nil)
2532 SetErrorMessage("ExtractFloatFromCStr: Bad parameter, outFloat == nil");
2533 errCode = kGenericError;
2534 goto EXITPOINT;
2538 // GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
2540 GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
2542 if (theNumberPartsTableHandle == nil)
2544 SetErrorMessage("ExtractFloatFromCStr: Can't get number parts table for converting string representations to/from numeric representations");
2545 errCode = kGenericError;
2546 goto EXITPOINT;
2549 if (theNumberPartsLength > sizeof(theNumberPartsTable))
2551 SetErrorMessage("ExtractFloatFromCStr: Number parts table has bad length");
2552 errCode = kGenericError;
2553 goto EXITPOINT;
2557 BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength);
2560 theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec);
2562 if (theFormatResultType != fFormatOK)
2564 SetErrorMessage("ExtractFloatFromCStr: StringToFormatRec() != fFormatOK");
2565 errCode = kGenericError;
2566 goto EXITPOINT;
2570 CopyCStrToPStr(inCString,theStr255,sizeof(theStr255));
2573 theFormatResultType = (FormatResultType) StringToExtended(theStr255,&theNumFormatStringRec,&theNumberPartsTable,outFloat);
2575 if (theFormatResultType != fFormatOK && theFormatResultType != fBestGuess)
2577 SetErrorMessageAndLongIntAndBail("ExtractFloatFromCStr: StringToExtended() = ",theFormatResultType);
2581 errCode = noErr;
2584 EXITPOINT:
2586 return(errCode);
2591 OSErr CopyFloatToCStr(const extended80 *theFloat,char *theCStr,const int maxCStrLength,const int inMaxNumIntDigits,const int inMaxNumFractDigits)
2593 OSErr errCode;
2594 Str255 theStr255;
2595 Handle theNumberPartsTableHandle = nil;
2596 long theNumberPartsOffset,theNumberPartsLength;
2597 FormatResultType theFormatResultType;
2598 NumberParts theNumberPartsTable;
2599 NumFormatStringRec theNumFormatStringRec;
2602 if (theCStr == nil)
2604 SetErrorMessage("CopyFloatToCStr: Bad parameter, theCStr == nil");
2605 errCode = kGenericError;
2606 goto EXITPOINT;
2609 if (theFloat == nil)
2611 SetErrorMessage("CopyFloatToCStr: Bad parameter, theFloat == nil");
2612 errCode = kGenericError;
2613 goto EXITPOINT;
2617 // GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
2619 GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
2621 if (theNumberPartsTableHandle == nil)
2623 SetErrorMessage("CopyFloatToCStr: Can't get number parts table for converting string representations to/from numeric representations");
2624 errCode = kGenericError;
2625 goto EXITPOINT;
2628 if (theNumberPartsLength > sizeof(theNumberPartsTable))
2630 SetErrorMessage("CopyFloatToCStr: Number parts table has bad length");
2631 errCode = kGenericError;
2632 goto EXITPOINT;
2636 BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength);
2639 if (inMaxNumIntDigits >= 0 || inMaxNumFractDigits >= 0)
2641 char numberFormat[64];
2642 int numberFormatLength = 0;
2644 for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
2646 numberFormat[numberFormatLength++] = '0';
2649 if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1)
2651 numberFormat[numberFormatLength++] = '.';
2653 for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
2655 numberFormat[numberFormatLength++] = '0';
2660 if (numberFormatLength < sizeof(numberFormat) - 1)
2662 numberFormat[numberFormatLength++] = ';';
2665 if (numberFormatLength < sizeof(numberFormat) - 1)
2667 numberFormat[numberFormatLength++] = '-';
2671 for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
2673 numberFormat[numberFormatLength++] = '0';
2676 if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1)
2678 numberFormat[numberFormatLength++] = '.';
2680 for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
2682 numberFormat[numberFormatLength++] = '0';
2686 numberFormat[numberFormatLength] = '\0';
2689 Str255 tempStr255;
2691 CopyCStrToPStr(numberFormat,tempStr255,sizeof(tempStr255));
2693 theFormatResultType = (FormatResultType) StringToFormatRec(tempStr255,&theNumberPartsTable,&theNumFormatStringRec);
2696 else
2698 theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec);
2701 if (theFormatResultType != fFormatOK)
2703 SetErrorMessage("CopyFloatToCStr: StringToFormatRec() != fFormatOK");
2704 errCode = kGenericError;
2705 goto EXITPOINT;
2709 theFormatResultType = (FormatResultType) ExtendedToString(theFloat,&theNumFormatStringRec,&theNumberPartsTable,theStr255);
2711 if (theFormatResultType != fFormatOK)
2713 SetErrorMessage("CopyFloatToCStr: ExtendedToString() != fFormatOK");
2714 errCode = kGenericError;
2715 goto EXITPOINT;
2719 CopyPStrToCStr(theStr255,theCStr,maxCStrLength);
2721 errCode = noErr;
2724 EXITPOINT:
2726 return(errCode);
2733 void SkipWhiteSpace(char **ioSrcCharPtr,const Boolean inStopAtEOL)
2735 if (ioSrcCharPtr != nil && *ioSrcCharPtr != nil)
2737 if (inStopAtEOL)
2739 while ((**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t') && **ioSrcCharPtr != '\r' && **ioSrcCharPtr != '\n')
2741 *ioSrcCharPtr++;
2745 else
2747 while (**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t')
2749 *ioSrcCharPtr++;