libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmText.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmText.h"
  10. #include "cmFloatTypes.h"
  11. #include "cmVectOps.h"
  12. typedef struct cmTextSys_str
  13. {
  14. cmErr_t err;
  15. cmLHeapH_t lhH;
  16. cmChar_t* buf;
  17. } cmTextSys_t;
  18. cmTextSysH_t cmTextSysNullHandle = cmSTATIC_NULL_HANDLE;
  19. cmTextSysH_t _cmTextSysGlobalH = cmSTATIC_NULL_HANDLE;
  20. cmTextSys_t* _cmTextSysHandleToPtr( cmTextSysH_t h )
  21. {
  22. cmTextSys_t* p = (cmTextSys_t*)h.h;
  23. assert(p != NULL);
  24. return p;
  25. }
  26. cmTxRC_t _cmTextSysFinalize( cmTextSys_t* p )
  27. {
  28. cmTxRC_t rc = kOkTxRC;
  29. if( cmLHeapIsValid(p->lhH) )
  30. cmLHeapDestroy(&p->lhH);
  31. cmMemFree(p->buf);
  32. cmMemFree(p);
  33. return rc;
  34. }
  35. cmTxRC_t cmTextSysInitialize( cmCtx_t* ctx, cmTextSysH_t* hp )
  36. {
  37. cmTxRC_t rc;
  38. if((rc = cmTextSysFinalize(hp)) != kOkTxRC )
  39. return rc;
  40. cmTextSys_t* p = cmMemAllocZ(cmTextSys_t,1);
  41. cmErrSetup(&p->err,&ctx->rpt,"Text System");
  42. if( cmLHeapIsValid( p->lhH = cmLHeapCreate( 8192, ctx )) == false )
  43. {
  44. rc = cmErrMsg(&p->err,kLHeapFailTxRC,"Linked Heap create failed.");
  45. goto errLabel;
  46. }
  47. hp->h = p;
  48. errLabel:
  49. return rc;
  50. }
  51. cmTxRC_t cmTextSysFinalize( cmTextSysH_t* hp )
  52. {
  53. cmTxRC_t rc = kOkTxRC;
  54. if( hp == NULL || cmTextSysIsValid(*hp)==false )
  55. return kOkTxRC;
  56. cmTextSys_t* p = _cmTextSysHandleToPtr(*hp);
  57. if((rc = _cmTextSysFinalize(p)) == kOkTxRC )
  58. {
  59. hp->h = NULL;
  60. }
  61. return rc;
  62. }
  63. bool cmTextSysIsValid( cmTextSysH_t h )
  64. { return h.h != NULL; }
  65. cmChar_t* _cmTextSysVPrintf( cmTextSysH_t h, cmLHeapH_t lhH, bool staticFl, const cmChar_t* fmt, va_list vl )
  66. {
  67. va_list vl1;
  68. va_copy(vl1,vl);
  69. cmTextSys_t* p = cmLHeapIsValid(lhH) ? NULL : _cmTextSysHandleToPtr(h);
  70. unsigned n = vsnprintf(NULL,0,fmt,vl);
  71. cmChar_t* s = NULL;
  72. if( staticFl )
  73. s = p->buf = cmMemResize(cmChar_t,p->buf,n+1);
  74. else
  75. s = cmLhAllocZ(cmLHeapIsValid(lhH) ? lhH : p->lhH,cmChar_t,n+1);
  76. unsigned m = vsnprintf(s,n+1,fmt,vl1);
  77. assert(m==n);
  78. s[n] = 0;
  79. va_end(vl1);
  80. return s;
  81. }
  82. cmChar_t* cmTextSysVPrintfH( cmLHeapH_t lhH, const cmChar_t* fmt, va_list vl )
  83. { return _cmTextSysVPrintf( cmTextSysNullHandle, lhH, false, fmt, vl ); }
  84. cmChar_t* cmTextSysPrintfH( cmLHeapH_t lhH, const cmChar_t* fmt, ... )
  85. {
  86. va_list vl;
  87. va_start( vl, fmt );
  88. cmChar_t* s = cmTextSysVPrintfH(lhH,fmt,vl);
  89. va_end(vl);
  90. return s;
  91. }
  92. cmChar_t* cmTextSysVPrintfS( cmTextSysH_t h, const cmChar_t* fmt, va_list vl )
  93. { return _cmTextSysVPrintf(h,cmLHeapNullHandle,true,fmt,vl); }
  94. cmChar_t* cmTextSysPrintfS( cmTextSysH_t h, const cmChar_t* fmt, ... )
  95. {
  96. va_list vl;
  97. va_start(vl,fmt);
  98. cmChar_t* s = cmTextSysVPrintfS(h,fmt,vl);
  99. va_end(vl);
  100. return s;
  101. }
  102. cmChar_t* cmTextSysVPrintf( cmTextSysH_t h, const cmChar_t* fmt, va_list vl )
  103. { return _cmTextSysVPrintf(h,cmLHeapNullHandle,false,fmt,vl); }
  104. cmChar_t* cmTextSysPrintf( cmTextSysH_t h, const cmChar_t* fmt, ... )
  105. {
  106. va_list vl;
  107. va_start(vl,fmt);
  108. cmChar_t* s = cmTextSysVPrintf(h,fmt,vl);
  109. va_end(vl);
  110. return s;
  111. }
  112. void cmTextSysFreeStr( cmTextSysH_t h, const cmChar_t* s )
  113. {
  114. cmTextSys_t* p = _cmTextSysHandleToPtr(h);
  115. cmLhFree(p->lhH,(cmChar_t*)s);
  116. }
  117. bool cmTextSysIsStored(cmTextSysH_t h, const cmChar_t* s )
  118. {
  119. cmTextSys_t* p = _cmTextSysHandleToPtr(h);
  120. return cmLHeapIsPtrInHeap(p->lhH,s);
  121. }
  122. //
  123. // Global interface
  124. //
  125. cmTxRC_t cmTsInitialize( cmCtx_t* ctx )
  126. { return cmTextSysInitialize(ctx,&_cmTextSysGlobalH); }
  127. cmTxRC_t cmTsFinalize()
  128. { return cmTextSysFinalize(&_cmTextSysGlobalH); }
  129. bool cmTsIsValid()
  130. { return cmTextSysIsValid(_cmTextSysGlobalH); }
  131. cmChar_t* cmTsVPrintfS( const cmChar_t* fmt, va_list vl )
  132. { return cmTextSysVPrintfS(_cmTextSysGlobalH,fmt,vl); }
  133. cmChar_t* cmTsPrintfS( const cmChar_t* fmt, ... )
  134. {
  135. va_list vl;
  136. va_start(vl,fmt);
  137. cmChar_t* s = cmTsVPrintfS(fmt,vl);
  138. va_end(vl);
  139. return s;
  140. }
  141. cmChar_t* cmTsVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl )
  142. { return cmTextSysVPrintfH(h,fmt,vl); }
  143. cmChar_t* cmTsPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... )
  144. {
  145. va_list vl;
  146. va_start(vl,fmt);
  147. cmChar_t* s = cmTsVPrintfH(h,fmt,vl);
  148. va_end(vl);
  149. return s;
  150. }
  151. cmChar_t* cmTsVPrintf( const cmChar_t* fmt, va_list vl )
  152. { return cmTextSysVPrintf(_cmTextSysGlobalH,fmt,vl); }
  153. cmChar_t* cmTsPrintf( const cmChar_t* fmt, ... )
  154. {
  155. va_list vl;
  156. va_start(vl,fmt);
  157. cmChar_t* s = cmTsVPrintf(fmt,vl);
  158. va_end(vl);
  159. return s;
  160. }
  161. void cmTsFreeStr( const cmChar_t* s )
  162. { cmTextSysFreeStr(_cmTextSysGlobalH,s); }
  163. bool cmTsIsStored( const cmChar_t* s )
  164. { return cmTextSysIsStored(_cmTextSysGlobalH,s); }
  165. cmChar_t* cmTsVPrintfP( cmChar_t* s, const cmChar_t* fmt, va_list vl )
  166. {
  167. va_list vl1;
  168. va_copy(vl1,vl);
  169. int n = vsnprintf(NULL,0,fmt,vl);
  170. assert(n != -1);
  171. s = cmMemResize(cmChar_t,s,n+1);
  172. unsigned m = vsnprintf(s,n+1,fmt,vl1);
  173. assert(m==n);
  174. s[n] = 0;
  175. return s;
  176. }
  177. cmChar_t* cmTsPrintfP( cmChar_t* s, const cmChar_t* fmt, ... )
  178. {
  179. va_list vl;
  180. va_start(vl,fmt);
  181. s = cmTsVPrintfP(s,fmt,vl);
  182. va_end(vl);
  183. return s;
  184. }
  185. void _cmTxError( cmErr_t* err, cmTxRC_t rc, const char* fmt, ... )
  186. {
  187. va_list vl;
  188. va_start(vl,fmt);
  189. cmErrVMsg(err,rc,fmt,vl);
  190. va_end(vl);
  191. }
  192. cmTxRC_t _cmTxRptError( cmErr_t* err, const char* msg, const char* inputText )
  193. {
  194. if( err == NULL )
  195. return kOkTxRC;
  196. if( inputText == NULL )
  197. {
  198. _cmTxError(err,kNullTxRC,"Text to %s conversion failed due to NULL input text.");
  199. return kNullTxRC;
  200. }
  201. if( errno != 0 )
  202. {
  203. _cmTxError(err,kCvtErrTxRC,"Text to %s conversion failed on input '%s'.",msg,inputText);
  204. return kCvtErrTxRC;
  205. }
  206. return kOkTxRC;
  207. }
  208. cmTxRC_t cmTextToInt(const char* text, int* vp, cmErr_t* err )
  209. {
  210. assert( vp != NULL );
  211. cmTxRC_t rc = kOkTxRC;
  212. int en = errno;
  213. errno = 0;
  214. *vp = text==NULL ? 0 : strtol(text,NULL,0);
  215. rc = _cmTxRptError(err,"integer",text);
  216. errno = en;
  217. return rc;
  218. }
  219. cmTxRC_t cmTextToUInt( const char* text, unsigned* vp, cmErr_t* err )
  220. {
  221. assert( vp != NULL );
  222. cmTxRC_t rc = kOkTxRC;
  223. int en = errno;
  224. errno = 0;
  225. *vp = text==NULL ? 0 : (unsigned)strtol(text,NULL,0);
  226. rc = _cmTxRptError(err,"unsigned integer",text);
  227. errno = en;
  228. return rc;
  229. }
  230. cmTxRC_t cmTextToFloat( const char* text, float* vp, cmErr_t* err )
  231. {
  232. assert( vp != NULL );
  233. cmTxRC_t rc = kOkTxRC;
  234. int en = errno;
  235. errno = 0;
  236. *vp = text==NULL ? 0 : (float)strtod(text,NULL);
  237. rc = _cmTxRptError(err,"float",text);
  238. errno = en;
  239. return rc;
  240. }
  241. cmTxRC_t cmTextToDouble( const char* text, double* vp, cmErr_t* err )
  242. {
  243. assert( vp != NULL );
  244. cmTxRC_t rc = kOkTxRC;
  245. int en = errno;
  246. errno = 0;
  247. *vp = text==NULL ? 0 : strtod(text,NULL);
  248. rc = _cmTxRptError(err,"double",text);
  249. errno = en;
  250. return rc;
  251. }
  252. cmTxRC_t cmTextToBool( const char* text, bool* vp, cmErr_t* err )
  253. {
  254. assert( vp != NULL );
  255. cmTxRC_t rc = kOkTxRC;
  256. if( strcasecmp(text,"true") == 0 || strcasecmp(text,"0") == 0 )
  257. *vp = true;
  258. else
  259. if( strcasecmp(text,"false") == 0 || strcasecmp(text,"1") == 0 )
  260. *vp = false;
  261. else
  262. rc = _cmTxRptError(err,"bool",text);
  263. return rc;
  264. }
  265. cmChar_t* cmTextNextNonWhiteOrEos( cmChar_t* s )
  266. {
  267. assert( s != NULL );
  268. while( (*s) && isspace(*s) )
  269. ++s;
  270. return s;
  271. }
  272. const cmChar_t* cmTextNextNonWhiteOrEosC( const cmChar_t* s )
  273. { return cmTextNextNonWhiteOrEos((cmChar_t*)s); }
  274. cmChar_t* cmTextNextNonWhite( cmChar_t* s )
  275. { //return (*(s=cmTextNextNonWhiteOrEos(s))) == 0 ? NULL : s;
  276. s=cmTextNextNonWhiteOrEos(s);
  277. if( *s == 0 )
  278. return NULL;
  279. return s;
  280. }
  281. const cmChar_t* cmTextNextNonWhiteC( const cmChar_t* s )
  282. { return cmTextNextNonWhite((cmChar_t*)s); }
  283. cmChar_t* cmTextPrevNonWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 )
  284. {
  285. assert( s0!=NULL && s1!=NULL && s0 <= s1 );
  286. for(; s0 < s1; --s1 )
  287. if( !isspace(*s1) )
  288. break;
  289. return (cmChar_t*)s1;
  290. }
  291. const cmChar_t* cmTextPrevNonWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 )
  292. { return cmTextPrevNonWhiteOrBos((cmChar_t*)s0,s1); }
  293. cmChar_t* cmTextPrevNonWhite( cmChar_t* s0, const cmChar_t* s1 )
  294. {
  295. cmChar_t* s2;
  296. if((s2 = cmTextPrevNonWhiteOrBos(s0,s1)) == s0 )
  297. return NULL;
  298. return s2;
  299. }
  300. const cmChar_t* cmTextPrevNonWhiteC( const cmChar_t* s0, const cmChar_t* s1 )
  301. { return cmTextPrevNonWhite((cmChar_t*)s0,s1); }
  302. cmChar_t* cmTextNextWhiteOrEos( cmChar_t* s )
  303. {
  304. assert( s!=NULL);
  305. while(*s && !isspace(*s) )
  306. ++s;
  307. return s;
  308. }
  309. const cmChar_t* cmTextNextWhiteOrEosC( const cmChar_t* s )
  310. { return cmTextNextWhiteOrEos((cmChar_t*)s); }
  311. cmChar_t* cmTextNextWhite( cmChar_t* s )
  312. { return (*(s=cmTextNextWhiteOrEos(s)))!=0 ? s : NULL; }
  313. const cmChar_t* cmTextNextWhiteC( const cmChar_t* s )
  314. { return cmTextNextWhite((cmChar_t*)s); }
  315. cmChar_t* cmTextPrevWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 )
  316. {
  317. assert( s0!=NULL && s1!=NULL && s0 <= s1 );
  318. while( s1>s0 && !isspace(*s1) )
  319. --s1;
  320. return (cmChar_t*)s1;
  321. }
  322. const cmChar_t* cmTextPrevWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 )
  323. { return cmTextPrevWhiteOrBos((cmChar_t*)s0,s1); }
  324. cmChar_t* cmTextPrevWhite( cmChar_t* s0, const cmChar_t* s1 )
  325. {
  326. cmChar_t* s2;
  327. if((s2 = cmTextPrevWhiteOrBos(s0,s1)) == s0 )
  328. return NULL;
  329. return s2;
  330. }
  331. const cmChar_t* cmTextPrevWhiteC( const cmChar_t* s0, const cmChar_t* s1 )
  332. { return cmTextPrevWhite((cmChar_t*)s0,s1); }
  333. cmChar_t* cmTextBegOfLine( cmChar_t* s0, const cmChar_t* s1 )
  334. {
  335. assert( s1!=NULL && s0!=NULL && s1 >= s0 );
  336. if( s0 == s1 )
  337. return s0;
  338. --s1;
  339. while( s1>s0 && *s1 != '\n' )
  340. --s1;
  341. if( *s1 == '\n' )
  342. ++s1;
  343. return (cmChar_t*)s1;
  344. }
  345. const cmChar_t* cmTextBegOfLineC( const cmChar_t* s0, const cmChar_t* s1 )
  346. { return cmTextBegOfLine((cmChar_t*)s0,s1); }
  347. cmChar_t* cmTextEndOfLine( cmChar_t* s )
  348. {
  349. assert( s!=NULL);
  350. while( *s!=0 && *s != '\n' )
  351. ++s;
  352. return s;
  353. }
  354. const cmChar_t* cmTextEndOfLineC( const cmChar_t* s )
  355. { return cmTextEndOfLine((cmChar_t*)s); }
  356. cmChar_t* cmTextLastNonWhiteChar( const cmChar_t* s )
  357. {
  358. unsigned n;
  359. if(s==NULL || (n = strlen(s)) == 0 )
  360. return NULL;
  361. cmChar_t* s0 = (cmChar_t*)s + n-1;
  362. for(; s0>=s; --s0)
  363. if( !isspace(*s0) )
  364. return s0;
  365. return NULL;
  366. }
  367. const cmChar_t* cmTextLastNonWhiteCharC( const cmChar_t* s )
  368. { return cmTextLastNonWhiteChar(s); }
  369. void cmTextShrinkS( cmChar_t* s, const cmChar_t* t, unsigned tn )
  370. { cmVOC_Shrink(s,strlen(s)+1,t,tn); }
  371. void cmTextShrinkSN(cmChar_t* s, unsigned sn, const cmChar_t* t, unsigned tn )
  372. { cmVOC_Shrink(s,sn,t,tn); }
  373. void cmTextClip( cmChar_t* s, unsigned n )
  374. {
  375. if( n == 0 || s == NULL || strlen(s)==0 )
  376. return;
  377. if( n >= strlen(s) )
  378. {
  379. s[0]=0;
  380. return;
  381. }
  382. s[ strlen(s)-n ] = 0;
  383. }
  384. cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn )
  385. { return cmVOC_Expand(s,strlen(s)+1,t,tn); }
  386. cmChar_t* cmTextReplaceSN( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u, unsigned un )
  387. {
  388. unsigned n = strlen(s)+1;
  389. return cmVOC_Replace(s,&n,t,tn,u,un);
  390. }
  391. cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u )
  392. { return cmTextReplaceSN(s,t,tn,u,u==NULL ? 0 : strlen(u)); }
  393. cmChar_t* _cmTextReplace( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned n )
  394. {
  395. // we will go into an endless loop if 't' is contained in 'u' and n > 1.
  396. //assert( s!= NULL && t!=NULL && u!=NULL && (n==1 || strstr(u,t) == NULL) );
  397. assert( s!= NULL && t!=NULL && u!=NULL );
  398. int tn = strlen(t);
  399. cmChar_t* c = NULL;
  400. unsigned i = 0;
  401. cmChar_t* s0 = s;
  402. while( (c = strstr(s0,t)) != NULL )
  403. {
  404. int offs = c - s;
  405. s = cmTextReplaceS(s,c,tn,u);
  406. assert(s!=NULL);
  407. s0 = s + offs + tn;
  408. ++i;
  409. if( n!=cmInvalidCnt && i>=n)
  410. break;
  411. };
  412. return s;
  413. }
  414. cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
  415. { return _cmTextReplace(s,t,u,cmInvalidCnt); }
  416. cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
  417. { return _cmTextReplace(s,t,u,1); }
  418. cmChar_t* cmTextInsertSN( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un )
  419. {
  420. unsigned n = strlen(s)+1;
  421. return cmVOC_Replace(s,&n,t,0,u,un);
  422. }
  423. cmChar_t* cmTextInsertS( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
  424. { return cmTextInsertSN(s,t,u,u==NULL?0:strlen(u)); }
  425. cmChar_t* cmTextAppend( cmChar_t* s, unsigned* sn, const cmChar_t* u, unsigned un )
  426. { return cmVOC_Replace(s,sn,s+(*sn),0,u,un); }
  427. cmChar_t* cmTextAppendSN( cmChar_t* s, const cmChar_t* u, unsigned un )
  428. {
  429. unsigned sn = s==NULL ? 0 : strlen(s);
  430. if( un > 0 )
  431. {
  432. s = cmTextAppend(s,&sn,u,un+1);
  433. s[sn-1] = 0;
  434. }
  435. return s;
  436. }
  437. // append u[un] to s[] and append terminating zero.
  438. cmChar_t* cmTextAppendSNZ( cmChar_t* s, const cmChar_t* u, unsigned un )
  439. {
  440. unsigned sn = s==NULL ? 0 : strlen(s)+1;
  441. cmChar_t z = 0;
  442. s = cmTextAppend(s,&sn,u,un);
  443. return cmTextAppend(s,&sn,&z,1);
  444. }
  445. // both s[] and u[] are strz's
  446. cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u )
  447. { return cmTextAppendSN(s,u,strlen(u)); }
  448. cmChar_t* cmTextVAppendSS( cmChar_t* s, ... )
  449. {
  450. va_list vl;
  451. va_start(vl,s);
  452. do
  453. {
  454. cmChar_t* s0 = va_arg(vl,cmChar_t*);
  455. if( s0 == NULL )
  456. break;
  457. s = cmTextAppendSS(s,s0);
  458. }while(1);
  459. va_end(vl);
  460. return s;
  461. }
  462. cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n )
  463. {
  464. if( n <= 0 )
  465. return s;
  466. cmChar_t t[ n+1 ];
  467. memset(t,' ',n);
  468. t[n] = 0;
  469. return cmTextAppendSS(s,t);
  470. }
  471. bool cmTextIsEmpty( const cmChar_t* s )
  472. {
  473. if( s!=NULL )
  474. for(; *s; ++s )
  475. if( !isspace(*s) )
  476. return false;
  477. return true;
  478. }
  479. bool cmTextIsNotEmpty( const cmChar_t* s )
  480. { return !cmTextIsEmpty(s); }
  481. unsigned cmTextLength( const cmChar_t* s0 )
  482. {
  483. if( s0 == NULL )
  484. return 0;
  485. return strlen(s0);
  486. }
  487. int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 )
  488. {
  489. if( s0 == NULL && s1 == NULL )
  490. return 0;
  491. if( s0 == NULL || s1 == NULL )
  492. {
  493. if( s0 == NULL )
  494. return -1;
  495. return 1;
  496. }
  497. return strcmp(s0,s1);
  498. }
  499. int cmTextCmpN( const cmChar_t* s0, const cmChar_t* s1, unsigned n )
  500. {
  501. if( s0 == NULL && s1 == NULL )
  502. return 0;
  503. if( s0 == NULL || s1 == NULL )
  504. {
  505. if( s0 == NULL )
  506. return -1;
  507. return 1;
  508. }
  509. return strncmp(s0,s1,n);
  510. }
  511. void cmTextToLower( const cmChar_t* s0, cmChar_t* s1 )
  512. {
  513. if( s0 == NULL || s1==NULL )
  514. return;
  515. for(; *s0; ++s0,++s1)
  516. *s1 = tolower(*s0);
  517. *s1 = 0;
  518. return;
  519. }
  520. void cmTextToUpper( const cmChar_t* s0, cmChar_t* s1 )
  521. {
  522. if( s0 == NULL || s1==NULL )
  523. return;
  524. for(; *s0; ++s0,++s1)
  525. *s1 = toupper(*s0);
  526. *s1 = 0;
  527. return;
  528. }
  529. cmChar_t* cmTextLine( cmChar_t* s, unsigned line )
  530. {
  531. assert( line>0);
  532. unsigned i;
  533. // count down to the code line containing the tag reference
  534. for(i=0; i<line-1; ++i)
  535. {
  536. s = strchr(s,'\n');
  537. if( s == NULL )
  538. return NULL;
  539. ++s;
  540. }
  541. return s+1;
  542. }
  543. const cmChar_t* cmTextLineC( const cmChar_t* s, unsigned line )
  544. { return cmTextLine((cmChar_t*)s,line); }
  545. cmChar_t* cmTextRemoveConsecutiveSpaces( cmChar_t* s )
  546. {
  547. if( s==NULL || strlen(s) < 2 )
  548. return s;
  549. int i=1;
  550. while( s[i] )
  551. {
  552. if( isspace(s[i-1]) && isspace(s[i]) )
  553. cmTextShrinkS(s, s+i, 1 );
  554. else
  555. ++i;
  556. }
  557. return s;
  558. }
  559. cmChar_t* cmTextColumize( cmChar_t* s, unsigned colCnt )
  560. {
  561. if( s==NULL || strlen(s) < colCnt )
  562. return s;
  563. int i = 0;
  564. int c = 0;
  565. cmChar_t* c0 = NULL;
  566. for(; s[i]; ++i)
  567. {
  568. // remove any existing newlines
  569. if( s[i] == '\n' )
  570. s[i] = ' ';
  571. // track the last space (which is a potential wrap point).
  572. if( isspace(s[i]) )
  573. c0 = s+i;
  574. if( c < colCnt )
  575. ++c;
  576. else
  577. {
  578. // if there is no previous wrap point ...
  579. if( c0 == NULL )
  580. {
  581. // ... then insert one
  582. s = cmTextInsertS(s,s+i,"\n");
  583. }
  584. else
  585. {
  586. // replace the wrap point with a '\n'
  587. *c0 = '\n';
  588. }
  589. c = 0;
  590. c0 = NULL;
  591. }
  592. }
  593. return s;
  594. }
  595. cmChar_t* _cmTextPrefixRows( cmChar_t* s, const cmChar_t* t )
  596. {
  597. if( s==NULL || t==NULL || strlen(t)==0 )
  598. return s;
  599. int i;
  600. for(i=0; s[i]; ++i)
  601. if( i==0 || s[i]=='\n')
  602. {
  603. cmChar_t* u = s + (i==0 ? 0 : i+1);
  604. s = cmTextInsertS(s,u,t);
  605. }
  606. return s;
  607. }
  608. cmChar_t* cmTextIndentRows( cmChar_t* s, unsigned indent )
  609. {
  610. if( s==NULL || indent==0 )
  611. return s;
  612. cmChar_t* t = cmMemAllocZ( cmChar_t, indent+1 );
  613. cmVOC_Fill(t,indent,' ');
  614. t[indent] = 0;
  615. s = _cmTextPrefixRows(s,t);
  616. cmMemFree(t);
  617. return s;
  618. }
  619. cmChar_t* cmTextPrefixRows( cmChar_t* s, const cmChar_t* t )
  620. { return _cmTextPrefixRows(s,t); }
  621. cmChar_t* cmTextTrimRows( cmChar_t* s )
  622. {
  623. bool fl = true;
  624. int i = 0;
  625. while( s[i] )
  626. {
  627. if( s[i] == '\n' )
  628. {
  629. fl = true;
  630. ++i;
  631. }
  632. else
  633. {
  634. if( isspace(s[i]) && fl )
  635. cmTextShrinkS(s, s+i, 1 );
  636. else
  637. {
  638. fl = false;
  639. ++i;
  640. }
  641. }
  642. }
  643. return s;
  644. }
  645. cmChar_t* cmTextEatLeadingSpace( cmChar_t* s )
  646. {
  647. if( s == NULL )
  648. return s;
  649. while( *s )
  650. {
  651. if( !isspace(*s) )
  652. break;
  653. cmTextShrinkS(s,s,1);
  654. }
  655. return s;
  656. }
  657. cmChar_t* cmTextNextRow( cmChar_t* s )
  658. {
  659. if( s == NULL)
  660. return NULL;
  661. for(; *s; ++s)
  662. if( *s == '\n' )
  663. {
  664. ++s;
  665. return *s==0 ? NULL : s;
  666. }
  667. return NULL;
  668. }
  669. const cmChar_t* cmTextNextRowC( const cmChar_t* s )
  670. { return cmTextNextRow((cmChar_t*)s); }
  671. unsigned cmTextMinIndent( const cmChar_t* s )
  672. {
  673. // leadFl=true if at beginning of row
  674. bool leadFl = true;
  675. unsigned min_indent = INT_MAX;
  676. unsigned indent = 0;
  677. for(; *s; ++s)
  678. {
  679. if( leadFl )
  680. {
  681. if( isspace(*s) && *s!='\n' )
  682. indent += 1;
  683. else
  684. {
  685. if( indent < min_indent )
  686. min_indent = indent;
  687. indent = 0;
  688. leadFl = false;
  689. }
  690. }
  691. else
  692. {
  693. if( *s == '\n' )
  694. leadFl = true;
  695. }
  696. }
  697. return min_indent==INT_MAX ? 0 : min_indent;
  698. }
  699. cmChar_t* cmTextOutdent( cmChar_t* s, unsigned outdent )
  700. {
  701. // leadFl=true if at beginning of row
  702. bool leadFl = true;
  703. unsigned indent = 0;
  704. cmChar_t* cs = s;
  705. cmChar_t* s0 = s;
  706. for(; *cs; ++cs)
  707. {
  708. if( leadFl )
  709. {
  710. if( isspace(*cs) && *cs!='\n' )
  711. indent += 1;
  712. else
  713. {
  714. unsigned n = cmMin(outdent,indent);
  715. cmTextShrinkS(s,s0,n);
  716. cs -= n;
  717. indent = 0;
  718. leadFl = false;
  719. }
  720. }
  721. else
  722. {
  723. if( *cs == '\n' )
  724. {
  725. leadFl = true;
  726. s0 = cs + 1;
  727. }
  728. }
  729. }
  730. return s;
  731. }