| 1 |
#ifndef _NSTRING_H |
| 2 |
#define _NSTRING_H |
| 3 |
|
| 4 |
#include <stdarg.h> |
| 5 |
#include <string.h> |
| 6 |
#include <strings.h> /* For strncasecmp */ |
| 7 |
#include <ctype.h> |
| 8 |
|
| 9 |
#include "pm_c_util.h" |
| 10 |
#include "pm.h" /* For PM_GNU_PRINTF_ATTR, __inline__ */ |
| 11 |
|
| 12 |
#ifdef __cplusplus |
| 13 |
extern "C" { |
| 14 |
#endif |
| 15 |
#if 0 |
| 16 |
} /* to fake out automatic code indenters */ |
| 17 |
#endif |
| 18 |
|
| 19 |
/* Here are string functions that respect the size of the array |
| 20 |
into which you are copying -- E.g. STRSCPY truncates the source string as |
| 21 |
required so that it fits, with the terminating null, in the destination |
| 22 |
array. |
| 23 |
*/ |
| 24 |
#define STRSCPY(A,B) \ |
| 25 |
(strncpy((A), (B), sizeof(A)), *((A)+sizeof(A)-1) = '\0') |
| 26 |
#define STRSCMP(A,B) \ |
| 27 |
(strncmp((A), (B), sizeof(A))) |
| 28 |
#define STRSCAT(A,B) \ |
| 29 |
(strncpy(A+strlen(A), B, sizeof(A)-strlen(A)), *((A)+sizeof(A)-1) = '\0') |
| 30 |
#define STRSEQ(A, B) \ |
| 31 |
(strneq((A), (B), sizeof(A))) |
| 32 |
|
| 33 |
#define MEMSEQ(a,b) (memeq(a, b, sizeof(*(a)))) |
| 34 |
|
| 35 |
#define MEMSSET(a,b) (memset(a, b, sizeof(*(a)))) |
| 36 |
|
| 37 |
#define MEMSCPY(a,b) (memcpy(a, b, sizeof(*(a)))) |
| 38 |
|
| 39 |
#define MEMSZERO(a) (MEMSSET(a, 0)) |
| 40 |
|
| 41 |
|
| 42 |
static __inline__ int |
| 43 |
streq(const char * const comparand, |
| 44 |
const char * const comparator) { |
| 45 |
|
| 46 |
return strcmp(comparand, comparator) == 0; |
| 47 |
} |
| 48 |
|
| 49 |
static __inline__ int |
| 50 |
strneq(const char * const comparand, |
| 51 |
const char * const comparator, |
| 52 |
size_t const size) { |
| 53 |
|
| 54 |
return strncmp(comparand, comparator, size) == 0; |
| 55 |
} |
| 56 |
|
| 57 |
static __inline__ int |
| 58 |
memeq(const void * const comparand, |
| 59 |
const void * const comparator, |
| 60 |
size_t const size) { |
| 61 |
|
| 62 |
return memcmp(comparand, comparator, size) == 0; |
| 63 |
} |
| 64 |
|
| 65 |
/* The Standard C Library may not declare strcasecmp() if the including source |
| 66 |
file doesn't request BSD functions, with _BSD_SOURCE or SUSv2 function, |
| 67 |
with _XOPEN_SOURCE >= 500. So we don't define functions that use |
| 68 |
strcasecmp() in that case. |
| 69 |
|
| 70 |
(Actually, _XOPEN_SOURCE 500 is stronger than you need for strcasecmp - |
| 71 |
_XOPEN_SOURCE_EXTENDED, which asks for XPG 4, would do, whereas |
| 72 |
_XOPEN_SOURCE 500 asks for XPG 5, but for simplicity, we don't use |
| 73 |
_XOPEN_SOURCE_EXTENDED in Netpbm. |
| 74 |
*/ |
| 75 |
#if defined(_BSD_SOURCE) || (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE - 0) >= 500) |
| 76 |
static __inline__ int |
| 77 |
strcaseeq(const char * const comparand, |
| 78 |
const char * const comparator) { |
| 79 |
|
| 80 |
return strcasecmp(comparand, comparator) == 0; |
| 81 |
} |
| 82 |
|
| 83 |
static __inline__ int |
| 84 |
strncaseeq(const char * const comparand, |
| 85 |
const char * const comparator, |
| 86 |
size_t const size) { |
| 87 |
|
| 88 |
return strncasecmp(comparand, comparator, size) == 0; |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
/* The standard C library routines isdigit(), for some weird |
| 94 |
historical reason, does not take a character (type 'char') as its |
| 95 |
argument. Instead it takes an integer. When the integer is a whole |
| 96 |
number, it represents a character in the obvious way using the local |
| 97 |
character set encoding. When the integer is negative, the results |
| 98 |
are undefined. |
| 99 |
|
| 100 |
Passing a character to isdigit(), which expects an integer, results in |
| 101 |
isdigit() sometimes getting a negative number. |
| 102 |
|
| 103 |
On some systems, when the integer is negative, it represents exactly |
| 104 |
the character you want it to anyway (e.g. -1 is the character that is |
| 105 |
encoded 0xFF). But on others, it does not. |
| 106 |
|
| 107 |
(The same is true of other routines like isdigit()). |
| 108 |
|
| 109 |
Therefore, we have the substitutes for isdigit() etc. that take an |
| 110 |
actual character (type 'char') as an argument. |
| 111 |
*/ |
| 112 |
|
| 113 |
#define ISALNUM(C) (isalnum((unsigned char)(C))) |
| 114 |
#define ISALPHA(C) (isalpha((unsigned char)(C))) |
| 115 |
#define ISCNTRL(C) (iscntrl((unsigned char)(C))) |
| 116 |
#define ISDIGIT(C) (isdigit((unsigned char)(C))) |
| 117 |
#define ISGRAPH(C) (isgraph((unsigned char)(C))) |
| 118 |
#define ISLOWER(C) (islower((unsigned char)(C))) |
| 119 |
#define ISPRINT(C) (isprint((unsigned char)(C))) |
| 120 |
#define ISPUNCT(C) (ispunct((unsigned char)(C))) |
| 121 |
#define ISSPACE(C) (isspace((unsigned char)(C))) |
| 122 |
#define ISUPPER(C) (isupper((unsigned char)(C))) |
| 123 |
#define ISXDIGIT(C) (isxdigit((unsigned char)(C))) |
| 124 |
#define TOUPPER(C) ((char)toupper((unsigned char)(C))) |
| 125 |
|
| 126 |
|
| 127 |
/* Most of these are private versions of commonly available standard C library |
| 128 |
subroutines whose names are similar. They're here because not all standard |
| 129 |
C libraries have them. |
| 130 |
|
| 131 |
The GNU C library has all of them. |
| 132 |
|
| 133 |
There are slight differences between the asprintf() family and that |
| 134 |
found in other libraries: |
| 135 |
|
| 136 |
- There is no return value. |
| 137 |
|
| 138 |
- The returned string is a const char * instead of a char *. The |
| 139 |
const is more correct. |
| 140 |
|
| 141 |
- If the function can't get the memory, it returns 'pm_strsol', |
| 142 |
which is a string that is in static memory that contains text |
| 143 |
indicating an out of memory failure has occurred, instead of |
| 144 |
NULL. This makes it much easier for programs to ignore this |
| 145 |
possibility. |
| 146 |
|
| 147 |
strfree() is strictly a Netpbm invention, to allow proper type checking |
| 148 |
when freeing storage allocated by the Netpbm pm_asprintf(). |
| 149 |
*/ |
| 150 |
|
| 151 |
extern const char * const pm_strsol; |
| 152 |
|
| 153 |
size_t |
| 154 |
pm_strnlen(const char * const s, |
| 155 |
size_t const maxlen); |
| 156 |
|
| 157 |
const char * |
| 158 |
pm_strdup(const char * const arg); |
| 159 |
|
| 160 |
void |
| 161 |
pm_asprintf(const char ** const resultP, |
| 162 |
const char * const fmt, |
| 163 |
...) PM_GNU_PRINTF_ATTR(2,3); |
| 164 |
|
| 165 |
void |
| 166 |
pm_vasprintf(const char ** const resultP, |
| 167 |
const char * const format, |
| 168 |
va_list args); |
| 169 |
|
| 170 |
bool |
| 171 |
pm_vasprintf_knows_float(void); |
| 172 |
|
| 173 |
void |
| 174 |
pm_strfree(const char * const string); |
| 175 |
|
| 176 |
const char * |
| 177 |
pm_strsep(char ** const stringP, const char * const delim); |
| 178 |
|
| 179 |
int |
| 180 |
pm_stripeq(const char * const comparand, |
| 181 |
const char * const comparator); |
| 182 |
|
| 183 |
const void * |
| 184 |
pm_memmem(const void * const haystackArg, |
| 185 |
size_t const haystacklen, |
| 186 |
const void * const needleArg, |
| 187 |
size_t const needlelen); |
| 188 |
|
| 189 |
bool |
| 190 |
pm_strishex(const char * const subject); |
| 191 |
|
| 192 |
void |
| 193 |
pm_string_to_long(const char * const string, |
| 194 |
long * const longP, |
| 195 |
const char ** const errorP); |
| 196 |
|
| 197 |
void |
| 198 |
pm_string_to_int(const char * const string, |
| 199 |
int * const intP, |
| 200 |
const char ** const errorP); |
| 201 |
|
| 202 |
void |
| 203 |
pm_string_to_uint(const char * const string, |
| 204 |
unsigned int * const uintP, |
| 205 |
const char ** const errorP); |
| 206 |
|
| 207 |
#ifdef __cplusplus |
| 208 |
} |
| 209 |
#endif |
| 210 |
|
| 211 |
#endif |