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
367 finally:
368 if (actual >= result + sizeof (result))
369 actual = result + sizeof (result) - 1;
370 actual[0] = '\0';
371 return result;
372 }
373
374
375
376 static const char *
377 str_ascii_term_trim (const char *text, int width)
378 {
379 static char result[BUF_MEDIUM];
380 size_t remain;
381 char *actual;
382 size_t length;
383
384 length = strlen (text);
385 actual = result;
386 remain = sizeof (result);
387
388 if (width > 0)
389 {
390 size_t pos;
391
392 if (width >= (int) length)
393 {
394
395 for (pos = 0; pos < length && remain > 1; pos++, actual++, remain--)
396 {
397 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
398 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
399 }
400 }
401 else if (width <= 3)
402 {
403 memset (actual, '.', width);
404 actual += width;
405 }
406 else
407 {
408 memset (actual, '.', 3);
409 actual += 3;
410 remain -= 3;
411
412
413 for (pos = length - width + 3; pos < length && remain > 1; pos++, actual++, remain--)
414 {
415 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
416 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
417 }
418 }
419 }
420
421 actual[0] = '\0';
422 return result;
423 }
424
425
426
427 static int
428 str_ascii_term_width2 (const char *text, size_t length)
429 {
430 size_t text_len;
431
432 text_len = strlen (text);
433
434 return (length != (size_t) (-1)) ? MIN (text_len, length) : text_len;
435 }
436
437
438
439 static int
440 str_ascii_term_width1 (const char *text)
441 {
442 return str_ascii_term_width2 (text, (size_t) (-1));
443 }
444
445
446
447 static int
448 str_ascii_term_char_width (const char *text)
449 {
450 (void) text;
451 return 1;
452 }
453
454
455
456 static const char *
457 str_ascii_term_substring (const char *text, int start, int width)
458 {
459 static char result[BUF_MEDIUM];
460 size_t remain;
461 char *actual;
462 size_t length;
463
464 actual = result;
465 remain = sizeof (result);
466 length = strlen (text);
467
468 if (start < (int) length)
469 {
470 size_t pos;
471
472
473 for (pos = start; pos < length && width > 0 && remain > 1;
474 pos++, width--, actual++, remain--)
475 {
476 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
477 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
478 }
479 }
480
481
482 for (; width > 0 && remain > 1; actual++, remain--, width--)
483 actual[0] = ' ';
484
485 actual[0] = '\0';
486 return result;
487 }
488
489
490
491 static const char *
492 str_ascii_trunc (const char *text, int width)
493 {
494 static char result[MC_MAXPATHLEN];
495 int remain;
496 char *actual;
497 size_t pos = 0;
498 size_t length;
499
500 actual = result;
501 remain = sizeof (result);
502 length = strlen (text);
503
504 if ((int) length > width)
505 {
506
507 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
508 {
509 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
510 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
511 }
512
513 if (remain <= 1)
514 goto finally;
515 actual[0] = '~';
516 actual++;
517 remain--;
518
519 pos += length - width + 1;
520
521
522 for (; pos < length && remain > 1; pos++, actual++, remain--)
523 {
524 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
525 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
526 }
527 }
528 else
529 {
530
531 for (; pos < length && remain > 1; pos++, actual++, remain--)
532 {
533 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
534 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
535 }
536 }
537
538 finally:
539 actual[0] = '\0';
540 return result;
541 }
542
543
544
545 static int
546 str_ascii_offset_to_pos (const char *text, size_t length)
547 {
548 (void) text;
549 return (int) length;
550 }
551
552
553
554 static int
555 str_ascii_column_to_pos (const char *text, size_t pos)
556 {
557 (void) text;
558 return (int) pos;
559 }
560
561
562
563 static char *
564 str_ascii_create_search_needle (const char *needle, gboolean case_sen)
565 {
566 (void) case_sen;
567 return (char *) needle;
568 }
569
570
571
572 static void
573 str_ascii_release_search_needle (char *needle, gboolean case_sen)
574 {
575 (void) case_sen;
576 (void) needle;
577
578 }
579
580
581
582 static const char *
583 str_ascii_search_first (const char *text, const char *search, gboolean case_sen)
584 {
585 char *fold_text;
586 char *fold_search;
587 const char *match;
588
589 fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
590 fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);
591
592 match = g_strstr_len (fold_text, -1, fold_search);
593 if (match != NULL)
594 {
595 size_t offset;
596
597 offset = match - fold_text;
598 match = text + offset;
599 }
600
601 if (!case_sen)
602 {
603 g_free (fold_text);
604 g_free (fold_search);
605 }
606
607 return match;
608 }
609
610
611
612 static const char *
613 str_ascii_search_last (const char *text, const char *search, gboolean case_sen)
614 {
615 char *fold_text;
616 char *fold_search;
617 const char *match;
618
619 fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
620 fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);
621
622 match = g_strrstr_len (fold_text, -1, fold_search);
623 if (match != NULL)
624 {
625 size_t offset;
626
627 offset = match - fold_text;
628 match = text + offset;
629 }
630
631 if (!case_sen)
632 {
633 g_free (fold_text);
634 g_free (fold_search);
635 }
636
637 return match;
638 }
639
640
641
642 static int
643 str_ascii_compare (const char *t1, const char *t2)
644 {
645 return strcmp (t1, t2);
646 }
647
648
649
650 static int
651 str_ascii_ncompare (const char *t1, const char *t2)
652 {
653 size_t l1, l2;
654
655 l1 = strlen (t1);
656 l2 = strlen (t2);
657
658 return strncmp (t1, t2, MIN (l1, l2));
659 }
660
661
662
663 static int
664 str_ascii_casecmp (const char *t1, const char *t2)
665 {
666 return g_ascii_strcasecmp (t1, t2);
667 }
668
669
670
671 static int
672 str_ascii_ncasecmp (const char *t1, const char *t2)
673 {
674 size_t l1, l2;
675
676 l1 = strlen (t1);
677 l2 = strlen (t2);
678
679 return g_ascii_strncasecmp (t1, t2, MIN (l1, l2));
680 }
681
682
683
684 static void
685 str_ascii_fix_string (char *text)
686 {
687 for (; text[0] != '\0'; text++)
688 text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
689 }
690
691
692
693 static char *
694 str_ascii_create_key (const char *text, gboolean case_sen)
695 {
696 (void) case_sen;
697 return (char *) text;
698 }
699
700
701
702 static int
703 str_ascii_key_collate (const char *t1, const char *t2, gboolean case_sen)
704 {
705 return case_sen ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
706 }
707
708
709
710 static void
711 str_ascii_release_key (char *key, gboolean case_sen)
712 {
713 (void) key;
714 (void) case_sen;
715 }
716
717
718
719 static int
720 str_ascii_prefix (const char *text, const char *prefix)
721 {
722 int result;
723
724 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
725 && text[result] == prefix[result]; result++);
726
727 return result;
728 }
729
730
731
732 static int
733 str_ascii_caseprefix (const char *text, const char *prefix)
734 {
735 int result;
736
737 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
738 && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);
739
740 return result;
741 }
742
743
744
745
746
747 struct str_class
748 str_ascii_init (void)
749 {
750 struct str_class result;
751
752 result.conv_gerror_message = str_ascii_conv_gerror_message;
753 result.vfs_convert_to = str_ascii_vfs_convert_to;
754 result.insert_replace_char = str_ascii_insert_replace_char;
755 result.is_valid_string = str_ascii_is_valid_string;
756 result.is_valid_char = str_ascii_is_valid_char;
757 result.cnext_char = str_ascii_cnext_char;
758 result.cprev_char = str_ascii_cprev_char;
759 result.cnext_char_safe = str_ascii_cnext_char;
760 result.cprev_char_safe = str_ascii_cprev_char;
761 result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
762 result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
763 result.char_isspace = str_ascii_isspace;
764 result.char_ispunct = str_ascii_ispunct;
765 result.char_isalnum = str_ascii_isalnum;
766 result.char_isdigit = str_ascii_isdigit;
767 result.char_isprint = str_ascii_isprint;
768 result.char_iscombiningmark = str_ascii_iscombiningmark;
769 result.char_toupper = str_ascii_toupper;
770 result.char_tolower = str_ascii_tolower;
771 result.length = str_ascii_length;
772 result.length2 = str_ascii_length2;
773 result.length_noncomb = str_ascii_length;
774 result.fix_string = str_ascii_fix_string;
775 result.term_form = str_ascii_term_form;
776 result.fit_to_term = str_ascii_fit_to_term;
777 result.term_trim = str_ascii_term_trim;
778 result.term_width2 = str_ascii_term_width2;
779 result.term_width1 = str_ascii_term_width1;
780 result.term_char_width = str_ascii_term_char_width;
781 result.term_substring = str_ascii_term_substring;
782 result.trunc = str_ascii_trunc;
783 result.offset_to_pos = str_ascii_offset_to_pos;
784 result.column_to_pos = str_ascii_column_to_pos;
785 result.create_search_needle = str_ascii_create_search_needle;
786 result.release_search_needle = str_ascii_release_search_needle;
787 result.search_first = str_ascii_search_first;
788 result.search_last = str_ascii_search_last;
789 result.compare = str_ascii_compare;
790 result.ncompare = str_ascii_ncompare;
791 result.casecmp = str_ascii_casecmp;
792 result.ncasecmp = str_ascii_ncasecmp;
793 result.prefix = str_ascii_prefix;
794 result.caseprefix = str_ascii_caseprefix;
795 result.create_key = str_ascii_create_key;
796 result.create_key_for_filename = str_ascii_create_key;
797 result.key_collate = str_ascii_key_collate;
798 result.release_key = str_ascii_release_key;
799
800 return result;
801 }
802
803