strutilascii.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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,
  151. int size, GString * buffer)
  152. {
  153. (void) coder;
  154. g_string_append_len (buffer, string, size);
  155. return ESTR_SUCCESS;
  156. }
  157. static const char *
  158. str_ascii_term_form (const char *text)
  159. {
  160. static char result[BUF_MEDIUM];
  161. char *actual;
  162. size_t remain;
  163. size_t length;
  164. size_t pos = 0;
  165. actual = result;
  166. remain = sizeof (result);
  167. length = strlen (text);
  168. /* go throw all characters and check, if they are ascii and printable */
  169. for (; pos < length && remain > 1; pos++, actual++, remain--)
  170. {
  171. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  172. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  173. }
  174. actual[0] = '\0';
  175. return result;
  176. }
  177. static const char *
  178. str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
  179. {
  180. static char result[BUF_MEDIUM];
  181. char *actual;
  182. size_t remain;
  183. int ident;
  184. size_t length;
  185. size_t pos = 0;
  186. length = strlen (text);
  187. actual = result;
  188. remain = sizeof (result);
  189. if ((int)length <= width)
  190. {
  191. ident = 0;
  192. switch (HIDE_FIT (just_mode))
  193. {
  194. case J_CENTER_LEFT:
  195. case J_CENTER:
  196. ident = (width - length) / 2;
  197. break;
  198. case J_RIGHT:
  199. ident = width - length;
  200. break;
  201. }
  202. /* add space before text */
  203. if ((int)remain <= ident)
  204. goto finally;
  205. memset (actual, ' ', ident);
  206. actual += ident;
  207. remain -= ident;
  208. /* copy all characters */
  209. for (; pos < (gsize)length && remain > 1; pos++, actual++, remain--)
  210. {
  211. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  212. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  213. }
  214. /* add space after text */
  215. if (width - length - ident > 0)
  216. {
  217. if (remain <= width - length - ident)
  218. goto finally;
  219. memset (actual, ' ', width - length - ident);
  220. actual += width - length - ident;
  221. remain -= width - length - ident;
  222. }
  223. }
  224. else
  225. {
  226. if (IS_FIT (just_mode))
  227. {
  228. /* copy prefix of text, that is not wider than width / 2 */
  229. for (; pos + 1 <= (gsize)width / 2 && remain > 1;
  230. actual++, pos++, remain--)
  231. {
  232. actual[0] = isascii ((unsigned char) text[pos])
  233. ? text[pos] : '?';
  234. actual[0] = g_ascii_isprint ((gchar) actual[0])
  235. ? actual[0] : '.';
  236. }
  237. if (remain <= 1)
  238. goto finally;
  239. actual[0] = '~';
  240. actual++;
  241. remain--;
  242. pos += length - width + 1;
  243. /* copy suffix of text */
  244. for (; pos < length && remain > 1; pos++, actual++, remain--)
  245. {
  246. actual[0] = isascii ((unsigned char) text[pos])
  247. ? text[pos] : '?';
  248. actual[0] = g_ascii_isprint ((gchar) actual[0])
  249. ? actual[0] : '.';
  250. }
  251. }
  252. else
  253. {
  254. ident = 0;
  255. switch (HIDE_FIT (just_mode))
  256. {
  257. case J_CENTER:
  258. ident = (length - width) / 2;
  259. break;
  260. case J_RIGHT:
  261. ident = length - width;
  262. break;
  263. }
  264. /* copy substring text, substring start from ident and take width
  265. * characters from text */
  266. pos += ident;
  267. for (; pos < (gsize)(ident + width) && remain > 1;
  268. pos++, actual++, remain--)
  269. {
  270. actual[0] = isascii ((unsigned char) text[pos])
  271. ? text[pos] : '?';
  272. actual[0] = g_ascii_isprint ((gchar) actual[0])
  273. ? actual[0] : '.';
  274. }
  275. }
  276. }
  277. finally:
  278. actual[0] = '\0';
  279. return result;
  280. }
  281. static const char *
  282. str_ascii_term_trim (const char *text, int width)
  283. {
  284. static char result[BUF_MEDIUM];
  285. size_t remain;
  286. char *actual;
  287. size_t pos = 0;
  288. size_t length;
  289. length = strlen (text);
  290. actual = result;
  291. remain = sizeof (result);
  292. if (width < (int)length)
  293. {
  294. if (width <= 3)
  295. {
  296. memset (actual, '.', width);
  297. actual += width;
  298. remain -= width;
  299. }
  300. else
  301. {
  302. memset (actual, '.', 3);
  303. actual += 3;
  304. remain -= 3;
  305. pos += length - width + 3;
  306. /* copy suffix of text */
  307. for (; pos < length && remain > 1; pos++, actual++, remain--)
  308. {
  309. actual[0] = isascii ((unsigned char) text[pos])
  310. ? text[pos] : '?';
  311. actual[0] = g_ascii_isprint ((gchar) actual[0])
  312. ? actual[0] : '.';
  313. }
  314. }
  315. }
  316. else
  317. {
  318. /* copy all characters */
  319. for (; pos < length && remain > 1; pos++, actual++, remain--)
  320. {
  321. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  322. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  323. }
  324. }
  325. actual[0] = '\0';
  326. return result;
  327. }
  328. static int
  329. str_ascii_term_width2 (const char *text, size_t length)
  330. {
  331. return (length != (size_t) (-1))
  332. ? min (strlen (text), length) : strlen (text);
  333. }
  334. static int
  335. str_ascii_term_width1 (const char *text)
  336. {
  337. return str_ascii_term_width2 (text, (size_t) (-1));
  338. }
  339. static int
  340. str_ascii_term_char_width (const char *text)
  341. {
  342. (void) text;
  343. return 1;
  344. }
  345. static void
  346. str_ascii_msg_term_size (const char *text, int *lines, int *columns)
  347. {
  348. char *p, *tmp;
  349. char *q;
  350. char c = '\0';
  351. int width;
  352. (*lines) = 1;
  353. (*columns) = 0;
  354. tmp = g_strdup (text);
  355. p = tmp;
  356. for (;;)
  357. {
  358. q = strchr (p, '\n');
  359. if (q != NULL)
  360. {
  361. c = q[0];
  362. q[0] = '\0';
  363. }
  364. width = str_ascii_term_width1 (p);
  365. if (width > (*columns))
  366. (*columns) = width;
  367. if (q == NULL)
  368. break;
  369. q[0] = c;
  370. p = q + 1;
  371. (*lines)++;
  372. }
  373. g_free (tmp);
  374. }
  375. static const char *
  376. str_ascii_term_substring (const char *text, int start, int width)
  377. {
  378. static char result[BUF_MEDIUM];
  379. size_t remain;
  380. char *actual;
  381. size_t pos = 0;
  382. size_t length;
  383. actual = result;
  384. remain = sizeof (result);
  385. length = strlen (text);
  386. if (start < (int)length)
  387. {
  388. pos += start;
  389. /* copy at most width characters from text from start */
  390. for (; pos < length && width > 0 && remain > 1;
  391. pos++, width--, actual++, remain--)
  392. {
  393. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  394. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  395. }
  396. }
  397. /* if text is shorter then width, add space to the end */
  398. for (; width > 0 && remain > 1; actual++, remain--, width--)
  399. {
  400. actual[0] = ' ';
  401. }
  402. actual[0] = '\0';
  403. return result;
  404. }
  405. static const char *
  406. str_ascii_trunc (const char *text, int width)
  407. {
  408. static char result[MC_MAXPATHLEN];
  409. int remain;
  410. char *actual;
  411. size_t pos = 0;
  412. size_t length;
  413. actual = result;
  414. remain = sizeof (result);
  415. length = strlen (text);
  416. if ((int)length > width)
  417. {
  418. /* copy prefix of text */
  419. for (; pos + 1 <= (gsize)width / 2 && remain > 1; actual++, pos++, remain--)
  420. {
  421. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  422. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  423. }
  424. if (remain <= 1)
  425. goto finally;
  426. actual[0] = '~';
  427. actual++;
  428. remain--;
  429. pos += length - width + 1;
  430. /* copy suffix of text */
  431. for (; pos < length && remain > 1; pos++, actual++, remain--)
  432. {
  433. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  434. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  435. }
  436. }
  437. else
  438. {
  439. /* copy all characters */
  440. for (; pos < length && remain > 1; pos++, actual++, remain--)
  441. {
  442. actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
  443. actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
  444. }
  445. }
  446. finally:
  447. actual[0] = '\0';
  448. return result;
  449. }
  450. static int
  451. str_ascii_offset_to_pos (const char *text, size_t length)
  452. {
  453. (void) text;
  454. return (int) length;
  455. }
  456. static int
  457. str_ascii_column_to_pos (const char *text, size_t pos)
  458. {
  459. (void) text;
  460. return (int)pos;
  461. }
  462. static char *
  463. str_ascii_create_search_needle (const char *needle, int case_sen)
  464. {
  465. (void) case_sen;
  466. return (char *) needle;
  467. }
  468. static void
  469. str_ascii_release_search_needle (char *needle, int case_sen)
  470. {
  471. (void) case_sen;
  472. (void) needle;
  473. }
  474. static const char *
  475. str_ascii_search_first (const char *text, const char *search, int case_sen)
  476. {
  477. char *fold_text;
  478. char *fold_search;
  479. const char *match;
  480. size_t offset;
  481. fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
  482. fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
  483. match = g_strstr_len (fold_text, -1, fold_search);
  484. if (match != NULL)
  485. {
  486. offset = match - fold_text;
  487. match = text + offset;
  488. }
  489. if (!case_sen)
  490. {
  491. g_free (fold_text);
  492. g_free (fold_search);
  493. }
  494. return match;
  495. }
  496. static const char *
  497. str_ascii_search_last (const char *text, const char *search, int case_sen)
  498. {
  499. char *fold_text;
  500. char *fold_search;
  501. const char *match;
  502. size_t offset;
  503. fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
  504. fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
  505. match = g_strrstr_len (fold_text, -1, fold_search);
  506. if (match != NULL)
  507. {
  508. offset = match - fold_text;
  509. match = text + offset;
  510. }
  511. if (!case_sen)
  512. {
  513. g_free (fold_text);
  514. g_free (fold_search);
  515. }
  516. return match;
  517. }
  518. static int
  519. str_ascii_compare (const char *t1, const char *t2)
  520. {
  521. return strcmp (t1, t2);
  522. }
  523. static int
  524. str_ascii_ncompare (const char *t1, const char *t2)
  525. {
  526. return strncmp (t1, t2, min (strlen (t1), strlen (t2)));
  527. }
  528. static int
  529. str_ascii_casecmp (const char *t1, const char *t2)
  530. {
  531. return g_ascii_strcasecmp (t1, t2);
  532. }
  533. static int
  534. str_ascii_ncasecmp (const char *t1, const char *t2)
  535. {
  536. return g_ascii_strncasecmp (t1, t2, min (strlen (t1), strlen (t2)));
  537. }
  538. static void
  539. str_ascii_fix_string (char *text)
  540. {
  541. for (; text[0] != '\0'; text++)
  542. {
  543. text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
  544. }
  545. }
  546. static char *
  547. str_ascii_create_key (const char *text, int case_sen)
  548. {
  549. (void) case_sen;
  550. return (char *) text;
  551. }
  552. static int
  553. str_ascii_key_collate (const char *t1, const char *t2, int case_sen)
  554. {
  555. return (case_sen) ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
  556. }
  557. static void
  558. str_ascii_release_key (char *key, int case_sen)
  559. {
  560. (void) key;
  561. (void) case_sen;
  562. }
  563. static int
  564. str_ascii_prefix (const char *text, const char *prefix)
  565. {
  566. int result;
  567. for (result = 0; text[result] != '\0' && prefix[result] != '\0'
  568. && text[result] == prefix[result]; result++);
  569. return result;
  570. }
  571. static int
  572. str_ascii_caseprefix (const char *text, const char *prefix)
  573. {
  574. int result;
  575. for (result = 0; text[result] != '\0' && prefix[result] != '\0'
  576. && g_ascii_toupper (text[result]) ==
  577. g_ascii_toupper (prefix[result]); result++);
  578. return result;
  579. }
  580. struct str_class
  581. str_ascii_init (void)
  582. {
  583. struct str_class result;
  584. result.conv_gerror_message = str_ascii_conv_gerror_message;
  585. result.vfs_convert_to = str_ascii_vfs_convert_to;
  586. result.insert_replace_char = str_ascii_insert_replace_char;
  587. result.is_valid_string = str_ascii_is_valid_string;
  588. result.is_valid_char = str_ascii_is_valid_char;
  589. result.cnext_char = str_ascii_cnext_char;
  590. result.cprev_char = str_ascii_cprev_char;
  591. result.cnext_char_safe = str_ascii_cnext_char;
  592. result.cprev_char_safe = str_ascii_cprev_char;
  593. result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
  594. result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
  595. result.isspace = str_ascii_isspace;
  596. result.ispunct = str_ascii_ispunct;
  597. result.isalnum = str_ascii_isalnum;
  598. result.isdigit = str_ascii_isdigit;
  599. result.isprint = str_ascii_isprint;
  600. result.iscombiningmark = str_ascii_iscombiningmark;
  601. result.toupper = str_ascii_toupper;
  602. result.tolower = str_ascii_tolower;
  603. result.length = str_ascii_length;
  604. result.length2 = str_ascii_length2;
  605. result.length_noncomb = str_ascii_length;
  606. result.fix_string = str_ascii_fix_string;
  607. result.term_form = str_ascii_term_form;
  608. result.fit_to_term = str_ascii_fit_to_term;
  609. result.term_trim = str_ascii_term_trim;
  610. result.term_width2 = str_ascii_term_width2;
  611. result.term_width1 = str_ascii_term_width1;
  612. result.term_char_width = str_ascii_term_char_width;
  613. result.msg_term_size = str_ascii_msg_term_size;
  614. result.term_substring = str_ascii_term_substring;
  615. result.trunc = str_ascii_trunc;
  616. result.offset_to_pos = str_ascii_offset_to_pos;
  617. result.column_to_pos = str_ascii_column_to_pos;
  618. result.create_search_needle = str_ascii_create_search_needle;
  619. result.release_search_needle = str_ascii_release_search_needle;
  620. result.search_first = str_ascii_search_first;
  621. result.search_last = str_ascii_search_last;
  622. result.compare = str_ascii_compare;
  623. result.ncompare = str_ascii_ncompare;
  624. result.casecmp = str_ascii_casecmp;
  625. result.ncasecmp = str_ascii_ncasecmp;
  626. result.prefix = str_ascii_prefix;
  627. result.caseprefix = str_ascii_caseprefix;
  628. result.create_key = str_ascii_create_key;
  629. result.create_key_for_filename = str_ascii_create_key;
  630. result.key_collate = str_ascii_key_collate;
  631. result.release_key = str_ascii_release_key;
  632. return result;
  633. }