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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // Return true if 's' is stored in the text systems internal heap,
  46. bool cmTextSysIsStored(cmTextSysH_t h, const cmChar_t* s );
  47. //
  48. // Global interface:
  49. //
  50. // These functions are equivalent to the same-named functions above
  51. // but use a globally allocated cmTextSysH_t handle.
  52. cmTxRC_t cmTsInitialize( cmCtx_t* ctx );
  53. cmTxRC_t cmTsFinalize();
  54. bool cmTsIsValid();
  55. cmChar_t* cmTsVPrintfS( const cmChar_t* fmt, va_list vl );
  56. cmChar_t* cmTsPrintfS( const cmChar_t* fmt, ... );
  57. cmChar_t* cmTsVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl );
  58. cmChar_t* cmTsPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... );
  59. cmChar_t* cmTsVPrintf( const cmChar_t* fmt, va_list vl );
  60. cmChar_t* cmTsPrintf( const cmChar_t* fmt, ... );
  61. void cmTsFreeStr( const cmChar_t* s );
  62. bool cmTsIsStored(const cmChar_t* s );
  63. // Print a formatted string into s[]. s[] is reallocated as necessary to
  64. // hold the string. s must be freed by the caller via cmMemFree().
  65. cmChar_t* cmTsVPrintfP( cmChar_t* s, const cmChar_t* fmt, va_list vl );
  66. cmChar_t* cmTsPrintfP( cmChar_t* s, const cmChar_t* fmt, ... );
  67. // Set err to NULL to ignore errors.
  68. //
  69. cmTxRC_t cmTextToInt( const char* text, int* ip, cmErr_t* err );
  70. cmTxRC_t cmTextToUInt( const char* text, unsigned* up, cmErr_t* err );
  71. cmTxRC_t cmTextToFloat( const char* text, float* fp, cmErr_t* err );
  72. cmTxRC_t cmTextToDouble( const char* text, double* dp, cmErr_t* err );
  73. cmTxRC_t cmTextToBool( const char* text, bool* bp, cmErr_t* err );
  74. // In all cases s, s0 and s1 are expected to be non-NULL.
  75. // If any of the arguments were allowed to be NULL, then the natural
  76. // return value would be NULL and this would lead to ambiguities
  77. // with the meaning of a NULL return value.
  78. // Return ptr to next non-white or terminating 0 if EOS is encountered.
  79. cmChar_t* cmTextNextNonWhiteOrEos( cmChar_t* s );
  80. const cmChar_t* cmTextNextNonWhiteOrEosC( const cmChar_t* s );
  81. // Return ptr to next non-white char. or NULL if EOS is encountered.
  82. cmChar_t* cmTextNextNonWhite( cmChar_t* s );
  83. const cmChar_t* cmTextNextNonWhiteC( const cmChar_t* s );
  84. // Return ptr to prev non-white or s if BOS is encountered.
  85. // If returned ptr == s0 then BOS was encountered.
  86. cmChar_t* cmTextPrevNonWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 );
  87. const cmChar_t* cmTextPrevNonWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 );
  88. // Return ptr to prev non-white char. or NULL if BOS is encountered.
  89. cmChar_t* cmTextPrevNonWhite( cmChar_t* s0, const cmChar_t* s1 );
  90. const cmChar_t* cmTextPrevNonWhiteC( const cmChar_t* s0, const cmChar_t* s1 );
  91. // Return ptr to next white or terminating 0 if EOS is encountered.
  92. // ( *(return_ptr)==0 if EOS is encountered )
  93. cmChar_t* cmTextNextWhiteOrEos( cmChar_t* s );
  94. const cmChar_t* cmTextNextWhiteOrEosC( const cmChar_t* s );
  95. // Return ptr to next white char. or NULL if EOS is encountered.
  96. cmChar_t* cmTextNextWhite( cmChar_t* s );
  97. const cmChar_t* cmTextNextWhiteC( const cmChar_t* s );
  98. // Return ptr to prev white or s if BOS is encountered.
  99. // If returned ptr != s then *(returned_ptr-1) == '\n'
  100. cmChar_t* cmTextPrevWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 );
  101. const cmChar_t* cmTextPrevWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 );
  102. // Return ptr to prev white char. or NULL if BOS is encountered.
  103. cmChar_t* cmTextPrevWhite( cmChar_t* s0, const cmChar_t* s1 );
  104. const cmChar_t* cmTextPrevWhiteC( const cmChar_t* s0, const cmChar_t* s1 );
  105. // Backup to first char on line.
  106. // Return ptr to first char on line or first char in buffer.
  107. // If return_ptr!=s0 then *(return_ptr-1) == '\n'.
  108. cmChar_t* cmTextBegOfLine( cmChar_t* s0, const cmChar_t* s1 );
  109. const cmChar_t* cmTextBegOfLineC( const cmChar_t* s0, const cmChar_t* s1 );
  110. // Move forward to last char on line.
  111. // Return ptr to last char on line.
  112. // If *return_ptr == 0 then the end of buffer was encountered prior
  113. // to the next '\n' otherwise *(return_ptr) == '\n'.
  114. cmChar_t* cmTextEndOfLine( cmChar_t* s );
  115. const cmChar_t* cmTextEndOfLineC( const cmChar_t* s );
  116. // Return a pointer to the last non-white character in the string
  117. cmChar_t* cmTextLastNonWhiteChar( const cmChar_t* s );
  118. const cmChar_t* cmTextLastNonWhiteCharC( const cmChar_t* s );
  119. // Shrink the text by copying all char's beginning with t+tn to t.
  120. // ex: 'abcd' s=a, t=b, tn=1 -> acd
  121. // 'abcd' s=a, t=b, tn=2 -> ad
  122. void cmTextShrinkS( cmChar_t* s, const cmChar_t* t, unsigned tn );
  123. void cmTextShrinkSN(cmChar_t* s, unsigned sn, const cmChar_t* t, unsigned tn );
  124. // Remove the last n characters from s by inserting a '\0' at s[ strlen(s)-n ].
  125. void cmTextClip( cmChar_t* s, unsigned n );
  126. // Expand s by copying all bytes past t to t+tn.
  127. cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn );
  128. // Replace t[tn] with u[un] in s[].
  129. cmChar_t* cmTextReplaceSN( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u, unsigned un );
  130. // Replace t[tn] with u in s[].
  131. cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u );
  132. // Replace all occurrences of t[] with u[] in s[].
  133. cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  134. // Replace the first occurence of t[] in s[] with u[].
  135. cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  136. // Insert u[un] at before t in s[].
  137. cmChar_t* cmTextInsertSN( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un );
  138. // Insert u[] at before t in s[].
  139. cmChar_t* cmTextInsertS( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  140. cmChar_t* cmTextAppend( cmChar_t* s, unsigned* sn, const cmChar_t* u, unsigned un );
  141. cmChar_t* cmTextAppendSN( cmChar_t* s, const cmChar_t* u, unsigned un );
  142. // append u[un] to s[] and append terminating zero.
  143. cmChar_t* cmTextAppendSNZ( cmChar_t* s, const cmChar_t* u, unsigned un );
  144. // both s[] and u[] are strz's
  145. cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u );
  146. // Append 'n' copies of 'c' to the end of s[].
  147. cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n );
  148. // Returns true if the string is NULL or all spaces.
  149. bool cmTextIsEmpty( const cmChar_t* s );
  150. bool cmTextIsNotEmpty( const cmChar_t* s );
  151. // Same as strcmp() but handles NULL. Note that if both s0 and s1 are NULL
  152. // then return is 0.
  153. int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 );
  154. // Returns NULL if string contains fewer than lineIdx lines.
  155. // Note: first line == 1.
  156. cmChar_t* cmTextLine( cmChar_t* s, unsigned line );
  157. const cmChar_t* cmTextLineC( const cmChar_t* s, unsigned line );
  158. // Reduce all consecutive white spaces to a single space.
  159. cmChar_t* cmTextRemoveConsecutiveSpaces( cmChar_t* s );
  160. // Columnize s[] by inserting EOL's at the first available
  161. // space prior 'colCnt' columns. If no space exists then
  162. // the words will be broken. If length s[] is less than
  163. // 'colCnt' then s[] is returned with no changes.
  164. cmChar_t* cmTextColumize( cmChar_t* s, unsigned colCnt );
  165. // Indent the rows of s[] by inserting 'indent' spaces
  166. // just after each '\n'. If s[] contains no '\n' then
  167. // s[] is returned with no changes.
  168. cmChar_t* cmTextIndentRows( cmChar_t* s, unsigned indent );
  169. // Prefix all rows of s[] with p[].
  170. cmChar_t* cmTextPrefixRows( cmChar_t* s, const cmChar_t* t );
  171. // Remove leading white space from all rows of s.
  172. cmChar_t* cmTextTrimRows( cmChar_t* s );
  173. // Remove all white space prior to the first non-white space
  174. // character.
  175. cmChar_t* cmTextEatLeadingSpace( cmChar_t* s );
  176. //)
  177. //}
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif