1 /* ASCII strings utilities
2 Copyright (C) 2007 Free Software Foundation, Inc.
7 The file_date routine is mostly from GNU's fileutils package,
8 written by Richard Stallman and David MacKenzie.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 /* using g_ascii function from glib
34 * on terminal are showed only ascii characters (lower then 0x80)
37 static const char replch
= '?';
40 str_ascii_insert_replace_char (GString
* buffer
)
42 g_string_append_c (buffer
, replch
);
46 str_ascii_is_valid_string (const char *text
)
53 str_ascii_is_valid_char (const char *ch
, size_t size
)
61 str_ascii_cnext_char (const char **text
)
67 str_ascii_cprev_char (const char **text
)
73 str_ascii_cnext_noncomb_char (const char **text
)
85 str_ascii_cprev_noncomb_char (const char **text
, const char *begin
)
97 str_ascii_isspace (const char *text
)
99 return g_ascii_isspace ((gchar
) text
[0]);
103 str_ascii_ispunct (const char *text
)
105 return g_ascii_ispunct ((gchar
) text
[0]);
109 str_ascii_isalnum (const char *text
)
111 return g_ascii_isalnum ((gchar
) text
[0]);
115 str_ascii_isdigit (const char *text
)
117 return g_ascii_isdigit ((gchar
) text
[0]);
121 str_ascii_isprint (const char *text
)
123 return g_ascii_isprint ((gchar
) text
[0]);
127 str_ascii_iscombiningmark (const char *text
)
134 str_ascii_toupper (const char *text
, char **out
, size_t * remain
)
138 (*out
)[0] = (char) g_ascii_toupper ((gchar
) text
[0]);
145 str_ascii_tolower (const char *text
, char **out
, size_t * remain
)
149 (*out
)[0] = (char) g_ascii_tolower ((gchar
) text
[0]);
156 str_ascii_length (const char *text
)
158 return strlen (text
);
162 str_ascii_length2 (const char *text
, int size
)
164 return (size
>= 0) ? min (strlen (text
), (gsize
) size
) : strlen (text
);
168 str_ascii_conv_gerror_message (GError
*error
, const char *def_msg
)
170 /* the same as str_utf8_conv_gerror_message() */
171 if ((error
!= NULL
) && (error
->message
!= NULL
))
172 return g_strdup (error
->message
);
174 return g_strdup (def_msg
!= NULL
? def_msg
: "");
178 str_ascii_vfs_convert_to (GIConv coder
, const char *string
,
179 int size
, GString
* buffer
)
182 g_string_append_len (buffer
, string
, size
);
188 str_ascii_term_form (const char *text
)
190 static char result
[BUF_MEDIUM
];
197 remain
= sizeof (result
);
198 length
= strlen (text
);
200 /* go throw all characters and check, if they are ascii and printable */
201 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
203 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
204 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
212 str_ascii_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
214 static char result
[BUF_MEDIUM
];
221 length
= strlen (text
);
223 remain
= sizeof (result
);
225 if ((int)length
<= width
)
228 switch (HIDE_FIT (just_mode
))
232 ident
= (width
- length
) / 2;
235 ident
= width
- length
;
239 /* add space before text */
240 if ((int)remain
<= ident
)
242 memset (actual
, ' ', ident
);
246 /* copy all characters */
247 for (; pos
< (gsize
)length
&& remain
> 1; pos
++, actual
++, remain
--)
249 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
250 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
253 /* add space after text */
254 if (width
- length
- ident
> 0)
256 if (remain
<= width
- length
- ident
)
258 memset (actual
, ' ', width
- length
- ident
);
259 actual
+= width
- length
- ident
;
260 remain
-= width
- length
- ident
;
265 if (IS_FIT (just_mode
))
267 /* copy prefix of text, that is not wider than width / 2 */
268 for (; pos
+ 1 <= (gsize
)width
/ 2 && remain
> 1;
269 actual
++, pos
++, remain
--)
271 actual
[0] = isascii ((unsigned char) text
[pos
])
273 actual
[0] = g_ascii_isprint ((gchar
) actual
[0])
283 pos
+= length
- width
+ 1;
285 /* copy suffix of text */
286 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
288 actual
[0] = isascii ((unsigned char) text
[pos
])
290 actual
[0] = g_ascii_isprint ((gchar
) actual
[0])
297 switch (HIDE_FIT (just_mode
))
300 ident
= (length
- width
) / 2;
303 ident
= length
- width
;
307 /* copy substring text, substring start from ident and take width
308 * characters from text */
310 for (; pos
< (gsize
)(ident
+ width
) && remain
> 1;
311 pos
++, actual
++, remain
--)
313 actual
[0] = isascii ((unsigned char) text
[pos
])
315 actual
[0] = g_ascii_isprint ((gchar
) actual
[0])
327 str_ascii_term_trim (const char *text
, int width
)
329 static char result
[BUF_MEDIUM
];
335 length
= strlen (text
);
337 remain
= sizeof (result
);
339 if (width
< (int)length
)
343 memset (actual
, '.', width
);
349 memset (actual
, '.', 3);
353 pos
+= length
- width
+ 3;
355 /* copy suffix of text */
356 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
358 actual
[0] = isascii ((unsigned char) text
[pos
])
360 actual
[0] = g_ascii_isprint ((gchar
) actual
[0])
367 /* copy all characters */
368 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
370 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
371 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
380 str_ascii_term_width2 (const char *text
, size_t length
)
382 return (length
!= (size_t) (-1))
383 ? min (strlen (text
), length
) : strlen (text
);
387 str_ascii_term_width1 (const char *text
)
389 return str_ascii_term_width2 (text
, (size_t) (-1));
393 str_ascii_term_char_width (const char *text
)
400 str_ascii_msg_term_size (const char *text
, int *lines
, int *columns
)
410 tmp
= g_strdup (text
);
415 q
= strchr (p
, '\n');
422 width
= str_ascii_term_width1 (p
);
423 if (width
> (*columns
))
436 str_ascii_term_substring (const char *text
, int start
, int width
)
438 static char result
[BUF_MEDIUM
];
445 remain
= sizeof (result
);
446 length
= strlen (text
);
448 if (start
< (int)length
)
451 /* copy at most width characters from text from start */
452 for (; pos
< length
&& width
> 0 && remain
> 1;
453 pos
++, width
--, actual
++, remain
--)
456 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
457 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
461 /* if text is shorter then width, add space to the end */
462 for (; width
> 0 && remain
> 1; actual
++, remain
--, width
--)
472 str_ascii_trunc (const char *text
, int width
)
474 static char result
[MC_MAXPATHLEN
];
481 remain
= sizeof (result
);
482 length
= strlen (text
);
484 if ((int)length
> width
)
486 /* copy prefix of text */
487 for (; pos
+ 1 <= (gsize
)width
/ 2 && remain
> 1; actual
++, pos
++, remain
--)
489 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
490 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
499 pos
+= length
- width
+ 1;
501 /* copy suffix of text */
502 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
504 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
505 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
510 /* copy all characters */
511 for (; pos
< length
&& remain
> 1; pos
++, actual
++, remain
--)
513 actual
[0] = isascii ((unsigned char) text
[pos
]) ? text
[pos
] : '?';
514 actual
[0] = g_ascii_isprint ((gchar
) actual
[0]) ? actual
[0] : '.';
524 str_ascii_offset_to_pos (const char *text
, size_t length
)
531 str_ascii_column_to_pos (const char *text
, size_t pos
)
538 str_ascii_create_search_needle (const char *needle
, int case_sen
)
541 return (char *) needle
;
545 str_ascii_release_search_needle (char *needle
, int case_sen
)
553 str_ascii_search_first (const char *text
, const char *search
, int case_sen
)
560 fold_text
= (case_sen
) ? (char *) text
: g_ascii_strdown (text
, -1);
561 fold_search
= (case_sen
) ? (char *) search
: g_ascii_strdown (search
, -1);
563 match
= g_strstr_len (fold_text
, -1, fold_search
);
566 offset
= match
- fold_text
;
567 match
= text
+ offset
;
573 g_free (fold_search
);
580 str_ascii_search_last (const char *text
, const char *search
, int case_sen
)
587 fold_text
= (case_sen
) ? (char *) text
: g_ascii_strdown (text
, -1);
588 fold_search
= (case_sen
) ? (char *) search
: g_ascii_strdown (search
, -1);
590 match
= g_strrstr_len (fold_text
, -1, fold_search
);
593 offset
= match
- fold_text
;
594 match
= text
+ offset
;
600 g_free (fold_search
);
607 str_ascii_compare (const char *t1
, const char *t2
)
609 return strcmp (t1
, t2
);
613 str_ascii_ncompare (const char *t1
, const char *t2
)
615 return strncmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
619 str_ascii_casecmp (const char *t1
, const char *t2
)
621 return g_ascii_strcasecmp (t1
, t2
);
625 str_ascii_ncasecmp (const char *t1
, const char *t2
)
627 return g_ascii_strncasecmp (t1
, t2
, min (strlen (t1
), strlen (t2
)));
631 str_ascii_fix_string (char *text
)
633 for (; text
[0] != '\0'; text
++)
635 text
[0] = ((unsigned char) text
[0] < 128) ? text
[0] : '?';
640 str_ascii_create_key (const char *text
, int case_sen
)
643 return (char *) text
;
647 str_ascii_key_collate (const char *t1
, const char *t2
, int case_sen
)
649 return (case_sen
) ? strcmp (t1
, t2
) : g_ascii_strcasecmp (t1
, t2
);
653 str_ascii_release_key (char *key
, int case_sen
)
660 str_ascii_prefix (const char *text
, const char *prefix
)
663 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
664 && text
[result
] == prefix
[result
]; result
++);
669 str_ascii_caseprefix (const char *text
, const char *prefix
)
672 for (result
= 0; text
[result
] != '\0' && prefix
[result
] != '\0'
673 && g_ascii_toupper (text
[result
]) ==
674 g_ascii_toupper (prefix
[result
]); result
++);
680 str_ascii_init (void)
682 struct str_class result
;
684 result
.conv_gerror_message
= str_ascii_conv_gerror_message
;
685 result
.vfs_convert_to
= str_ascii_vfs_convert_to
;
686 result
.insert_replace_char
= str_ascii_insert_replace_char
;
687 result
.is_valid_string
= str_ascii_is_valid_string
;
688 result
.is_valid_char
= str_ascii_is_valid_char
;
689 result
.cnext_char
= str_ascii_cnext_char
;
690 result
.cprev_char
= str_ascii_cprev_char
;
691 result
.cnext_char_safe
= str_ascii_cnext_char
;
692 result
.cprev_char_safe
= str_ascii_cprev_char
;
693 result
.cnext_noncomb_char
= str_ascii_cnext_noncomb_char
;
694 result
.cprev_noncomb_char
= str_ascii_cprev_noncomb_char
;
695 result
.isspace
= str_ascii_isspace
;
696 result
.ispunct
= str_ascii_ispunct
;
697 result
.isalnum
= str_ascii_isalnum
;
698 result
.isdigit
= str_ascii_isdigit
;
699 result
.isprint
= str_ascii_isprint
;
700 result
.iscombiningmark
= str_ascii_iscombiningmark
;
701 result
.toupper
= str_ascii_toupper
;
702 result
.tolower
= str_ascii_tolower
;
703 result
.length
= str_ascii_length
;
704 result
.length2
= str_ascii_length2
;
705 result
.length_noncomb
= str_ascii_length
;
706 result
.fix_string
= str_ascii_fix_string
;
707 result
.term_form
= str_ascii_term_form
;
708 result
.fit_to_term
= str_ascii_fit_to_term
;
709 result
.term_trim
= str_ascii_term_trim
;
710 result
.term_width2
= str_ascii_term_width2
;
711 result
.term_width1
= str_ascii_term_width1
;
712 result
.term_char_width
= str_ascii_term_char_width
;
713 result
.msg_term_size
= str_ascii_msg_term_size
;
714 result
.term_substring
= str_ascii_term_substring
;
715 result
.trunc
= str_ascii_trunc
;
716 result
.offset_to_pos
= str_ascii_offset_to_pos
;
717 result
.column_to_pos
= str_ascii_column_to_pos
;
718 result
.create_search_needle
= str_ascii_create_search_needle
;
719 result
.release_search_needle
= str_ascii_release_search_needle
;
720 result
.search_first
= str_ascii_search_first
;
721 result
.search_last
= str_ascii_search_last
;
722 result
.compare
= str_ascii_compare
;
723 result
.ncompare
= str_ascii_ncompare
;
724 result
.casecmp
= str_ascii_casecmp
;
725 result
.ncasecmp
= str_ascii_ncasecmp
;
726 result
.prefix
= str_ascii_prefix
;
727 result
.caseprefix
= str_ascii_caseprefix
;
728 result
.create_key
= str_ascii_create_key
;
729 result
.create_key_for_filename
= str_ascii_create_key
;
730 result
.key_collate
= str_ascii_key_collate
;
731 result
.release_key
= str_ascii_release_key
;