strutil.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. Common strings utilities
  3. Copyright (C) 2007-2015
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Rostislav Benes, 2007
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <langinfo.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "lib/global.h"
  25. #include "lib/strutil.h"
  26. /*names, that are used for utf-8 */
  27. static const char *str_utf8_encodings[] = {
  28. "utf-8",
  29. "utf8",
  30. NULL
  31. };
  32. /* standard 8bit encodings, no wide or multibytes characters */
  33. static const char *str_8bit_encodings[] = {
  34. "cp-1251",
  35. "cp1251",
  36. "cp-1250",
  37. "cp1250",
  38. "cp-866",
  39. "cp866",
  40. "ibm-866",
  41. "ibm866",
  42. "cp-850",
  43. "cp850",
  44. "cp-852",
  45. "cp852",
  46. "iso-8859",
  47. "iso8859",
  48. "koi8",
  49. NULL
  50. };
  51. /* terminal encoding */
  52. static char *codeset = NULL;
  53. static char *term_encoding = NULL;
  54. /* function for encoding specific operations */
  55. static struct str_class used_class;
  56. GIConv str_cnv_to_term;
  57. GIConv str_cnv_from_term;
  58. GIConv str_cnv_not_convert = INVALID_CONV;
  59. /* if enc is same encoding like on terminal */
  60. static int
  61. str_test_not_convert (const char *enc)
  62. {
  63. return g_ascii_strcasecmp (enc, codeset) == 0;
  64. }
  65. GIConv
  66. str_crt_conv_to (const char *to_enc)
  67. {
  68. return (!str_test_not_convert (to_enc)) ? g_iconv_open (to_enc, codeset) : str_cnv_not_convert;
  69. }
  70. GIConv
  71. str_crt_conv_from (const char *from_enc)
  72. {
  73. return (!str_test_not_convert (from_enc))
  74. ? g_iconv_open (codeset, from_enc) : str_cnv_not_convert;
  75. }
  76. void
  77. str_close_conv (GIConv conv)
  78. {
  79. if (conv != str_cnv_not_convert)
  80. g_iconv_close (conv);
  81. }
  82. static estr_t
  83. _str_convert (GIConv coder, const char *string, int size, GString * buffer)
  84. {
  85. estr_t state = ESTR_SUCCESS;
  86. gssize left;
  87. gsize bytes_read = 0;
  88. gsize bytes_written = 0;
  89. errno = 0; /* FIXME: is it really needed? */
  90. if (coder == INVALID_CONV)
  91. return ESTR_FAILURE;
  92. if (string == NULL || buffer == NULL)
  93. return ESTR_FAILURE;
  94. /*
  95. if (! used_class.is_valid_string (string))
  96. {
  97. return ESTR_FAILURE;
  98. }
  99. */
  100. if (size < 0)
  101. size = strlen (string);
  102. else
  103. {
  104. left = strlen (string);
  105. if (left < size)
  106. size = left;
  107. }
  108. left = size;
  109. g_iconv (coder, NULL, NULL, NULL, NULL);
  110. while (left != 0)
  111. {
  112. gchar *tmp_buff;
  113. GError *mcerror = NULL;
  114. tmp_buff = g_convert_with_iconv ((const gchar *) string,
  115. left, coder, &bytes_read, &bytes_written, &mcerror);
  116. if (mcerror != NULL)
  117. {
  118. int code = mcerror->code;
  119. g_error_free (mcerror);
  120. mcerror = NULL;
  121. switch (code)
  122. {
  123. case G_CONVERT_ERROR_NO_CONVERSION:
  124. /* Conversion between the requested character sets is not supported. */
  125. tmp_buff = g_strnfill (strlen (string), '?');
  126. g_string_append (buffer, tmp_buff);
  127. g_free (tmp_buff);
  128. return ESTR_FAILURE;
  129. case G_CONVERT_ERROR_ILLEGAL_SEQUENCE:
  130. /* Invalid byte sequence in conversion input. */
  131. if ((tmp_buff == NULL) && (bytes_read != 0))
  132. /* recode valid byte sequence */
  133. tmp_buff = g_convert_with_iconv ((const gchar *) string,
  134. bytes_read, coder, NULL, NULL, NULL);
  135. if (tmp_buff != NULL)
  136. {
  137. g_string_append (buffer, tmp_buff);
  138. g_free (tmp_buff);
  139. }
  140. if ((int) bytes_read >= left)
  141. return ESTR_PROBLEM;
  142. string += bytes_read + 1;
  143. size -= (bytes_read + 1);
  144. left -= (bytes_read + 1);
  145. g_string_append_c (buffer, *(string - 1));
  146. state = ESTR_PROBLEM;
  147. break;
  148. case G_CONVERT_ERROR_PARTIAL_INPUT:
  149. /* Partial character sequence at end of input. */
  150. g_string_append (buffer, tmp_buff);
  151. g_free (tmp_buff);
  152. if ((int) bytes_read < left)
  153. {
  154. left = left - bytes_read;
  155. tmp_buff = g_strnfill (left, '?');
  156. g_string_append (buffer, tmp_buff);
  157. g_free (tmp_buff);
  158. }
  159. return ESTR_PROBLEM;
  160. case G_CONVERT_ERROR_BAD_URI: /* Don't know how handle this error :( */
  161. case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH: /* Don't know how handle this error :( */
  162. case G_CONVERT_ERROR_FAILED: /* Conversion failed for some reason. */
  163. default:
  164. g_free (tmp_buff);
  165. return ESTR_FAILURE;
  166. }
  167. }
  168. else if (tmp_buff == NULL)
  169. {
  170. g_string_append (buffer, string);
  171. return ESTR_PROBLEM;
  172. }
  173. else if (*tmp_buff == '\0')
  174. {
  175. g_free (tmp_buff);
  176. g_string_append (buffer, string);
  177. return state;
  178. }
  179. else
  180. {
  181. g_string_append (buffer, tmp_buff);
  182. g_free (tmp_buff);
  183. string += bytes_read;
  184. left -= bytes_read;
  185. }
  186. }
  187. return state;
  188. }
  189. estr_t
  190. str_convert (GIConv coder, const char *string, GString * buffer)
  191. {
  192. return _str_convert (coder, string, -1, buffer);
  193. }
  194. estr_t
  195. str_nconvert (GIConv coder, const char *string, int size, GString * buffer)
  196. {
  197. return _str_convert (coder, string, size, buffer);
  198. }
  199. gchar *
  200. str_conv_gerror_message (GError * mcerror, const char *def_msg)
  201. {
  202. return used_class.conv_gerror_message (mcerror, def_msg);
  203. }
  204. estr_t
  205. str_vfs_convert_from (GIConv coder, const char *string, GString * buffer)
  206. {
  207. estr_t result = ESTR_SUCCESS;
  208. if (coder == str_cnv_not_convert)
  209. g_string_append (buffer, string != NULL ? string : "");
  210. else
  211. result = _str_convert (coder, string, -1, buffer);
  212. return result;
  213. }
  214. estr_t
  215. str_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
  216. {
  217. return used_class.vfs_convert_to (coder, string, size, buffer);
  218. }
  219. void
  220. str_printf (GString * buffer, const char *format, ...)
  221. {
  222. va_list ap;
  223. va_start (ap, format);
  224. g_string_append_vprintf (buffer, format, ap);
  225. va_end (ap);
  226. }
  227. void
  228. str_insert_replace_char (GString * buffer)
  229. {
  230. used_class.insert_replace_char (buffer);
  231. }
  232. estr_t
  233. str_translate_char (GIConv conv, const char *keys, size_t ch_size, char *output, size_t out_size)
  234. {
  235. size_t left;
  236. size_t cnv;
  237. g_iconv (conv, NULL, NULL, NULL, NULL);
  238. left = (ch_size == (size_t) (-1)) ? strlen (keys) : ch_size;
  239. cnv = g_iconv (conv, (gchar **) & keys, &left, &output, &out_size);
  240. if (cnv == (size_t) (-1))
  241. return (errno == EINVAL) ? ESTR_PROBLEM : ESTR_FAILURE;
  242. output[0] = '\0';
  243. return ESTR_SUCCESS;
  244. }
  245. const char *
  246. str_detect_termencoding (void)
  247. {
  248. if (term_encoding == NULL)
  249. {
  250. /* On Linux, nl_langinfo (CODESET) returns upper case UTF-8 whether the LANG is set
  251. to utf-8 or UTF-8.
  252. On Mac OS X, it returns the same case as the LANG input.
  253. So let tranform result of nl_langinfo (CODESET) to upper case unconditionally. */
  254. term_encoding = g_ascii_strup (nl_langinfo (CODESET), -1);
  255. }
  256. return term_encoding;
  257. }
  258. static int
  259. str_test_encoding_class (const char *encoding, const char **table)
  260. {
  261. int result = 0;
  262. if (encoding != NULL)
  263. {
  264. int t;
  265. for (t = 0; table[t] != NULL; t++)
  266. if (g_ascii_strncasecmp (encoding, table[t], strlen (table[t])) == 0)
  267. result++;
  268. }
  269. return result;
  270. }
  271. static void
  272. str_choose_str_functions (void)
  273. {
  274. if (str_test_encoding_class (codeset, str_utf8_encodings))
  275. used_class = str_utf8_init ();
  276. else if (str_test_encoding_class (codeset, str_8bit_encodings))
  277. used_class = str_8bit_init ();
  278. else
  279. used_class = str_ascii_init ();
  280. }
  281. gboolean
  282. str_isutf8 (const char *codeset_name)
  283. {
  284. return (str_test_encoding_class (codeset_name, str_utf8_encodings) != 0);
  285. }
  286. void
  287. str_init_strings (const char *termenc)
  288. {
  289. codeset = termenc != NULL ? g_ascii_strup (termenc, -1) : g_strdup (str_detect_termencoding ());
  290. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  291. if (str_cnv_not_convert == INVALID_CONV)
  292. {
  293. if (termenc != NULL)
  294. {
  295. g_free (codeset);
  296. codeset = g_strdup (str_detect_termencoding ());
  297. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  298. }
  299. if (str_cnv_not_convert == INVALID_CONV)
  300. {
  301. g_free (codeset);
  302. codeset = g_strdup (DEFAULT_CHARSET);
  303. str_cnv_not_convert = g_iconv_open (codeset, codeset);
  304. }
  305. }
  306. str_cnv_to_term = str_cnv_not_convert;
  307. str_cnv_from_term = str_cnv_not_convert;
  308. str_choose_str_functions ();
  309. }
  310. void
  311. str_uninit_strings (void)
  312. {
  313. if (str_cnv_not_convert != INVALID_CONV)
  314. g_iconv_close (str_cnv_not_convert);
  315. g_free (term_encoding);
  316. g_free (codeset);
  317. }
  318. const char *
  319. str_term_form (const char *text)
  320. {
  321. return used_class.term_form (text);
  322. }
  323. const char *
  324. str_fit_to_term (const char *text, int width, align_crt_t just_mode)
  325. {
  326. return used_class.fit_to_term (text, width, just_mode);
  327. }
  328. const char *
  329. str_term_trim (const char *text, int width)
  330. {
  331. return used_class.term_trim (text, width);
  332. }
  333. const char *
  334. str_term_substring (const char *text, int start, int width)
  335. {
  336. return used_class.term_substring (text, start, width);
  337. }
  338. char *
  339. str_get_next_char (char *text)
  340. {
  341. used_class.cnext_char ((const char **) &text);
  342. return text;
  343. }
  344. const char *
  345. str_cget_next_char (const char *text)
  346. {
  347. used_class.cnext_char (&text);
  348. return text;
  349. }
  350. void
  351. str_next_char (char **text)
  352. {
  353. used_class.cnext_char ((const char **) text);
  354. }
  355. void
  356. str_cnext_char (const char **text)
  357. {
  358. used_class.cnext_char (text);
  359. }
  360. char *
  361. str_get_prev_char (char *text)
  362. {
  363. used_class.cprev_char ((const char **) &text);
  364. return text;
  365. }
  366. const char *
  367. str_cget_prev_char (const char *text)
  368. {
  369. used_class.cprev_char (&text);
  370. return text;
  371. }
  372. void
  373. str_prev_char (char **text)
  374. {
  375. used_class.cprev_char ((const char **) text);
  376. }
  377. void
  378. str_cprev_char (const char **text)
  379. {
  380. used_class.cprev_char (text);
  381. }
  382. char *
  383. str_get_next_char_safe (char *text)
  384. {
  385. used_class.cnext_char_safe ((const char **) &text);
  386. return text;
  387. }
  388. const char *
  389. str_cget_next_char_safe (const char *text)
  390. {
  391. used_class.cnext_char_safe (&text);
  392. return text;
  393. }
  394. void
  395. str_next_char_safe (char **text)
  396. {
  397. used_class.cnext_char_safe ((const char **) text);
  398. }
  399. void
  400. str_cnext_char_safe (const char **text)
  401. {
  402. used_class.cnext_char_safe (text);
  403. }
  404. char *
  405. str_get_prev_char_safe (char *text)
  406. {
  407. used_class.cprev_char_safe ((const char **) &text);
  408. return text;
  409. }
  410. const char *
  411. str_cget_prev_char_safe (const char *text)
  412. {
  413. used_class.cprev_char_safe (&text);
  414. return text;
  415. }
  416. void
  417. str_prev_char_safe (char **text)
  418. {
  419. used_class.cprev_char_safe ((const char **) text);
  420. }
  421. void
  422. str_cprev_char_safe (const char **text)
  423. {
  424. used_class.cprev_char_safe (text);
  425. }
  426. int
  427. str_next_noncomb_char (char **text)
  428. {
  429. return used_class.cnext_noncomb_char ((const char **) text);
  430. }
  431. int
  432. str_cnext_noncomb_char (const char **text)
  433. {
  434. return used_class.cnext_noncomb_char (text);
  435. }
  436. int
  437. str_prev_noncomb_char (char **text, const char *begin)
  438. {
  439. return used_class.cprev_noncomb_char ((const char **) text, begin);
  440. }
  441. int
  442. str_cprev_noncomb_char (const char **text, const char *begin)
  443. {
  444. return used_class.cprev_noncomb_char (text, begin);
  445. }
  446. int
  447. str_is_valid_char (const char *ch, size_t size)
  448. {
  449. return used_class.is_valid_char (ch, size);
  450. }
  451. int
  452. str_term_width1 (const char *text)
  453. {
  454. return used_class.term_width1 (text);
  455. }
  456. int
  457. str_term_width2 (const char *text, size_t length)
  458. {
  459. return used_class.term_width2 (text, length);
  460. }
  461. int
  462. str_term_char_width (const char *text)
  463. {
  464. return used_class.term_char_width (text);
  465. }
  466. int
  467. str_offset_to_pos (const char *text, size_t length)
  468. {
  469. return used_class.offset_to_pos (text, length);
  470. }
  471. int
  472. str_length (const char *text)
  473. {
  474. return used_class.length (text);
  475. }
  476. int
  477. str_length_char (const char *text)
  478. {
  479. return str_cget_next_char_safe (text) - text;
  480. }
  481. int
  482. str_length2 (const char *text, int size)
  483. {
  484. return used_class.length2 (text, size);
  485. }
  486. int
  487. str_length_noncomb (const char *text)
  488. {
  489. return used_class.length_noncomb (text);
  490. }
  491. int
  492. str_column_to_pos (const char *text, size_t pos)
  493. {
  494. return used_class.column_to_pos (text, pos);
  495. }
  496. int
  497. str_isspace (const char *ch)
  498. {
  499. return used_class.char_isspace (ch);
  500. }
  501. int
  502. str_ispunct (const char *ch)
  503. {
  504. return used_class.char_ispunct (ch);
  505. }
  506. int
  507. str_isalnum (const char *ch)
  508. {
  509. return used_class.char_isalnum (ch);
  510. }
  511. int
  512. str_isdigit (const char *ch)
  513. {
  514. return used_class.char_isdigit (ch);
  515. }
  516. int
  517. str_toupper (const char *ch, char **out, size_t * remain)
  518. {
  519. return used_class.char_toupper (ch, out, remain);
  520. }
  521. int
  522. str_tolower (const char *ch, char **out, size_t * remain)
  523. {
  524. return used_class.char_tolower (ch, out, remain);
  525. }
  526. int
  527. str_isprint (const char *ch)
  528. {
  529. return used_class.char_isprint (ch);
  530. }
  531. gboolean
  532. str_iscombiningmark (const char *ch)
  533. {
  534. return used_class.char_iscombiningmark (ch);
  535. }
  536. const char *
  537. str_trunc (const char *text, int width)
  538. {
  539. return used_class.trunc (text, width);
  540. }
  541. char *
  542. str_create_search_needle (const char *needle, int case_sen)
  543. {
  544. return used_class.create_search_needle (needle, case_sen);
  545. }
  546. void
  547. str_release_search_needle (char *needle, int case_sen)
  548. {
  549. used_class.release_search_needle (needle, case_sen);
  550. }
  551. const char *
  552. str_search_first (const char *text, const char *search, int case_sen)
  553. {
  554. return used_class.search_first (text, search, case_sen);
  555. }
  556. const char *
  557. str_search_last (const char *text, const char *search, int case_sen)
  558. {
  559. return used_class.search_last (text, search, case_sen);
  560. }
  561. int
  562. str_is_valid_string (const char *text)
  563. {
  564. return used_class.is_valid_string (text);
  565. }
  566. int
  567. str_compare (const char *t1, const char *t2)
  568. {
  569. return used_class.compare (t1, t2);
  570. }
  571. int
  572. str_ncompare (const char *t1, const char *t2)
  573. {
  574. return used_class.ncompare (t1, t2);
  575. }
  576. int
  577. str_casecmp (const char *t1, const char *t2)
  578. {
  579. return used_class.casecmp (t1, t2);
  580. }
  581. int
  582. str_ncasecmp (const char *t1, const char *t2)
  583. {
  584. return used_class.ncasecmp (t1, t2);
  585. }
  586. int
  587. str_prefix (const char *text, const char *prefix)
  588. {
  589. return used_class.prefix (text, prefix);
  590. }
  591. int
  592. str_caseprefix (const char *text, const char *prefix)
  593. {
  594. return used_class.caseprefix (text, prefix);
  595. }
  596. void
  597. str_fix_string (char *text)
  598. {
  599. used_class.fix_string (text);
  600. }
  601. char *
  602. str_create_key (const char *text, int case_sen)
  603. {
  604. return used_class.create_key (text, case_sen);
  605. }
  606. char *
  607. str_create_key_for_filename (const char *text, int case_sen)
  608. {
  609. return used_class.create_key_for_filename (text, case_sen);
  610. }
  611. int
  612. str_key_collate (const char *t1, const char *t2, int case_sen)
  613. {
  614. return used_class.key_collate (t1, t2, case_sen);
  615. }
  616. void
  617. str_release_key (char *key, int case_sen)
  618. {
  619. used_class.release_key (key, case_sen);
  620. }
  621. void
  622. str_msg_term_size (const char *text, int *lines, int *columns)
  623. {
  624. char *p, *tmp;
  625. char *q;
  626. char c = '\0';
  627. *lines = 1;
  628. *columns = 0;
  629. tmp = g_strdup (text);
  630. p = tmp;
  631. while (TRUE)
  632. {
  633. int width;
  634. q = strchr (p, '\n');
  635. if (q != NULL)
  636. {
  637. c = q[0];
  638. q[0] = '\0';
  639. }
  640. width = str_term_width1 (p);
  641. if (width > *columns)
  642. *columns = width;
  643. if (q == NULL)
  644. break;
  645. q[0] = c;
  646. p = q + 1;
  647. (*lines)++;
  648. }
  649. g_free (tmp);
  650. }
  651. /* --------------------------------------------------------------------------------------------- */
  652. char *
  653. strrstr_skip_count (const char *haystack, const char *needle, size_t skip_count)
  654. {
  655. char *semi;
  656. ssize_t len;
  657. len = strlen (haystack);
  658. do
  659. {
  660. semi = g_strrstr_len (haystack, len, needle);
  661. if (semi == NULL)
  662. return NULL;
  663. len = semi - haystack - 1;
  664. }
  665. while (skip_count-- != 0);
  666. return semi;
  667. }
  668. /* --------------------------------------------------------------------------------------------- */
  669. /* Interprete string as a non-negative decimal integer, optionally multiplied by various values.
  670. *
  671. * @param str input value
  672. * @param invalid set to TRUE if "str" does not represent a number in this format
  673. *
  674. * @return non-integer representation of "str", 0 in case of error.
  675. */
  676. uintmax_t
  677. parse_integer (const char *str, gboolean * invalid)
  678. {
  679. uintmax_t n;
  680. char *suffix;
  681. strtol_error_t e;
  682. e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
  683. if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
  684. {
  685. uintmax_t multiplier;
  686. multiplier = parse_integer (suffix + 1, invalid);
  687. if (multiplier != 0 && n * multiplier / multiplier != n)
  688. {
  689. *invalid = TRUE;
  690. return 0;
  691. }
  692. n *= multiplier;
  693. }
  694. else if (e != LONGINT_OK)
  695. {
  696. *invalid = TRUE;
  697. n = 0;
  698. }
  699. return n;
  700. }
  701. /* --------------------------------------------------------------------------------------------- */