]> git.xonotic.org Git - xonotic/gmqcc.git/blob - util.c
replaced getline with util_getline
[xonotic/gmqcc.git] / util.c
1 /*
2  * Copyright (C) 2012 
3  *      Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the 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 THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include <stdarg.h>
24 #include <errno.h>
25 #include "gmqcc.h"
26  
27 struct memblock_t {
28         const char  *file;
29         unsigned int line;
30         unsigned int byte;
31 };
32
33 void *util_memory_a(unsigned int byte, unsigned int line, const char *file) {
34         struct memblock_t *data = malloc(sizeof(struct memblock_t) + byte);
35         if (!data) return NULL;
36         data->line = line;
37         data->byte = byte;
38         data->file = file;
39         
40         util_debug("MEM", "allocation: %08u (bytes) at %s:%u\n", byte, file, line);
41         return (void*)((uintptr_t)data+sizeof(struct memblock_t));
42 }
43
44 void util_memory_d(void *ptrn, unsigned int line, const char *file) {
45         if (!ptrn) return;
46         void              *data = (void*)((uintptr_t)ptrn-sizeof(struct memblock_t));
47         struct memblock_t *info = (struct memblock_t*)data;
48         
49         util_debug("MEM", "released:   %08u (bytes) at %s:%u\n", info->byte, file, line);
50         free(data);
51 }
52
53 #ifndef mem_d
54 #define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
55 #endif
56 #ifndef mem_a
57 #define mem_a(x) util_memory_a((x), __LINE__, __FILE__)
58 #endif
59
60 /*
61  * Some string utility functions, because strdup uses malloc, and we want
62  * to track all memory (without replacing malloc).
63  */
64 char *util_strdup(const char *s) {
65         size_t  len;
66         char   *ptr;
67         
68         if (!s)
69                 return NULL;
70                 
71         len = strlen(s);
72         ptr = mem_a (len+1);
73         
74         if (ptr && len) {
75                 memcpy(ptr, s, len);
76                 ptr[len] = '\0';
77         }
78         
79         return ptr;
80 }
81
82 /*
83  * Remove quotes from a string, escapes from \ in string
84  * as well.  This function shouldn't be used to create a
85  * char array that is later freed (it uses pointer arith)
86  */
87 char *util_strrq(char *s) {
88         char *dst = s;
89         char *src = s;
90         char  chr;
91         while ((chr = *src++) != '\0') {
92                 if (chr == '\\') {
93                         *dst++ = chr;
94                         if ((chr = *src++) == '\0')
95                                 break;
96                         *dst++ = chr;
97                 } else if (chr != '"')
98                         *dst++ = chr;
99         }
100         *dst = '\0';
101         return dst;
102 }
103
104 /*
105  * Remove newline from a string (if it exists).  This is
106  * done pointer wise instead of strlen(), and an array
107  * access.
108  */
109 char *util_strrnl(char *s) {
110         char  *cpy = s;
111         while (cpy && *cpy && *cpy != '\n')
112                 cpy++;
113                 
114         *cpy = '\0';
115         return s;
116 }
117
118 void util_debug(const char *area, const char *ms, ...) {
119         va_list  va;
120         va_start(va, ms);
121         fprintf (stdout, "DEBUG: ");
122         fputc   ('[',  stdout);
123         fprintf (stdout, area);
124         fputs   ("] ", stdout);
125         vfprintf(stdout, ms, va);
126         va_end  (va);
127 }
128
129 /*
130  * Implements libc getline for systems that don't have it, which is 
131  * assmed all.  This works the same as getline().
132  */
133 int util_getline(char **lineptr, size_t *n, FILE *stream) {
134         int   chr;
135         int   ret;
136         char *pos;      
137
138         if (!lineptr || !n || !stream)
139                 return -1;
140         if (!*lineptr) {
141                 if (!(*lineptr = mem_a((*n = 64))))
142                         return -1;
143         }
144
145         chr = *n;
146         pos = *lineptr;
147
148         for (;;) {
149                 int c = getc(stream);
150                 
151                 if (chr < 2) {
152                         char *tmp = mem_a((*n+=(*n>16)?*n:64));
153                         if  (!tmp)
154                                 return -1;
155                         
156                         chr = *n + *lineptr - pos;
157                         strcpy(tmp,*lineptr);
158                         
159                         if (!(*lineptr = tmp))
160                                 return -1;
161                                 
162                         pos = *n - chr + *lineptr;
163                 }
164
165                 if (ferror(stream)) 
166                         return -1;
167                 if (c == EOF) {
168                         if (pos == *lineptr)
169                                 return -1;
170                         else
171                                 break;
172                 }
173
174                 *pos++ = c;
175                  chr--;
176                 if (c == '\n')
177                         break;
178         }
179         *pos = '\0';
180         return (ret = pos - *lineptr);
181 }