This source file includes following definitions.
- str_ascii_insert_replace_char
- str_ascii_is_valid_string
- str_ascii_is_valid_char
- str_ascii_cnext_char
- str_ascii_cprev_char
- str_ascii_cnext_noncomb_char
- str_ascii_cprev_noncomb_char
- str_ascii_isspace
- str_ascii_ispunct
- str_ascii_isalnum
- str_ascii_isdigit
- str_ascii_isprint
- str_ascii_iscombiningmark
- str_ascii_toupper
- str_ascii_tolower
- str_ascii_length
- str_ascii_length2
- str_ascii_conv_gerror_message
- str_ascii_vfs_convert_to
- str_ascii_term_form
- str_ascii_fit_to_term
- str_ascii_term_trim
- str_ascii_term_width2
- str_ascii_term_width1
- str_ascii_term_char_width
- str_ascii_term_substring
- str_ascii_trunc
- str_ascii_offset_to_pos
- str_ascii_column_to_pos
- str_ascii_create_search_needle
- str_ascii_release_search_needle
- str_ascii_search_first
- str_ascii_search_last
- str_ascii_compare
- str_ascii_ncompare
- str_ascii_casecmp
- str_ascii_ncasecmp
- str_ascii_fix_string
- str_ascii_create_key
- str_ascii_key_collate
- str_ascii_release_key
- str_ascii_prefix
- str_ascii_caseprefix
- str_ascii_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #include <config.h>
27
28 #include <ctype.h>
29 #include <stdlib.h>
30
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 static const char replch = '?';
49
50
51
52
53
54 static void
55 str_ascii_insert_replace_char (GString *buffer)
56 {
57 g_string_append_c (buffer, replch);
58 }
59
60
61
62 static gboolean
63 str_ascii_is_valid_string (const char *text)
64 {
65 (void) text;
66 return TRUE;
67 }
68
69
70
71 static int
72 str_ascii_is_valid_char (const char *ch, size_t size)
73 {
74 (void) ch;
75 (void) size;
76 return 1;
77 }
78
79
80
81 static void
82 str_ascii_cnext_char (const char **text)
83 {
84 (*text)++;
85 }
86
87
88
89 static void
90 str_ascii_cprev_char (const char **text)
91 {
92 (*text)--;
93 }
94
95
96
97 static int
98 str_ascii_cnext_noncomb_char (const char **text)
99 {
100 if (*text[0] == '\0')
101 return 0;
102
103 (*text)++;
104 return 1;
105 }
106
107
108
109 static int
110 str_ascii_cprev_noncomb_char (const char **text, const char *begin)
111 {
112 if ((*text) == begin)
113 return 0;
114
115 (*text)--;
116 return 1;
117 }
118
119
120
121 static gboolean
122 str_ascii_isspace (const char *text)
123 {
124 return g_ascii_isspace ((gchar) text[0]);
125 }
126
127
128
129 static gboolean
130 str_ascii_ispunct (const char *text)
131 {
132 return g_ascii_ispunct ((gchar) text[0]);
133 }
134
135
136
137 static gboolean
138 str_ascii_isalnum (const char *text)
139 {
140 return g_ascii_isalnum ((gchar) text[0]);
141 }
142
143
144
145 static gboolean
146 str_ascii_isdigit (const char *text)
147 {
148 return g_ascii_isdigit ((gchar) text[0]);
149 }
150
151
152
153 static gboolean
154 str_ascii_isprint (const char *text)
155 {
156 return g_ascii_isprint ((gchar) text[0]);
157 }
158
159
160
161 static gboolean
162 str_ascii_iscombiningmark (const char *text)
163 {
164 (void) text;
165 return FALSE;
166 }
167
168
169
170 static int
171 str_ascii_toupper (const char *text, char **out, size_t *remain)
172 {
173 if (*remain <= 1)
174 return FALSE;
175
176 (*out)[0] = (char) g_ascii_toupper ((gchar) text[0]);
177 (*out)++;
178 (*remain)--;
179 return TRUE;
180 }
181
182
183
184 static gboolean
185 str_ascii_tolower (const char *text, char **out, size_t *remain)
186 {
187 if (*remain <= 1)
188 return FALSE;
189
190 (*out)[0] = (char) g_ascii_tolower ((gchar) text[0]);
191 (*out)++;
192 (*remain)--;
193 return TRUE;
194 }
195
196
197
198 static int
199 str_ascii_length (const char *text)
200 {
201 return strlen (text);
202 }
203
204
205
206 static int
207 str_ascii_length2 (const char *text, int size)
208 {
209 size_t length;
210
211 length = strlen (text);
212
213 return (size >= 0) ? MIN (length, (size_t) size) : length;
214 }
215
216
217
218 static gchar *
219 str_ascii_conv_gerror_message (GError *mcerror, const char *def_msg)
220 {
221
222 if (mcerror != NULL)
223 return g_strdup (mcerror->message);
224
225 return g_strdup (def_msg != NULL ? def_msg : "");
226 }
227
228
229
230 static estr_t
231 str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString *buffer)
232 {
233 (void) coder;
234 g_string_append_len (buffer, string, size);
235 return ESTR_SUCCESS;
236 }
237
238
239
240 static const char *
241 str_ascii_term_form (const char *text)
242 {
243 static char result[BUF_MEDIUM];
244 char *actual;
245 size_t remain;
246 size_t length;
247 size_t pos = 0;
248
249 actual = result;
250 remain = sizeof (result);
251 length = strlen (text);
252
253
254 for (; pos < length && remain > 1; pos++, actual++, remain--)
255 {
256 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
257 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
258 }
259
260 actual[0] = '\0';
261 return result;
262 }
263
264
265
266 static const char *
267 str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
268 {
269 static char result[BUF_MEDIUM];
270 char *actual;
271 size_t remain;
272 int ident = 0;
273 size_t length;
274 size_t pos = 0;
275
276 length = strlen (text);
277 actual = result;
278 remain = sizeof (result);
279
280 if ((int) length <= width)
281 {
282 switch (HIDE_FIT (just_mode))
283 {
284 case J_CENTER_LEFT:
285 case J_CENTER:
286 ident = (width - length) / 2;
287 break;
288 case J_RIGHT:
289 ident = width - length;
290 break;
291 default:
292 break;
293 }
294
295
296 if ((int) remain <= ident)
297 goto finally;
298 memset (actual, ' ', ident);
299 actual += ident;
300 remain -= ident;
301
302
303 for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
304 {
305 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
306 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
307 }
308
309
310 if (width - length - ident > 0)
311 {
312 if (remain <= width - length - ident)
313 goto finally;
314 memset (actual, ' ', width - length - ident);
315 actual += width - length - ident;
316 }
317 }
318 else if (IS_FIT (just_mode))
319 {
320
321 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
322 {
323 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
324 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
325 }
326
327 if (remain <= 1)
328 goto finally;
329 actual[0] = '~';
330 actual++;
331 remain--;
332
333 pos += length - width + 1;
334
335
336 for (; pos < length && remain > 1; pos++, actual++, remain--)
337 {
338 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
339 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
340 }
341 }
342 else
343 {
344 switch (HIDE_FIT (just_mode))
345 {
346 case J_CENTER:
347 ident = (length - width) / 2;
348 break;
349 case J_RIGHT:
350 ident = length - width;
351 break;
352 default:
353 break;
354 }
355
356
357
358 pos += ident;
359 for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
360 {
361 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
362 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
363 }
364 }
365
366 finally:
367 if (actual >= result + sizeof (result))
368 actual = result + sizeof (result) - 1;
369 actual[0] = '\0';
370 return result;
371 }
372
373
374
375 static const char *
376 str_ascii_term_trim (const char *text, int width)
377 {
378 static char result[BUF_MEDIUM];
379 size_t remain;
380 char *actual;
381 size_t length;
382
383 length = strlen (text);
384 actual = result;
385 remain = sizeof (result);
386
387 if (width > 0)
388 {
389 size_t pos;
390
391 if (width >= (int) length)
392 {
393
394 for (pos = 0; pos < length && remain > 1; pos++, actual++, remain--)
395 {
396 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
397 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
398 }
399 }
400 else if (width <= 3)
401 {
402 memset (actual, '.', width);
403 actual += width;
404 }
405 else
406 {
407 memset (actual, '.', 3);
408 actual += 3;
409 remain -= 3;
410
411
412 for (pos = length - width + 3; pos < length && remain > 1; pos++, actual++, remain--)
413 {
414 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
415 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
416 }
417 }
418 }
419
420 actual[0] = '\0';
421 return result;
422 }
423
424
425
426 static int
427 str_ascii_term_width2 (const char *text, size_t length)
428 {
429 size_t text_len;
430
431 text_len = strlen (text);
432
433 return (length != (size_t) (-1)) ? MIN (text_len, length) : text_len;
434 }
435
436
437
438 static int
439 str_ascii_term_width1 (const char *text)
440 {
441 return str_ascii_term_width2 (text, (size_t) (-1));
442 }
443
444
445
446 static int
447 str_ascii_term_char_width (const char *text)
448 {
449 (void) text;
450 return 1;
451 }
452
453
454
455 static const char *
456 str_ascii_term_substring (const char *text, int start, int width)
457 {
458 static char result[BUF_MEDIUM];
459 size_t remain;
460 char *actual;
461 size_t length;
462
463 actual = result;
464 remain = sizeof (result);
465 length = strlen (text);
466
467 if (start < (int) length)
468 {
469 size_t pos;
470
471
472 for (pos = start; pos < length && width > 0 && remain > 1;
473 pos++, width--, actual++, remain--)
474 {
475 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
476 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
477 }
478 }
479
480
481 for (; width > 0 && remain > 1; actual++, remain--, width--)
482 actual[0] = ' ';
483
484 actual[0] = '\0';
485 return result;
486 }
487
488
489
490 static const char *
491 str_ascii_trunc (const char *text, int width)
492 {
493 static char result[MC_MAXPATHLEN];
494 int remain;
495 char *actual;
496 size_t pos = 0;
497 size_t length;
498
499 actual = result;
500 remain = sizeof (result);
501 length = strlen (text);
502
503 if ((int) length > width)
504 {
505
506 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
507 {
508 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
509 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
510 }
511
512 if (remain <= 1)
513 goto finally;
514 actual[0] = '~';
515 actual++;
516 remain--;
517
518 pos += length - width + 1;
519
520
521 for (; pos < length && remain > 1; pos++, actual++, remain--)
522 {
523 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
524 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
525 }
526 }
527 else
528 {
529
530 for (; pos < length && remain > 1; pos++, actual++, remain--)
531 {
532 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
533 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
534 }
535 }
536
537 finally:
538 actual[0] = '\0';
539 return result;
540 }
541
542
543
544 static int
545 str_ascii_offset_to_pos (const char *text, size_t length)
546 {
547 (void) text;
548 return (int) length;
549 }
550
551
552
553 static int
554 str_ascii_column_to_pos (const char *text, size_t pos)
555 {
556 (void) text;
557 return (int) pos;
558 }
559
560
561
562 static char *
563 str_ascii_create_search_needle (const char *needle, gboolean case_sen)
564 {
565 (void) case_sen;
566 return (char *) needle;
567 }
568
569
570
571 static void
572 str_ascii_release_search_needle (char *needle, gboolean case_sen)
573 {
574 (void) case_sen;
575 (void) needle;
576 }
577
578
579
580 static const char *
581 str_ascii_search_first (const char *text, const char *search, gboolean case_sen)
582 {
583 char *fold_text;
584 char *fold_search;
585 const char *match;
586
587 fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
588 fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);
589
590 match = g_strstr_len (fold_text, -1, fold_search);
591 if (match != NULL)
592 {
593 size_t offset;
594
595 offset = match - fold_text;
596 match = text + offset;
597 }
598
599 if (!case_sen)
600 {
601 g_free (fold_text);
602 g_free (fold_search);
603 }
604
605 return match;
606 }
607
608
609
610 static const char *
611 str_ascii_search_last (const char *text, const char *search, gboolean case_sen)
612 {
613 char *fold_text;
614 char *fold_search;
615 const char *match;
616
617 fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
618 fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);
619
620 match = g_strrstr_len (fold_text, -1, fold_search);
621 if (match != NULL)
622 {
623 size_t offset;
624
625 offset = match - fold_text;
626 match = text + offset;
627 }
628
629 if (!case_sen)
630 {
631 g_free (fold_text);
632 g_free (fold_search);
633 }
634
635 return match;
636 }
637
638
639
640 static int
641 str_ascii_compare (const char *t1, const char *t2)
642 {
643 return strcmp (t1, t2);
644 }
645
646
647
648 static int
649 str_ascii_ncompare (const char *t1, const char *t2)
650 {
651 size_t l1, l2;
652
653 l1 = strlen (t1);
654 l2 = strlen (t2);
655
656 return strncmp (t1, t2, MIN (l1, l2));
657 }
658
659
660
661 static int
662 str_ascii_casecmp (const char *t1, const char *t2)
663 {
664 return g_ascii_strcasecmp (t1, t2);
665 }
666
667
668
669 static int
670 str_ascii_ncasecmp (const char *t1, const char *t2)
671 {
672 size_t l1, l2;
673
674 l1 = strlen (t1);
675 l2 = strlen (t2);
676
677 return g_ascii_strncasecmp (t1, t2, MIN (l1, l2));
678 }
679
680
681
682 static void
683 str_ascii_fix_string (char *text)
684 {
685 for (; text[0] != '\0'; text++)
686 text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
687 }
688
689
690
691 static char *
692 str_ascii_create_key (const char *text, gboolean case_sen)
693 {
694 (void) case_sen;
695 return (char *) text;
696 }
697
698
699
700 static int
701 str_ascii_key_collate (const char *t1, const char *t2, gboolean case_sen)
702 {
703 return case_sen ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
704 }
705
706
707
708 static void
709 str_ascii_release_key (char *key, gboolean case_sen)
710 {
711 (void) key;
712 (void) case_sen;
713 }
714
715
716
717 static int
718 str_ascii_prefix (const char *text, const char *prefix)
719 {
720 int result;
721
722 for (result = 0;
723 text[result] != '\0' && prefix[result] != '\0' && text[result] == prefix[result]; result++)
724 ;
725
726 return result;
727 }
728
729
730
731 static int
732 str_ascii_caseprefix (const char *text, const char *prefix)
733 {
734 int result;
735
736 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
737 && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]);
738 result++)
739 ;
740
741 return result;
742 }
743
744
745
746
747
748 struct str_class
749 str_ascii_init (void)
750 {
751 struct str_class result;
752
753 result.conv_gerror_message = str_ascii_conv_gerror_message;
754 result.vfs_convert_to = str_ascii_vfs_convert_to;
755 result.insert_replace_char = str_ascii_insert_replace_char;
756 result.is_valid_string = str_ascii_is_valid_string;
757 result.is_valid_char = str_ascii_is_valid_char;
758 result.cnext_char = str_ascii_cnext_char;
759 result.cprev_char = str_ascii_cprev_char;
760 result.cnext_char_safe = str_ascii_cnext_char;
761 result.cprev_char_safe = str_ascii_cprev_char;
762 result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
763 result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
764 result.char_isspace = str_ascii_isspace;
765 result.char_ispunct = str_ascii_ispunct;
766 result.char_isalnum = str_ascii_isalnum;
767 result.char_isdigit = str_ascii_isdigit;
768 result.char_isprint = str_ascii_isprint;
769 result.char_iscombiningmark = str_ascii_iscombiningmark;
770 result.char_toupper = str_ascii_toupper;
771 result.char_tolower = str_ascii_tolower;
772 result.length = str_ascii_length;
773 result.length2 = str_ascii_length2;
774 result.length_noncomb = str_ascii_length;
775 result.fix_string = str_ascii_fix_string;
776 result.term_form = str_ascii_term_form;
777 result.fit_to_term = str_ascii_fit_to_term;
778 result.term_trim = str_ascii_term_trim;
779 result.term_width2 = str_ascii_term_width2;
780 result.term_width1 = str_ascii_term_width1;
781 result.term_char_width = str_ascii_term_char_width;
782 result.term_substring = str_ascii_term_substring;
783 result.trunc = str_ascii_trunc;
784 result.offset_to_pos = str_ascii_offset_to_pos;
785 result.column_to_pos = str_ascii_column_to_pos;
786 result.create_search_needle = str_ascii_create_search_needle;
787 result.release_search_needle = str_ascii_release_search_needle;
788 result.search_first = str_ascii_search_first;
789 result.search_last = str_ascii_search_last;
790 result.compare = str_ascii_compare;
791 result.ncompare = str_ascii_ncompare;
792 result.casecmp = str_ascii_casecmp;
793 result.ncasecmp = str_ascii_ncasecmp;
794 result.prefix = str_ascii_prefix;
795 result.caseprefix = str_ascii_caseprefix;
796 result.create_key = str_ascii_create_key;
797 result.create_key_for_filename = str_ascii_create_key;
798 result.key_collate = str_ascii_key_collate;
799 result.release_key = str_ascii_release_key;
800
801 return result;
802 }
803
804