CPStringUtils.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753
  1. /* ====================================================================
  2. * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  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.
  15. *
  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/)"
  20. *
  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.
  25. *
  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.
  29. *
  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/)"
  34. *
  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. * ====================================================================
  48. *
  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).
  52. *
  53. */
  54. #include "CPStringUtils.hpp"
  55. #include "ErrorHandling.hpp"
  56. #define kNumberFormatString "\p########0.00#######;-########0.00#######"
  57. // Useful utility functions which could be optimized a whole lot
  58. void CopyPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength)
  59. {
  60. int i,numPChars;
  61. if (thePStr != nil && theCStr != nil && maxCStrLength > 0)
  62. {
  63. numPChars = thePStr[0];
  64. for (i = 0;;i++)
  65. {
  66. if (i >= numPChars || i >= maxCStrLength - 1)
  67. {
  68. theCStr[i] = 0;
  69. break;
  70. }
  71. else
  72. {
  73. theCStr[i] = thePStr[i + 1];
  74. }
  75. }
  76. }
  77. }
  78. void CopyPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength)
  79. {
  80. int theMaxDstStrLength;
  81. theMaxDstStrLength = maxDstStrLength;
  82. if (theDstPStr != nil && theSrcPStr != nil && theMaxDstStrLength > 0)
  83. {
  84. if (theMaxDstStrLength > 255)
  85. {
  86. theMaxDstStrLength = 255;
  87. }
  88. if (theMaxDstStrLength - 1 < theSrcPStr[0])
  89. {
  90. BlockMove(theSrcPStr + 1,theDstPStr + 1,theMaxDstStrLength - 1);
  91. theDstPStr[0] = theMaxDstStrLength - 1;
  92. }
  93. else
  94. {
  95. BlockMove(theSrcPStr,theDstPStr,theSrcPStr[0] + 1);
  96. }
  97. }
  98. }
  99. void CopyCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxDstStrLength)
  100. {
  101. int i;
  102. if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0)
  103. {
  104. for (i = 0;;i++)
  105. {
  106. if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1)
  107. {
  108. theDstCStr[i] = 0;
  109. break;
  110. }
  111. else
  112. {
  113. theDstCStr[i] = theSrcCStr[i];
  114. }
  115. }
  116. }
  117. }
  118. void CopyCSubstrToCStr(const char *theSrcCStr,const int maxCharsToCopy,char *theDstCStr,const int maxDstStrLength)
  119. {
  120. int i;
  121. if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0)
  122. {
  123. for (i = 0;;i++)
  124. {
  125. if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1 || i >= maxCharsToCopy)
  126. {
  127. theDstCStr[i] = 0;
  128. break;
  129. }
  130. else
  131. {
  132. theDstCStr[i] = theSrcCStr[i];
  133. }
  134. }
  135. }
  136. }
  137. void CopyCSubstrToPStr(const char *theSrcCStr,const int maxCharsToCopy,unsigned char *theDstPStr,const int maxDstStrLength)
  138. {
  139. int i;
  140. int theMaxDstStrLength;
  141. theMaxDstStrLength = maxDstStrLength;
  142. if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0)
  143. {
  144. if (theMaxDstStrLength > 255)
  145. {
  146. theMaxDstStrLength = 255;
  147. }
  148. for (i = 0;;i++)
  149. {
  150. if (theSrcCStr[i] == 0 || i >= theMaxDstStrLength - 1 || i >= maxCharsToCopy)
  151. {
  152. theDstPStr[0] = i;
  153. break;
  154. }
  155. else
  156. {
  157. theDstPStr[i + 1] = theSrcCStr[i];
  158. }
  159. }
  160. }
  161. }
  162. void CopyCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength)
  163. {
  164. int i;
  165. int theMaxDstStrLength;
  166. theMaxDstStrLength = maxDstStrLength;
  167. if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0)
  168. {
  169. if (theMaxDstStrLength > 255)
  170. {
  171. theMaxDstStrLength = 255;
  172. }
  173. for (i = 0;;i++)
  174. {
  175. if (i >= theMaxDstStrLength - 1 || theSrcCStr[i] == 0)
  176. {
  177. theDstPStr[0] = i;
  178. break;
  179. }
  180. else
  181. {
  182. theDstPStr[i + 1] = theSrcCStr[i];
  183. }
  184. }
  185. }
  186. }
  187. void ConcatPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength)
  188. {
  189. int i,numPChars,cStrLength;
  190. if (thePStr != nil && theCStr != nil && maxCStrLength > 0)
  191. {
  192. for (cStrLength = 0;theCStr[cStrLength] != 0;cStrLength++)
  193. {
  194. }
  195. numPChars = thePStr[0];
  196. for (i = 0;;i++)
  197. {
  198. if (i >= numPChars || cStrLength >= maxCStrLength - 1)
  199. {
  200. theCStr[cStrLength++] = 0;
  201. break;
  202. }
  203. else
  204. {
  205. theCStr[cStrLength++] = thePStr[i + 1];
  206. }
  207. }
  208. }
  209. }
  210. void ConcatPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength)
  211. {
  212. int theMaxDstStrLength;
  213. theMaxDstStrLength = maxDstStrLength;
  214. if (theSrcPStr != nil && theDstPStr != nil && theMaxDstStrLength > 0)
  215. {
  216. if (theMaxDstStrLength > 255)
  217. {
  218. theMaxDstStrLength = 255;
  219. }
  220. if (theMaxDstStrLength - theDstPStr[0] - 1 < theSrcPStr[0])
  221. {
  222. BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theMaxDstStrLength - 1 - theDstPStr[0]);
  223. theDstPStr[0] = theMaxDstStrLength - 1;
  224. }
  225. else
  226. {
  227. BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theSrcPStr[0]);
  228. theDstPStr[0] += theSrcPStr[0];
  229. }
  230. }
  231. }
  232. void ConcatCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength)
  233. {
  234. int i,thePStrLength;
  235. int theMaxDstStrLength;
  236. theMaxDstStrLength = maxDstStrLength;
  237. if (theSrcCStr != nil && theDstPStr != nil && theMaxDstStrLength > 0)
  238. {
  239. if (theMaxDstStrLength > 255)
  240. {
  241. theMaxDstStrLength = 255;
  242. }
  243. thePStrLength = theDstPStr[0];
  244. for (i = 0;;i++)
  245. {
  246. if (theSrcCStr[i] == 0 || thePStrLength >= theMaxDstStrLength - 1)
  247. {
  248. theDstPStr[0] = thePStrLength;
  249. break;
  250. }
  251. else
  252. {
  253. theDstPStr[thePStrLength + 1] = theSrcCStr[i];
  254. thePStrLength++;
  255. }
  256. }
  257. }
  258. }
  259. void ConcatCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxCStrLength)
  260. {
  261. int cStrLength;
  262. if (theSrcCStr != nil && theDstCStr != nil && maxCStrLength > 0)
  263. {
  264. for (cStrLength = 0;theDstCStr[cStrLength] != 0;cStrLength++)
  265. {
  266. }
  267. for (;;)
  268. {
  269. if (*theSrcCStr == 0 || cStrLength >= maxCStrLength - 1)
  270. {
  271. theDstCStr[cStrLength++] = 0;
  272. break;
  273. }
  274. else
  275. {
  276. theDstCStr[cStrLength++] = *theSrcCStr++;
  277. }
  278. }
  279. }
  280. }
  281. void ConcatCharToCStr(const char theChar,char *theDstCStr,const int maxCStrLength)
  282. {
  283. int cStrLength;
  284. if (theDstCStr != nil && maxCStrLength > 0)
  285. {
  286. cStrLength = CStrLength(theDstCStr);
  287. if (cStrLength < maxCStrLength - 1)
  288. {
  289. theDstCStr[cStrLength++] = theChar;
  290. theDstCStr[cStrLength++] = '\0';
  291. }
  292. }
  293. }
  294. void ConcatCharToPStr(const char theChar,unsigned char *theDstPStr,const int maxPStrLength)
  295. {
  296. int pStrLength;
  297. if (theDstPStr != nil && maxPStrLength > 0)
  298. {
  299. pStrLength = PStrLength(theDstPStr);
  300. if (pStrLength < maxPStrLength - 1 && pStrLength < 255)
  301. {
  302. theDstPStr[pStrLength + 1] = theChar;
  303. theDstPStr[0] += 1;
  304. }
  305. }
  306. }
  307. int CompareCStrs(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase)
  308. {
  309. int returnValue;
  310. char firstChar,secondChar;
  311. returnValue = 0;
  312. if (theFirstCStr != nil && theSecondCStr != nil)
  313. {
  314. for (;;)
  315. {
  316. firstChar = *theFirstCStr;
  317. secondChar = *theSecondCStr;
  318. if (ignoreCase == true)
  319. {
  320. if (firstChar >= 'A' && firstChar <= 'Z')
  321. {
  322. firstChar = 'a' + (firstChar - 'A');
  323. }
  324. if (secondChar >= 'A' && secondChar <= 'Z')
  325. {
  326. secondChar = 'a' + (secondChar - 'A');
  327. }
  328. }
  329. if (firstChar == 0 && secondChar != 0)
  330. {
  331. returnValue = -1;
  332. break;
  333. }
  334. else if (firstChar != 0 && secondChar == 0)
  335. {
  336. returnValue = 1;
  337. break;
  338. }
  339. else if (firstChar == 0 && secondChar == 0)
  340. {
  341. returnValue = 0;
  342. break;
  343. }
  344. else if (firstChar < secondChar)
  345. {
  346. returnValue = -1;
  347. break;
  348. }
  349. else if (firstChar > secondChar)
  350. {
  351. returnValue = 1;
  352. break;
  353. }
  354. theFirstCStr++;
  355. theSecondCStr++;
  356. }
  357. }
  358. return(returnValue);
  359. }
  360. Boolean CStrsAreEqual(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase)
  361. {
  362. if (CompareCStrs(theFirstCStr,theSecondCStr,ignoreCase) == 0)
  363. {
  364. return true;
  365. }
  366. else
  367. {
  368. return false;
  369. }
  370. }
  371. Boolean PStrsAreEqual(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase)
  372. {
  373. if (ComparePStrs(theFirstPStr,theSecondPStr,ignoreCase) == 0)
  374. {
  375. return true;
  376. }
  377. else
  378. {
  379. return false;
  380. }
  381. }
  382. int ComparePStrs(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase)
  383. {
  384. int i,returnValue;
  385. char firstChar,secondChar;
  386. returnValue = 0;
  387. if (theFirstPStr != nil && theSecondPStr != nil)
  388. {
  389. for (i = 1;;i++)
  390. {
  391. firstChar = theFirstPStr[i];
  392. secondChar = theSecondPStr[i];
  393. if (ignoreCase == true)
  394. {
  395. if (firstChar >= 'A' && firstChar <= 'Z')
  396. {
  397. firstChar = 'a' + (firstChar - 'A');
  398. }
  399. if (secondChar >= 'A' && secondChar <= 'Z')
  400. {
  401. secondChar = 'a' + (secondChar - 'A');
  402. }
  403. }
  404. if (theFirstPStr[0] < i && theSecondPStr[0] >= i)
  405. {
  406. returnValue = -1;
  407. break;
  408. }
  409. else if (theFirstPStr[0] >= i && theSecondPStr[0] < i)
  410. {
  411. returnValue = 1;
  412. break;
  413. }
  414. else if (theFirstPStr[0] < i && theSecondPStr[0] < i)
  415. {
  416. returnValue = 0;
  417. break;
  418. }
  419. else if (firstChar < secondChar)
  420. {
  421. returnValue = -1;
  422. break;
  423. }
  424. else if (firstChar > secondChar)
  425. {
  426. returnValue = 1;
  427. break;
  428. }
  429. }
  430. }
  431. return(returnValue);
  432. }
  433. int CompareCStrToPStr(const char *theCStr,const unsigned char *thePStr,const Boolean ignoreCase)
  434. {
  435. int returnValue;
  436. char tempString[256];
  437. returnValue = 0;
  438. if (theCStr != nil && thePStr != nil)
  439. {
  440. CopyPStrToCStr(thePStr,tempString,sizeof(tempString));
  441. returnValue = CompareCStrs(theCStr,tempString,ignoreCase);
  442. }
  443. return(returnValue);
  444. }
  445. void ConcatLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits)
  446. {
  447. Str255 theStr255;
  448. NumToString(theNum,theStr255);
  449. if (numDigits > 0)
  450. {
  451. int charsToInsert;
  452. charsToInsert = numDigits - PStrLength(theStr255);
  453. if (charsToInsert > 0)
  454. {
  455. char tempString[256];
  456. CopyCStrToCStr("",tempString,sizeof(tempString));
  457. for (;charsToInsert > 0;charsToInsert--)
  458. {
  459. ConcatCStrToCStr("0",tempString,sizeof(tempString));
  460. }
  461. ConcatPStrToCStr(theStr255,tempString,sizeof(tempString));
  462. CopyCStrToPStr(tempString,theStr255,sizeof(theStr255));
  463. }
  464. }
  465. ConcatPStrToCStr(theStr255,theCStr,maxCStrLength);
  466. }
  467. void ConcatLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits)
  468. {
  469. Str255 theStr255;
  470. NumToString(theNum,theStr255);
  471. if (numDigits > 0)
  472. {
  473. int charsToInsert;
  474. charsToInsert = numDigits - PStrLength(theStr255);
  475. if (charsToInsert > 0)
  476. {
  477. char tempString[256];
  478. CopyCStrToCStr("",tempString,sizeof(tempString));
  479. for (;charsToInsert > 0;charsToInsert--)
  480. {
  481. ConcatCStrToCStr("0",tempString,sizeof(tempString));
  482. }
  483. ConcatPStrToCStr(theStr255,tempString,sizeof(tempString));
  484. CopyCStrToPStr(tempString,theStr255,sizeof(theStr255));
  485. }
  486. }
  487. ConcatPStrToPStr(theStr255,thePStr,maxPStrLength);
  488. }
  489. void CopyCStrAndConcatLongIntToCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength)
  490. {
  491. CopyCStrToCStr(theSrcCStr,theDstCStr,maxDstStrLength);
  492. ConcatLongIntToCStr(theNum,theDstCStr,maxDstStrLength);
  493. }
  494. void CopyLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits)
  495. {
  496. Str255 theStr255;
  497. NumToString(theNum,theStr255);
  498. if (numDigits > 0)
  499. {
  500. int charsToInsert;
  501. charsToInsert = numDigits - PStrLength(theStr255);
  502. if (charsToInsert > 0)
  503. {
  504. char tempString[256];
  505. CopyCStrToCStr("",tempString,sizeof(tempString));
  506. for (;charsToInsert > 0;charsToInsert--)
  507. {
  508. ConcatCStrToCStr("0",tempString,sizeof(tempString));
  509. }
  510. ConcatPStrToCStr(theStr255,tempString,sizeof(tempString));
  511. CopyCStrToPStr(tempString,theStr255,sizeof(theStr255));
  512. }
  513. }
  514. CopyPStrToCStr(theStr255,theCStr,maxCStrLength);
  515. }
  516. void CopyUnsignedLongIntToCStr(const unsigned long theNum,char *theCStr,const int maxCStrLength)
  517. {
  518. char tempString[256];
  519. int srcCharIndex,dstCharIndex;
  520. unsigned long tempNum,quotient,remainder;
  521. if (theNum == 0)
  522. {
  523. CopyCStrToCStr("0",theCStr,maxCStrLength);
  524. }
  525. else
  526. {
  527. srcCharIndex = 0;
  528. tempNum = theNum;
  529. for (;;)
  530. {
  531. if (srcCharIndex >= sizeof(tempString) - 1 || tempNum == 0)
  532. {
  533. for (dstCharIndex = 0;;)
  534. {
  535. if (dstCharIndex >= maxCStrLength - 1 || srcCharIndex <= 0)
  536. {
  537. theCStr[dstCharIndex] = 0;
  538. break;
  539. }
  540. theCStr[dstCharIndex++] = tempString[--srcCharIndex];
  541. }
  542. break;
  543. }
  544. quotient = tempNum / 10;
  545. remainder = tempNum - (quotient * 10);
  546. tempString[srcCharIndex] = '0' + remainder;
  547. srcCharIndex++;
  548. tempNum = quotient;
  549. }
  550. }
  551. }
  552. void CopyLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits)
  553. {
  554. char tempString[256];
  555. CopyLongIntToCStr(theNum,tempString,sizeof(tempString),numDigits);
  556. CopyCStrToPStr(tempString,thePStr,maxPStrLength);
  557. }
  558. OSErr CopyLongIntToNewHandle(const long inTheLongInt,Handle *theHandle)
  559. {
  560. OSErr errCode = noErr;
  561. char tempString[32];
  562. CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString));
  563. errCode = CopyCStrToNewHandle(tempString,theHandle);
  564. return(errCode);
  565. }
  566. OSErr CopyLongIntToExistingHandle(const long inTheLongInt,Handle theHandle)
  567. {
  568. OSErr errCode = noErr;
  569. char tempString[32];
  570. CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString));
  571. errCode = CopyCStrToExistingHandle(tempString,theHandle);
  572. return(errCode);
  573. }
  574. OSErr CopyCStrToExistingHandle(const char *theCString,Handle theHandle)
  575. {
  576. OSErr errCode = noErr;
  577. long stringLength;
  578. if (theCString == nil)
  579. {
  580. SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, theCString == nil"));
  581. }
  582. if (theHandle == nil)
  583. {
  584. SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, theHandle == nil"));
  585. }
  586. if (*theHandle == nil)
  587. {
  588. SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, *theHandle == nil"));
  589. }
  590. stringLength = CStrLength(theCString) + 1;
  591. SetHandleSize(theHandle,stringLength);
  592. if (GetHandleSize(theHandle) < stringLength)
  593. {
  594. SetErrorMessageAndLongIntAndBail("CopyCStrToExistingHandle: Can't set Handle size, MemError() = ",MemError());
  595. }
  596. ::BlockMove(theCString,*theHandle,stringLength);
  597. EXITPOINT:
  598. return(errCode);
  599. }
  600. OSErr CopyCStrToNewHandle(const char *theCString,Handle *theHandle)
  601. {
  602. OSErr errCode = noErr;
  603. long stringLength;
  604. if (theCString == nil)
  605. {
  606. SetErrorMessageAndBail(("CopyCStrToNewHandle: Bad parameter, theCString == nil"));
  607. }
  608. if (theHandle == nil)
  609. {
  610. SetErrorMessageAndBail(("CopyCStrToNewHandle: Bad parameter, theHandle == nil"));
  611. }
  612. stringLength = CStrLength(theCString) + 1;
  613. *theHandle = NewHandle(stringLength);
  614. if (*theHandle == nil)
  615. {
  616. SetErrorMessageAndLongIntAndBail("CopyCStrToNewHandle: Can't allocate Handle, MemError() = ",MemError());
  617. }
  618. ::BlockMove(theCString,**theHandle,stringLength);
  619. EXITPOINT:
  620. return(errCode);
  621. }
  622. OSErr CopyPStrToNewHandle(const unsigned char *thePString,Handle *theHandle)
  623. {
  624. OSErr errCode = noErr;
  625. long stringLength;
  626. if (thePString == nil)
  627. {
  628. SetErrorMessageAndBail(("CopyPStrToNewHandle: Bad parameter, thePString == nil"));
  629. }
  630. if (theHandle == nil)
  631. {
  632. SetErrorMessageAndBail(("CopyPStrToNewHandle: Bad parameter, theHandle == nil"));
  633. }
  634. stringLength = PStrLength(thePString) + 1;
  635. *theHandle = NewHandle(stringLength);
  636. if (*theHandle == nil)
  637. {
  638. SetErrorMessageAndLongIntAndBail("CopyPStrToNewHandle: Can't allocate Handle, MemError() = ",MemError());
  639. }
  640. if (stringLength > 1)
  641. {
  642. BlockMove(thePString + 1,**theHandle,stringLength - 1);
  643. }
  644. (**theHandle)[stringLength - 1] = 0;
  645. EXITPOINT:
  646. return(errCode);
  647. }
  648. OSErr AppendPStrToHandle(const unsigned char *thePString,Handle theHandle,long *currentLength)
  649. {
  650. OSErr errCode = noErr;
  651. char tempString[256];
  652. CopyPStrToCStr(thePString,tempString,sizeof(tempString));
  653. errCode = AppendCStrToHandle(tempString,theHandle,currentLength);
  654. EXITPOINT:
  655. return(errCode);
  656. }
  657. OSErr AppendCStrToHandle(const char *theCString,Handle theHandle,long *currentLength,long *maxLength)
  658. {
  659. OSErr errCode = noErr;
  660. long handleMaxLength,handleCurrentLength,stringLength,byteCount;
  661. if (theCString == nil)
  662. {
  663. SetErrorMessageAndBail(("AppendCStrToHandle: Bad parameter, theCString == nil"));
  664. }
  665. if (theHandle == nil)
  666. {
  667. SetErrorMessageAndBail(("AppendCStrToHandle: Bad parameter, theHandle == nil"));
  668. }
  669. if (maxLength != nil)
  670. {
  671. handleMaxLength = *maxLength;
  672. }
  673. else
  674. {
  675. handleMaxLength = GetHandleSize(theHandle);
  676. }
  677. if (currentLength != nil && *currentLength >= 0)
  678. {
  679. handleCurrentLength = *currentLength;
  680. }
  681. else
  682. {
  683. handleCurrentLength = CStrLength(*theHandle);
  684. }
  685. stringLength = CStrLength(theCString);
  686. byteCount = handleCurrentLength + stringLength + 1;
  687. if (byteCount > handleMaxLength)
  688. {
  689. SetHandleSize(theHandle,handleCurrentLength + stringLength + 1);
  690. if (maxLength != nil)
  691. {
  692. *maxLength = GetHandleSize(theHandle);
  693. handleMaxLength = *maxLength;
  694. }
  695. else
  696. {
  697. handleMaxLength = GetHandleSize(theHandle);
  698. }
  699. if (byteCount > handleMaxLength)
  700. {
  701. SetErrorMessageAndLongIntAndBail("AppendCStrToHandle: Can't increase Handle allocation, MemError() = ",MemError());
  702. }
  703. }
  704. BlockMove(theCString,*theHandle + handleCurrentLength,stringLength + 1);
  705. if (currentLength != nil)
  706. {
  707. *currentLength += stringLength;
  708. }
  709. errCode = noErr;
  710. EXITPOINT:
  711. return(errCode);
  712. }
  713. OSErr AppendCharsToHandle(const char *theChars,const int numChars,Handle theHandle,long *currentLength,long *maxLength)
  714. {
  715. OSErr errCode = noErr;
  716. long handleMaxLength,handleCurrentLength,byteCount;
  717. if (theChars == nil)
  718. {
  719. SetErrorMessageAndBail(("AppendCharsToHandle: Bad parameter, theChars == nil"));
  720. }
  721. if (theHandle == nil)
  722. {
  723. SetErrorMessageAndBail(("AppendCharsToHandle: Bad parameter, theHandle == nil"));
  724. }
  725. if (maxLength != nil)
  726. {
  727. handleMaxLength = *maxLength;
  728. }
  729. else
  730. {
  731. handleMaxLength = GetHandleSize(theHandle);
  732. }
  733. if (currentLength != nil && *currentLength >= 0)
  734. {
  735. handleCurrentLength = *currentLength;
  736. }
  737. else
  738. {
  739. handleCurrentLength = CStrLength(*theHandle);
  740. }
  741. byteCount = handleCurrentLength + numChars + 1;
  742. if (byteCount > handleMaxLength)
  743. {
  744. SetHandleSize(theHandle,handleCurrentLength + numChars + 1);
  745. if (maxLength != nil)
  746. {
  747. *maxLength = GetHandleSize(theHandle);
  748. handleMaxLength = *maxLength;
  749. }
  750. else
  751. {
  752. handleMaxLength = GetHandleSize(theHandle);
  753. }
  754. if (byteCount > handleMaxLength)
  755. {
  756. SetErrorMessageAndLongIntAndBail("AppendCharsToHandle: Can't increase Handle allocation, MemError() = ",MemError());
  757. }
  758. }
  759. BlockMove(theChars,*theHandle + handleCurrentLength,numChars);
  760. (*theHandle)[handleCurrentLength + numChars] = '\0';
  761. if (currentLength != nil)
  762. {
  763. *currentLength += numChars;
  764. }
  765. errCode = noErr;
  766. EXITPOINT:
  767. return(errCode);
  768. }
  769. OSErr AppendLongIntToHandle(const long inTheLongInt,Handle theHandle,long *currentLength)
  770. {
  771. OSErr errCode = noErr;
  772. char tempString[32];
  773. CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString));
  774. errCode = AppendCStrToHandle(tempString,theHandle,currentLength);
  775. return(errCode);
  776. }
  777. long CStrLength(const char *theCString)
  778. {
  779. long cStrLength = 0;
  780. if (theCString != nil)
  781. {
  782. for (cStrLength = 0;theCString[cStrLength] != 0;cStrLength++)
  783. {
  784. }
  785. }
  786. return(cStrLength);
  787. }
  788. long PStrLength(const unsigned char *thePString)
  789. {
  790. long pStrLength = 0;
  791. if (thePString != nil)
  792. {
  793. pStrLength = thePString[0];
  794. }
  795. return(pStrLength);
  796. }
  797. void ZeroMem(void *theMemPtr,const unsigned long numBytes)
  798. {
  799. unsigned char *theBytePtr;
  800. unsigned long *theLongPtr;
  801. unsigned long numSingleBytes;
  802. unsigned long theNumBytes;
  803. theNumBytes = numBytes;
  804. if (theMemPtr != nil && theNumBytes > 0)
  805. {
  806. theBytePtr = (unsigned char *) theMemPtr;
  807. numSingleBytes = (unsigned long) theBytePtr & 0x0003;
  808. while (numSingleBytes > 0)
  809. {
  810. *theBytePtr++ = 0;
  811. theNumBytes--;
  812. numSingleBytes--;
  813. }
  814. theLongPtr = (unsigned long *) theBytePtr;
  815. while (theNumBytes >= 4)
  816. {
  817. *theLongPtr++ = 0;
  818. theNumBytes -= 4;
  819. }
  820. theBytePtr = (unsigned char *) theLongPtr;
  821. while (theNumBytes > 0)
  822. {
  823. *theBytePtr++ = 0;
  824. theNumBytes--;
  825. }
  826. }
  827. }
  828. char *FindCharInCStr(const char theChar,const char *theCString)
  829. {
  830. char *theStringSearchPtr;
  831. theStringSearchPtr = (char *) theCString;
  832. if (theStringSearchPtr != nil)
  833. {
  834. while (*theStringSearchPtr != '\0' && *theStringSearchPtr != theChar)
  835. {
  836. theStringSearchPtr++;
  837. }
  838. if (*theStringSearchPtr == '\0')
  839. {
  840. theStringSearchPtr = nil;
  841. }
  842. }
  843. return(theStringSearchPtr);
  844. }
  845. long FindCharOffsetInCStr(const char theChar,const char *theCString,const Boolean inIgnoreCase)
  846. {
  847. long theOffset = -1;
  848. if (theCString != nil)
  849. {
  850. theOffset = 0;
  851. if (inIgnoreCase)
  852. {
  853. char searchChar = theChar;
  854. if (searchChar >= 'a' && searchChar <= 'z')
  855. {
  856. searchChar = searchChar - 'a' + 'A';
  857. }
  858. while (*theCString != 0)
  859. {
  860. char currentChar = *theCString;
  861. if (currentChar >= 'a' && currentChar <= 'z')
  862. {
  863. currentChar = currentChar - 'a' + 'A';
  864. }
  865. if (currentChar == searchChar)
  866. {
  867. break;
  868. }
  869. theCString++;
  870. theOffset++;
  871. }
  872. }
  873. else
  874. {
  875. while (*theCString != 0 && *theCString != theChar)
  876. {
  877. theCString++;
  878. theOffset++;
  879. }
  880. }
  881. if (*theCString == 0)
  882. {
  883. theOffset = -1;
  884. }
  885. }
  886. return(theOffset);
  887. }
  888. long FindCStrOffsetInCStr(const char *theCSubstring,const char *theCString,const Boolean inIgnoreCase)
  889. {
  890. long theOffset = -1;
  891. if (theCSubstring != nil && theCString != nil)
  892. {
  893. for (theOffset = 0;;theOffset++)
  894. {
  895. if (theCString[theOffset] == 0)
  896. {
  897. theOffset = -1;
  898. goto EXITPOINT;
  899. }
  900. for (const char *tempSubstringPtr = theCSubstring,*tempCStringPtr = theCString + theOffset;;tempSubstringPtr++,tempCStringPtr++)
  901. {
  902. if (*tempSubstringPtr == 0)
  903. {
  904. goto EXITPOINT;
  905. }
  906. else if (*tempCStringPtr == 0)
  907. {
  908. break;
  909. }
  910. char searchChar = *tempSubstringPtr;
  911. char currentChar = *tempCStringPtr;
  912. if (inIgnoreCase && searchChar >= 'a' && searchChar <= 'z')
  913. {
  914. searchChar = searchChar - 'a' + 'A';
  915. }
  916. if (inIgnoreCase && currentChar >= 'a' && currentChar <= 'z')
  917. {
  918. currentChar = currentChar - 'a' + 'A';
  919. }
  920. if (currentChar != searchChar)
  921. {
  922. break;
  923. }
  924. }
  925. }
  926. theOffset = -1;
  927. }
  928. EXITPOINT:
  929. return(theOffset);
  930. }
  931. void InsertCStrIntoCStr(const char *theSrcCStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength)
  932. {
  933. int currentLength;
  934. int insertLength;
  935. int numCharsToInsert;
  936. int numCharsToShift;
  937. if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0 && theInsertionOffset < maxDstStrLength - 1)
  938. {
  939. currentLength = CStrLength(theDstCStr);
  940. insertLength = CStrLength(theSrcCStr);
  941. if (theInsertionOffset + insertLength < maxDstStrLength - 1)
  942. {
  943. numCharsToInsert = insertLength;
  944. }
  945. else
  946. {
  947. numCharsToInsert = maxDstStrLength - 1 - theInsertionOffset;
  948. }
  949. if (numCharsToInsert + currentLength < maxDstStrLength - 1)
  950. {
  951. numCharsToShift = currentLength - theInsertionOffset;
  952. }
  953. else
  954. {
  955. numCharsToShift = maxDstStrLength - 1 - theInsertionOffset - numCharsToInsert;
  956. }
  957. if (numCharsToShift > 0)
  958. {
  959. BlockMove(theDstCStr + theInsertionOffset,theDstCStr + theInsertionOffset + numCharsToInsert,numCharsToShift);
  960. }
  961. if (numCharsToInsert > 0)
  962. {
  963. BlockMove(theSrcCStr,theDstCStr + theInsertionOffset,numCharsToInsert);
  964. }
  965. theDstCStr[theInsertionOffset + numCharsToInsert + numCharsToShift] = 0;
  966. }
  967. }
  968. void InsertPStrIntoCStr(const unsigned char *theSrcPStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength)
  969. {
  970. int currentLength;
  971. int insertLength;
  972. int numCharsToInsert;
  973. int numCharsToShift;
  974. if (theDstCStr != nil && theSrcPStr != nil && maxDstStrLength > 0 && theInsertionOffset < maxDstStrLength - 1)
  975. {
  976. currentLength = CStrLength(theDstCStr);
  977. insertLength = PStrLength(theSrcPStr);
  978. if (theInsertionOffset + insertLength < maxDstStrLength - 1)
  979. {
  980. numCharsToInsert = insertLength;
  981. }
  982. else
  983. {
  984. numCharsToInsert = maxDstStrLength - 1 - theInsertionOffset;
  985. }
  986. if (numCharsToInsert + currentLength < maxDstStrLength - 1)
  987. {
  988. numCharsToShift = currentLength - theInsertionOffset;
  989. }
  990. else
  991. {
  992. numCharsToShift = maxDstStrLength - 1 - theInsertionOffset - numCharsToInsert;
  993. }
  994. if (numCharsToShift > 0)
  995. {
  996. BlockMove(theDstCStr + theInsertionOffset,theDstCStr + theInsertionOffset + numCharsToInsert,numCharsToShift);
  997. }
  998. if (numCharsToInsert > 0)
  999. {
  1000. BlockMove(theSrcPStr + 1,theDstCStr + theInsertionOffset,numCharsToInsert);
  1001. }
  1002. theDstCStr[theInsertionOffset + numCharsToInsert + numCharsToShift] = 0;
  1003. }
  1004. }
  1005. OSErr InsertCStrIntoHandle(const char *theCString,Handle theHandle,const long inInsertOffset)
  1006. {
  1007. OSErr errCode;
  1008. int currentLength;
  1009. int insertLength;
  1010. SetErrorMessageAndBailIfNil(theCString,"InsertCStrIntoHandle: Bad parameter, theCString == nil");
  1011. SetErrorMessageAndBailIfNil(theHandle,"InsertCStrIntoHandle: Bad parameter, theHandle == nil");
  1012. currentLength = CStrLength(*theHandle);
  1013. if (currentLength + 1 > ::GetHandleSize(theHandle))
  1014. {
  1015. SetErrorMessageAndBail("InsertCStrIntoHandle: Handle has been overflowed");
  1016. }
  1017. if (inInsertOffset > currentLength)
  1018. {
  1019. SetErrorMessageAndBail("InsertCStrIntoHandle: Insertion offset is greater than string length");
  1020. }
  1021. insertLength = CStrLength(theCString);
  1022. ::SetHandleSize(theHandle,currentLength + 1 + insertLength);
  1023. if (::GetHandleSize(theHandle) < currentLength + 1 + insertLength)
  1024. {
  1025. SetErrorMessageAndLongIntAndBail("InsertCStrIntoHandle: Can't expand storage for Handle, MemError() = ",MemError());
  1026. }
  1027. ::BlockMove(*theHandle + inInsertOffset,*theHandle + inInsertOffset + insertLength,currentLength - inInsertOffset + 1);
  1028. ::BlockMove(theCString,*theHandle + inInsertOffset,insertLength);
  1029. errCode = noErr;
  1030. EXITPOINT:
  1031. return(errCode);
  1032. }
  1033. void CopyCStrAndInsert1LongIntIntoCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength)
  1034. {
  1035. CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr,nil,theNum,theDstCStr,maxDstStrLength);
  1036. }
  1037. void CopyCStrAndInsert2LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,char *theDstCStr,const int maxDstStrLength)
  1038. {
  1039. const long theLongInts[] = { long1,long2 };
  1040. CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,nil,theLongInts,theDstCStr,maxDstStrLength);
  1041. }
  1042. void CopyCStrAndInsert3LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,const long long3,char *theDstCStr,const int maxDstStrLength)
  1043. {
  1044. const long theLongInts[] = { long1,long2,long3 };
  1045. CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,nil,theLongInts,theDstCStr,maxDstStrLength);
  1046. }
  1047. void CopyCStrAndInsertCStrIntoCStr(const char *theSrcCStr,const char *theInsertCStr,char *theDstCStr,const int maxDstStrLength)
  1048. {
  1049. const char *theCStrs[2] = { theInsertCStr,nil };
  1050. CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,theCStrs,nil,theDstCStr,maxDstStrLength);
  1051. }
  1052. void CopyCStrAndInsertCStrLongIntIntoCStr(const char *theSrcCStr,const char *theInsertCStr,const long theNum,char *theDstCStr,const int maxDstStrLength)
  1053. {
  1054. const char *theCStrs[2] = { theInsertCStr,nil };
  1055. const long theLongInts[1] = { theNum };
  1056. CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,theCStrs,theLongInts,theDstCStr,maxDstStrLength);
  1057. }
  1058. void CopyCStrAndInsertCStrsLongIntsIntoCStr(const char *theSrcCStr,const char **theInsertCStrs,const long *theLongInts,char *theDstCStr,const int maxDstStrLength)
  1059. {
  1060. int dstCharIndex,srcCharIndex,theMaxDstStrLength;
  1061. int theCStrIndex = 0;
  1062. int theLongIntIndex = 0;
  1063. theMaxDstStrLength = maxDstStrLength;
  1064. if (theDstCStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0)
  1065. {
  1066. dstCharIndex = 0;
  1067. srcCharIndex = 0;
  1068. // Allow room for NULL at end of string
  1069. theMaxDstStrLength--;
  1070. for (;;)
  1071. {
  1072. // Hit end of buffer?
  1073. if (dstCharIndex >= theMaxDstStrLength)
  1074. {
  1075. theDstCStr[dstCharIndex++] = 0;
  1076. goto EXITPOINT;
  1077. }
  1078. // End of source string?
  1079. else if (theSrcCStr[srcCharIndex] == 0)
  1080. {
  1081. theDstCStr[dstCharIndex++] = 0;
  1082. goto EXITPOINT;
  1083. }
  1084. // Did we find a '%s'?
  1085. else if (theInsertCStrs != nil && theInsertCStrs[theCStrIndex] != nil && theSrcCStr[srcCharIndex] == '%' && theSrcCStr[srcCharIndex + 1] == 's')
  1086. {
  1087. // Skip over the '%s'
  1088. srcCharIndex += 2;
  1089. // Terminate the dest string and then concat the string
  1090. theDstCStr[dstCharIndex] = 0;
  1091. ConcatCStrToCStr(theInsertCStrs[theCStrIndex],theDstCStr,theMaxDstStrLength);
  1092. dstCharIndex = CStrLength(theDstCStr);
  1093. theCStrIndex++;
  1094. }
  1095. // Did we find a '%ld'?
  1096. else if (theLongInts != nil && theSrcCStr[srcCharIndex] == '%' && theSrcCStr[srcCharIndex + 1] == 'l' && theSrcCStr[srcCharIndex + 2] == 'd')
  1097. {
  1098. // Skip over the '%ld'
  1099. srcCharIndex += 3;
  1100. // Terminate the dest string and then concat the number
  1101. theDstCStr[dstCharIndex] = 0;
  1102. ConcatLongIntToCStr(theLongInts[theLongIntIndex],theDstCStr,theMaxDstStrLength);
  1103. theLongIntIndex++;
  1104. dstCharIndex = CStrLength(theDstCStr);
  1105. }
  1106. else
  1107. {
  1108. theDstCStr[dstCharIndex++] = theSrcCStr[srcCharIndex++];
  1109. }
  1110. }
  1111. }
  1112. EXITPOINT:
  1113. return;
  1114. }
  1115. OSErr CopyCStrAndInsertCStrLongIntIntoHandle(const char *theSrcCStr,const char *theInsertCStr,const long theNum,Handle *theHandle)
  1116. {
  1117. OSErr errCode;
  1118. long byteCount;
  1119. if (theHandle != nil)
  1120. {
  1121. byteCount = CStrLength(theSrcCStr) + CStrLength(theInsertCStr) + 32;
  1122. *theHandle = NewHandle(byteCount);
  1123. if (*theHandle == nil)
  1124. {
  1125. SetErrorMessageAndLongIntAndBail("CopyCStrAndInsertCStrLongIntIntoHandle: Can't allocate Handle, MemError() = ",MemError());
  1126. }
  1127. HLock(*theHandle);
  1128. CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr,theInsertCStr,theNum,**theHandle,byteCount);
  1129. HUnlock(*theHandle);
  1130. }
  1131. errCode = noErr;
  1132. EXITPOINT:
  1133. return(errCode);
  1134. }
  1135. OSErr CopyIndexedWordToCStr(char *theSrcCStr,int whichWord,char *theDstCStr,int maxDstCStrLength)
  1136. {
  1137. OSErr errCode;
  1138. char *srcCharPtr,*dstCharPtr;
  1139. int wordCount;
  1140. int byteCount;
  1141. if (theSrcCStr == nil)
  1142. {
  1143. SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theSrcCStr == nil"));
  1144. }
  1145. if (theDstCStr == nil)
  1146. {
  1147. SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theDstCStr == nil"));
  1148. }
  1149. if (whichWord < 0)
  1150. {
  1151. SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, whichWord < 0"));
  1152. }
  1153. if (maxDstCStrLength <= 0)
  1154. {
  1155. SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, maxDstCStrLength <= 0"));
  1156. }
  1157. *theDstCStr = '\0';
  1158. srcCharPtr = theSrcCStr;
  1159. while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
  1160. {
  1161. srcCharPtr++;
  1162. }
  1163. for (wordCount = 0;wordCount < whichWord;wordCount++)
  1164. {
  1165. while (*srcCharPtr != ' ' && *srcCharPtr != '\t' && *srcCharPtr != '\r' && *srcCharPtr != '\n' && *srcCharPtr != '\0')
  1166. {
  1167. srcCharPtr++;
  1168. }
  1169. if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
  1170. {
  1171. errCode = noErr;
  1172. goto EXITPOINT;
  1173. }
  1174. while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
  1175. {
  1176. srcCharPtr++;
  1177. }
  1178. if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
  1179. {
  1180. errCode = noErr;
  1181. goto EXITPOINT;
  1182. }
  1183. }
  1184. dstCharPtr = theDstCStr;
  1185. byteCount = 0;
  1186. for(;;)
  1187. {
  1188. if (byteCount >= maxDstCStrLength - 1 || *srcCharPtr == '\0' || *srcCharPtr == ' ' || *srcCharPtr == '\t' || *srcCharPtr == '\r' || *srcCharPtr == '\n')
  1189. {
  1190. *dstCharPtr = '\0';
  1191. break;
  1192. }
  1193. *dstCharPtr++ = *srcCharPtr++;
  1194. byteCount++;
  1195. }
  1196. errCode = noErr;
  1197. EXITPOINT:
  1198. return(errCode);
  1199. }
  1200. OSErr CopyIndexedWordToNewHandle(char *theSrcCStr,int whichWord,Handle *outTheHandle)
  1201. {
  1202. OSErr errCode;
  1203. char *srcCharPtr;
  1204. int wordCount;
  1205. int byteCount;
  1206. if (theSrcCStr == nil)
  1207. {
  1208. SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, theSrcCStr == nil"));
  1209. }
  1210. if (outTheHandle == nil)
  1211. {
  1212. SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, outTheHandle == nil"));
  1213. }
  1214. if (whichWord < 0)
  1215. {
  1216. SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, whichWord < 0"));
  1217. }
  1218. *outTheHandle = nil;
  1219. srcCharPtr = theSrcCStr;
  1220. while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
  1221. {
  1222. srcCharPtr++;
  1223. }
  1224. for (wordCount = 0;wordCount < whichWord;wordCount++)
  1225. {
  1226. while (*srcCharPtr != ' ' && *srcCharPtr != '\t' && *srcCharPtr != '\r' && *srcCharPtr != '\n' && *srcCharPtr != '\0')
  1227. {
  1228. srcCharPtr++;
  1229. }
  1230. if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
  1231. {
  1232. break;
  1233. }
  1234. while (*srcCharPtr == ' ' || *srcCharPtr == '\t')
  1235. {
  1236. srcCharPtr++;
  1237. }
  1238. if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0')
  1239. {
  1240. break;
  1241. }
  1242. }
  1243. for (byteCount = 0;;byteCount++)
  1244. {
  1245. if (srcCharPtr[byteCount] == ' ' || srcCharPtr[byteCount] == '\t' || srcCharPtr[byteCount] == '\r' || srcCharPtr[byteCount] == '\n' || srcCharPtr[byteCount] == '\0')
  1246. {
  1247. break;
  1248. }
  1249. }
  1250. *outTheHandle = NewHandle(byteCount + 1);
  1251. if (*outTheHandle == nil)
  1252. {
  1253. SetErrorMessageAndLongIntAndBail("CopyIndexedWordToNewHandle: Can't allocate Handle, MemError() = ",MemError());
  1254. }
  1255. ::BlockMove(srcCharPtr,**outTheHandle,byteCount);
  1256. (**outTheHandle)[byteCount] = '\0';
  1257. errCode = noErr;
  1258. EXITPOINT:
  1259. return(errCode);
  1260. }
  1261. OSErr CopyIndexedLineToCStr(const char *theSrcCStr,int inWhichLine,int *lineEndIndex,Boolean *gotLastLine,char *theDstCStr,const int maxDstCStrLength)
  1262. {
  1263. OSErr errCode;
  1264. int theCurrentLine;
  1265. int theCurrentLineOffset;
  1266. int theEOSOffset;
  1267. if (theSrcCStr == nil)
  1268. {
  1269. SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, theSrcCStr == nil"));
  1270. }
  1271. if (theDstCStr == nil)
  1272. {
  1273. SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, theDstCStr == nil"));
  1274. }
  1275. if (inWhichLine < 0)
  1276. {
  1277. SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, inWhichLine < 0"));
  1278. }
  1279. if (maxDstCStrLength <= 0)
  1280. {
  1281. SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, maxDstCStrLength <= 0"));
  1282. }
  1283. if (gotLastLine != nil)
  1284. {
  1285. *gotLastLine = false;
  1286. }
  1287. *theDstCStr = 0;
  1288. theCurrentLineOffset = 0;
  1289. theCurrentLine = 0;
  1290. while (theCurrentLine < inWhichLine)
  1291. {
  1292. while (theSrcCStr[theCurrentLineOffset] != '\r' && theSrcCStr[theCurrentLineOffset] != 0)
  1293. {
  1294. theCurrentLineOffset++;
  1295. }
  1296. if (theSrcCStr[theCurrentLineOffset] == 0)
  1297. {
  1298. break;
  1299. }
  1300. theCurrentLineOffset++;
  1301. theCurrentLine++;
  1302. }
  1303. if (theSrcCStr[theCurrentLineOffset] == 0)
  1304. {
  1305. SetErrorMessageAndLongIntAndBail("CopyIndexedLineToCStr: Too few lines in source text, can't get line ",inWhichLine);
  1306. }
  1307. theEOSOffset = FindCharOffsetInCStr('\r',theSrcCStr + theCurrentLineOffset);
  1308. if (theEOSOffset >= 0)
  1309. {
  1310. CopyCSubstrToCStr(theSrcCStr + theCurrentLineOffset,theEOSOffset,theDstCStr,maxDstCStrLength);
  1311. if (gotLastLine != nil)
  1312. {
  1313. *gotLastLine = false;
  1314. }
  1315. if (lineEndIndex != nil)
  1316. {
  1317. *lineEndIndex = theEOSOffset;
  1318. }
  1319. }
  1320. else
  1321. {
  1322. theEOSOffset = CStrLength(theSrcCStr + theCurrentLineOffset);
  1323. CopyCSubstrToCStr(theSrcCStr + theCurrentLineOffset,theEOSOffset,theDstCStr,maxDstCStrLength);
  1324. if (gotLastLine != nil)
  1325. {
  1326. *gotLastLine = true;
  1327. }
  1328. if (lineEndIndex != nil)
  1329. {
  1330. *lineEndIndex = theEOSOffset;
  1331. }
  1332. }
  1333. errCode = noErr;
  1334. EXITPOINT:
  1335. return(errCode);
  1336. }
  1337. OSErr CopyIndexedLineToNewHandle(const char *theSrcCStr,int inWhichLine,Handle *outNewHandle)
  1338. {
  1339. OSErr errCode;
  1340. int theCurrentLine;
  1341. int theCurrentLineOffset;
  1342. int byteCount;
  1343. SetErrorMessageAndBailIfNil(theSrcCStr,"CopyIndexedLineToNewHandle: Bad parameter, theSrcCStr == nil");
  1344. SetErrorMessageAndBailIfNil(outNewHandle,"CopyIndexedLineToNewHandle: Bad parameter, outNewHandle == nil");
  1345. if (inWhichLine < 0)
  1346. {
  1347. SetErrorMessageAndBail(("CopyIndexedLineToNewHandle: Bad parameter, inWhichLine < 0"));
  1348. }
  1349. theCurrentLineOffset = 0;
  1350. theCurrentLine = 0;
  1351. while (theCurrentLine < inWhichLine)
  1352. {
  1353. while (theSrcCStr[theCurrentLineOffset] != '\r' && theSrcCStr[theCurrentLineOffset] != '\0')
  1354. {
  1355. theCurrentLineOffset++;
  1356. }
  1357. if (theSrcCStr[theCurrentLineOffset] == '\0')
  1358. {
  1359. break;
  1360. }
  1361. theCurrentLineOffset++;
  1362. theCurrentLine++;
  1363. }
  1364. if (theSrcCStr[theCurrentLineOffset] == '\0')
  1365. {
  1366. SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Too few lines in source text, can't get line #",inWhichLine);
  1367. }
  1368. byteCount = 0;
  1369. while (theSrcCStr[theCurrentLineOffset + byteCount] != '\r' && theSrcCStr[theCurrentLineOffset + byteCount] != '\0')
  1370. {
  1371. byteCount++;
  1372. }
  1373. *outNewHandle = NewHandle(byteCount + 1);
  1374. if (*outNewHandle == nil)
  1375. {
  1376. SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Can't allocate Handle, MemError() = ",MemError());
  1377. }
  1378. ::BlockMove(theSrcCStr + theCurrentLineOffset,**outNewHandle,byteCount);
  1379. (**outNewHandle)[byteCount] = '\0';
  1380. errCode = noErr;
  1381. EXITPOINT:
  1382. return(errCode);
  1383. }
  1384. OSErr CountDigits(const char *inCStr,int *outNumIntegerDigits,int *outNumFractDigits)
  1385. {
  1386. OSErr errCode = noErr;
  1387. int numIntDigits = 0;
  1388. int numFractDigits = 0;
  1389. int digitIndex = 0;
  1390. SetErrorMessageAndBailIfNil(inCStr,"CountDigits: Bad parameter, theSrcCStr == nil");
  1391. SetErrorMessageAndBailIfNil(outNumIntegerDigits,"CountDigits: Bad parameter, outNumIntegerDigits == nil");
  1392. SetErrorMessageAndBailIfNil(outNumFractDigits,"CountDigits: Bad parameter, outNumFractDigits == nil");
  1393. digitIndex = 0;
  1394. while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9')
  1395. {
  1396. digitIndex++;
  1397. numIntDigits++;
  1398. }
  1399. if (inCStr[digitIndex] == '.')
  1400. {
  1401. digitIndex++;
  1402. while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9')
  1403. {
  1404. digitIndex++;
  1405. numFractDigits++;
  1406. }
  1407. }
  1408. *outNumIntegerDigits = numIntDigits;
  1409. *outNumFractDigits = numFractDigits;
  1410. errCode = noErr;
  1411. EXITPOINT:
  1412. return(errCode);
  1413. }
  1414. OSErr ExtractIntFromCStr(const char *theSrcCStr,int *outInt,Boolean skipLeadingSpaces)
  1415. {
  1416. OSErr errCode;
  1417. int theCharIndex;
  1418. if (theSrcCStr == nil)
  1419. {
  1420. SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr == nil"));
  1421. }
  1422. if (outInt == nil)
  1423. {
  1424. SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, outInt == nil"));
  1425. }
  1426. *outInt = 0;
  1427. theCharIndex = 0;
  1428. if (skipLeadingSpaces == true)
  1429. {
  1430. while (theSrcCStr[theCharIndex] == ' ')
  1431. {
  1432. theCharIndex++;
  1433. }
  1434. }
  1435. if (theSrcCStr[theCharIndex] < '0' || theSrcCStr[theCharIndex] > '9')
  1436. {
  1437. SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr contains a bogus numeric representation"));
  1438. }
  1439. while (theSrcCStr[theCharIndex] >= '0' && theSrcCStr[theCharIndex] <= '9')
  1440. {
  1441. *outInt = (*outInt * 10) + (theSrcCStr[theCharIndex] - '0');
  1442. theCharIndex++;
  1443. }
  1444. errCode = noErr;
  1445. EXITPOINT:
  1446. return(errCode);
  1447. }
  1448. OSErr ExtractIntFromPStr(const unsigned char *theSrcPStr,int *outInt,Boolean skipLeadingSpaces)
  1449. {
  1450. OSErr errCode;
  1451. char theCStr[256];
  1452. if (theSrcPStr == nil)
  1453. {
  1454. SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, theSrcPStr == nil"));
  1455. }
  1456. if (outInt == nil)
  1457. {
  1458. SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, outInt == nil"));
  1459. }
  1460. CopyPStrToCStr(theSrcPStr,theCStr,sizeof(theCStr));
  1461. errCode = ExtractIntFromCStr(theCStr,outInt,skipLeadingSpaces);
  1462. EXITPOINT:
  1463. return(errCode);
  1464. }
  1465. int CountOccurencesOfCharInCStr(const char inChar,const char *inSrcCStr)
  1466. {
  1467. int theSrcCharIndex;
  1468. int numOccurrences = -1;
  1469. if (inSrcCStr != nil && inChar != '\0')
  1470. {
  1471. numOccurrences = 0;
  1472. for (theSrcCharIndex = 0;inSrcCStr[theSrcCharIndex] != '\0';theSrcCharIndex++)
  1473. {
  1474. if (inSrcCStr[theSrcCharIndex] == inChar)
  1475. {
  1476. numOccurrences++;
  1477. }
  1478. }
  1479. }
  1480. return(numOccurrences);
  1481. }
  1482. int CountWordsInCStr(const char *inSrcCStr)
  1483. {
  1484. int numWords = -1;
  1485. if (inSrcCStr != nil)
  1486. {
  1487. numWords = 0;
  1488. // Skip lead spaces
  1489. while (*inSrcCStr == ' ')
  1490. {
  1491. inSrcCStr++;
  1492. }
  1493. while (*inSrcCStr != '\0')
  1494. {
  1495. numWords++;
  1496. while (*inSrcCStr != ' ' && *inSrcCStr != '\0')
  1497. {
  1498. inSrcCStr++;
  1499. }
  1500. while (*inSrcCStr == ' ')
  1501. {
  1502. inSrcCStr++;
  1503. }
  1504. }
  1505. }
  1506. return(numWords);
  1507. }
  1508. void ConvertCStrToUpperCase(char *theSrcCStr)
  1509. {
  1510. char *theCharPtr;
  1511. if (theSrcCStr != nil)
  1512. {
  1513. theCharPtr = theSrcCStr;
  1514. while (*theCharPtr != 0)
  1515. {
  1516. if (*theCharPtr >= 'a' && *theCharPtr <= 'z')
  1517. {
  1518. *theCharPtr = *theCharPtr - 'a' + 'A';
  1519. }
  1520. theCharPtr++;
  1521. }
  1522. }
  1523. }
  1524. void ExtractCStrItemFromCStr(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,char *outDstCharPtr,const int inDstCharPtrMaxLength,const Boolean inTreatMultipleDelimsAsSingleDelim)
  1525. {
  1526. int theItem;
  1527. int theSrcCharIndex;
  1528. int theDstCharIndex;
  1529. if (foundItem != nil)
  1530. {
  1531. *foundItem = false;
  1532. }
  1533. if (outDstCharPtr != nil && inDstCharPtrMaxLength > 0 && inItemNumber >= 0 && inItemDelimiter != 0)
  1534. {
  1535. *outDstCharPtr = 0;
  1536. theSrcCharIndex = 0;
  1537. for (theItem = 0;theItem < inItemNumber;theItem++)
  1538. {
  1539. while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0')
  1540. {
  1541. theSrcCharIndex++;
  1542. }
  1543. if (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
  1544. {
  1545. theSrcCharIndex++;
  1546. if (inTreatMultipleDelimsAsSingleDelim)
  1547. {
  1548. while (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
  1549. {
  1550. theSrcCharIndex++;
  1551. }
  1552. }
  1553. }
  1554. if (inSrcCStr[theSrcCharIndex] == '\0')
  1555. {
  1556. goto EXITPOINT;
  1557. }
  1558. }
  1559. if (foundItem != nil)
  1560. {
  1561. *foundItem = true;
  1562. }
  1563. theDstCharIndex = 0;
  1564. for (;;)
  1565. {
  1566. if (inSrcCStr[theSrcCharIndex] == 0 || inSrcCStr[theSrcCharIndex] == inItemDelimiter || theDstCharIndex >= inDstCharPtrMaxLength - 1)
  1567. {
  1568. outDstCharPtr[theDstCharIndex] = 0;
  1569. break;
  1570. }
  1571. outDstCharPtr[theDstCharIndex++] = inSrcCStr[theSrcCharIndex++];
  1572. }
  1573. }
  1574. EXITPOINT:
  1575. return;
  1576. }
  1577. OSErr ExtractCStrItemFromCStrIntoNewHandle(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,Handle *outNewHandle,const Boolean inTreatMultipleDelimsAsSingleDelim)
  1578. {
  1579. OSErr errCode;
  1580. int theItem;
  1581. int theSrcCharIndex;
  1582. int theItemLength;
  1583. if (inSrcCStr == nil)
  1584. {
  1585. SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inSrcCStr == nil");
  1586. errCode = kGenericError;
  1587. goto EXITPOINT;
  1588. }
  1589. if (outNewHandle == nil)
  1590. {
  1591. SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, outNewHandle == nil");
  1592. errCode = kGenericError;
  1593. goto EXITPOINT;
  1594. }
  1595. if (foundItem == nil)
  1596. {
  1597. SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, foundItem == nil");
  1598. errCode = kGenericError;
  1599. goto EXITPOINT;
  1600. }
  1601. if (inItemNumber < 0)
  1602. {
  1603. SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemNumber < 0");
  1604. errCode = kGenericError;
  1605. goto EXITPOINT;
  1606. }
  1607. if (inItemDelimiter == 0)
  1608. {
  1609. SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemDelimiter == 0");
  1610. errCode = kGenericError;
  1611. goto EXITPOINT;
  1612. }
  1613. *foundItem = false;
  1614. theSrcCharIndex = 0;
  1615. for (theItem = 0;theItem < inItemNumber;theItem++)
  1616. {
  1617. while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0')
  1618. {
  1619. theSrcCharIndex++;
  1620. }
  1621. if (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
  1622. {
  1623. theSrcCharIndex++;
  1624. if (inTreatMultipleDelimsAsSingleDelim)
  1625. {
  1626. while (inSrcCStr[theSrcCharIndex] == inItemDelimiter)
  1627. {
  1628. theSrcCharIndex++;
  1629. }
  1630. }
  1631. }
  1632. if (inSrcCStr[theSrcCharIndex] == '\0')
  1633. {
  1634. errCode = noErr;
  1635. goto EXITPOINT;
  1636. }
  1637. }
  1638. *foundItem = true;
  1639. for (theItemLength = 0;;theItemLength++)
  1640. {
  1641. if (inSrcCStr[theSrcCharIndex + theItemLength] == 0 || inSrcCStr[theSrcCharIndex + theItemLength] == inItemDelimiter)
  1642. {
  1643. break;
  1644. }
  1645. }
  1646. *outNewHandle = NewHandle(theItemLength + 1);
  1647. if (*outNewHandle == nil)
  1648. {
  1649. SetErrorMessageAndLongIntAndBail("ExtractCStrItemFromCStrIntoNewHandle: Can't allocate Handle, MemError() = ",MemError());
  1650. }
  1651. BlockMove(inSrcCStr + theSrcCharIndex,**outNewHandle,theItemLength);
  1652. (**outNewHandle)[theItemLength] = 0;
  1653. errCode = noErr;
  1654. EXITPOINT:
  1655. return(errCode);
  1656. }
  1657. OSErr ExtractFloatFromCStr(const char *inCString,extended80 *outFloat)
  1658. {
  1659. OSErr errCode;
  1660. Str255 theStr255;
  1661. Handle theNumberPartsTableHandle = nil;
  1662. long theNumberPartsOffset,theNumberPartsLength;
  1663. FormatResultType theFormatResultType;
  1664. NumberParts theNumberPartsTable;
  1665. NumFormatStringRec theNumFormatStringRec;
  1666. if (inCString == nil)
  1667. {
  1668. SetErrorMessage("ExtractFloatFromCStr: Bad parameter, inCString == nil");
  1669. errCode = kGenericError;
  1670. goto EXITPOINT;
  1671. }
  1672. if (outFloat == nil)
  1673. {
  1674. SetErrorMessage("ExtractFloatFromCStr: Bad parameter, outFloat == nil");
  1675. errCode = kGenericError;
  1676. goto EXITPOINT;
  1677. }
  1678. // GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
  1679. GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
  1680. if (theNumberPartsTableHandle == nil)
  1681. {
  1682. SetErrorMessage("ExtractFloatFromCStr: Can't get number parts table for converting string representations to/from numeric representations");
  1683. errCode = kGenericError;
  1684. goto EXITPOINT;
  1685. }
  1686. if (theNumberPartsLength > sizeof(theNumberPartsTable))
  1687. {
  1688. SetErrorMessage("ExtractFloatFromCStr: Number parts table has bad length");
  1689. errCode = kGenericError;
  1690. goto EXITPOINT;
  1691. }
  1692. BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength);
  1693. theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec);
  1694. if (theFormatResultType != fFormatOK)
  1695. {
  1696. SetErrorMessage("ExtractFloatFromCStr: StringToFormatRec() != fFormatOK");
  1697. errCode = kGenericError;
  1698. goto EXITPOINT;
  1699. }
  1700. CopyCStrToPStr(inCString,theStr255,sizeof(theStr255));
  1701. theFormatResultType = (FormatResultType) StringToExtended(theStr255,&theNumFormatStringRec,&theNumberPartsTable,outFloat);
  1702. if (theFormatResultType != fFormatOK && theFormatResultType != fBestGuess)
  1703. {
  1704. SetErrorMessageAndLongIntAndBail("ExtractFloatFromCStr: StringToExtended() = ",theFormatResultType);
  1705. }
  1706. errCode = noErr;
  1707. EXITPOINT:
  1708. return(errCode);
  1709. }
  1710. OSErr CopyFloatToCStr(const extended80 *theFloat,char *theCStr,const int maxCStrLength,const int inMaxNumIntDigits,const int inMaxNumFractDigits)
  1711. {
  1712. OSErr errCode;
  1713. Str255 theStr255;
  1714. Handle theNumberPartsTableHandle = nil;
  1715. long theNumberPartsOffset,theNumberPartsLength;
  1716. FormatResultType theFormatResultType;
  1717. NumberParts theNumberPartsTable;
  1718. NumFormatStringRec theNumFormatStringRec;
  1719. if (theCStr == nil)
  1720. {
  1721. SetErrorMessage("CopyFloatToCStr: Bad parameter, theCStr == nil");
  1722. errCode = kGenericError;
  1723. goto EXITPOINT;
  1724. }
  1725. if (theFloat == nil)
  1726. {
  1727. SetErrorMessage("CopyFloatToCStr: Bad parameter, theFloat == nil");
  1728. errCode = kGenericError;
  1729. goto EXITPOINT;
  1730. }
  1731. // GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
  1732. GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength);
  1733. if (theNumberPartsTableHandle == nil)
  1734. {
  1735. SetErrorMessage("CopyFloatToCStr: Can't get number parts table for converting string representations to/from numeric representations");
  1736. errCode = kGenericError;
  1737. goto EXITPOINT;
  1738. }
  1739. if (theNumberPartsLength > sizeof(theNumberPartsTable))
  1740. {
  1741. SetErrorMessage("CopyFloatToCStr: Number parts table has bad length");
  1742. errCode = kGenericError;
  1743. goto EXITPOINT;
  1744. }
  1745. BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength);
  1746. if (inMaxNumIntDigits >= 0 || inMaxNumFractDigits >= 0)
  1747. {
  1748. char numberFormat[64];
  1749. int numberFormatLength = 0;
  1750. for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
  1751. {
  1752. numberFormat[numberFormatLength++] = '0';
  1753. }
  1754. if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1)
  1755. {
  1756. numberFormat[numberFormatLength++] = '.';
  1757. for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
  1758. {
  1759. numberFormat[numberFormatLength++] = '0';
  1760. }
  1761. }
  1762. if (numberFormatLength < sizeof(numberFormat) - 1)
  1763. {
  1764. numberFormat[numberFormatLength++] = ';';
  1765. }
  1766. if (numberFormatLength < sizeof(numberFormat) - 1)
  1767. {
  1768. numberFormat[numberFormatLength++] = '-';
  1769. }
  1770. for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
  1771. {
  1772. numberFormat[numberFormatLength++] = '0';
  1773. }
  1774. if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1)
  1775. {
  1776. numberFormat[numberFormatLength++] = '.';
  1777. for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++)
  1778. {
  1779. numberFormat[numberFormatLength++] = '0';
  1780. }
  1781. }
  1782. numberFormat[numberFormatLength] = '\0';
  1783. Str255 tempStr255;
  1784. CopyCStrToPStr(numberFormat,tempStr255,sizeof(tempStr255));
  1785. theFormatResultType = (FormatResultType) StringToFormatRec(tempStr255,&theNumberPartsTable,&theNumFormatStringRec);
  1786. }
  1787. else
  1788. {
  1789. theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec);
  1790. }
  1791. if (theFormatResultType != fFormatOK)
  1792. {
  1793. SetErrorMessage("CopyFloatToCStr: StringToFormatRec() != fFormatOK");
  1794. errCode = kGenericError;
  1795. goto EXITPOINT;
  1796. }
  1797. theFormatResultType = (FormatResultType) ExtendedToString(theFloat,&theNumFormatStringRec,&theNumberPartsTable,theStr255);
  1798. if (theFormatResultType != fFormatOK)
  1799. {
  1800. SetErrorMessage("CopyFloatToCStr: ExtendedToString() != fFormatOK");
  1801. errCode = kGenericError;
  1802. goto EXITPOINT;
  1803. }
  1804. CopyPStrToCStr(theStr255,theCStr,maxCStrLength);
  1805. errCode = noErr;
  1806. EXITPOINT:
  1807. return(errCode);
  1808. }
  1809. void SkipWhiteSpace(char **ioSrcCharPtr,const Boolean inStopAtEOL)
  1810. {
  1811. if (ioSrcCharPtr != nil && *ioSrcCharPtr != nil)
  1812. {
  1813. if (inStopAtEOL)
  1814. {
  1815. while ((**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t') && **ioSrcCharPtr != '\r' && **ioSrcCharPtr != '\n')
  1816. {
  1817. *ioSrcCharPtr++;
  1818. }
  1819. }
  1820. else
  1821. {
  1822. while (**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t')
  1823. {
  1824. *ioSrcCharPtr++;
  1825. }
  1826. }
  1827. }
  1828. }