2 * Copyright © 2013 Ran Benita <ran234@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
34 #include <sys/types.h>
37 map_file(FILE *file, char **string_out, size_t *size_out)
43 /* Make sure to keep the errno on failure! */
48 if (fstat(fd, &stat_buf) != 0)
51 string = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
52 if (string == MAP_FAILED)
56 *size_out = stat_buf.st_size;
61 unmap_file(char *str, size_t size)
69 map_file(FILE *file, char **string_out, size_t *size_out)
76 /* Make sure to keep the errno on failure! */
78 ret = fseek(file, 0, SEEK_END);
87 ret = fseek(file, 0, SEEK_SET);
91 string = malloc(size);
95 ret_s = fread(string, 1, size, file);
101 *string_out = string;
107 unmap_file(char *str, size_t size)
114 // ASCII lower-case map.
115 static const unsigned char lower_map[] = {
116 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
117 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
118 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
119 59, 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
120 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
121 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
122 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
123 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
124 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
125 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
126 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
127 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
128 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
129 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
130 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
131 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
134 // ASCII tolower (to avoid locale issues).
138 return (char) lower_map[(unsigned char) c];
141 // ASCII strcasecmp (to avoid locale issues).
143 istrcmp(const char *a, const char *b)
145 for (size_t i = 0; ; i++) {
146 if (to_lower(a[i]) != to_lower(b[i]))
147 return (int) to_lower(a[i]) - (int) to_lower(b[i]);
154 // ASCII strncasecmp (to avoid locale issues).
156 istrncmp(const char *a, const char *b, size_t n)
158 for (size_t i = 0; i < n; i++) {
159 if (to_lower(a[i]) != to_lower(b[i]))
160 return (int) to_lower(a[i]) - (int) to_lower(b[i]);
167 #if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF)
169 asprintf(char **strp, const char *fmt, ...)
174 ret = vasprintf(strp, fmt, ap);
179 # if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF)
181 vasprintf(char **strp, const char *fmt, va_list ap)
188 * The value of the va_list parameter is undefined after the call to
189 * vsnprintf() returns: pass a copy to make sure "ap" remains valid.
191 va_copy(ap_copy, ap);
192 ret = vsnprintf(NULL, 0, fmt, ap_copy);
198 if (!(buf = malloc(ret + 1)))
201 if ((ret = vsnprintf(buf, ret + 1, fmt, ap)) < 0) {
209 # endif /* !HAVE_VASPRINTF */
210 #endif /* !HAVE_ASPRINTF */