libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmText.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. //
  118. // Global interface
  119. //
  120. cmTxRC_t cmTsInitialize( cmCtx_t* ctx )
  121. { return cmTextSysInitialize(ctx,&_cmTextSysGlobalH); }
  122. cmTxRC_t cmTsFinalize()
  123. { return cmTextSysFinalize(&_cmTextSysGlobalH); }
  124. bool cmTsIsValid()
  125. { return cmTextSysIsValid(_cmTextSysGlobalH); }
  126. cmChar_t* cmTsVPrintfS( const cmChar_t* fmt, va_list vl )
  127. { return cmTextSysVPrintfS(_cmTextSysGlobalH,fmt,vl); }
  128. cmChar_t* cmTsPrintfS( const cmChar_t* fmt, ... )
  129. {
  130. va_list vl;
  131. va_start(vl,fmt);
  132. cmChar_t* s = cmTsVPrintfS(fmt,vl);
  133. va_end(vl);
  134. return s;
  135. }
  136. cmChar_t* cmTsVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl )
  137. { return cmTextSysVPrintfH(h,fmt,vl); }
  138. cmChar_t* cmTsPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... )
  139. {
  140. va_list vl;
  141. va_start(vl,fmt);
  142. cmChar_t* s = cmTsVPrintfH(h,fmt,vl);
  143. va_end(vl);
  144. return s;
  145. }
  146. cmChar_t* cmTsVPrintf( const cmChar_t* fmt, va_list vl )
  147. { return cmTextSysVPrintf(_cmTextSysGlobalH,fmt,vl); }
  148. cmChar_t* cmTsPrintf( const cmChar_t* fmt, ... )
  149. {
  150. va_list vl;
  151. va_start(vl,fmt);
  152. cmChar_t* s = cmTsVPrintf(fmt,vl);
  153. va_end(vl);
  154. return s;
  155. }
  156. void cmTsFreeStr( const cmChar_t* s )
  157. { cmTextSysFreeStr(_cmTextSysGlobalH,s); }
  158. cmChar_t* cmTsVPrintfP( cmChar_t* s, const cmChar_t* fmt, va_list vl )
  159. {
  160. va_list vl1;
  161. va_copy(vl1,vl);
  162. int n = vsnprintf(NULL,0,fmt,vl);
  163. assert(n != -1);
  164. s = cmMemResize(cmChar_t,s,n+1);
  165. unsigned m = vsnprintf(s,n+1,fmt,vl1);
  166. assert(m==n);
  167. s[n] = 0;
  168. return s;
  169. }
  170. cmChar_t* cmTsPrintfP( cmChar_t* s, const cmChar_t* fmt, ... )
  171. {
  172. va_list vl;
  173. va_start(vl,fmt);
  174. s = cmTsVPrintfP(s,fmt,vl);
  175. va_end(vl);
  176. return s;
  177. }
  178. void _cmTxError( cmErr_t* err, cmTxRC_t rc, const char* fmt, ... )
  179. {
  180. va_list vl;
  181. va_start(vl,fmt);
  182. cmErrVMsg(err,rc,fmt,vl);
  183. va_end(vl);
  184. }
  185. cmTxRC_t _cmTxRptError( cmErr_t* err, const char* msg, const char* inputText )
  186. {
  187. if( err == NULL )
  188. return kOkTxRC;
  189. if( inputText == NULL )
  190. {
  191. _cmTxError(err,kNullTxRC,"Text to %s conversion failed due to NULL input text.");
  192. return kNullTxRC;
  193. }
  194. if( errno != 0 )
  195. {
  196. _cmTxError(err,kCvtErrTxRC,"Text to %s conversion failed on input '%s'.",msg,inputText);
  197. return kCvtErrTxRC;
  198. }
  199. return kOkTxRC;
  200. }
  201. cmTxRC_t cmTextToInt(const char* text, int* vp, cmErr_t* err )
  202. {
  203. assert( vp != NULL );
  204. cmTxRC_t rc = kOkTxRC;
  205. int en = errno;
  206. errno = 0;
  207. *vp = text==NULL ? 0 : strtol(text,NULL,0);
  208. rc = _cmTxRptError(err,"integer",text);
  209. errno = en;
  210. return rc;
  211. }
  212. cmTxRC_t cmTextToUInt( const char* text, unsigned* vp, cmErr_t* err )
  213. {
  214. assert( vp != NULL );
  215. cmTxRC_t rc = kOkTxRC;
  216. int en = errno;
  217. errno = 0;
  218. *vp = text==NULL ? 0 : (unsigned)strtol(text,NULL,0);
  219. rc = _cmTxRptError(err,"unsigned integer",text);
  220. errno = en;
  221. return rc;
  222. }
  223. cmTxRC_t cmTextToFloat( const char* text, float* vp, cmErr_t* err )
  224. {
  225. assert( vp != NULL );
  226. cmTxRC_t rc = kOkTxRC;
  227. int en = errno;
  228. errno = 0;
  229. *vp = text==NULL ? 0 : (float)strtod(text,NULL);
  230. rc = _cmTxRptError(err,"float",text);
  231. errno = en;
  232. return rc;
  233. }
  234. cmTxRC_t cmTextToDouble( const char* text, double* vp, cmErr_t* err )
  235. {
  236. assert( vp != NULL );
  237. cmTxRC_t rc = kOkTxRC;
  238. int en = errno;
  239. errno = 0;
  240. *vp = text==NULL ? 0 : strtod(text,NULL);
  241. rc = _cmTxRptError(err,"double",text);
  242. errno = en;
  243. return rc;
  244. }
  245. cmTxRC_t cmTextToBool( const char* text, bool* vp, cmErr_t* err )
  246. {
  247. assert( vp != NULL );
  248. cmTxRC_t rc = kOkTxRC;
  249. if( strcasecmp(text,"true") == 0 || strcasecmp(text,"0") == 0 )
  250. *vp = true;
  251. else
  252. if( strcasecmp(text,"false") == 0 || strcasecmp(text,"1") == 0 )
  253. *vp = false;
  254. else
  255. rc = _cmTxRptError(err,"bool",text);
  256. return rc;
  257. }
  258. cmChar_t* cmTextNextNonWhiteOrEos( cmChar_t* s )
  259. {
  260. assert( s != NULL );
  261. while( (*s) && isspace(*s) )
  262. ++s;
  263. return s;
  264. }
  265. const cmChar_t* cmTextNextNonWhiteOrEosC( const cmChar_t* s )
  266. { return cmTextNextNonWhiteOrEos((cmChar_t*)s); }
  267. cmChar_t* cmTextNextNonWhite( cmChar_t* s )
  268. { //return (*(s=cmTextNextNonWhiteOrEos(s))) == 0 ? NULL : s;
  269. s=cmTextNextNonWhiteOrEos(s);
  270. if( *s == 0 )
  271. return NULL;
  272. return s;
  273. }
  274. const cmChar_t* cmTextNextNonWhiteC( const cmChar_t* s )
  275. { return cmTextNextNonWhite((cmChar_t*)s); }
  276. cmChar_t* cmTextPrevNonWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 )
  277. {
  278. assert( s0!=NULL && s1!=NULL && s0 <= s1 );
  279. for(; s0 < s1; --s1 )
  280. if( !isspace(*s1) )
  281. break;
  282. return (cmChar_t*)s1;
  283. }
  284. const cmChar_t* cmTextPrevNonWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 )
  285. { return cmTextPrevNonWhiteOrBos((cmChar_t*)s0,s1); }
  286. cmChar_t* cmTextPrevNonWhite( cmChar_t* s0, const cmChar_t* s1 )
  287. {
  288. cmChar_t* s2;
  289. if((s2 = cmTextPrevNonWhiteOrBos(s0,s1)) == s0 )
  290. return NULL;
  291. return s2;
  292. }
  293. const cmChar_t* cmTextPrevNonWhiteC( const cmChar_t* s0, const cmChar_t* s1 )
  294. { return cmTextPrevNonWhite((cmChar_t*)s0,s1); }
  295. cmChar_t* cmTextNextWhiteOrEos( cmChar_t* s )
  296. {
  297. assert( s!=NULL);
  298. while(*s && !isspace(*s) )
  299. ++s;
  300. return s;
  301. }
  302. const cmChar_t* cmTextNextWhiteOrEosC( const cmChar_t* s )
  303. { return cmTextNextWhiteOrEos((cmChar_t*)s); }
  304. cmChar_t* cmTextNextWhite( cmChar_t* s )
  305. { return (*(s=cmTextNextWhiteOrEos(s)))!=0 ? s : NULL; }
  306. const cmChar_t* cmTextNextWhiteC( const cmChar_t* s )
  307. { return cmTextNextWhite((cmChar_t*)s); }
  308. cmChar_t* cmTextPrevWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 )
  309. {
  310. assert( s0!=NULL && s1!=NULL && s0 <= s1 );
  311. while( s1>s0 && !isspace(*s1) )
  312. --s1;
  313. return (cmChar_t*)s1;
  314. }
  315. const cmChar_t* cmTextPrevWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 )
  316. { return cmTextPrevWhiteOrBos((cmChar_t*)s0,s1); }
  317. cmChar_t* cmTextPrevWhite( cmChar_t* s0, const cmChar_t* s1 )
  318. {
  319. cmChar_t* s2;
  320. if((s2 = cmTextPrevWhiteOrBos(s0,s1)) == s0 )
  321. return NULL;
  322. return s2;
  323. }
  324. const cmChar_t* cmTextPrevWhiteC( const cmChar_t* s0, const cmChar_t* s1 )
  325. { return cmTextPrevWhite((cmChar_t*)s0,s1); }
  326. cmChar_t* cmTextBegOfLine( cmChar_t* s0, const cmChar_t* s1 )
  327. {
  328. assert( s1!=NULL && s0!=NULL && s1 >= s0 );
  329. if( s0 == s1 )
  330. return s0;
  331. --s1;
  332. while( s1>s0 && *s1 != '\n' )
  333. --s1;
  334. if( *s1 == '\n' )
  335. ++s1;
  336. return (cmChar_t*)s1;
  337. }
  338. const cmChar_t* cmTextBegOfLineC( const cmChar_t* s0, const cmChar_t* s1 )
  339. { return cmTextBegOfLine((cmChar_t*)s0,s1); }
  340. cmChar_t* cmTextEndOfLine( cmChar_t* s )
  341. {
  342. assert( s!=NULL);
  343. while( *s!=0 && *s != '\n' )
  344. ++s;
  345. return s;
  346. }
  347. const cmChar_t* cmTextEndOfLineC( const cmChar_t* s )
  348. { return cmTextEndOfLine((cmChar_t*)s); }
  349. cmChar_t* cmTextLastNonWhiteChar( const cmChar_t* s )
  350. {
  351. unsigned n;
  352. if(s==NULL || (n = strlen(s)) == 0 )
  353. return NULL;
  354. cmChar_t* s0 = (cmChar_t*)s + n-1;
  355. for(; s0>=s; --s0)
  356. if( !isspace(*s0) )
  357. return s0;
  358. return NULL;
  359. }
  360. const cmChar_t* cmTextLastNonWhiteCharC( const cmChar_t* s )
  361. { return cmTextLastNonWhiteChar(s); }
  362. void cmTextShrinkS( cmChar_t* s, const cmChar_t* t, unsigned tn )
  363. { cmVOC_Shrink(s,strlen(s)+1,t,tn); }
  364. void cmTextShrinkSN(cmChar_t* s, unsigned sn, const cmChar_t* t, unsigned tn )
  365. { cmVOC_Shrink(s,sn,t,tn); }
  366. void cmTextClip( cmChar_t* s, unsigned n )
  367. {
  368. if( n == 0 || s == NULL || strlen(s)==0 )
  369. return;
  370. if( n >= strlen(s) )
  371. {
  372. s[0]=0;
  373. return;
  374. }
  375. s[ strlen(s)-n ] = 0;
  376. }
  377. cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn )
  378. { return cmVOC_Expand(s,strlen(s)+1,t,tn); }
  379. cmChar_t* cmTextReplaceSN( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u, unsigned un )
  380. {
  381. unsigned n = strlen(s)+1;
  382. return cmVOC_Replace(s,&n,t,tn,u,un);
  383. }
  384. cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u )
  385. { return cmTextReplaceSN(s,t,tn,u,u==NULL ? 0 : strlen(u)); }
  386. cmChar_t* _cmTextReplace( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned n )
  387. {
  388. // we will go into an endless loop if 't' is contained in 'u' and n > 1.
  389. assert( s!= NULL && t!=NULL && u!=NULL && (n==1 || strstr(u,t) == NULL) );
  390. int tn = strlen(t);
  391. cmChar_t* c = NULL;
  392. unsigned i = 0;
  393. while( (c = strstr(s,t)) != NULL )
  394. {
  395. s = cmTextReplaceS(s,c,tn,u);
  396. assert(s!=NULL);
  397. ++i;
  398. if( n!=cmInvalidCnt && i>=n)
  399. break;
  400. };
  401. return s;
  402. }
  403. cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
  404. { return _cmTextReplace(s,t,u,cmInvalidCnt); }
  405. cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
  406. { return _cmTextReplace(s,t,u,1); }
  407. cmChar_t* cmTextInsertSN( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un )
  408. {
  409. unsigned n = strlen(s)+1;
  410. return cmVOC_Replace(s,&n,t,0,u,un);
  411. }
  412. cmChar_t* cmTextInsertS( cmChar_t* s, const cmChar_t* t, const cmChar_t* u )
  413. { return cmTextInsertSN(s,t,u,u==NULL?0:strlen(u)); }
  414. cmChar_t* cmTextAppend( cmChar_t* s, unsigned* sn, const cmChar_t* u, unsigned un )
  415. { return cmVOC_Replace(s,sn,s+(*sn),0,u,un); }
  416. cmChar_t* cmTextAppendSN( cmChar_t* s, const cmChar_t* u, unsigned un )
  417. {
  418. unsigned sn = s==NULL ? 0 : strlen(s);
  419. if( un > 0 )
  420. {
  421. s = cmTextAppend(s,&sn,u,un+1);
  422. s[sn-1] = 0;
  423. }
  424. return s;
  425. }
  426. // append u[un] to s[] and append terminating zero.
  427. cmChar_t* cmTextAppendSNZ( cmChar_t* s, const cmChar_t* u, unsigned un )
  428. {
  429. unsigned sn = s==NULL ? 0 : strlen(s)+1;
  430. cmChar_t z = 0;
  431. s = cmTextAppend(s,&sn,u,un);
  432. return cmTextAppend(s,&sn,&z,1);
  433. }
  434. // both s[] and u[] are strz's
  435. cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u )
  436. { return cmTextAppendSN(s,u,strlen(u)); }
  437. cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n )
  438. {
  439. if( n <= 0 )
  440. return s;
  441. cmChar_t t[ n+1 ];
  442. memset(t,' ',n);
  443. t[n] = 0;
  444. return cmTextAppendSS(s,t);
  445. }
  446. bool cmTextIsEmpty( const cmChar_t* s )
  447. {
  448. if( s!=NULL )
  449. for(; *s; ++s )
  450. if( !isspace(*s) )
  451. return false;
  452. return true;
  453. }
  454. bool cmTextIsNotEmpty( const cmChar_t* s )
  455. { return !cmTextIsEmpty(s); }
  456. cmChar_t* cmTextLine( cmChar_t* s, unsigned line )
  457. {
  458. assert( line>0);
  459. unsigned i;
  460. // count down to the code line containing the tag reference
  461. for(i=0; i<line-1; ++i)
  462. {
  463. s = strchr(s,'\n');
  464. if( s == NULL )
  465. return NULL;
  466. ++s;
  467. }
  468. return s+1;
  469. }
  470. const cmChar_t* cmTextLineC( const cmChar_t* s, unsigned line )
  471. { return cmTextLine((cmChar_t*)s,line); }
  472. cmChar_t* cmTextRemoveConsecutiveSpaces( cmChar_t* s )
  473. {
  474. if( s==NULL || strlen(s) < 2 )
  475. return s;
  476. int i=1;
  477. while( s[i] )
  478. {
  479. if( isspace(s[i-1]) && isspace(s[i]) )
  480. cmTextShrinkS(s, s+i, 1 );
  481. else
  482. ++i;
  483. }
  484. return s;
  485. }
  486. cmChar_t* cmTextColumize( cmChar_t* s, unsigned colCnt )
  487. {
  488. if( s==NULL || strlen(s) < colCnt )
  489. return s;
  490. int i = 0;
  491. int c = 0;
  492. cmChar_t* c0 = NULL;
  493. for(; s[i]; ++i)
  494. {
  495. // remove any existing newlines
  496. if( s[i] == '\n' )
  497. s[i] = ' ';
  498. // track the last space (which is a potential wrap point).
  499. if( isspace(s[i]) )
  500. c0 = s+i;
  501. if( c < colCnt )
  502. ++c;
  503. else
  504. {
  505. // if there is no previous wrap point ...
  506. if( c0 == NULL )
  507. {
  508. // ... then insert one
  509. s = cmTextInsertS(s,s+i,"\n");
  510. }
  511. else
  512. {
  513. // replace the wrap point with a '\n'
  514. *c0 = '\n';
  515. }
  516. c = 0;
  517. c0 = NULL;
  518. }
  519. }
  520. return s;
  521. }
  522. cmChar_t* _cmTextPrefixRows( cmChar_t* s, const cmChar_t* t )
  523. {
  524. if( s==NULL || t==NULL || strlen(t)==0 )
  525. return s;
  526. int i;
  527. for(i=0; s[i]; ++i)
  528. if( i==0 || s[i]=='\n')
  529. {
  530. cmChar_t* u = s + (i==0 ? 0 : i+1);
  531. s = cmTextInsertS(s,u,t);
  532. }
  533. return s;
  534. }
  535. cmChar_t* cmTextIndentRows( cmChar_t* s, unsigned indent )
  536. {
  537. if( s==NULL || indent==0 )
  538. return s;
  539. cmChar_t* t = cmMemAllocZ( cmChar_t, indent+1 );
  540. cmVOC_Fill(t,indent,' ');
  541. t[indent] = 0;
  542. s = _cmTextPrefixRows(s,t);
  543. cmMemFree(t);
  544. return s;
  545. }
  546. cmChar_t* cmTextPrefixRows( cmChar_t* s, const cmChar_t* t )
  547. { return _cmTextPrefixRows(s,t); }
  548. cmChar_t* cmTextTrimRows( cmChar_t* s )
  549. {
  550. bool fl = true;
  551. int i = 0;
  552. while( s[i] )
  553. {
  554. if( s[i] == '\n' )
  555. {
  556. fl = true;
  557. ++i;
  558. }
  559. else
  560. {
  561. if( isspace(s[i]) && fl )
  562. cmTextShrinkS(s, s+i, 1 );
  563. else
  564. {
  565. fl = false;
  566. ++i;
  567. }
  568. }
  569. }
  570. return s;
  571. }
  572. cmChar_t* cmTextEatLeadingSpace( cmChar_t* s )
  573. {
  574. if( s == NULL )
  575. return s;
  576. while( *s )
  577. {
  578. if( !isspace(*s) )
  579. break;
  580. cmTextShrinkS(s,s,1);
  581. }
  582. return s;
  583. }