libcm is a C development framework with an emphasis on audio signal processing applications.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

cmText.c 15KB

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