Ticket #3782: highlight txz extension as archive like tgz, etc.
[midnight-commander.git] / lib / strutil / strutilascii.c
bloba293566f5744d4f0b54f0375e47d86a386ce75d7
1 /*
2 ASCII strings utilities
4 Copyright (C) 2007-2017
5 Free Software Foundation, Inc.
7 Written by:
8 Rostislav Benes, 2007
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <config.h>
28 #include <ctype.h>
29 #include <stdlib.h>
31 #include "lib/global.h"
32 #include "lib/strutil.h"
34 /* using g_ascii function from glib
35 * on terminal are showed only ascii characters (lower than 0x80)
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 static const char replch = '?';
48 /* --------------------------------------------------------------------------------------------- */
49 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
52 static void
53 str_ascii_insert_replace_char (GString * buffer)
55 g_string_append_c (buffer, replch);
58 /* --------------------------------------------------------------------------------------------- */
60 static int
61 str_ascii_is_valid_string (const char *text)
63 (void) text;
64 return 1;
67 /* --------------------------------------------------------------------------------------------- */
69 static int
70 str_ascii_is_valid_char (const char *ch, size_t size)
72 (void) ch;
73 (void) size;
74 return 1;
77 /* --------------------------------------------------------------------------------------------- */
79 static void
80 str_ascii_cnext_char (const char **text)
82 (*text)++;
85 /* --------------------------------------------------------------------------------------------- */
87 static void
88 str_ascii_cprev_char (const char **text)
90 (*text)--;
93 /* --------------------------------------------------------------------------------------------- */
95 static int
96 str_ascii_cnext_noncomb_char (const char **text)
98 if (*text[0] == '\0')
99 return 0;
101 (*text)++;
102 return 1;
105 /* --------------------------------------------------------------------------------------------- */
107 static int
108 str_ascii_cprev_noncomb_char (const char **text, const char *begin)
110 if ((*text) == begin)
111 return 0;
113 (*text)--;
114 return 1;
117 /* --------------------------------------------------------------------------------------------- */
119 static int
120 str_ascii_isspace (const char *text)
122 return g_ascii_isspace ((gchar) text[0]);
125 /* --------------------------------------------------------------------------------------------- */
127 static int
128 str_ascii_ispunct (const char *text)
130 return g_ascii_ispunct ((gchar) text[0]);
133 /* --------------------------------------------------------------------------------------------- */
135 static int
136 str_ascii_isalnum (const char *text)
138 return g_ascii_isalnum ((gchar) text[0]);
141 /* --------------------------------------------------------------------------------------------- */
143 static int
144 str_ascii_isdigit (const char *text)
146 return g_ascii_isdigit ((gchar) text[0]);
149 /* --------------------------------------------------------------------------------------------- */
151 static int
152 str_ascii_isprint (const char *text)
154 return g_ascii_isprint ((gchar) text[0]);
157 /* --------------------------------------------------------------------------------------------- */
159 static gboolean
160 str_ascii_iscombiningmark (const char *text)
162 (void) text;
163 return FALSE;
166 /* --------------------------------------------------------------------------------------------- */
168 static int
169 str_ascii_toupper (const char *text, char **out, size_t * remain)
171 if (*remain <= 1)
172 return 0;
174 (*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
175 (*out)++;
176 (*remain)--;
177 return 1;
180 /* --------------------------------------------------------------------------------------------- */
182 static int
183 str_ascii_tolower (const char *text, char **out, size_t * remain)
185 if (*remain <= 1)
186 return 0;
188 (*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
189 (*out)++;
190 (*remain)--;
191 return 1;
194 /* --------------------------------------------------------------------------------------------- */
196 static int
197 str_ascii_length (const char *text)
199 return strlen (text);
202 /* --------------------------------------------------------------------------------------------- */
204 static int
205 str_ascii_length2 (const char *text, int size)
207 return (size >= 0) ? MIN (strlen (text), (gsize) size) : strlen (text);
210 /* --------------------------------------------------------------------------------------------- */
212 static gchar *
213 str_ascii_conv_gerror_message (GError * mcerror, const char *def_msg)
215 /* the same as str_utf8_conv_gerror_message() */
216 if (mcerror != NULL)
217 return g_strdup (mcerror->message);
219 return g_strdup (def_msg != NULL ? def_msg : "");
222 /* --------------------------------------------------------------------------------------------- */
224 static estr_t
225 str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
227 (void) coder;
228 g_string_append_len (buffer, string, size);
229 return ESTR_SUCCESS;
232 /* --------------------------------------------------------------------------------------------- */
234 static const char *
235 str_ascii_term_form (const char *text)
237 static char result[BUF_MEDIUM];
238 char *actual;
239 size_t remain;
240 size_t length;
241 size_t pos = 0;
243 actual = result;
244 remain = sizeof (result);
245 length = strlen (text);
247 /* go throw all characters and check, if they are ascii and printable */
248 for (; pos < length && remain > 1; pos++, actual++, remain--)
250 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
251 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
254 actual[0] = '\0';
255 return result;
258 /* --------------------------------------------------------------------------------------------- */
260 static const char *
261 str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
263 static char result[BUF_MEDIUM];
264 char *actual;
265 size_t remain;
266 int ident = 0;
267 size_t length;
268 size_t pos = 0;
270 length = strlen (text);
271 actual = result;
272 remain = sizeof (result);
274 if ((int) length <= width)
276 switch (HIDE_FIT (just_mode))
278 case J_CENTER_LEFT:
279 case J_CENTER:
280 ident = (width - length) / 2;
281 break;
282 case J_RIGHT:
283 ident = width - length;
284 break;
285 default:
286 break;
289 /* add space before text */
290 if ((int) remain <= ident)
291 goto finally;
292 memset (actual, ' ', ident);
293 actual += ident;
294 remain -= ident;
296 /* copy all characters */
297 for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
299 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
300 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
303 /* add space after text */
304 if (width - length - ident > 0)
306 if (remain <= width - length - ident)
307 goto finally;
308 memset (actual, ' ', width - length - ident);
309 actual += width - length - ident;
312 else if (IS_FIT (just_mode))
314 /* copy prefix of text, that is not wider than width / 2 */
315 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
317 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
318 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
321 if (remain <= 1)
322 goto finally;
323 actual[0] = '~';
324 actual++;
325 remain--;
327 pos += length - width + 1;
329 /* copy suffix of text */
330 for (; pos < length && remain > 1; pos++, actual++, remain--)
332 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
333 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
336 else
338 switch (HIDE_FIT (just_mode))
340 case J_CENTER:
341 ident = (length - width) / 2;
342 break;
343 case J_RIGHT:
344 ident = length - width;
345 break;
346 default:
347 break;
350 /* copy substring text, substring start from ident and take width
351 * characters from text */
352 pos += ident;
353 for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
355 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
356 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
361 finally:
362 if (actual >= result + sizeof (result))
363 actual = result + sizeof (result) - 1;
364 actual[0] = '\0';
365 return result;
368 /* --------------------------------------------------------------------------------------------- */
370 static const char *
371 str_ascii_term_trim (const char *text, int width)
373 static char result[BUF_MEDIUM];
374 size_t remain;
375 char *actual;
376 size_t length;
378 length = strlen (text);
379 actual = result;
380 remain = sizeof (result);
382 if (width > 0)
384 size_t pos;
386 if (width >= (int) length)
388 /* copy all characters */
389 for (pos = 0; pos < length && remain > 1; pos++, actual++, remain--)
391 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
392 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
395 else if (width <= 3)
397 memset (actual, '.', width);
398 actual += width;
400 else
402 memset (actual, '.', 3);
403 actual += 3;
404 remain -= 3;
406 /* copy suffix of text */
407 for (pos = length - width + 3; pos < length && remain > 1; pos++, actual++, remain--)
409 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
410 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
415 actual[0] = '\0';
416 return result;
419 /* --------------------------------------------------------------------------------------------- */
421 static int
422 str_ascii_term_width2 (const char *text, size_t length)
424 return (length != (size_t) (-1)) ? MIN (strlen (text), length) : strlen (text);
427 /* --------------------------------------------------------------------------------------------- */
429 static int
430 str_ascii_term_width1 (const char *text)
432 return str_ascii_term_width2 (text, (size_t) (-1));
435 /* --------------------------------------------------------------------------------------------- */
437 static int
438 str_ascii_term_char_width (const char *text)
440 (void) text;
441 return 1;
444 /* --------------------------------------------------------------------------------------------- */
446 static const char *
447 str_ascii_term_substring (const char *text, int start, int width)
449 static char result[BUF_MEDIUM];
450 size_t remain;
451 char *actual;
452 size_t length;
454 actual = result;
455 remain = sizeof (result);
456 length = strlen (text);
458 if (start < (int) length)
460 size_t pos;
462 /* copy at most width characters from text from start */
463 for (pos = start; pos < length && width > 0 && remain > 1;
464 pos++, width--, actual++, remain--)
466 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
467 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
471 /* if text is shorter then width, add space to the end */
472 for (; width > 0 && remain > 1; actual++, remain--, width--)
473 actual[0] = ' ';
475 actual[0] = '\0';
476 return result;
479 /* --------------------------------------------------------------------------------------------- */
481 static const char *
482 str_ascii_trunc (const char *text, int width)
484 static char result[MC_MAXPATHLEN];
485 int remain;
486 char *actual;
487 size_t pos = 0;
488 size_t length;
490 actual = result;
491 remain = sizeof (result);
492 length = strlen (text);
494 if ((int) length > width)
496 /* copy prefix of text */
497 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
499 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
500 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
503 if (remain <= 1)
504 goto finally;
505 actual[0] = '~';
506 actual++;
507 remain--;
509 pos += length - width + 1;
511 /* copy suffix of text */
512 for (; pos < length && remain > 1; pos++, actual++, remain--)
514 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
515 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
518 else
520 /* copy all characters */
521 for (; pos < length && remain > 1; pos++, actual++, remain--)
523 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
524 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
528 finally:
529 actual[0] = '\0';
530 return result;
533 /* --------------------------------------------------------------------------------------------- */
535 static int
536 str_ascii_offset_to_pos (const char *text, size_t length)
538 (void) text;
539 return (int) length;
542 /* --------------------------------------------------------------------------------------------- */
544 static int
545 str_ascii_column_to_pos (const char *text, size_t pos)
547 (void) text;
548 return (int) pos;
551 /* --------------------------------------------------------------------------------------------- */
553 static char *
554 str_ascii_create_search_needle (const char *needle, int case_sen)
556 (void) case_sen;
557 return (char *) needle;
560 /* --------------------------------------------------------------------------------------------- */
562 static void
563 str_ascii_release_search_needle (char *needle, int case_sen)
565 (void) case_sen;
566 (void) needle;
570 /* --------------------------------------------------------------------------------------------- */
572 static const char *
573 str_ascii_search_first (const char *text, const char *search, int case_sen)
575 char *fold_text;
576 char *fold_search;
577 const char *match;
579 fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
580 fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
582 match = g_strstr_len (fold_text, -1, fold_search);
583 if (match != NULL)
585 size_t offset;
587 offset = match - fold_text;
588 match = text + offset;
591 if (!case_sen)
593 g_free (fold_text);
594 g_free (fold_search);
597 return match;
600 /* --------------------------------------------------------------------------------------------- */
602 static const char *
603 str_ascii_search_last (const char *text, const char *search, int case_sen)
605 char *fold_text;
606 char *fold_search;
607 const char *match;
609 fold_text = (case_sen) ? (char *) text : g_ascii_strdown (text, -1);
610 fold_search = (case_sen) ? (char *) search : g_ascii_strdown (search, -1);
612 match = g_strrstr_len (fold_text, -1, fold_search);
613 if (match != NULL)
615 size_t offset;
617 offset = match - fold_text;
618 match = text + offset;
621 if (!case_sen)
623 g_free (fold_text);
624 g_free (fold_search);
627 return match;
630 /* --------------------------------------------------------------------------------------------- */
632 static int
633 str_ascii_compare (const char *t1, const char *t2)
635 return strcmp (t1, t2);
638 /* --------------------------------------------------------------------------------------------- */
640 static int
641 str_ascii_ncompare (const char *t1, const char *t2)
643 return strncmp (t1, t2, MIN (strlen (t1), strlen (t2)));
646 /* --------------------------------------------------------------------------------------------- */
648 static int
649 str_ascii_casecmp (const char *t1, const char *t2)
651 return g_ascii_strcasecmp (t1, t2);
654 /* --------------------------------------------------------------------------------------------- */
656 static int
657 str_ascii_ncasecmp (const char *t1, const char *t2)
659 return g_ascii_strncasecmp (t1, t2, MIN (strlen (t1), strlen (t2)));
662 /* --------------------------------------------------------------------------------------------- */
664 static void
665 str_ascii_fix_string (char *text)
667 for (; text[0] != '\0'; text++)
668 text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
671 /* --------------------------------------------------------------------------------------------- */
673 static char *
674 str_ascii_create_key (const char *text, int case_sen)
676 (void) case_sen;
677 return (char *) text;
680 /* --------------------------------------------------------------------------------------------- */
682 static int
683 str_ascii_key_collate (const char *t1, const char *t2, int case_sen)
685 return (case_sen) ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
688 /* --------------------------------------------------------------------------------------------- */
690 static void
691 str_ascii_release_key (char *key, int case_sen)
693 (void) key;
694 (void) case_sen;
697 /* --------------------------------------------------------------------------------------------- */
699 static int
700 str_ascii_prefix (const char *text, const char *prefix)
702 int result;
704 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
705 && text[result] == prefix[result]; result++);
707 return result;
710 /* --------------------------------------------------------------------------------------------- */
712 static int
713 str_ascii_caseprefix (const char *text, const char *prefix)
715 int result;
717 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
718 && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);
720 return result;
723 /* --------------------------------------------------------------------------------------------- */
724 /*** public functions ****************************************************************************/
725 /* --------------------------------------------------------------------------------------------- */
727 struct str_class
728 str_ascii_init (void)
730 struct str_class result;
732 result.conv_gerror_message = str_ascii_conv_gerror_message;
733 result.vfs_convert_to = str_ascii_vfs_convert_to;
734 result.insert_replace_char = str_ascii_insert_replace_char;
735 result.is_valid_string = str_ascii_is_valid_string;
736 result.is_valid_char = str_ascii_is_valid_char;
737 result.cnext_char = str_ascii_cnext_char;
738 result.cprev_char = str_ascii_cprev_char;
739 result.cnext_char_safe = str_ascii_cnext_char;
740 result.cprev_char_safe = str_ascii_cprev_char;
741 result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
742 result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
743 result.char_isspace = str_ascii_isspace;
744 result.char_ispunct = str_ascii_ispunct;
745 result.char_isalnum = str_ascii_isalnum;
746 result.char_isdigit = str_ascii_isdigit;
747 result.char_isprint = str_ascii_isprint;
748 result.char_iscombiningmark = str_ascii_iscombiningmark;
749 result.char_toupper = str_ascii_toupper;
750 result.char_tolower = str_ascii_tolower;
751 result.length = str_ascii_length;
752 result.length2 = str_ascii_length2;
753 result.length_noncomb = str_ascii_length;
754 result.fix_string = str_ascii_fix_string;
755 result.term_form = str_ascii_term_form;
756 result.fit_to_term = str_ascii_fit_to_term;
757 result.term_trim = str_ascii_term_trim;
758 result.term_width2 = str_ascii_term_width2;
759 result.term_width1 = str_ascii_term_width1;
760 result.term_char_width = str_ascii_term_char_width;
761 result.term_substring = str_ascii_term_substring;
762 result.trunc = str_ascii_trunc;
763 result.offset_to_pos = str_ascii_offset_to_pos;
764 result.column_to_pos = str_ascii_column_to_pos;
765 result.create_search_needle = str_ascii_create_search_needle;
766 result.release_search_needle = str_ascii_release_search_needle;
767 result.search_first = str_ascii_search_first;
768 result.search_last = str_ascii_search_last;
769 result.compare = str_ascii_compare;
770 result.ncompare = str_ascii_ncompare;
771 result.casecmp = str_ascii_casecmp;
772 result.ncasecmp = str_ascii_ncasecmp;
773 result.prefix = str_ascii_prefix;
774 result.caseprefix = str_ascii_caseprefix;
775 result.create_key = str_ascii_create_key;
776 result.create_key_for_filename = str_ascii_create_key;
777 result.key_collate = str_ascii_key_collate;
778 result.release_key = str_ascii_release_key;
780 return result;
783 /* --------------------------------------------------------------------------------------------- */