strutil.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #ifndef MC_STRUTIL_H
  2. #define MC_STRUTIL_H
  3. #include "lib/global.h" /* include glib.h */
  4. #include <sys/types.h>
  5. #include <string.h>
  6. #ifdef HAVE_ASSERT_H
  7. #include <assert.h> /* assert() */
  8. #endif
  9. /* Header file for strutil.c, strutilascii.c, strutil8bit.c, strutilutf8.c.
  10. * There are two sort of functions:
  11. * 1. functions for working with growing strings and conversion strings between
  12. * different encodings.
  13. * (implemented directly in strutil.c)
  14. * 2. functions, that hide differences between encodings derived from ASCII.
  15. * (implemented separately in strutilascii.c, strutil8bit.c, strutilutf8.c)
  16. * documentation is made for UTF-8 version of functions.
  17. */
  18. /* invalid strings
  19. * function, that works with invalid strings are marked with "I"
  20. * in documentation
  21. * invalid bytes of string are handled as one byte characters with width 1, they
  22. * are displayed as questionmarks, I-maked comparing functions try to keep
  23. * the original value of these bytes.
  24. */
  25. /* combining characters
  26. * displaynig: all handled as zero with characters, expect combing character
  27. * at the begin of string, this character has with one (space add before),
  28. * so str_term_width is not good for computing width of singles characters
  29. * (never return zero, expect emtpy string)
  30. * for compatibility are strings composed before displaynig
  31. * comparing: comparing decompose all string before comparing, n-compare
  32. * functions do not work as is usual, because same strings do not have to be
  33. * same length in UTF-8. So they return 0 if one string is prefix of the other
  34. * one.
  35. * str_prefix is used to determine, how many characters from one string are
  36. * prefix in second string. However, str_prefix return number of characters in
  37. * decompose form. (used in do_search (screen.c))
  38. */
  39. /*** typedefs(not structures) and defined constants **********************************************/
  40. #define IS_FIT(x) ((x) & 0x0010)
  41. #define MAKE_FIT(x) ((x) | 0x0010)
  42. #define HIDE_FIT(x) ((x) & 0x000f)
  43. #define INVALID_CONV ((GIConv) (-1))
  44. /*** enums ***************************************************************************************/
  45. /* results of conversion function
  46. */
  47. typedef enum
  48. {
  49. /* success means, that convertion has been finished successully
  50. */
  51. ESTR_SUCCESS = 0,
  52. /* problem means, that not every characters was successfully converted (They are
  53. * replaced with questionmark). So is impossible convert string back.
  54. */
  55. ESTR_PROBLEM = 1,
  56. /* failure means, that conversion is not possible (example: wrong encoding
  57. * of input string)
  58. */
  59. ESTR_FAILURE = 2
  60. } estr_t;
  61. /* alignment strings on terminal
  62. */
  63. typedef enum
  64. {
  65. J_LEFT = 0x01,
  66. J_RIGHT = 0x02,
  67. J_CENTER = 0x03,
  68. /* if there is enough space for string on terminal,
  69. * string is centered otherwise is aligned to left */
  70. J_CENTER_LEFT = 0x04,
  71. /* fit alignment, if string is to long, is truncated with '~' */
  72. J_LEFT_FIT = 0x11,
  73. J_RIGHT_FIT = 0x12,
  74. J_CENTER_FIT = 0x13,
  75. J_CENTER_LEFT_FIT = 0x14
  76. } align_crt_t;
  77. /*** structures declarations (and typedefs of structures)*****************************************/
  78. /* all functions in str_class must be defined for every encoding */
  79. struct str_class
  80. {
  81. gchar *(*conv_gerror_message) (GError * error, const char *def_msg);
  82. /*I*/ estr_t (*vfs_convert_to) (GIConv coder, const char *string, int size, GString * buffer);
  83. /*I*/ void (*insert_replace_char) (GString * buffer);
  84. int (*is_valid_string) (const char *);
  85. /*I*/ int (*is_valid_char) (const char *, size_t);
  86. /*I*/ void (*cnext_char) (const char **);
  87. void (*cprev_char) (const char **);
  88. void (*cnext_char_safe) (const char **);
  89. /*I*/ void (*cprev_char_safe) (const char **);
  90. /*I*/ int (*cnext_noncomb_char) (const char **text);
  91. /*I*/ int (*cprev_noncomb_char) (const char **text, const char *begin);
  92. /*I*/ int (*char_isspace) (const char *);
  93. /*I*/ int (*char_ispunct) (const char *);
  94. /*I*/ int (*char_isalnum) (const char *);
  95. /*I*/ int (*char_isdigit) (const char *);
  96. /*I*/ int (*char_isprint) (const char *);
  97. /*I*/ gboolean (*char_iscombiningmark) (const char *);
  98. /*I*/ int (*length) (const char *);
  99. /*I*/ int (*length2) (const char *, int);
  100. /*I*/ int (*length_noncomb) (const char *);
  101. /*I*/ int (*char_toupper) (const char *, char **, size_t *);
  102. int (*char_tolower) (const char *, char **, size_t *);
  103. void (*fix_string) (char *);
  104. /*I*/ const char *(*term_form) (const char *);
  105. /*I*/ const char *(*fit_to_term) (const char *, int, align_crt_t);
  106. /*I*/ const char *(*term_trim) (const char *text, int width);
  107. /*I*/ const char *(*term_substring) (const char *, int, int);
  108. /*I*/ int (*term_width1) (const char *);
  109. /*I*/ int (*term_width2) (const char *, size_t);
  110. /*I*/ int (*term_char_width) (const char *);
  111. /*I*/ const char *(*trunc) (const char *, int);
  112. /*I*/ int (*offset_to_pos) (const char *, size_t);
  113. /*I*/ int (*column_to_pos) (const char *, size_t);
  114. /*I*/ char *(*create_search_needle) (const char *, int);
  115. void (*release_search_needle) (char *, int);
  116. const char *(*search_first) (const char *, const char *, int);
  117. const char *(*search_last) (const char *, const char *, int);
  118. int (*compare) (const char *, const char *);
  119. /*I*/ int (*ncompare) (const char *, const char *);
  120. /*I*/ int (*casecmp) (const char *, const char *);
  121. /*I*/ int (*ncasecmp) (const char *, const char *);
  122. /*I*/ int (*prefix) (const char *, const char *);
  123. /*I*/ int (*caseprefix) (const char *, const char *);
  124. /*I*/ char *(*create_key) (const char *text, int case_sen);
  125. /*I*/ char *(*create_key_for_filename) (const char *text, int case_sen);
  126. /*I*/ int (*key_collate) (const char *t1, const char *t2, int case_sen);
  127. /*I*/ void (*release_key) (char *key, int case_sen);
  128. /*I*/};
  129. /*** global variables defined in .c file *********************************************************/
  130. /* standard convertors */
  131. extern GIConv str_cnv_to_term;
  132. extern GIConv str_cnv_from_term;
  133. /* from terminal encoding to terminal encoding */
  134. extern GIConv str_cnv_not_convert;
  135. /*** declarations of public functions ************************************************************/
  136. struct str_class str_utf8_init (void);
  137. struct str_class str_8bit_init (void);
  138. struct str_class str_ascii_init (void);
  139. /* create convertor from "from_enc" to terminal encoding
  140. * if "from_enc" is not supported return INVALID_CONV
  141. */
  142. GIConv str_crt_conv_from (const char *);
  143. /* create convertor from terminal encoding to "to_enc"
  144. * if "to_enc" is not supported return INVALID_CONV
  145. */
  146. GIConv str_crt_conv_to (const char *);
  147. /* close convertor, do not close str_cnv_to_term, str_cnv_from_term,
  148. * str_cnv_not_convert
  149. */
  150. void str_close_conv (GIConv);
  151. /* return on of not used buffers (.used == 0) or create new
  152. * returned buffer has set .used to 1
  153. */
  154. /* convert string using coder, result of conversion is appended at end of buffer
  155. * return ESTR_SUCCESS if there was no problem.
  156. * otherwise return ESTR_PROBLEM or ESTR_FAILURE
  157. */
  158. estr_t str_convert (GIConv, const char *, GString *);
  159. estr_t str_nconvert (GIConv, const char *, int, GString *);
  160. /* convert GError message (which in UTF-8) to terminal charset
  161. * def_char is used if result of error->str conversion if ESTR_FAILURE
  162. * return new allocated null-terminated string, which is need to be freed
  163. * I
  164. */
  165. gchar *str_conv_gerror_message (GError * error, const char *def_msg);
  166. /* return only ESTR_SUCCESS or ESTR_FAILURE, because vfs must be able to convert
  167. * result to original string. (so no replace with questionmark)
  168. * if coder is str_cnv_from_term or str_cnv_not_convert, string is only copied,
  169. * so is possible to show file, that is not valid in terminal encoding
  170. */
  171. estr_t str_vfs_convert_from (GIConv, const char *, GString *);
  172. /* if coder is str_cnv_to_term or str_cnv_not_convert, string is only copied,
  173. * does replace with questionmark
  174. * I
  175. */
  176. estr_t str_vfs_convert_to (GIConv, const char *, int, GString *);
  177. /* printf functin for str_buffer, append result of printf at the end of buffer
  178. */
  179. void str_printf (GString *, const char *, ...);
  180. /* add standard replacement character in terminal encoding
  181. */
  182. void str_insert_replace_char (GString *);
  183. /* init strings and set terminal encoding,
  184. * if is termenc NULL, detect terminal encoding
  185. * create all str_cnv_* and set functions for terminal encoding
  186. */
  187. void str_init_strings (const char *termenc);
  188. /* free all str_buffer and all str_cnv_*
  189. */
  190. void str_uninit_strings (void);
  191. /* try convert characters in ch to output using conv
  192. * ch_size is size of ch, can by (size_t)(-1) (-1 only for ASCII
  193. * compatible encoding, for other must be set)
  194. * return ESTR_SUCCESS if conversion was successfully,
  195. * ESTR_PROBLEM if ch contains only part of characters,
  196. * ESTR_FAILURE if conversion is not possible
  197. */
  198. estr_t str_translate_char (GIConv conv, const char *ch, size_t ch_size,
  199. char *output, size_t out_size);
  200. /* test, if text is valid in terminal encoding
  201. * I
  202. */
  203. int str_is_valid_string (const char *text);
  204. /* test, if first char of ch is valid
  205. * size, how many bytes characters occupied, could be (size_t)(-1)
  206. * return 1 if it is valid, -1 if it is invalid or -2 if it is only part of
  207. * multibyte character
  208. * I
  209. */
  210. int str_is_valid_char (const char *ch, size_t size);
  211. /* return next characters after text, do not call on the end of string
  212. */
  213. char *str_get_next_char (char *text);
  214. const char *str_cget_next_char (const char *text);
  215. /* return previous characters before text, do not call on the start of strings
  216. */
  217. char *str_get_prev_char (char *text);
  218. const char *str_cget_prev_char (const char *text);
  219. /* set text to next characters, do not call on the end of string
  220. */
  221. void str_next_char (char **text);
  222. void str_cnext_char (const char **text);
  223. /* set text to previous characters, do not call on the start of strings
  224. */
  225. void str_prev_char (char **text);
  226. void str_cprev_char (const char **text);
  227. /* return next characters after text, do not call on the end of string
  228. * works with invalid string
  229. * I
  230. */
  231. char *str_get_next_char_safe (char *text);
  232. const char *str_cget_next_char_safe (const char *text);
  233. /* return previous characters before text, do not call on the start of strings
  234. * works with invalid string
  235. * I
  236. */
  237. char *str_get_prev_char_safe (char *text);
  238. const char *str_cget_prev_char_safe (const char *text);
  239. /* set text to next characters, do not call on the end of string
  240. * works with invalid string
  241. * I
  242. */
  243. void str_next_char_safe (char **text);
  244. void str_cnext_char_safe (const char **text);
  245. /* set text to previous characters, do not call on the start of strings
  246. * works with invalid string
  247. * I
  248. */
  249. void str_prev_char_safe (char **text);
  250. void str_cprev_char_safe (const char **text);
  251. /* set text to next noncombining characters, check the end of text
  252. * return how many characters was skipped
  253. * works with invalid string
  254. * I
  255. */
  256. int str_next_noncomb_char (char **text);
  257. int str_cnext_noncomb_char (const char **text);
  258. /* set text to previous noncombining characters, search stop at begin
  259. * return how many characters was skipped
  260. * works with invalid string
  261. * I
  262. */
  263. int str_prev_noncomb_char (char **text, const char *begin);
  264. int str_cprev_noncomb_char (const char **text, const char *begin);
  265. /* if first characters in ch is space, tabulator or new lines
  266. * I
  267. */
  268. int str_isspace (const char *ch);
  269. /* if first characters in ch is punctuation or symbol
  270. * I
  271. */
  272. int str_ispunct (const char *ch);
  273. /* if first characters in ch is alphanum
  274. * I
  275. */
  276. int str_isalnum (const char *ch);
  277. /* if first characters in ch is digit
  278. * I
  279. */
  280. int str_isdigit (const char *ch);
  281. /* if first characters in ch is printable
  282. * I
  283. */
  284. int str_isprint (const char *ch);
  285. /* if first characters in ch is a combining mark (only in utf-8)
  286. * combining makrs are assumed to be zero width
  287. * I
  288. */
  289. gboolean str_iscombiningmark (const char *ch);
  290. /* write lower from of fisrt characters in ch into out
  291. * decrase remain by size of returned characters
  292. * if out is not big enough, do nothing
  293. */
  294. int str_toupper (const char *ch, char **out, size_t * remain);
  295. /* write upper from of fisrt characters in ch into out
  296. * decrase remain by size of returned characters
  297. * if out is not big enough, do nothing
  298. */
  299. int str_tolower (const char *ch, char **out, size_t * remain);
  300. /* return length of text in characters
  301. * I
  302. */
  303. int str_length (const char *text);
  304. /* return length of text in characters, limit to size
  305. * I
  306. */
  307. int str_length2 (const char *text, int size);
  308. /* return length of one char
  309. * I
  310. */
  311. int str_length_char (const char *);
  312. /* return length of text in characters, count only noncombining characters
  313. * I
  314. */
  315. int str_length_noncomb (const char *text);
  316. /* replace all invalid characters in text with questionmark
  317. * after return, text is valid string in terminal encoding
  318. * I
  319. */
  320. void str_fix_string (char *text);
  321. /* replace all invalid characters in text with questionmark
  322. * replace all unprintable characters with '.'
  323. * return static allocated string, "text" is not changed
  324. * returned string do not need to be freed
  325. * I
  326. */
  327. const char *str_term_form (const char *text);
  328. /* like str_term_form, but text can be alignment to width
  329. * alignment is specified in just_mode (J_LEFT, J_LEFT_FIT, ...)
  330. * result is completed with spaces to width
  331. * I
  332. */
  333. const char *str_fit_to_term (const char *text, int width, align_crt_t just_mode);
  334. /* like str_term_form, but when text is wider than width, three dots are
  335. * inserted at begin and result is completed with suffix of text
  336. * no additional spaces are inserted
  337. * I
  338. */
  339. const char *str_term_trim (const char *text, int width);
  340. /* like str_term_form, but return only specified substring
  341. * start - column (position) on terminal, where substring begin
  342. * result is completed with spaces to width
  343. * I
  344. */
  345. const char *str_term_substring (const char *text, int start, int width);
  346. /* return width, that will be text occupied on terminal
  347. * I
  348. */
  349. int str_term_width1 (const char *text);
  350. /* return width, that will be text occupied on terminal
  351. * text is limited by length in characters
  352. * I
  353. */
  354. int str_term_width2 (const char *text, size_t length);
  355. /* return width, that will be character occupied on terminal
  356. * combining characters are always zero width
  357. * I
  358. */
  359. int str_term_char_width (const char *text);
  360. /* convert position in characters to position in bytes
  361. * I
  362. */
  363. int str_offset_to_pos (const char *text, size_t length);
  364. /* convert position on terminal to position in characters
  365. * I
  366. */
  367. int str_column_to_pos (const char *text, size_t pos);
  368. /* like str_fit_to_term width just_mode = J_LEFT_FIT,
  369. * but do not insert additional spaces
  370. * I
  371. */
  372. const char *str_trunc (const char *text, int width);
  373. /* create needle, that will be searched in str_search_fist/last,
  374. * so needle can be reused
  375. * in UTF-8 return normalized form of needle
  376. */
  377. char *str_create_search_needle (const char *needle, int case_sen);
  378. /* free needle returned by str_create_search_needle
  379. */
  380. void str_release_search_needle (char *needle, int case_sen);
  381. /* search for first occurrence of search in text
  382. */
  383. const char *str_search_first (const char *text, const char *needle, int case_sen);
  384. /* search for last occurrence of search in text
  385. */
  386. const char *str_search_last (const char *text, const char *needle, int case_sen);
  387. /* case sensitive compare two strings
  388. * I
  389. */
  390. int str_compare (const char *t1, const char *t2);
  391. /* case sensitive compare two strings
  392. * if one string is prefix of the other string, return 0
  393. * I
  394. */
  395. int str_ncompare (const char *t1, const char *t2);
  396. /* case insensitive compare two strings
  397. * I
  398. */
  399. int str_casecmp (const char *t1, const char *t2);
  400. /* case insensitive compare two strings
  401. * if one string is prefix of the other string, return 0
  402. * I
  403. */
  404. int str_ncasecmp (const char *t1, const char *t2);
  405. /* return, how many bytes are are same from start in text and prefix
  406. * both strings are decomposed befor comapring and return value is counted
  407. * in decomposed form, too. caling with prefix, prefix, you get size in bytes
  408. * of prefix in decomposed form,
  409. * I
  410. */
  411. int str_prefix (const char *text, const char *prefix);
  412. /* case insensitive version of str_prefix
  413. * I
  414. */
  415. int str_caseprefix (const char *text, const char *prefix);
  416. /* create a key that is used by str_key_collate
  417. * I
  418. */
  419. char *str_create_key (const char *text, int case_sen);
  420. /* create a key that is used by str_key_collate
  421. * should aware dot '.' in text
  422. * I
  423. */
  424. char *str_create_key_for_filename (const char *text, int case_sen);
  425. /* compare two string using LC_COLLATE, if is possible
  426. * if case_sen is set, comparing is case sensitive,
  427. * case_sen must be same for str_create_key, str_key_collate and str_release_key
  428. * I
  429. */
  430. int str_key_collate (const char *t1, const char *t2, int case_sen);
  431. /* release_key created by str_create_key, only rigth way to release key
  432. * I
  433. */
  434. void str_release_key (char *key, int case_sen);
  435. /* return TRUE if codeset_name is utf8 or utf-8
  436. * I
  437. */
  438. gboolean str_isutf8 (const char *codeset_name);
  439. const char *str_detect_termencoding (void);
  440. int str_verscmp (const char *s1, const char *s2);
  441. /* return how many lines and columns will text occupy on terminal
  442. */
  443. void str_msg_term_size (const char *text, int *lines, int *columns);
  444. /**
  445. skip first <skip_count> needle's in haystack and returns pointer to
  446. <skip_count+1> needle (or NULL if not found).
  447. */
  448. char *strrstr_skip_count (const char *haystack, const char *needle, size_t skip_count);
  449. /*** inline functions ****************************************************************************/
  450. static inline void
  451. str_replace (char *s, char from, char to)
  452. {
  453. for (; *s != '\0'; s++)
  454. {
  455. if (*s == from)
  456. *s = to;
  457. }
  458. }
  459. /*
  460. * strcpy is unsafe on overlapping memory areas, so define memmove-alike
  461. * string function.
  462. * Have sense only when:
  463. * * dest <= src
  464. * AND
  465. * * dest and str are pointers to one object (as Roland Illig pointed).
  466. *
  467. * We can't use str*cpy funs here:
  468. * http://kerneltrap.org/mailarchive/openbsd-misc/2008/5/27/1951294
  469. */
  470. static inline char *
  471. str_move (char *dest, const char *src)
  472. {
  473. size_t n;
  474. #ifdef HAVE_ASSERT_H
  475. assert (dest <= src);
  476. #endif
  477. n = strlen (src) + 1; /* + '\0' */
  478. return (char *) memmove (dest, src, n);
  479. }
  480. #endif /* MC_STRUTIL_H */