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.h 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef cmText_h
  2. #define cmText_h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. //{
  7. //(
  8. // The cmText API supports two basic tasks: the generation of
  9. // formatted text into dynamic arrays and text to number parsing.
  10. //
  11. //)
  12. //(
  13. enum
  14. {
  15. kOkTxRC = 0,
  16. kNullTxRC,
  17. kCvtErrTxRC,
  18. kLHeapFailTxRC
  19. };
  20. typedef unsigned cmTxRC_t;
  21. typedef cmHandle_t cmTextSysH_t;
  22. extern cmTextSysH_t cmTextSysNullHandle;
  23. cmTxRC_t cmTextSysInitialize( cmCtx_t* ctx, cmTextSysH_t* hp );
  24. cmTxRC_t cmTextSysFinalize( cmTextSysH_t* hp );
  25. bool cmTextSysIsValid( cmTextSysH_t h );
  26. // Print the string into a static, global, but resizable, buffer.
  27. // This string is only valid until the next call to cmTextSysPrintS().
  28. // This function is not currently thread safe - calls from different
  29. // threads can therefore overwrite one anothers buffered strings
  30. // unexpectedly.
  31. cmChar_t* cmTextSysVPrintfS( cmTextSysH_t h, const cmChar_t* fmt, va_list vl );
  32. cmChar_t* cmTextSysPrintfS( cmTextSysH_t h, const cmChar_t* fmt, ... );
  33. // Print the string into memory supplied by a linked heap.
  34. // Strings returned from this function may be deallocated with a call to
  35. // cmLhFree().
  36. cmChar_t* cmTextSysVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl );
  37. cmChar_t* cmTextSysPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... );
  38. // Print a string and store it in the internal text system heap.
  39. // The memory used by these strings will be automatically released
  40. // whent he text system is destroyed. The caller may optionally release
  41. // a string via cmTextSysFreeStr().
  42. cmChar_t* cmTextSysVPrintf( cmTextSysH_t h, const cmChar_t* fmt, va_list vl );
  43. cmChar_t* cmTextSysPrintf( cmTextSysH_t h, const cmChar_t* fmt, ... );
  44. void cmTextSysFreeStr( cmTextSysH_t h, const cmChar_t* s );
  45. //
  46. // Global interface:
  47. //
  48. // These functions are equivalent to the same-named functions above
  49. // but use a globally allocated cmTextSysH_t handle.
  50. cmTxRC_t cmTsInitialize( cmCtx_t* ctx );
  51. cmTxRC_t cmTsFinalize();
  52. bool cmTsIsValid();
  53. cmChar_t* cmTsVPrintfS( const cmChar_t* fmt, va_list vl );
  54. cmChar_t* cmTsPrintfS( const cmChar_t* fmt, ... );
  55. cmChar_t* cmTsVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl );
  56. cmChar_t* cmTsPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... );
  57. cmChar_t* cmTsVPrintf( const cmChar_t* fmt, va_list vl );
  58. cmChar_t* cmTsPrintf( const cmChar_t* fmt, ... );
  59. void cmTsFreeStr( const cmChar_t* s );
  60. // Print a formatted string into s[]. s[] is reallocated as necessary to
  61. // hold the string. s must be freed by the caller via cmMemFree().
  62. cmChar_t* cmTsVPrintfP( cmChar_t* s, const cmChar_t* fmt, va_list vl );
  63. cmChar_t* cmTsPrintfP( cmChar_t* s, const cmChar_t* fmt, ... );
  64. // Set err to NULL to ignore errors.
  65. //
  66. cmTxRC_t cmTextToInt( const char* text, int* ip, cmErr_t* err );
  67. cmTxRC_t cmTextToUInt( const char* text, unsigned* up, cmErr_t* err );
  68. cmTxRC_t cmTextToFloat( const char* text, float* fp, cmErr_t* err );
  69. cmTxRC_t cmTextToDouble( const char* text, double* dp, cmErr_t* err );
  70. cmTxRC_t cmTextToBool( const char* text, bool* bp, cmErr_t* err );
  71. // In all cases s, s0 and s1 are expected to be non-NULL.
  72. // If any of the arguments were allowed to be NULL, then the natural
  73. // return value would be NULL and this would lead to ambiguities
  74. // with the meaning of a NULL return value.
  75. // Return ptr to next non-white or terminating 0 if EOS is encountered.
  76. cmChar_t* cmTextNextNonWhiteOrEos( cmChar_t* s );
  77. const cmChar_t* cmTextNextNonWhiteOrEosC( const cmChar_t* s );
  78. // Return ptr to next non-white char. or NULL if EOS is encountered.
  79. cmChar_t* cmTextNextNonWhite( cmChar_t* s );
  80. const cmChar_t* cmTextNextNonWhiteC( const cmChar_t* s );
  81. // Return ptr to prev non-white or s if BOS is encountered.
  82. // If returned ptr == s0 then BOS was encountered.
  83. cmChar_t* cmTextPrevNonWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 );
  84. const cmChar_t* cmTextPrevNonWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 );
  85. // Return ptr to prev non-white char. or NULL if BOS is encountered.
  86. cmChar_t* cmTextPrevNonWhite( cmChar_t* s0, const cmChar_t* s1 );
  87. const cmChar_t* cmTextPrevNonWhiteC( const cmChar_t* s0, const cmChar_t* s1 );
  88. // Return ptr to next white or terminating 0 if EOS is encountered.
  89. // ( *(return_ptr)==0 if EOS is encountered )
  90. cmChar_t* cmTextNextWhiteOrEos( cmChar_t* s );
  91. const cmChar_t* cmTextNextWhiteOrEosC( const cmChar_t* s );
  92. // Return ptr to next white char. or NULL if EOS is encountered.
  93. cmChar_t* cmTextNextWhite( cmChar_t* s );
  94. const cmChar_t* cmTextNextWhiteC( const cmChar_t* s );
  95. // Return ptr to prev white or s if BOS is encountered.
  96. // If returned ptr != s then *(returned_ptr-1) == '\n'
  97. cmChar_t* cmTextPrevWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 );
  98. const cmChar_t* cmTextPrevWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 );
  99. // Return ptr to prev white char. or NULL if BOS is encountered.
  100. cmChar_t* cmTextPrevWhite( cmChar_t* s0, const cmChar_t* s1 );
  101. const cmChar_t* cmTextPrevWhiteC( const cmChar_t* s0, const cmChar_t* s1 );
  102. // Backup to first char on line.
  103. // Return ptr to first char on line or first char in buffer.
  104. // If return_ptr!=s0 then *(return_ptr-1) == '\n'.
  105. cmChar_t* cmTextBegOfLine( cmChar_t* s0, const cmChar_t* s1 );
  106. const cmChar_t* cmTextBegOfLineC( const cmChar_t* s0, const cmChar_t* s1 );
  107. // Move forward to last char on line.
  108. // Return ptr to last char on line.
  109. // If *return_ptr == 0 then the end of buffer was encountered prior
  110. // to the next '\n' otherwise *(return_ptr) == '\n'.
  111. cmChar_t* cmTextEndOfLine( cmChar_t* s );
  112. const cmChar_t* cmTextEndOfLineC( const cmChar_t* s );
  113. // Return a pointer to the last non-white character in the string
  114. cmChar_t* cmTextLastNonWhiteChar( const cmChar_t* s );
  115. const cmChar_t* cmTextLastNonWhiteCharC( const cmChar_t* s );
  116. // Shrink the text by copying all char's beginning with t+tn to t.
  117. // ex: 'abcd' s=a, t=b, tn=1 -> acd
  118. // 'abcd' s=a, t=b, tn=2 -> ad
  119. void cmTextShrinkS( cmChar_t* s, const cmChar_t* t, unsigned tn );
  120. void cmTextShrinkSN(cmChar_t* s, unsigned sn, const cmChar_t* t, unsigned tn );
  121. // Remove the last n characters from s by inserting a '\0' at s[ strlen(s)-n ].
  122. void cmTextClip( cmChar_t* s, unsigned n );
  123. // Expand s by copying all bytes past t to t+tn.
  124. cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn );
  125. // Replace t[tn] with u[un] in s[].
  126. cmChar_t* cmTextReplaceSN( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u, unsigned un );
  127. // Replace t[tn] with u in s[].
  128. cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u );
  129. // Replace all occurrences of t[] with u[] in s[].
  130. cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  131. // Replace the first occurence of t[] in s[] with u[].
  132. cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  133. // Insert u[un] at before t in s[].
  134. cmChar_t* cmTextInsertSN( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un );
  135. // Insert u[] at before t in s[].
  136. cmChar_t* cmTextInsertS( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  137. cmChar_t* cmTextAppend( cmChar_t* s, unsigned* sn, const cmChar_t* u, unsigned un );
  138. cmChar_t* cmTextAppendSN( cmChar_t* s, const cmChar_t* u, unsigned un );
  139. // append u[un] to s[] and append terminating zero.
  140. cmChar_t* cmTextAppendSNZ( cmChar_t* s, const cmChar_t* u, unsigned un );
  141. // both s[] and u[] are strz's
  142. cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u );
  143. // Append 'n' copies of 'c' to the end of s[].
  144. cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n );
  145. // Returns true if the string is all spaces.
  146. bool cmTextIsEmpty( const cmChar_t* s );
  147. // Returns NULL if string contains fewer than lineIdx lines.
  148. // Note: first line == 1.
  149. cmChar_t* cmTextLine( cmChar_t* s, unsigned line );
  150. const cmChar_t* cmTextLineC( const cmChar_t* s, unsigned line );
  151. // Reduce all consecutive white spaces to a single space.
  152. cmChar_t* cmTextRemoveConsecutiveSpaces( cmChar_t* s );
  153. // Columnize s[] by inserting EOL's at the first available
  154. // space prior 'colCnt' columns. If no space exists then
  155. // the words will be broken. If length s[] is less than
  156. // 'colCnt' then s[] is returned with no changes.
  157. cmChar_t* cmTextColumize( cmChar_t* s, unsigned colCnt );
  158. // Indent the rows of s[] by inserting 'indent' spaces
  159. // just after each '\n'. If s[] contains no '\n' then
  160. // s[] is returned with no changes.
  161. cmChar_t* cmTextIndentRows( cmChar_t* s, unsigned indent );
  162. // Prefix all rows of s[] with p[].
  163. cmChar_t* cmTextPrefixRows( cmChar_t* s, const cmChar_t* t );
  164. // Remove leading white space from all rows of s.
  165. cmChar_t* cmTextTrimRows( cmChar_t* s );
  166. // Remove all white space prior to the first non-white space
  167. // character.
  168. cmChar_t* cmTextEatLeadingSpace( cmChar_t* s );
  169. //)
  170. //}
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif