chiark / gitweb /
gnupg2 (2.1.17-3) unstable; urgency=medium
[gnupg2.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 <https://www.gnu.org/licenses/>.
31  */
32
33 #ifndef GNUPG_COMMON_STRINGHELP_H
34 #define GNUPG_COMMON_STRINGHELP_H
35
36 #include <stdint.h>
37 #include "types.h"
38
39 /*-- stringhelp.c --*/
40 char *has_leading_keyword (const char *string, const char *keyword);
41
42 const char *memistr (const void *buf, size_t buflen, const char *sub);
43 char *mem2str( char *, const void *, size_t);
44 char *trim_spaces( char *string );
45 char *trim_trailing_spaces( char *string );
46 unsigned int trim_trailing_chars( unsigned char *line, unsigned len,
47                                               const char *trimchars);
48 unsigned int trim_trailing_ws( unsigned char *line, unsigned len );
49 size_t length_sans_trailing_chars (const unsigned char *line, size_t len,
50                                    const char *trimchars );
51 size_t length_sans_trailing_ws (const unsigned char *line, size_t len);
52
53
54 char *make_basename(const char *filepath, const char *inputpath);
55 char *make_dirname(const char *filepath);
56 char *make_filename( const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
57 char *make_filename_try (const char *first_part, ... ) GPGRT_ATTR_SENTINEL(0);
58 char *make_absfilename (const char *first_part, ...) GPGRT_ATTR_SENTINEL(0);
59 char *make_absfilename_try (const char *first_part,
60                             ...) GPGRT_ATTR_SENTINEL(0);
61 int compare_filenames( const char *a, const char *b );
62
63 uint64_t string_to_u64 (const char *string);
64 int hextobyte (const char *s);
65
66 size_t utf8_charcount (const char *s, int len);
67
68
69 #ifdef HAVE_W32_SYSTEM
70 const char *w32_strerror (int ec);
71 #endif
72
73
74 int ascii_isupper (int c);
75 int ascii_islower (int c);
76 int ascii_toupper (int c);
77 int ascii_tolower (int c);
78 char *ascii_strlwr (char *s);
79 int ascii_strcasecmp( const char *a, const char *b );
80 int ascii_strncasecmp (const char *a, const char *b, size_t n);
81 int ascii_memcasecmp( const void *a, const void *b, size_t n );
82 const char *ascii_memistr ( const void *buf, size_t buflen, const char *sub);
83 void *ascii_memcasemem (const void *haystack, size_t nhaystack,
84                         const void *needle, size_t nneedle);
85
86
87 #ifndef HAVE_MEMICMP
88 int memicmp( const char *a, const char *b, size_t n );
89 #endif
90 #ifndef HAVE_STPCPY
91 char *stpcpy(char *a,const char *b);
92 #endif
93 #ifndef HAVE_STRPBRK
94 char *strpbrk (const char *s, const char *accept);
95 #endif
96 #ifndef HAVE_STRSEP
97 char *strsep (char **stringp, const char *delim);
98 #endif
99 #ifndef HAVE_STRLWR
100 char *strlwr(char *a);
101 #endif
102 #ifndef HAVE_STRTOUL
103 #  define strtoul(a,b,c)  ((unsigned long)strtol((a),(b),(c)))
104 #endif
105 #ifndef HAVE_MEMMOVE
106 #  define memmove(d, s, n) bcopy((s), (d), (n))
107 #endif
108 #ifndef HAVE_STRICMP
109 #  define stricmp(a,b)   strcasecmp( (a), (b) )
110 #endif
111 #ifndef HAVE_MEMRCHR
112 void *memrchr (const void *buffer, int c, size_t n);
113 #endif
114
115
116 #ifndef HAVE_ISASCII
117 static inline int
118 isascii (int c)
119 {
120   return (((c) & ~0x7f) == 0);
121 }
122 #endif /* !HAVE_ISASCII */
123
124
125 #ifndef STR
126 #  define STR(v) #v
127 #endif
128 #define STR2(v) STR(v)
129
130 /* Percent-escape the string STR by replacing colons with '%3a'.  If
131    EXTRA is not NULL, also replace all characters given in EXTRA.  The
132    "try_" variant fails with NULL if not enough memory can be
133    allocated.  */
134 char *percent_escape (const char *str, const char *extra);
135 char *try_percent_escape (const char *str, const char *extra);
136
137
138 /* Concatenate the string S1 with all the following strings up to a
139    NULL.  Returns a malloced buffer with the new string or NULL on a
140    malloc error or if too many arguments are given.  */
141 char *strconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
142 /* Ditto, but die on error.  */
143 char *xstrconcat (const char *s1, ...) GPGRT_ATTR_SENTINEL(0);
144
145 char **strsplit (char *string, char delim, char replacement, int *count);
146
147 /* Tokenize STRING using the set of delimiters in DELIM.  */
148 char **strtokenize (const char *string, const char *delim);
149
150 /* Split STRING into space delimited fields and store them in the
151  * provided ARRAY.  */
152 int split_fields (char *string, char **array, int arraysize);
153
154 /* Return True if MYVERSION is greater or equal than REQ_VERSION.  */
155 int compare_version_strings (const char *my_version, const char *req_version);
156
157 /* Format a string so that it fits within about TARGET_COLS columns.  */
158 char *format_text (char *text, int in_place, int target_cols, int max_cols);
159
160
161 /*-- mapstrings.c --*/
162 const char *map_static_macro_string (const char *string);
163
164 #endif /*GNUPG_COMMON_STRINGHELP_H*/