atom: describe how this odd data structure works
[platform/upstream/libxkbcommon.git] / src / utils.c
1 /*
2  * Copyright © 2013 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "utils.h"
25
26 #ifdef HAVE_MMAP
27
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33
34 bool
35 map_file(FILE *file, char **string_out, size_t *size_out)
36 {
37     struct stat stat_buf;
38     int fd;
39     char *string;
40
41     /* Make sure to keep the errno on failure! */
42     fd = fileno(file);
43     if (fd < 0)
44         return false;
45
46     if (fstat(fd, &stat_buf) != 0)
47         return false;
48
49     string = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
50     if (string == MAP_FAILED)
51         return false;
52
53     *string_out = string;
54     *size_out = stat_buf.st_size;
55     return true;
56 }
57
58 void
59 unmap_file(char *str, size_t size)
60 {
61     munmap(str, size);
62 }
63
64 #else
65
66 bool
67 map_file(FILE *file, char **string_out, size_t *size_out)
68 {
69     long ret;
70     size_t ret_s;
71     char *string;
72     size_t size;
73
74     /* Make sure to keep the errno on failure! */
75
76     ret = fseek(file, 0, SEEK_END);
77     if (ret != 0)
78         return false;
79
80     ret = ftell(file);
81     if (ret < 0)
82         return false;
83     size = (size_t) ret;
84
85     ret = fseek(file, 0, SEEK_SET);
86     if (ret < 0)
87         return false;
88
89     string = malloc(size);
90     if (!string)
91         return false;
92
93     ret_s = fread(string, 1, size, file);
94     if (ret_s < size) {
95         free(string);
96         return false;
97     }
98
99     *string_out = string;
100     *size_out = size;
101     return true;
102 }
103
104 void
105 unmap_file(char *str, size_t size)
106 {
107     free(str);
108 }
109
110 #endif
111
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
130 };
131
132 // ASCII tolower (to avoid locale issues).
133 char
134 to_lower(char c)
135 {
136     return (char) lower_map[(unsigned char) c];
137 }
138
139 // ASCII strcasecmp (to avoid locale issues).
140 int
141 istrcmp(const char *a, const char *b)
142 {
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]);
146         if (!a[i])
147             break;
148     }
149     return 0;
150 }
151
152 // ASCII strncasecmp (to avoid locale issues).
153 int
154 istrncmp(const char *a, const char *b, size_t n)
155 {
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]);
159         if (!a[i])
160             break;
161     }
162     return 0;
163 }