strutilascii.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* ASCII strings utilities
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. Written 2007 by:
  4. Rostislav Benes
  5. The file_date routine is mostly from GNU's fileutils package,
  6. written by Richard Stallman and David MacKenzie.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <config.h>
  23. #include <errno.h>
  24. #include "lib/global.h"
  25. #include "lib/strutil.h"
  26. /* using g_ascii function from glib
  27. * on terminal are showed only ascii characters (lower then 0x80)
  28. */
  29. static const char replch = '?';
  30. static void
  31. str_ascii_insert_replace_char (GString * buffer)
  32. {
  33. g_string_append_c (buffer, replch);
  34. }
  35. static int
  36. str_ascii_is_valid_string (const char *text)
  37. {
  38. (void) text;
  39. return 1;
  40. }
  41. static int
  42. str_ascii_is_valid_char (const char *ch, size_t size)
  43. {
  44. (void) ch;
  45. (void) size;
  46. return 1;
  47. }
  48. static void
  49. str_ascii_cnext_char (const char **text)
  50. {
  51. (*text)++;
  52. }
  53. static void
  54. str_ascii_cprev_char (const char **text)
  55. {
  56. (*text)--;
  57. }
  58. static int
  59. str_ascii_cnext_noncomb_char (const char **text)
  60. {
  61. if (*text[0] != '\0')
  62. {
  63. (*text)++;
  64. return 1;
  65. }
  66. else
  67. return 0;
  68. }
  69. static int
  70. str_ascii_cprev_noncomb_char (const char **text, const char *begin)
  71. {
  72. if ((*text) != begin)
  73. {
  74. (*text)--;
  75. return 1;
  76. }
  77. else
  78. return 0;
  79. }
  80. static int
  81. str_ascii_isspace (const char *text)
  82. {
  83. return g_ascii_isspace ((gchar) text[0]);
  84. }
  85. static int
  86. str_ascii_ispunct (const char *text)
  87. {
  88. return g_ascii_ispunct ((gchar) text[0]);
  89. }
  90. static int
  91. str_ascii_isalnum (const char *text)
  92. {
  93. return g_ascii_isalnum ((gchar) text[0]);
  94. }
  95. static int
  96. str_ascii_isdigit (const char *text)
  97. {
  98. return g_ascii_isdigit ((gchar) text[0]);
  99. }
  100. static int
  101. str_ascii_isprint (const char *text)
  102. {
  103. return g_ascii_isprint ((gchar) text[0]);
  104. }
  105. static int
  106. str_ascii_iscombiningmark (const char *text)
  107. {
  108. (void) text;
  109. return 0;
  110. }
  111. static int
  112. str_ascii_toupper (const char *text, char **out, size_t * remain)
  113. {
  114. if (*remain <= 1)
  115. return 0;
  116. (*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
  117. (*out)++;
  118. (*remain)--;
  119. return 1;
  120. }
  121. static int
  122. str_ascii_tolower (const char *text, char **out, size_t * remain)
  123. {
  124. if (*remain <= 1)
  125. return 0;
  126. (*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
  127. (*out)++;
  128. (*remain)--;
  129. return 1;
  130. }
  131. static int
  132. str_ascii_length (const char *text)
  133. {
  134. return strlen (text);
  135. }
  136. static int
  137. str_ascii_length2 (const char *text, int size)
  138. {
  139. return (size >= 0) ? min (strlen (text), (gsize) size) : strlen (text);
  140. }
  141. static gchar *
  142. str_ascii_conv_gerror_message (GError * error, const char *def_msg)
  143. {
  144. /* the same as str_utf8_conv_gerror_message() */
  145. if ((error != NULL) && (error->message != NULL))
  146. return g_strdup (error->message);
  147. return g_strdup (def_msg != NULL ? def_msg : "");
  148. }
  149. static estr_t
  150. str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
  151. {
  152. (void) coder;
  153. g_string_append_len (buffer, string, size);
  154. return ESTR_SUCCESS;
  155. }
  156. static const char *
  157. str_ascii_term_form (const char *text)
  158. {
  159. static char result[BUF_MEDIUM];
  160. char *actual;
  161. size_t remain;
  162. size_t length;
  163. size_t pos = 0;
  164. actual = result;
  165. remain = sizeof (result);
  166. length = strlen (text);
  167. /* go throw all characters and check, if they are ascii and printable */
  168. for (; pos < length && remain > 1; pos++, actual++, remain--)
  169. {
  170. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  171. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  172. }
  173. actual[0] = '\0';
  174. return result;
  175. }
  176. static const char *
  177. str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
  178. {
  179. static char result[BUF_MEDIUM];
  180. char *actual;
  181. size_t remain;
  182. int ident;
  183. size_t length;
  184. size_t pos = 0;
  185. length = strlen (text);
  186. actual = result;
  187. remain = sizeof (result);
  188. if ((int) length <= width)
  189. {
  190. ident = 0;
  191. switch (HIDE_FIT (just_mode))
  192. {
  193. case J_CENTER_LEFT:
  194. case J_CENTER:
  195. ident = (width - length) / 2;
  196. break;
  197. case J_RIGHT:
  198. ident = width - length;
  199. break;
  200. }
  201. /* add space before text */
  202. if ((int) remain <= ident)
  203. goto finally;
  204. memset (actual, ' ', ident);
  205. actual += ident;
  206. remain -= ident;
  207. /* copy all characters */
  208. for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
  209. {
  210. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  211. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  212. }
  213. /* add space after text */
  214. if (width - length - ident > 0)
  215. {
  216. if (remain <= width - length - ident)
  217. goto finally;
  218. memset (actual, ' ', width - length - ident);
  219. actual += width - length - ident;
  220. remain -= width - length - ident;
  221. }
  222. }
  223. else
  224. {
  225. if (IS_FIT (just_mode))
  226. {
  227. /* copy prefix of text, that is not wider than width / 2 */
  228. for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
  229. {
  230. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  231. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  232. }
  233. if (remain <= 1)
  234. goto finally;
  235. actual[0] = '~';
  236. actual++;
  237. remain--;
  238. pos += length - width + 1;
  239. /* copy suffix of text */
  240. for (; pos < length && remain > 1; pos++, actual++, remain--)
  241. {
  242. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  243. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  244. }
  245. }
  246. else
  247. {
  248. ident = 0;
  249. switch (HIDE_FIT (just_mode))
  250. {
  251. case J_CENTER:
  252. ident = (length - width) / 2;
  253. break;
  254. case J_RIGHT:
  255. ident = length - width;
  256. break;
  257. }
  258. /* copy substring text, substring start from ident and take width
  259. * characters from text */
  260. pos += ident;
  261. for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
  262. {
  263. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  264. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  265. }
  266. }
  267. }
  268. finally:
  269. actual[0] = '\0';
  270. return result;
  271. }
  272. static const char *
  273. str_ascii_term_trim (const char *text, int width)
  274. {
  275. static char result[BUF_MEDIUM];
  276. size_t remain;
  277. char *actual;
  278. size_t pos = 0;
  279. size_t length;
  280. length = strlen (text);
  281. actual = result;
  282. remain = sizeof (result);
  283. if (width < (int) length)
  284. {
  285. if (width <= 3)
  286. {
  287. memset (actual, '.', width);
  288. actual += width;
  289. remain -= width;
  290. }
  291. else
  292. {
  293. memset (actual, '.', 3);
  294. actual += 3;
  295. remain -= 3;
  296. pos += length - width + 3;
  297. /* copy suffix of text */
  298. for (; pos < length && remain > 1; pos++, actual++, remain--)
  299. {
  300. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  301. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  302. }
  303. }
  304. }
  305. else
  306. {
  307. /* copy all characters */
  308. for (; pos < length && remain > 1; pos++, actual++, remain--)
  309. {
  310. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  311. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  312. }
  313. }
  314. actual[0] = '\0';
  315. return result;
  316. }
  317. static int
  318. str_ascii_term_width2 (const char *text, size_t length)
  319. {
  320. return (length != (size_t) (-1)) ? min (strlen (text), length) : strlen (text);
  321. }
  322. static int
  323. str_ascii_term_width1 (const char *text)
  324. {
  325. return str_ascii_term_width2 (text, (size_t) (-1));
  326. }
  327. static int
  328. str_ascii_term_char_width (const char *text)
  329. {
  330. (void) text;
  331. return 1;
  332. }
  333. static void
  334. str_ascii_msg_term_size (const char *text, int *lines, int *columns)
  335. {
  336. char *p, *tmp;
  337. char *q;
  338. char c = '\0';
  339. int width;
  340. (*lines) = 1;
  341. (*columns) = 0;
  342. tmp = g_strdup (text);
  343. p = tmp;
  344. for (;;)
  345. {
  346. q = strchr (p, '\n');
  347. if (q != NULL)
  348. {
  349. c = q[0];
  350. q[0] = '\0';
  351. }
  352. width = str_ascii_term_width1 (p);
  353. if (width > (*columns))
  354. (*columns) = width;
  355. if (q == NULL)
  356. break;
  357. q[0] = c;
  358. p = q + 1;
  359. (*lines)++;
  360. }
  361. g_free (tmp);
  362. }
  363. static const char *
  364. str_ascii_term_substring (const char *text, int start, int width)
  365. {
  366. static char result[BUF_MEDIUM];
  367. size_t remain;
  368. char *actual;
  369. size_t pos = 0;
  370. size_t length;
  371. actual = result;
  372. remain = sizeof (result);
  373. length = strlen (text);
  374. if (start < (int) length)
  375. {
  376. pos += start;
  377. /* copy at most width characters from text from start */
  378. for (; pos < length && width > 0 && remain > 1; pos++, width--, actual++, remain--)
  379. {
  380. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  381. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  382. }
  383. }
  384. /* if text is shorter then width, add space to the end */
  385. for (; width > 0 && remain > 1; actual++, remain--, width--)
  386. {
  387. actual[0] = ' ';
  388. }
  389. actual[0] = '\0';
  390. return result;
  391. }
  392. static const char *
  393. str_ascii_trunc (const char *text, int width)
  394. {
  395. static char result[MC_MAXPATHLEN];
  396. int remain;
  397. char *actual;
  398. size_t pos = 0;
  399. size_t length;
  400. actual = result;
  401. remain = sizeof (result);
  402. length = strlen (text);
  403. if ((int) length > width)
  404. {
  405. /* copy prefix of text */
  406. for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
  407. {
  408. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  409. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  410. }
  411. if (remain <= 1)
  412. goto finally;
  413. actual[0] = '~';
  414. actual++;
  415. remain--;
  416. pos += length - width + 1;
  417. /* copy suffix of text */
  418. for (; pos < length && remain > 1; pos++, actual++, remain--)
  419. {
  420. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  421. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  422. }
  423. }
  424. else
  425. {
  426. /* copy all characters */
  427. for (; pos < length && remain > 1; pos++, actual++, remain--)
  428. {
  429. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  430. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  431. }
  432. }
  433. finally:
  434. actual[0] = '\0';
  435. return result;
  436. }
  437. static int
  438. str_ascii_offset_to_pos (const char *text, size_t length)
  439. {
  440. (void) text;
  441. return (int) length;
  442. }
  443. static int
  444. str_ascii_column_to_pos (const char *text, size_t pos)
  445. {
  446. (void) text;
  447. return (int) pos;
  448. }
  449. static char *
  450. str_ascii_create_search_needle (const char *needle, int case_sen)
  451. {
  452. (void) case_sen;
  453. return (char *) needle;
  454. }
  455. static void
  456. str_ascii_release_search_needle (char *needle, int case_sen)
  457. {
  458. (void) case_sen;
  459. (void) needle;
  460. }
  461. static const char *
  462. str_ascii_search_first (const char *text, const char *search, int case_sen)
  463. {
  464. char *fold_text;
  465. char *fold_search;
  466. const char *match;
  467. size_t offset;
  468. fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
  469. fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
  470. match = g_strstr_len (fold_text, -1, fold_search);
  471. if (match != NULL)
  472. {
  473. offset = match - fold_text;
  474. match = text + offset;
  475. }
  476. if (!case_sen)
  477. {
  478. g_free (fold_text);
  479. g_free (fold_search);
  480. }
  481. return match;
  482. }
  483. static const char *
  484. str_ascii_search_last (const char *text, const char *search, int case_sen)
  485. {
  486. char *fold_text;
  487. char *fold_search;
  488. const char *match;
  489. size_t offset;
  490. fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
  491. fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
  492. match = g_strrstr_len (fold_text, -1, fold_search);
  493. if (match != NULL)
  494. {
  495. offset = match - fold_text;
  496. match = text + offset;
  497. }
  498. if (!case_sen)
  499. {
  500. g_free (fold_text);
  501. g_free (fold_search);
  502. }
  503. return match;
  504. }
  505. static int
  506. str_ascii_compare (const char *t1, const char *t2)
  507. {
  508. return strcmp (t1, t2);
  509. }
  510. static int
  511. str_ascii_ncompare (const char *t1, const char *t2)
  512. {
  513. return strncmp (t1, t2, min (strlen (t1), strlen (t2)));
  514. }
  515. static int
  516. str_ascii_casecmp (const char *t1, const char *t2)
  517. {
  518. return g_ascii_strcasecmp (t1, t2);
  519. }
  520. static int
  521. str_ascii_ncasecmp (const char *t1, const char *t2)
  522. {
  523. return g_ascii_strncasecmp (t1, t2, min (strlen (t1), strlen (t2)));
  524. }
  525. static void
  526. str_ascii_fix_string (char *text)
  527. {
  528. for (; text[0] != '\0'; text++)
  529. {
  530. text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
  531. }
  532. }
  533. static char *
  534. str_ascii_create_key (const char *text, int case_sen)
  535. {
  536. (void) case_sen;
  537. return (char *) text;
  538. }
  539. static int
  540. str_ascii_key_collate (const char *t1, const char *t2, int case_sen)
  541. {
  542. return (case_sen) ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
  543. }
  544. static void
  545. str_ascii_release_key (char *key, int case_sen)
  546. {
  547. (void) key;
  548. (void) case_sen;
  549. }
  550. static int
  551. str_ascii_prefix (const char *text, const char *prefix)
  552. {
  553. int result;
  554. for (result = 0; text[result] != '\0' && prefix[result] != '\0'
  555. && text[result] == prefix[result]; result++);
  556. return result;
  557. }
  558. static int
  559. str_ascii_caseprefix (const char *text, const char *prefix)
  560. {
  561. int result;
  562. for (result = 0; text[result] != '\0' && prefix[result] != '\0'
  563. && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);
  564. return result;
  565. }
  566. struct str_class
  567. str_ascii_init (void)
  568. {
  569. struct str_class result;
  570. result.conv_gerror_message = str_ascii_conv_gerror_message;
  571. result.vfs_convert_to = str_ascii_vfs_convert_to;
  572. result.insert_replace_char = str_ascii_insert_replace_char;
  573. result.is_valid_string = str_ascii_is_valid_string;
  574. result.is_valid_char = str_ascii_is_valid_char;
  575. result.cnext_char = str_ascii_cnext_char;
  576. result.cprev_char = str_ascii_cprev_char;
  577. result.cnext_char_safe = str_ascii_cnext_char;
  578. result.cprev_char_safe = str_ascii_cprev_char;
  579. result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
  580. result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
  581. result.isspace = str_ascii_isspace;
  582. result.ispunct = str_ascii_ispunct;
  583. result.isalnum = str_ascii_isalnum;
  584. result.isdigit = str_ascii_isdigit;
  585. result.isprint = str_ascii_isprint;
  586. result.iscombiningmark = str_ascii_iscombiningmark;
  587. result.toupper = str_ascii_toupper;
  588. result.tolower = str_ascii_tolower;
  589. result.length = str_ascii_length;
  590. result.length2 = str_ascii_length2;
  591. result.length_noncomb = str_ascii_length;
  592. result.fix_string = str_ascii_fix_string;
  593. result.term_form = str_ascii_term_form;
  594. result.fit_to_term = str_ascii_fit_to_term;
  595. result.term_trim = str_ascii_term_trim;
  596. result.term_width2 = str_ascii_term_width2;
  597. result.term_width1 = str_ascii_term_width1;
  598. result.term_char_width = str_ascii_term_char_width;
  599. result.msg_term_size = str_ascii_msg_term_size;
  600. result.term_substring = str_ascii_term_substring;
  601. result.trunc = str_ascii_trunc;
  602. result.offset_to_pos = str_ascii_offset_to_pos;
  603. result.column_to_pos = str_ascii_column_to_pos;
  604. result.create_search_needle = str_ascii_create_search_needle;
  605. result.release_search_needle = str_ascii_release_search_needle;
  606. result.search_first = str_ascii_search_first;
  607. result.search_last = str_ascii_search_last;
  608. result.compare = str_ascii_compare;
  609. result.ncompare = str_ascii_ncompare;
  610. result.casecmp = str_ascii_casecmp;
  611. result.ncasecmp = str_ascii_ncasecmp;
  612. result.prefix = str_ascii_prefix;
  613. result.caseprefix = str_ascii_caseprefix;
  614. result.create_key = str_ascii_create_key;
  615. result.create_key_for_filename = str_ascii_create_key;
  616. result.key_collate = str_ascii_key_collate;
  617. result.release_key = str_ascii_release_key;
  618. return result;
  619. }