4 * Authors: Lukas Zeller (luz@synthesis.ch)
5 * Beat Forster (bfo@synthesis.ch)
9 * Copyright (c) 2001-2009 by Synthesis AG (www.synthesis.ch)
13 #include "target_options.h"
16 #if defined __MACH__ && !defined __GNUC__ /* used for va_list support */
17 #include <mw_stdarg.h>
30 #include "stringutil.h"
37 // case insensitive strcmp, NULL allowed as empty string input
38 sInt16 strucmp( cAppCharP s1, cAppCharP s2, size_t len1, size_t len2 )
40 // allow NULL as empty strings
43 // s1>s2 : 1, s1==s2 : 0, s1<s2 : -1
45 // calc number of chars we must compare
46 size_t len = len1==0 ? len2 : (len2==0 ? len1 : (len1>len2 ? len2 : len1));
47 for (i=0; (!len || i<len) && *s1 && *s2; i++) {
48 // while both strings have chars and not len reached
49 if (toupper(*s1)!=toupper(*s2))
50 return toupper(*s1)>toupper(*s2) ? 1 : -1; // different
55 // equal up to end of shorter string or reached len
56 // - if both reached end or len -> equal
57 if ( ((len1 ? i==len1 : false) || *s1==0) && ((len2 ? i==len2 : false) || *s2==0) ) return 0;
58 // - not equal, longer string is larger
59 // (if not reached end of s1 or stopped before len1, s1 is longer
60 // but note than len1 can be longer than actual length of s1, so we
61 // must check for *s1 to make sure we have really not reached end of s1)
62 return (len1 ? i<len1 && *s1 : *s1) ? 1 : -1;
66 // returns number of C-string-escaped chars successfully converted to string
67 sInt16 CStrToStrAppend( cAppCharP aStr, string &aString, bool aStopAtQuoteOrCtrl, char ignore )
76 if (!c) break; // unfinished escape sequence is ignored
78 case 't' : c=0x09; break; // TAB
79 case 'n' : c=0x0A; break; // line feed
80 case 'r' : c=0x0D; break; // carriage return
83 // continued line, swallow line end
89 while (isxdigit(*p) && n<2) {
90 c=(c<<4) | (*p>'9' ? toupper(*p)-'A'+0x0A : *p-'0');
95 // c is the char to add
97 else if (aStopAtQuoteOrCtrl && (c=='"' || c<0x20)) {
98 // terminating char is NOT consumed
102 // otherwise, ignore any control characters
103 else if (c<0x20 && c!=ignore)
105 // add it to the result
108 // return number of converted chars
113 // returns number of string-escaped chars successfully converted to C-string
114 sInt16 StrToCStrAppend( cAppCharP aStr, string &aString, bool aAllow8Bit, char ignore )
119 // check for specials
120 if (c==ignore) aString+= c;
121 else if (c==0x09) aString+="\\t";
122 else if (c==0x0A) aString+="\\n";
123 else if (c==0x0D) aString+="\\r";
124 else if (c==0x22) aString+="\\\"";
125 else if (c=='\\') aString+="\\\\"; // escape the backslash as well
126 else if (c<0x20 || c==0x7F || (!aAllow8Bit && c>=0x80)) {
127 StringObjAppendPrintf(aString,"\\x%02hX",(uInt16)c);
134 // return number of converted chars
140 // old-style C-formatted output into string object
141 static void vStringObjPrintf( string &aStringObj, cAppCharP aFormat, bool aAppend, va_list aArgs )
144 const size_t bufsiz=128;
146 const size_t bufsiz=2048;
153 if (!aAppend) aStringObj.erase();
154 actualsize = vsnprintf(buf, bufsiz, aFormat, aArgs);
157 if (actualsize>=(int)bufsiz) {
158 // default buffer was too small, create bigger dynamic buffer
159 bufP = new char[actualsize+1];
160 actualsize = vsnprintf(bufP, actualsize+1, aFormat, aArgs);
170 // small default buffer was big enough, add it
171 if (actualsize<0) return; // abort, error
174 } // vStringObjPrintf
177 // old-style C-formatted output appending into string object
178 void StringObjAppendPrintf( string &aStringObj, cAppCharP aFormat, ... )
182 va_start(args, aFormat);
183 // now make the string
184 vStringObjPrintf(aStringObj,aFormat,true,args);
186 } // StringObjAppendPrintf
189 // returns number of successfully converted chars
190 sInt16 HexStrToULong( cAppCharP aStr, uInt32 &aLong, sInt16 aMaxDigits )
192 // our own implementation
197 while (aStr && (c=*aStr++) && (n<aMaxDigits)) {
198 if (!isxdigit(c)) break;
200 aLong+=(toupper(c)-0x30);
201 if (!isdigit(c)) aLong-=7;
209 // returns number of successfully converted chars
210 sInt16 HexStrToULongLong( cAppCharP aStr, uInt64 &aLongLong, sInt16 aMaxDigits )
212 // our own implementation
217 while (aStr && (c=*aStr++) && (n<aMaxDigits)) {
218 if (!isxdigit(c)) break;
220 aLongLong+=(toupper(c)-0x30);
221 if (!isdigit(c)) aLongLong-=7;
226 } // HexStrToULongLong
229 sInt16 HexStrToUIntPtr( cAppCharP aStr, uIntPtr &aIntPtr, sInt16 aMaxDigits )
232 return HexStrToULongLong( aStr, aIntPtr, aMaxDigits );
234 uInt32 v= (uInt32)aIntPtr;
235 sInt16 rslt= HexStrToULong( aStr, v, aMaxDigits );