libcm is a C development framework with an emphasis on audio signal processing applications.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmText_h
  4. #define cmText_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"Text processing functions." kw:[text]}
  9. //
  10. // The cmText API supports two basic tasks: the generation of
  11. // formatted text into dynamic arrays and text to number parsing.
  12. //
  13. //)
  14. //(
  15. enum
  16. {
  17. kOkTxRC = 0,
  18. kNullTxRC,
  19. kCvtErrTxRC,
  20. kLHeapFailTxRC,
  21. kBufTooSmallTxRC
  22. };
  23. typedef unsigned cmTxRC_t;
  24. typedef cmHandle_t cmTextSysH_t;
  25. extern cmTextSysH_t cmTextSysNullHandle;
  26. cmTxRC_t cmTextSysInitialize( cmCtx_t* ctx, cmTextSysH_t* hp );
  27. cmTxRC_t cmTextSysFinalize( cmTextSysH_t* hp );
  28. bool cmTextSysIsValid( cmTextSysH_t h );
  29. // Print the string into a static, global, but resizable, buffer.
  30. // This string is only valid until the next call to cmTextSysPrintS().
  31. // This function is not currently thread safe - calls from different
  32. // threads can therefore overwrite one anothers buffered strings
  33. // unexpectedly.
  34. cmChar_t* cmTextSysVPrintfS( cmTextSysH_t h, const cmChar_t* fmt, va_list vl );
  35. cmChar_t* cmTextSysPrintfS( cmTextSysH_t h, const cmChar_t* fmt, ... );
  36. // Print the string into memory supplied by a linked heap.
  37. // Strings returned from this function may be deallocated with a call to
  38. // cmLhFree().
  39. cmChar_t* cmTextSysVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl );
  40. cmChar_t* cmTextSysPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... );
  41. // Print a string and store it in the internal text system heap.
  42. // The memory used by these strings will be automatically released
  43. // whent he text system is destroyed. The caller may optionally release
  44. // a string via cmTextSysFreeStr().
  45. cmChar_t* cmTextSysVPrintf( cmTextSysH_t h, const cmChar_t* fmt, va_list vl );
  46. cmChar_t* cmTextSysPrintf( cmTextSysH_t h, const cmChar_t* fmt, ... );
  47. void cmTextSysFreeStr( cmTextSysH_t h, const cmChar_t* s );
  48. // Return true if 's' is stored in the text systems internal heap,
  49. bool cmTextSysIsStored(cmTextSysH_t h, const cmChar_t* s );
  50. //
  51. // Global interface:
  52. //
  53. // These functions are equivalent to the same-named functions above
  54. // but use a globally allocated cmTextSysH_t handle.
  55. cmTxRC_t cmTsInitialize( cmCtx_t* ctx );
  56. cmTxRC_t cmTsFinalize();
  57. bool cmTsIsValid();
  58. cmChar_t* cmTsVPrintfS( const cmChar_t* fmt, va_list vl );
  59. cmChar_t* cmTsPrintfS( const cmChar_t* fmt, ... );
  60. cmChar_t* cmTsVPrintfH( cmLHeapH_t h, const cmChar_t* fmt, va_list vl );
  61. cmChar_t* cmTsPrintfH( cmLHeapH_t h, const cmChar_t* fmt, ... );
  62. cmChar_t* cmTsVPrintf( const cmChar_t* fmt, va_list vl );
  63. cmChar_t* cmTsPrintf( const cmChar_t* fmt, ... );
  64. void cmTsFreeStr( const cmChar_t* s );
  65. bool cmTsIsStored(const cmChar_t* s );
  66. // Print a formatted string into s[]. s[] is reallocated as necessary to
  67. // hold the string. s must be freed by the caller via cmMemFree().
  68. cmChar_t* cmTsVPrintfP( cmChar_t* s, const cmChar_t* fmt, va_list vl );
  69. cmChar_t* cmTsPrintfP( cmChar_t* s, const cmChar_t* fmt, ... );
  70. // Set err to NULL to ignore errors.
  71. //
  72. cmTxRC_t cmTextToInt( const char* text, int* ip, cmErr_t* err );
  73. cmTxRC_t cmTextToUInt( const char* text, unsigned* up, cmErr_t* err );
  74. cmTxRC_t cmTextToFloat( const char* text, float* fp, cmErr_t* err );
  75. cmTxRC_t cmTextToDouble( const char* text, double* dp, cmErr_t* err );
  76. cmTxRC_t cmTextToBool( const char* text, bool* bp, cmErr_t* err );
  77. // In all cases s, s0 and s1 are expected to be non-NULL.
  78. // If any of the arguments were allowed to be NULL, then the natural
  79. // return value would be NULL and this would lead to ambiguities
  80. // with the meaning of a NULL return value.
  81. // Return ptr to next non-white or terminating 0 if EOS is encountered.
  82. cmChar_t* cmTextNextNonWhiteOrEos( cmChar_t* s );
  83. const cmChar_t* cmTextNextNonWhiteOrEosC( const cmChar_t* s );
  84. // Return ptr to next non-white char. or NULL if EOS is encountered.
  85. cmChar_t* cmTextNextNonWhite( cmChar_t* s );
  86. const cmChar_t* cmTextNextNonWhiteC( const cmChar_t* s );
  87. // Return ptr to prev non-white or s if BOS is encountered.
  88. // If returned ptr == s0 then BOS was encountered.
  89. cmChar_t* cmTextPrevNonWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 );
  90. const cmChar_t* cmTextPrevNonWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 );
  91. // Return ptr to prev non-white char. or NULL if BOS is encountered.
  92. cmChar_t* cmTextPrevNonWhite( cmChar_t* s0, const cmChar_t* s1 );
  93. const cmChar_t* cmTextPrevNonWhiteC( const cmChar_t* s0, const cmChar_t* s1 );
  94. // Return ptr to next white or terminating 0 if EOS is encountered.
  95. // ( *(return_ptr)==0 if EOS is encountered )
  96. cmChar_t* cmTextNextWhiteOrEos( cmChar_t* s );
  97. const cmChar_t* cmTextNextWhiteOrEosC( const cmChar_t* s );
  98. // Return ptr to next white char. or NULL if EOS is encountered.
  99. cmChar_t* cmTextNextWhite( cmChar_t* s );
  100. const cmChar_t* cmTextNextWhiteC( const cmChar_t* s );
  101. // Return ptr to prev white or s if BOS is encountered.
  102. // If returned ptr != s then *(returned_ptr-1) == '\n'
  103. cmChar_t* cmTextPrevWhiteOrBos( cmChar_t* s0, const cmChar_t* s1 );
  104. const cmChar_t* cmTextPrevWhiteOrBosC( const cmChar_t* s0, const cmChar_t* s1 );
  105. // Return ptr to prev white char. or NULL if BOS is encountered.
  106. cmChar_t* cmTextPrevWhite( cmChar_t* s0, const cmChar_t* s1 );
  107. const cmChar_t* cmTextPrevWhiteC( const cmChar_t* s0, const cmChar_t* s1 );
  108. // Backup to first char on line.
  109. // Return ptr to first char on line or first char in buffer.
  110. // If return_ptr!=s0 then *(return_ptr-1) == '\n'.
  111. cmChar_t* cmTextBegOfLine( cmChar_t* s0, const cmChar_t* s1 );
  112. const cmChar_t* cmTextBegOfLineC( const cmChar_t* s0, const cmChar_t* s1 );
  113. // Move forward to last char on line.
  114. // Return ptr to last char on line.
  115. // If *return_ptr == 0 then the end of buffer was encountered prior
  116. // to the next '\n' otherwise *(return_ptr) == '\n'.
  117. cmChar_t* cmTextEndOfLine( cmChar_t* s );
  118. const cmChar_t* cmTextEndOfLineC( const cmChar_t* s );
  119. // Return a pointer to the last non-white character in the string
  120. // or NULL if s is NULL or empty.
  121. cmChar_t* cmTextLastNonWhiteChar( const cmChar_t* s );
  122. const cmChar_t* cmTextLastNonWhiteCharC( const cmChar_t* s );
  123. // Return a pointer to the last white character in the string
  124. // or NULL if s is NULL or empty.
  125. cmChar_t* cmTextLastWhiteChar( const cmChar_t* s );
  126. const cmChar_t* cmTextLastWhiteCharC( const cmChar_t* s );
  127. // Shrink the text by copying all char's beginning with t+tn to t.
  128. // ex: 'abcd' s=a, t=b, tn=1 -> acd
  129. // 'abcd' s=a, t=b, tn=2 -> ad
  130. void cmTextShrinkS( cmChar_t* s, const cmChar_t* t, unsigned tn );
  131. void cmTextShrinkSN(cmChar_t* s, unsigned sn, const cmChar_t* t, unsigned tn );
  132. // Remove the last n characters from s by inserting a '\0' at s[ strlen(s)-n ].
  133. void cmTextClip( cmChar_t* s, unsigned n );
  134. // Trim white space from the begining/end/both of a string
  135. cmChar_t* cmTextTrimBegin( cmChar_t* s );
  136. cmChar_t* cmTextTrimEnd( cmChar_t* s );
  137. cmChar_t* cmTextTrim( cmChar_t* );
  138. // Expand s by copying all bytes past t to t+tn.
  139. cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn );
  140. // Replace t[tn] with u[un] in s[].
  141. cmChar_t* cmTextReplaceSN( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u, unsigned un );
  142. // Replace t[tn] with u in s[].
  143. cmChar_t* cmTextReplaceS( cmChar_t* s, const cmChar_t* t, unsigned tn, const cmChar_t* u );
  144. // Replace all occurrences of t[] with u[] in s[].
  145. cmChar_t* cmTextReplaceAll( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  146. // Replace the first occurence of t[] in s[] with u[].
  147. cmChar_t* cmTextReplaceFirst( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  148. // Insert u[un] at before t in s[].
  149. cmChar_t* cmTextInsertSN( cmChar_t* s, const cmChar_t* t, const cmChar_t* u, unsigned un );
  150. // Insert u[] at before t in s[].
  151. cmChar_t* cmTextInsertS( cmChar_t* s, const cmChar_t* t, const cmChar_t* u );
  152. cmChar_t* cmTextAppend( cmChar_t* s, unsigned* sn, const cmChar_t* u, unsigned un );
  153. cmChar_t* cmTextAppendSN( cmChar_t* s, const cmChar_t* u, unsigned un );
  154. // append u[un] to s[] and append terminating zero.
  155. cmChar_t* cmTextAppendSNZ( cmChar_t* s, const cmChar_t* u, unsigned un );
  156. // both s[] and u[] are strz's
  157. cmChar_t* cmTextAppendSS( cmChar_t* s, const cmChar_t* u );
  158. // Same as multiple calls to cmTextAppendSS().
  159. // Terminate the var-args list with NULL.
  160. cmChar_t* cmTextVAppendSS( cmChar_t* s, ... );
  161. // Append 'n' copies of 'c' to the end of s[].
  162. cmChar_t* cmTextAppendChar( cmChar_t* s, cmChar_t c, unsigned n );
  163. // Returns true if the string is NULL or all spaces.
  164. bool cmTextIsEmpty( const cmChar_t* s );
  165. bool cmTextIsNotEmpty( const cmChar_t* s );
  166. // Same as strlen() but handles case where s0 == NULL as length==0.
  167. unsigned cmTextLength( const cmChar_t* s0 );
  168. // Same as strcmp() but handles NULL. Note that if both s0 and s1 are NULL
  169. // then return is 0.
  170. int cmTextCmp( const cmChar_t* s0, const cmChar_t* s1 );
  171. // Same as cmTextCmp() but only compare the first 'n' characters.
  172. int cmTextCmpN( const cmChar_t* s0, const cmChar_t* s1, unsigned n );
  173. // Convert text in s0[] to upper/lower case in s1[].
  174. // Note that s0[] and s1[] may point to the same string
  175. void cmTextToLower( const cmChar_t* s0, cmChar_t* s1 );
  176. void cmTextToUpper( const cmChar_t* s0, cmChar_t* s1 );
  177. // Returns NULL if string contains fewer than lineIdx lines.
  178. // Note: first line == 1.
  179. cmChar_t* cmTextLine( cmChar_t* s, unsigned line );
  180. const cmChar_t* cmTextLineC( const cmChar_t* s, unsigned line );
  181. // Return the count of lines begining with s.
  182. unsigned cmTextLineCount( const cmChar_t* s );
  183. // Reduce all consecutive white spaces to a single space.
  184. cmChar_t* cmTextRemoveConsecutiveSpaces( cmChar_t* s );
  185. // Columnize s[] by inserting EOL's at the first available
  186. // space prior 'colCnt' columns. If no space exists then
  187. // the words will be broken. If length s[] is less than
  188. // 'colCnt' then s[] is returned with no changes.
  189. cmChar_t* cmTextColumize( cmChar_t* s, unsigned colCnt );
  190. // Indent the rows of s[] by inserting 'indent' spaces
  191. // just after each '\n'. If s[] contains no '\n' then
  192. // s[] is returned with no changes.
  193. cmChar_t* cmTextIndentRows( cmChar_t* s, unsigned indent );
  194. // Prefix all rows of s[] with p[].
  195. cmChar_t* cmTextPrefixRows( cmChar_t* s, const cmChar_t* t );
  196. // Remove leading white space from all rows of s.
  197. cmChar_t* cmTextTrimRows( cmChar_t* s );
  198. // Remove all white space prior to the first non-white space
  199. // character.
  200. cmChar_t* cmTextEatLeadingSpace( cmChar_t* s );
  201. // Return a pointer to the beginning of the next row
  202. // or NULL if there is no next row.
  203. cmChar_t* cmTextNextRow( cmChar_t* s );
  204. const cmChar_t* cmTextNextRowC( const cmChar_t* s );
  205. // Return the minimum indent of all rows in s[].
  206. unsigned cmTextMinIndent( const cmChar_t* s );
  207. // Outdent s[] by 'n'.
  208. // If a row is indented by less than 'n' then it is
  209. // then all leading white space is removed.
  210. cmChar_t* cmTextOutdent( cmChar_t* s, unsigned n );
  211. // Given a string containing binary data encoded as base64
  212. // return the size of the buffer required to hold the
  213. // decoded binary data. Note that if xV[] is a legal base64
  214. // string then xN must be a multiple of 4. If xN is not
  215. // a mulitple of 4 then the function returns kInvalidCnt.
  216. unsigned cmTextDecodeBase64BufferByteCount( const char* xV, unsigned xN );
  217. // Decode the base64 data in xV[xN] into yV[yN]. Note that the
  218. // minimum value of yN can be determined via
  219. // cmTextDecodeBase64BufferByteCount().
  220. // Return the actual count of bytes decoded into yV[].
  221. unsigned cmTextDecodeBase64( const char* xV, unsigned xN, void* yV, unsigned yN );
  222. // Given the count of bytes in a binary array, return
  223. // the count of bytes required to store the array in base64.
  224. unsigned cmTextEncodeBase64BufferByteCount( unsigned xN );
  225. // Encode the binary array xV[xN] into yV[yN]. Note that the value
  226. // of yN can be determined via cmTextEncodeBase64BufferByteCount().
  227. cmTxRC_t cmTextEncodeBase64( const void* xV, unsigned xN, char* yV, unsigned yN );
  228. //)
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif