Imported Upstream version 2.1.13
[platform/upstream/gpg2.git] / common / stringhelp.h
1 /* stringhelp.h
2  * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3  *               2006, 2007, 2009  Free Software Foundation, Inc.
4  *               2015  g10 Code GmbH
5  *
6  * This file is part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify it
9  * under the terms of either
10  *
11  *   - the GNU Lesser General Public License as published by the Free
12  *     Software Foundation; either version 3 of the License, or (at
13  *     your option) any later version.
14  *
15  * or
16  *
17  *   - the GNU General Public License as published by the Free
18  *     Software Foundation; either version 2 of the License, or (at
19  *     your option) any later version.
20  *
21  * or both in parallel, as here.
22  *
23  * GnuPG is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copies of the GNU General Public License
29  * and the GNU Lesser General Public License along with this program;
30  * if not, see <http://www.gnu.org/licenses/>.
31  */
32
33 #ifndef GNUPG_COMMON_STRINGHELP_H
34 #define GNUPG_COMMON_STRINGHELP_H
35
36 #include "types.h"
37
38 /*-- stringhelp.c --*/
39 char *has_leading_keyword (const char *string, const char *keyword);
40
41 const char *memistr (const void *buf, size_t buflen, const char *sub);
42 char *mem2str( char *, const void *, size_t);
43 char *trim_spaces( char *string );
44 char *trim_trailing_spaces( char *string );
45 unsigned int trim_trailing_chars( unsigned char *line, unsigned len,
46                                               const char *trimchars);
47 unsigned int trim_trailing_ws( unsigned char *line, unsigned len );
48 size_t length_sans_trailing_chars (const unsigned char *line, size_t len,
49                                    const char *trimchars );
50 size_t length_sans_trailing_ws (const unsigned char *line, size_t len);
51
52
53 char *make_basename(const char *filepath, const char *inputpath);
54 char *make_dirname(const char *filepath);
55 char *make_filename( const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
56 char *make_filename_try (const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
57 char *make_absfilename (const char *first_part, ...) GPGRT_ATTR_SENTINEL(0);
58 char *make_absfilename_try (const char *first_part,
59                             ...) GPGRT_ATTR_SENTINEL(0);
60 int compare_filenames( const char *a, const char *b );
61
62 int hextobyte (const char *s);
63
64 char *sanitize_buffer (const void *p, size_t n, int delim);
65
66
67 size_t utf8_charcount (const char *s, int len);
68
69
70 #ifdef HAVE_W32_SYSTEM
71 const char *w32_strerror (int ec);
72 #endif
73
74
75 int ascii_isupper (int c);
76 int ascii_islower (int c);
77 int ascii_toupper (int c);
78 int ascii_tolower (int c);
79 char *ascii_strlwr (char *s);
80 int ascii_strcasecmp( const char *a, const char *b );
81 int ascii_strncasecmp (const char *a, const char *b, size_t n);
82 int ascii_memcasecmp( const void *a, const void *b, size_t n );
83 const char *ascii_memistr ( const void *buf, size_t buflen, const char *sub);
84 void *ascii_memcasemem (const void *haystack, size_t nhaystack,
85                         const void *needle, size_t nneedle);
86
87
88 #ifndef HAVE_MEMICMP
89 int memicmp( const char *a, const char *b, size_t n );
90 #endif
91 #ifndef HAVE_STPCPY
92 char *stpcpy(char *a,const char *b);
93 #endif
94 #ifndef HAVE_STRPBRK
95 char *strpbrk (const char *s, const char *accept);
96 #endif
97 #ifndef HAVE_STRSEP
98 char *strsep (char **stringp, const char *delim);
99 #endif
100 #ifndef HAVE_STRLWR
101 char *strlwr(char *a);
102 #endif
103 #ifndef HAVE_STRTOUL
104 #  define strtoul(a,b,c)  ((unsigned long)strtol((a),(b),(c)))
105 #endif
106 #ifndef HAVE_MEMMOVE
107 #  define memmove(d, s, n) bcopy((s), (d), (n))
108 #endif
109 #ifndef HAVE_STRICMP
110 #  define stricmp(a,b)   strcasecmp( (a), (b) )
111 #endif
112 #ifndef HAVE_MEMRCHR
113 void *memrchr (const void *buffer, int c, size_t n);
114 #endif
115
116
117 #ifndef HAVE_ISASCII
118 static inline int
119 isascii (int c)
120 {
121   return (((c) & ~0x7f) == 0);
122 }
123 #endif /* !HAVE_ISASCII */
124
125
126 #ifndef STR
127 #  define STR(v) #v
128 #endif
129 #define STR2(v) STR(v)
130
131 /* Percent-escape the string STR by replacing colons with '%3a'.  If
132    EXTRA is not NULL, also replace all characters given in EXTRA.  The
133    "try_" variant fails with NULL if not enough memory can be
134    allocated.  */
135 char *percent_escape (const char *str, const char *extra);
136 char *try_percent_escape (const char *str, const char *extra);
137
138
139 /* Concatenate the string S1 with all the following strings up to a
140    NULL.  Returns a malloced buffer with the new string or NULL on a
141    malloc error or if too many arguments are given.  */
142 char *strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
143 /* Ditto, but die on error.  */
144 char *xstrconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
145
146 char **strsplit (char *string, char delim, char replacement, int *count);
147
148 /* Tokenize STRING using the set of delimiters in DELIM.  */
149 char **strtokenize (const char *string, const char *delim);
150
151 /* Split STRING into space delimited fields and store them in the
152  * provided ARRAY.  */
153 int split_fields (char *string, char **array, int arraysize);
154
155 /* Return True if MYVERSION is greater or equal than REQ_VERSION.  */
156 int compare_version_strings (const char *my_version, const char *req_version);
157
158 /* Format a string so that it fits within about TARGET_COLS columns.  */
159 char *format_text (char *text, int in_place, int target_cols, int max_cols);
160
161
162 /*-- mapstrings.c --*/
163 const char *map_static_macro_string (const char *string);
164
165 #endif /*GNUPG_COMMON_STRINGHELP_H*/