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.
32 #include <sys/types.h>
35 map_file(FILE *file, char **string_out, size_t *size_out)
41 /* Make sure to keep the errno on failure! */
46 if (fstat(fd, &stat_buf) != 0)
49 string = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
50 if (string == MAP_FAILED)
54 *size_out = stat_buf.st_size;
59 unmap_file(char *str, size_t size)
67 map_file(FILE *file, char **string_out, size_t *size_out)
74 /* Make sure to keep the errno on failure! */
76 ret = fseek(file, 0, SEEK_END);
85 ret = fseek(file, 0, SEEK_SET);
89 string = malloc(size);
93 ret_s = fread(string, 1, size, file);
105 unmap_file(char *str, size_t size)
112 // ASCII lower-case map.
113 static const unsigned char lower_map[] = {
114 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
115 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
116 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
117 59, 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
118 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
119 91, 92, 93, 94, 95, 96, 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 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
122 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
123 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
124 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
125 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
126 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
127 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
128 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
129 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
132 // ASCII tolower (to avoid locale issues).
136 return (char) lower_map[(unsigned char) c];
139 // ASCII strcasecmp (to avoid locale issues).
141 istrcmp(const char *a, const char *b)
143 for (size_t i = 0; ; i++) {
144 if (to_lower(a[i]) != to_lower(b[i]))
145 return (int) to_lower(a[i]) - (int) to_lower(b[i]);
152 // ASCII strncasecmp (to avoid locale issues).
154 istrncmp(const char *a, const char *b, size_t n)
156 for (size_t i = 0; i < n; i++) {
157 if (to_lower(a[i]) != to_lower(b[i]))
158 return (int) to_lower(a[i]) - (int) to_lower(b[i]);