root/op/1.30/util.c

Revision 232, 1.7 kB (checked in by athomas, 4 years ago)
  • Added patch by Pierre fixing strnprintf issues.
  • Added patch by Pierre fixing wildcard constraint bug.
  • Property svn:eol-style set to native
Line 
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include "defs.h"
6
7 #ifndef HAVE_VSNPRINTF
8 #warning You have not compiled op with vsnprintf
9 #warning support, presumably because your system
10 #warning does not have it. This leaves op open
11 #warning to potential buffer overflows.
12 #endif
13
14 #ifdef HAVE_SNPRINTF
15 #error "Now using 'vsnprintf' instead of snprintf. Adjust your build to define HAVE_VSNPRINTF."
16 #endif
17
18 void strnprintf(char *out, int len, const char *format, va_list args) {
19
20 #ifdef HAVE_VSNPRINTF
21         vsnprintf(out, len, format, args);
22 #else
23         vsprintf(out, format, args);
24 #endif
25 }
26
27 char *strtolower(char *in) {
28 char *i;
29
30         for (i = in; *i; ++i) *i = tolower(*i);
31         return in;
32 }
33
34 array_t *array_alloc() {
35 array_t *array = malloc(sizeof(array_t));
36
37         if (!array || !(array->data = malloc(sizeof(void**) * ARRAY_CHUNK)))
38                 fatal(1, "failed to allocate array");
39         array->capacity = ARRAY_CHUNK;
40         array->size = 0;
41         return array;
42 }
43
44 void array_free(array_t *array) {
45         free(array->data);
46         free(array);
47 }
48
49 array_t *array_free_contents(array_t *array) {
50 int i;
51
52         for (i = 0; i < array->size; ++i)
53                 free(array->data[i]);
54         array->size = 0;
55         return array;
56 }
57
58 void *array_push(array_t *array, void *object) {
59         if (array->size + 1 >= array->capacity) {
60                 array->capacity += ARRAY_CHUNK;
61                 if (!(array->data = realloc(array->data, sizeof(void**) * array->capacity)))
62                         fatal(1, "failed to extend array");
63         }
64         return (array->data[array->size++] = object);
65 }
66
67 void *array_pop(array_t *array) {
68         if (array->size <= 0) return NULL;
69         return array->data[--array->size];
70 }
71
72 int array_extend(array_t *array, int capacity) {
73         if (capacity < array->capacity) return 0;
74         array->capacity = capacity;
75         array->data = realloc(array->data, sizeof(void**) * array->capacity);
76         return 1;
77 }
Note: See TracBrowser for help on using the browser.