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
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
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
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
)
74 if (thePStr
!= nil
&& theCStr
!= nil
&& maxCStrLength
> 0)
76 numPChars
= thePStr
[0];
80 if (i
>= numPChars
|| i
>= maxCStrLength
- 1)
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;
121 BlockMove(theSrcPStr
,theDstPStr
,theSrcPStr
[0] + 1);
127 void CopyCStrToCStr(const char *theSrcCStr
,char *theDstCStr
,const int maxDstStrLength
)
132 if (theDstCStr
!= nil
&& theSrcCStr
!= nil
&& maxDstStrLength
> 0)
136 if (theSrcCStr
[i
] == 0 || i
>= maxDstStrLength
- 1)
145 theDstCStr
[i
] = theSrcCStr
[i
];
153 void CopyCSubstrToCStr(const char *theSrcCStr
,const int maxCharsToCopy
,char *theDstCStr
,const int maxDstStrLength
)
158 if (theDstCStr
!= nil
&& theSrcCStr
!= nil
&& maxDstStrLength
> 0)
162 if (theSrcCStr
[i
] == 0 || i
>= maxDstStrLength
- 1 || i
>= maxCharsToCopy
)
171 theDstCStr
[i
] = theSrcCStr
[i
];
179 void CopyCSubstrToPStr(const char *theSrcCStr
,const int maxCharsToCopy
,unsigned char *theDstPStr
,const int maxDstStrLength
)
182 int theMaxDstStrLength
;
185 theMaxDstStrLength
= maxDstStrLength
;
187 if (theDstPStr
!= nil
&& theSrcCStr
!= nil
&& theMaxDstStrLength
> 0)
189 if (theMaxDstStrLength
> 255)
191 theMaxDstStrLength
= 255;
197 if (theSrcCStr
[i
] == 0 || i
>= theMaxDstStrLength
- 1 || i
>= maxCharsToCopy
)
206 theDstPStr
[i
+ 1] = theSrcCStr
[i
];
214 void CopyCStrToPStr(const char *theSrcCStr
,unsigned char *theDstPStr
,const int maxDstStrLength
)
217 int theMaxDstStrLength
;
220 theMaxDstStrLength
= maxDstStrLength
;
222 if (theDstPStr
!= nil
&& theSrcCStr
!= nil
&& theMaxDstStrLength
> 0)
224 if (theMaxDstStrLength
> 255)
226 theMaxDstStrLength
= 255;
232 if (i
>= theMaxDstStrLength
- 1 || theSrcCStr
[i
] == 0)
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];
266 if (i
>= numPChars
|| cStrLength
>= maxCStrLength
- 1)
268 theCStr
[cStrLength
++] = 0;
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;
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
)
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];
336 if (theSrcCStr
[i
] == 0 || thePStrLength
>= theMaxDstStrLength
- 1)
338 theDstPStr
[0] = thePStrLength
;
345 theDstPStr
[thePStrLength
+ 1] = theSrcCStr
[i
];
355 void ConcatCStrToCStr(const char *theSrcCStr
,char *theDstCStr
,const int maxCStrLength
)
360 if (theSrcCStr
!= nil
&& theDstCStr
!= nil
&& maxCStrLength
> 0)
362 for (cStrLength
= 0;theDstCStr
[cStrLength
] != 0;cStrLength
++)
370 if (*theSrcCStr
== 0 || cStrLength
>= maxCStrLength
- 1)
372 theDstCStr
[cStrLength
++] = 0;
379 theDstCStr
[cStrLength
++] = *theSrcCStr
++;
387 void ConcatCharToCStr(const char theChar
,char *theDstCStr
,const int maxCStrLength
)
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
)
411 if (theDstPStr
!= nil
&& maxPStrLength
> 0)
413 pStrLength
= PStrLength(theDstPStr
);
415 if (pStrLength
< maxPStrLength
- 1 && pStrLength
< 255)
417 theDstPStr
[pStrLength
+ 1] = theChar
;
426 int CompareCStrs(const char *theFirstCStr
,const char *theSecondCStr
,const Boolean ignoreCase
)
429 char firstChar
,secondChar
;
435 if (theFirstCStr
!= nil
&& theSecondCStr
!= nil
)
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)
463 else if (firstChar
!= 0 && secondChar
== 0)
470 else if (firstChar
== 0 && secondChar
== 0)
477 else if (firstChar
< secondChar
)
484 else if (firstChar
> secondChar
)
502 Boolean
CStrsAreEqual(const char *theFirstCStr
,const char *theSecondCStr
,const Boolean ignoreCase
)
504 if (CompareCStrs(theFirstCStr
,theSecondCStr
,ignoreCase
) == 0)
516 Boolean
PStrsAreEqual(const unsigned char *theFirstPStr
,const unsigned char *theSecondPStr
,const Boolean ignoreCase
)
518 if (ComparePStrs(theFirstPStr
,theSecondPStr
,ignoreCase
) == 0)
531 int ComparePStrs(const unsigned char *theFirstPStr
,const unsigned char *theSecondPStr
,const Boolean ignoreCase
)
534 char firstChar
,secondChar
;
540 if (theFirstPStr
!= nil
&& theSecondPStr
!= nil
)
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
)
568 else if (theFirstPStr
[0] >= i
&& theSecondPStr
[0] < i
)
575 else if (theFirstPStr
[0] < i
&& theSecondPStr
[0] < i
)
582 else if (firstChar
< secondChar
)
589 else if (firstChar
> secondChar
)
604 int CompareCStrToPStr(const char *theCStr
,const unsigned char *thePStr
,const Boolean ignoreCase
)
607 char tempString
[256];
612 if (theCStr
!= nil
&& thePStr
!= nil
)
614 CopyPStrToCStr(thePStr
,tempString
,sizeof(tempString
));
616 returnValue
= CompareCStrs(theCStr
,tempString
,ignoreCase
);
625 void ConcatLongIntToCStr(const long theNum
,char *theCStr
,const int maxCStrLength
,const int numDigits
)
630 NumToString(theNum
,theStr255
);
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
)
669 NumToString(theNum
,theStr255
);
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
)
716 NumToString(theNum
,theStr255
);
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
;
760 CopyCStrToCStr("0",theCStr
,maxCStrLength
);
771 if (srcCharIndex
>= sizeof(tempString
) - 1 || tempNum
== 0)
773 for (dstCharIndex
= 0;;)
775 if (dstCharIndex
>= maxCStrLength
- 1 || srcCharIndex
<= 0)
777 theCStr
[dstCharIndex
] = 0;
782 theCStr
[dstCharIndex
++] = tempString
[--srcCharIndex
];
789 quotient
= tempNum
/ 10;
791 remainder
= tempNum
- (quotient
* 10);
793 tempString
[srcCharIndex
] = '0' + remainder
;
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
;
823 CopyLongIntToCStr(inTheLongInt
,tempString
,sizeof(tempString
));
825 errCode
= CopyCStrToNewHandle(tempString
,theHandle
);
831 OSErr
CopyLongIntToExistingHandle(const long inTheLongInt
,Handle theHandle
)
833 OSErr errCode
= noErr
;
837 CopyLongIntToCStr(inTheLongInt
,tempString
,sizeof(tempString
));
839 errCode
= CopyCStrToExistingHandle(tempString
,theHandle
);
847 OSErr
CopyCStrToExistingHandle(const char *theCString
,Handle theHandle
)
849 OSErr errCode
= noErr
;
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
);
892 OSErr
CopyCStrToNewHandle(const char *theCString
,Handle
*theHandle
)
894 OSErr errCode
= noErr
;
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
);
930 OSErr
CopyPStrToNewHandle(const unsigned char *thePString
,Handle
*theHandle
)
932 OSErr errCode
= noErr
;
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;
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
);
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
;
1014 handleMaxLength
= GetHandleSize(theHandle
);
1018 if (currentLength
!= nil
&& *currentLength
>= 0)
1020 handleCurrentLength
= *currentLength
;
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
;
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
;
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
;
1099 handleMaxLength
= GetHandleSize(theHandle
);
1103 if (currentLength
!= nil
&& *currentLength
>= 0)
1105 handleCurrentLength
= *currentLength
;
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
;
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
;
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
);
1175 long CStrLength(const char *theCString
)
1177 long cStrLength
= 0;
1180 if (theCString
!= nil
)
1182 for (cStrLength
= 0;theCString
[cStrLength
] != 0;cStrLength
++)
1194 long PStrLength(const unsigned char *thePString
)
1196 long pStrLength
= 0;
1199 if (thePString
!= nil
)
1201 pStrLength
= thePString
[0];
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)
1237 theLongPtr
= (unsigned long *) theBytePtr
;
1239 while (theNumBytes
>= 4)
1247 theBytePtr
= (unsigned char *) theLongPtr
;
1249 while (theNumBytes
> 0)
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
)
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
)
1327 while (*theCString
!= 0 && *theCString
!= theChar
)
1334 if (*theCString
== 0)
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)
1361 for (const char *tempSubstringPtr
= theCSubstring
,*tempCStringPtr
= theCString
+ theOffset
;;tempSubstringPtr
++,tempCStringPtr
++)
1363 if (*tempSubstringPtr
== 0)
1368 else if (*tempCStringPtr
== 0)
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
)
1404 void InsertCStrIntoCStr(const char *theSrcCStr
,const int theInsertionOffset
,char *theDstCStr
,const int maxDstStrLength
)
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
;
1426 numCharsToInsert
= maxDstStrLength
- 1 - theInsertionOffset
;
1430 if (numCharsToInsert
+ currentLength
< maxDstStrLength
- 1)
1432 numCharsToShift
= currentLength
- theInsertionOffset
;
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
)
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
;
1479 numCharsToInsert
= maxDstStrLength
- 1 - theInsertionOffset
;
1483 if (numCharsToInsert
+ currentLength
< maxDstStrLength
- 1)
1485 numCharsToShift
= currentLength
- theInsertionOffset
;
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
)
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
);
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)
1615 // Allow room for NULL at end of string
1617 theMaxDstStrLength
--;
1622 // Hit end of buffer?
1624 if (dstCharIndex
>= theMaxDstStrLength
)
1626 theDstCStr
[dstCharIndex
++] = 0;
1631 // End of source string?
1633 else if (theSrcCStr
[srcCharIndex
] == 0)
1635 theDstCStr
[dstCharIndex
++] = 0;
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'
1649 // Terminate the dest string and then concat the string
1651 theDstCStr
[dstCharIndex
] = 0;
1653 ConcatCStrToCStr(theInsertCStrs
[theCStrIndex
],theDstCStr
,theMaxDstStrLength
);
1655 dstCharIndex
= CStrLength(theDstCStr
);
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'
1669 // Terminate the dest string and then concat the number
1671 theDstCStr
[dstCharIndex
] = 0;
1673 ConcatLongIntToCStr(theLongInts
[theLongIntIndex
],theDstCStr
,theMaxDstStrLength
);
1677 dstCharIndex
= CStrLength(theDstCStr
);
1682 theDstCStr
[dstCharIndex
++] = theSrcCStr
[srcCharIndex
++];
1698 OSErr
CopyCStrAndInsertCStrLongIntIntoHandle(const char *theSrcCStr
,const char *theInsertCStr
,const long theNum
,Handle
*theHandle
)
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());
1718 CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr
,theInsertCStr
,theNum
,**theHandle
,byteCount
);
1720 HUnlock(*theHandle
);
1735 OSErr
CopyIndexedWordToCStr(char *theSrcCStr
,int whichWord
,char *theDstCStr
,int maxDstCStrLength
)
1738 char *srcCharPtr
,*dstCharPtr
;
1743 if (theSrcCStr
== nil
)
1745 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theSrcCStr == nil"));
1748 if (theDstCStr
== nil
)
1750 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theDstCStr == nil"));
1755 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, whichWord < 0"));
1758 if (maxDstCStrLength
<= 0)
1760 SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, maxDstCStrLength <= 0"));
1766 srcCharPtr
= theSrcCStr
;
1768 while (*srcCharPtr
== ' ' || *srcCharPtr
== '\t')
1774 for (wordCount
= 0;wordCount
< whichWord
;wordCount
++)
1776 while (*srcCharPtr
!= ' ' && *srcCharPtr
!= '\t' && *srcCharPtr
!= '\r' && *srcCharPtr
!= '\n' && *srcCharPtr
!= '\0')
1781 if (*srcCharPtr
== '\r' || *srcCharPtr
== '\n' || *srcCharPtr
== '\0')
1788 while (*srcCharPtr
== ' ' || *srcCharPtr
== '\t')
1793 if (*srcCharPtr
== '\r' || *srcCharPtr
== '\n' || *srcCharPtr
== '\0')
1802 dstCharPtr
= theDstCStr
;
1808 if (byteCount
>= maxDstCStrLength
- 1 || *srcCharPtr
== '\0' || *srcCharPtr
== ' ' || *srcCharPtr
== '\t' || *srcCharPtr
== '\r' || *srcCharPtr
== '\n')
1814 *dstCharPtr
++ = *srcCharPtr
++;
1832 OSErr
CopyIndexedWordToNewHandle(char *theSrcCStr
,int whichWord
,Handle
*outTheHandle
)
1840 if (theSrcCStr
== nil
)
1842 SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, theSrcCStr == nil"));
1845 if (outTheHandle
== nil
)
1847 SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, outTheHandle == nil"));
1852 SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, whichWord < 0"));
1856 *outTheHandle
= nil
;
1859 srcCharPtr
= theSrcCStr
;
1861 while (*srcCharPtr
== ' ' || *srcCharPtr
== '\t')
1867 for (wordCount
= 0;wordCount
< whichWord
;wordCount
++)
1869 while (*srcCharPtr
!= ' ' && *srcCharPtr
!= '\t' && *srcCharPtr
!= '\r' && *srcCharPtr
!= '\n' && *srcCharPtr
!= '\0')
1874 if (*srcCharPtr
== '\r' || *srcCharPtr
== '\n' || *srcCharPtr
== '\0')
1879 while (*srcCharPtr
== ' ' || *srcCharPtr
== '\t')
1884 if (*srcCharPtr
== '\r' || *srcCharPtr
== '\n' || *srcCharPtr
== '\0')
1891 for (byteCount
= 0;;byteCount
++)
1893 if (srcCharPtr
[byteCount
] == ' ' || srcCharPtr
[byteCount
] == '\t' || srcCharPtr
[byteCount
] == '\r' || srcCharPtr
[byteCount
] == '\n' || srcCharPtr
[byteCount
] == '\0')
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';
1922 OSErr
CopyIndexedLineToCStr(const char *theSrcCStr
,int inWhichLine
,int *lineEndIndex
,Boolean
*gotLastLine
,char *theDstCStr
,const int maxDstCStrLength
)
1926 int theCurrentLineOffset
;
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;
1959 theCurrentLineOffset
= 0;
1964 while (theCurrentLine
< inWhichLine
)
1966 while (theSrcCStr
[theCurrentLineOffset
] != '\r' && theSrcCStr
[theCurrentLineOffset
] != 0)
1968 theCurrentLineOffset
++;
1971 if (theSrcCStr
[theCurrentLineOffset
] == 0)
1976 theCurrentLineOffset
++;
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
;
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
;
2031 OSErr
CopyIndexedLineToNewHandle(const char *theSrcCStr
,int inWhichLine
,Handle
*outNewHandle
)
2035 int theCurrentLineOffset
;
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;
2053 while (theCurrentLine
< inWhichLine
)
2055 while (theSrcCStr
[theCurrentLineOffset
] != '\r' && theSrcCStr
[theCurrentLineOffset
] != '\0')
2057 theCurrentLineOffset
++;
2060 if (theSrcCStr
[theCurrentLineOffset
] == '\0')
2065 theCurrentLineOffset
++;
2069 if (theSrcCStr
[theCurrentLineOffset
] == '\0')
2071 SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Too few lines in source text, can't get line #",inWhichLine
);
2077 while (theSrcCStr
[theCurrentLineOffset
+ byteCount
] != '\r' && theSrcCStr
[theCurrentLineOffset
+ byteCount
] != '\0')
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';
2105 OSErr
CountDigits(const char *inCStr
,int *outNumIntegerDigits
,int *outNumFractDigits
)
2107 OSErr errCode
= noErr
;
2108 int numIntDigits
= 0;
2109 int numFractDigits
= 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");
2119 while (inCStr
[digitIndex
] >= '0' && inCStr
[digitIndex
] <= '9')
2125 if (inCStr
[digitIndex
] == '.')
2129 while (inCStr
[digitIndex
] >= '0' && inCStr
[digitIndex
] <= '9')
2136 *outNumIntegerDigits
= numIntDigits
;
2138 *outNumFractDigits
= numFractDigits
;
2149 OSErr
ExtractIntFromCStr(const char *theSrcCStr
,int *outInt
,Boolean skipLeadingSpaces
)
2155 if (theSrcCStr
== nil
)
2157 SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr == nil"));
2162 SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, outInt == nil"));
2170 if (skipLeadingSpaces
== true)
2172 while (theSrcCStr
[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');
2202 OSErr
ExtractIntFromPStr(const unsigned char *theSrcPStr
,int *outInt
,Boolean skipLeadingSpaces
)
2208 if (theSrcPStr
== nil
)
2210 SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, theSrcPStr == nil"));
2215 SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, outInt == nil"));
2219 CopyPStrToCStr(theSrcPStr
,theCStr
,sizeof(theCStr
));
2222 errCode
= ExtractIntFromCStr(theCStr
,outInt
,skipLeadingSpaces
);
2232 int CountOccurencesOfCharInCStr(const char inChar
,const char *inSrcCStr
)
2234 int theSrcCharIndex
;
2235 int numOccurrences
= -1;
2238 if (inSrcCStr
!= nil
&& inChar
!= '\0')
2242 for (theSrcCharIndex
= 0;inSrcCStr
[theSrcCharIndex
] != '\0';theSrcCharIndex
++)
2244 if (inSrcCStr
[theSrcCharIndex
] == inChar
)
2251 return(numOccurrences
);
2255 int CountWordsInCStr(const char *inSrcCStr
)
2260 if (inSrcCStr
!= nil
)
2266 while (*inSrcCStr
== ' ')
2271 while (*inSrcCStr
!= '\0')
2275 while (*inSrcCStr
!= ' ' && *inSrcCStr
!= '\0')
2280 while (*inSrcCStr
== ' ')
2293 void ConvertCStrToUpperCase(char *theSrcCStr
)
2298 if (theSrcCStr
!= nil
)
2300 theCharPtr
= theSrcCStr
;
2302 while (*theCharPtr
!= 0)
2304 if (*theCharPtr
>= 'a' && *theCharPtr
<= 'z')
2306 *theCharPtr
= *theCharPtr
- 'a' + 'A';
2320 void ExtractCStrItemFromCStr(const char *inSrcCStr
,const char inItemDelimiter
,const int inItemNumber
,Boolean
*foundItem
,char *outDstCharPtr
,const int inDstCharPtrMaxLength
,const Boolean inTreatMultipleDelimsAsSingleDelim
)
2323 int theSrcCharIndex
;
2324 int theDstCharIndex
;
2327 if (foundItem
!= nil
)
2333 if (outDstCharPtr
!= nil
&& inDstCharPtrMaxLength
> 0 && inItemNumber
>= 0 && inItemDelimiter
!= 0)
2338 theSrcCharIndex
= 0;
2340 for (theItem
= 0;theItem
< inItemNumber
;theItem
++)
2342 while (inSrcCStr
[theSrcCharIndex
] != inItemDelimiter
&& inSrcCStr
[theSrcCharIndex
] != '\0')
2347 if (inSrcCStr
[theSrcCharIndex
] == inItemDelimiter
)
2351 if (inTreatMultipleDelimsAsSingleDelim
)
2353 while (inSrcCStr
[theSrcCharIndex
] == inItemDelimiter
)
2361 if (inSrcCStr
[theSrcCharIndex
] == '\0')
2368 if (foundItem
!= nil
)
2374 theDstCharIndex
= 0;
2378 if (inSrcCStr
[theSrcCharIndex
] == 0 || inSrcCStr
[theSrcCharIndex
] == inItemDelimiter
|| theDstCharIndex
>= inDstCharPtrMaxLength
- 1)
2380 outDstCharPtr
[theDstCharIndex
] = 0;
2385 outDstCharPtr
[theDstCharIndex
++] = inSrcCStr
[theSrcCharIndex
++];
2397 OSErr
ExtractCStrItemFromCStrIntoNewHandle(const char *inSrcCStr
,const char inItemDelimiter
,const int inItemNumber
,Boolean
*foundItem
,Handle
*outNewHandle
,const Boolean inTreatMultipleDelimsAsSingleDelim
)
2401 int theSrcCharIndex
;
2405 if (inSrcCStr
== nil
)
2407 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inSrcCStr == nil");
2408 errCode
= kGenericError
;
2412 if (outNewHandle
== nil
)
2414 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, outNewHandle == nil");
2415 errCode
= kGenericError
;
2419 if (foundItem
== nil
)
2421 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, foundItem == nil");
2422 errCode
= kGenericError
;
2426 if (inItemNumber
< 0)
2428 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemNumber < 0");
2429 errCode
= kGenericError
;
2433 if (inItemDelimiter
== 0)
2435 SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemDelimiter == 0");
2436 errCode
= kGenericError
;
2443 theSrcCharIndex
= 0;
2445 for (theItem
= 0;theItem
< inItemNumber
;theItem
++)
2447 while (inSrcCStr
[theSrcCharIndex
] != inItemDelimiter
&& inSrcCStr
[theSrcCharIndex
] != '\0')
2452 if (inSrcCStr
[theSrcCharIndex
] == inItemDelimiter
)
2456 if (inTreatMultipleDelimsAsSingleDelim
)
2458 while (inSrcCStr
[theSrcCharIndex
] == inItemDelimiter
)
2466 if (inSrcCStr
[theSrcCharIndex
] == '\0')
2478 for (theItemLength
= 0;;theItemLength
++)
2480 if (inSrcCStr
[theSrcCharIndex
+ theItemLength
] == 0 || inSrcCStr
[theSrcCharIndex
+ theItemLength
] == inItemDelimiter
)
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;
2512 OSErr
ExtractFloatFromCStr(const char *inCString
,extended80
*outFloat
)
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
;
2530 if (outFloat
== nil
)
2532 SetErrorMessage("ExtractFloatFromCStr: Bad parameter, outFloat == nil");
2533 errCode
= kGenericError
;
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
;
2549 if (theNumberPartsLength
> sizeof(theNumberPartsTable
))
2551 SetErrorMessage("ExtractFloatFromCStr: Number parts table has bad length");
2552 errCode
= kGenericError
;
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
;
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
);
2591 OSErr
CopyFloatToCStr(const extended80
*theFloat
,char *theCStr
,const int maxCStrLength
,const int inMaxNumIntDigits
,const int inMaxNumFractDigits
)
2595 Handle theNumberPartsTableHandle
= nil
;
2596 long theNumberPartsOffset
,theNumberPartsLength
;
2597 FormatResultType theFormatResultType
;
2598 NumberParts theNumberPartsTable
;
2599 NumFormatStringRec theNumFormatStringRec
;
2604 SetErrorMessage("CopyFloatToCStr: Bad parameter, theCStr == nil");
2605 errCode
= kGenericError
;
2609 if (theFloat
== nil
)
2611 SetErrorMessage("CopyFloatToCStr: Bad parameter, theFloat == nil");
2612 errCode
= kGenericError
;
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
;
2628 if (theNumberPartsLength
> sizeof(theNumberPartsTable
))
2630 SetErrorMessage("CopyFloatToCStr: Number parts table has bad length");
2631 errCode
= kGenericError
;
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';
2691 CopyCStrToPStr(numberFormat
,tempStr255
,sizeof(tempStr255
));
2693 theFormatResultType
= (FormatResultType
) StringToFormatRec(tempStr255
,&theNumberPartsTable
,&theNumFormatStringRec
);
2698 theFormatResultType
= (FormatResultType
) StringToFormatRec(kNumberFormatString
,&theNumberPartsTable
,&theNumFormatStringRec
);
2701 if (theFormatResultType
!= fFormatOK
)
2703 SetErrorMessage("CopyFloatToCStr: StringToFormatRec() != fFormatOK");
2704 errCode
= kGenericError
;
2709 theFormatResultType
= (FormatResultType
) ExtendedToString(theFloat
,&theNumFormatStringRec
,&theNumberPartsTable
,theStr255
);
2711 if (theFormatResultType
!= fFormatOK
)
2713 SetErrorMessage("CopyFloatToCStr: ExtendedToString() != fFormatOK");
2714 errCode
= kGenericError
;
2719 CopyPStrToCStr(theStr255
,theCStr
,maxCStrLength
);
2733 void SkipWhiteSpace(char **ioSrcCharPtr
,const Boolean inStopAtEOL
)
2735 if (ioSrcCharPtr
!= nil
&& *ioSrcCharPtr
!= nil
)
2739 while ((**ioSrcCharPtr
== ' ' || **ioSrcCharPtr
== '\t') && **ioSrcCharPtr
!= '\r' && **ioSrcCharPtr
!= '\n')
2747 while (**ioSrcCharPtr
== ' ' || **ioSrcCharPtr
== '\t')