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 return (size >= 0) ? MIN (strlen (text), (gsize) size) : strlen (text);
210 }
211
212
213
214 static gchar *
215 str_ascii_conv_gerror_message (GError * mcerror, const char *def_msg)
216 {
217
218 if (mcerror != NULL)
219 return g_strdup (mcerror->message);
220
221 return g_strdup (def_msg != NULL ? def_msg : "");
222 }
223
224
225
226 static estr_t
227 str_ascii_vfs_convert_to (GIConv coder, const char *string, int size, GString * buffer)
228 {
229 (void) coder;
230 g_string_append_len (buffer, string, size);
231 return ESTR_SUCCESS;
232 }
233
234
235
236 static const char *
237 str_ascii_term_form (const char *text)
238 {
239 static char result[BUF_MEDIUM];
240 char *actual;
241 size_t remain;
242 size_t length;
243 size_t pos = 0;
244
245 actual = result;
246 remain = sizeof (result);
247 length = strlen (text);
248
249
250 for (; pos < length && remain > 1; pos++, actual++, remain--)
251 {
252 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
253 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
254 }
255
256 actual[0] = '\0';
257 return result;
258 }
259
260
261
262 static const char *
263 str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
264 {
265 static char result[BUF_MEDIUM];
266 char *actual;
267 size_t remain;
268 int ident = 0;
269 size_t length;
270 size_t pos = 0;
271
272 length = strlen (text);
273 actual = result;
274 remain = sizeof (result);
275
276 if ((int) length <= width)
277 {
278 switch (HIDE_FIT (just_mode))
279 {
280 case J_CENTER_LEFT:
281 case J_CENTER:
282 ident = (width - length) / 2;
283 break;
284 case J_RIGHT:
285 ident = width - length;
286 break;
287 default:
288 break;
289 }
290
291
292 if ((int) remain <= ident)
293 goto finally;
294 memset (actual, ' ', ident);
295 actual += ident;
296 remain -= ident;
297
298
299 for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
300 {
301 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
302 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
303 }
304
305
306 if (width - length - ident > 0)
307 {
308 if (remain <= width - length - ident)
309 goto finally;
310 memset (actual, ' ', width - length - ident);
311 actual += width - length - ident;
312 }
313 }
314 else if (IS_FIT (just_mode))
315 {
316
317 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
318 {
319 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
320 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
321 }
322
323 if (remain <= 1)
324 goto finally;
325 actual[0] = '~';
326 actual++;
327 remain--;
328
329 pos += length - width + 1;
330
331
332 for (; pos < length && remain > 1; pos++, actual++, remain--)
333 {
334 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
335 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
336 }
337 }
338 else
339 {
340 switch (HIDE_FIT (just_mode))
341 {
342 case J_CENTER:
343 ident = (length - width) / 2;
344 break;
345 case J_RIGHT:
346 ident = length - width;
347 break;
348 default:
349 break;
350 }
351
352
353
354 pos += ident;
355 for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
356 {
357 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
358 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
359 }
360
361 }
362
363 finally:
364 if (actual >= result + sizeof (result))
365 actual = result + sizeof (result) - 1;
366 actual[0] = '\0';
367 return result;
368 }
369
370
371
372 static const char *
373 str_ascii_term_trim (const char *text, int width)
374 {
375 static char result[BUF_MEDIUM];
376 size_t remain;
377 char *actual;
378 size_t length;
379
380 length = strlen (text);
381 actual = result;
382 remain = sizeof (result);
383
384 if (width > 0)
385 {
386 size_t pos;
387
388 if (width >= (int) length)
389 {
390
391 for (pos = 0; pos < length && remain > 1; pos++, 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 else if (width <= 3)
398 {
399 memset (actual, '.', width);
400 actual += width;
401 }
402 else
403 {
404 memset (actual, '.', 3);
405 actual += 3;
406 remain -= 3;
407
408
409 for (pos = length - width + 3; pos < length && remain > 1; pos++, actual++, remain--)
410 {
411 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
412 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
413 }
414 }
415 }
416
417 actual[0] = '\0';
418 return result;
419 }
420
421
422
423 static int
424 str_ascii_term_width2 (const char *text, size_t length)
425 {
426 return (length != (size_t) (-1)) ? MIN (strlen (text), length) : strlen (text);
427 }
428
429
430
431 static int
432 str_ascii_term_width1 (const char *text)
433 {
434 return str_ascii_term_width2 (text, (size_t) (-1));
435 }
436
437
438
439 static int
440 str_ascii_term_char_width (const char *text)
441 {
442 (void) text;
443 return 1;
444 }
445
446
447
448 static const char *
449 str_ascii_term_substring (const char *text, int start, int width)
450 {
451 static char result[BUF_MEDIUM];
452 size_t remain;
453 char *actual;
454 size_t length;
455
456 actual = result;
457 remain = sizeof (result);
458 length = strlen (text);
459
460 if (start < (int) length)
461 {
462 size_t pos;
463
464
465 for (pos = start; pos < length && width > 0 && remain > 1;
466 pos++, width--, actual++, remain--)
467 {
468 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
469 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
470 }
471 }
472
473
474 for (; width > 0 && remain > 1; actual++, remain--, width--)
475 actual[0] = ' ';
476
477 actual[0] = '\0';
478 return result;
479 }
480
481
482
483 static const char *
484 str_ascii_trunc (const char *text, int width)
485 {
486 static char result[MC_MAXPATHLEN];
487 int remain;
488 char *actual;
489 size_t pos = 0;
490 size_t length;
491
492 actual = result;
493 remain = sizeof (result);
494 length = strlen (text);
495
496 if ((int) length > width)
497 {
498
499 for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
500 {
501 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
502 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
503 }
504
505 if (remain <= 1)
506 goto finally;
507 actual[0] = '~';
508 actual++;
509 remain--;
510
511 pos += length - width + 1;
512
513
514 for (; pos < length && remain > 1; pos++, actual++, remain--)
515 {
516 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
517 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
518 }
519 }
520 else
521 {
522
523 for (; pos < length && remain > 1; pos++, actual++, remain--)
524 {
525 actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
526 actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
527 }
528 }
529
530 finally:
531 actual[0] = '\0';
532 return result;
533 }
534
535
536
537 static int
538 str_ascii_offset_to_pos (const char *text, size_t length)
539 {
540 (void) text;
541 return (int) length;
542 }
543
544
545
546 static int
547 str_ascii_column_to_pos (const char *text, size_t pos)
548 {
549 (void) text;
550 return (int) pos;
551 }
552
553
554
555 static char *
556 str_ascii_create_search_needle (const char *needle, gboolean case_sen)
557 {
558 (void) case_sen;
559 return (char *) needle;
560 }
561
562
563
564 static void
565 str_ascii_release_search_needle (char *needle, gboolean case_sen)
566 {
567 (void) case_sen;
568 (void) needle;
569
570 }
571
572
573
574 static const char *
575 str_ascii_search_first (const char *text, const char *search, gboolean case_sen)
576 {
577 char *fold_text;
578 char *fold_search;
579 const char *match;
580
581 fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
582 fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);
583
584 match = g_strstr_len (fold_text, -1, fold_search);
585 if (match != NULL)
586 {
587 size_t offset;
588
589 offset = match - fold_text;
590 match = text + offset;
591 }
592
593 if (!case_sen)
594 {
595 g_free (fold_text);
596 g_free (fold_search);
597 }
598
599 return match;
600 }
601
602
603
604 static const char *
605 str_ascii_search_last (const char *text, const char *search, gboolean case_sen)
606 {
607 char *fold_text;
608 char *fold_search;
609 const char *match;
610
611 fold_text = case_sen ? (char *) text : g_ascii_strdown (text, -1);
612 fold_search = case_sen ? (char *) search : g_ascii_strdown (search, -1);
613
614 match = g_strrstr_len (fold_text, -1, fold_search);
615 if (match != NULL)
616 {
617 size_t offset;
618
619 offset = match - fold_text;
620 match = text + offset;
621 }
622
623 if (!case_sen)
624 {
625 g_free (fold_text);
626 g_free (fold_search);
627 }
628
629 return match;
630 }
631
632
633
634 static int
635 str_ascii_compare (const char *t1, const char *t2)
636 {
637 return strcmp (t1, t2);
638 }
639
640
641
642 static int
643 str_ascii_ncompare (const char *t1, const char *t2)
644 {
645 return strncmp (t1, t2, MIN (strlen (t1), strlen (t2)));
646 }
647
648
649
650 static int
651 str_ascii_casecmp (const char *t1, const char *t2)
652 {
653 return g_ascii_strcasecmp (t1, t2);
654 }
655
656
657
658 static int
659 str_ascii_ncasecmp (const char *t1, const char *t2)
660 {
661 return g_ascii_strncasecmp (t1, t2, MIN (strlen (t1), strlen (t2)));
662 }
663
664
665
666 static void
667 str_ascii_fix_string (char *text)
668 {
669 for (; text[0] != '\0'; text++)
670 text[0] = ((unsigned char) text[0] < 128) ? text[0] : '?';
671 }
672
673
674
675 static char *
676 str_ascii_create_key (const char *text, gboolean case_sen)
677 {
678 (void) case_sen;
679 return (char *) text;
680 }
681
682
683
684 static int
685 str_ascii_key_collate (const char *t1, const char *t2, gboolean case_sen)
686 {
687 return case_sen ? strcmp (t1, t2) : g_ascii_strcasecmp (t1, t2);
688 }
689
690
691
692 static void
693 str_ascii_release_key (char *key, gboolean case_sen)
694 {
695 (void) key;
696 (void) case_sen;
697 }
698
699
700
701 static int
702 str_ascii_prefix (const char *text, const char *prefix)
703 {
704 int result;
705
706 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
707 && text[result] == prefix[result]; result++);
708
709 return result;
710 }
711
712
713
714 static int
715 str_ascii_caseprefix (const char *text, const char *prefix)
716 {
717 int result;
718
719 for (result = 0; text[result] != '\0' && prefix[result] != '\0'
720 && g_ascii_toupper (text[result]) == g_ascii_toupper (prefix[result]); result++);
721
722 return result;
723 }
724
725
726
727
728
729 struct str_class
730 str_ascii_init (void)
731 {
732 struct str_class result;
733
734 result.conv_gerror_message = str_ascii_conv_gerror_message;
735 result.vfs_convert_to = str_ascii_vfs_convert_to;
736 result.insert_replace_char = str_ascii_insert_replace_char;
737 result.is_valid_string = str_ascii_is_valid_string;
738 result.is_valid_char = str_ascii_is_valid_char;
739 result.cnext_char = str_ascii_cnext_char;
740 result.cprev_char = str_ascii_cprev_char;
741 result.cnext_char_safe = str_ascii_cnext_char;
742 result.cprev_char_safe = str_ascii_cprev_char;
743 result.cnext_noncomb_char = str_ascii_cnext_noncomb_char;
744 result.cprev_noncomb_char = str_ascii_cprev_noncomb_char;
745 result.char_isspace = str_ascii_isspace;
746 result.char_ispunct = str_ascii_ispunct;
747 result.char_isalnum = str_ascii_isalnum;
748 result.char_isdigit = str_ascii_isdigit;
749 result.char_isprint = str_ascii_isprint;
750 result.char_iscombiningmark = str_ascii_iscombiningmark;
751 result.char_toupper = str_ascii_toupper;
752 result.char_tolower = str_ascii_tolower;
753 result.length = str_ascii_length;
754 result.length2 = str_ascii_length2;
755 result.length_noncomb = str_ascii_length;
756 result.fix_string = str_ascii_fix_string;
757 result.term_form = str_ascii_term_form;
758 result.fit_to_term = str_ascii_fit_to_term;
759 result.term_trim = str_ascii_term_trim;
760 result.term_width2 = str_ascii_term_width2;
761 result.term_width1 = str_ascii_term_width1;
762 result.term_char_width = str_ascii_term_char_width;
763 result.term_substring = str_ascii_term_substring;
764 result.trunc = str_ascii_trunc;
765 result.offset_to_pos = str_ascii_offset_to_pos;
766 result.column_to_pos = str_ascii_column_to_pos;
767 result.create_search_needle = str_ascii_create_search_needle;
768 result.release_search_needle = str_ascii_release_search_needle;
769 result.search_first = str_ascii_search_first;
770 result.search_last = str_ascii_search_last;
771 result.compare = str_ascii_compare;
772 result.ncompare = str_ascii_ncompare;
773 result.casecmp = str_ascii_casecmp;
774 result.ncasecmp = str_ascii_ncasecmp;
775 result.prefix = str_ascii_prefix;
776 result.caseprefix = str_ascii_caseprefix;
777 result.create_key = str_ascii_create_key;
778 result.create_key_for_filename = str_ascii_create_key;
779 result.key_collate = str_ascii_key_collate;
780 result.release_key = str_ascii_release_key;
781
782 return result;
783 }
784
785