rev |
line source |
nuclear@2
|
1 /* reimplementation of the subset of glib used by libsmf
|
nuclear@2
|
2 *
|
nuclear@2
|
3 * Copyright (C) 2012 John Tsiombikas <nuclear@mutantstargoat.com>
|
nuclear@2
|
4 * All rights reserved.
|
nuclear@2
|
5 *
|
nuclear@2
|
6 * Redistribution and use in source and binary forms, with or without
|
nuclear@2
|
7 * modification, are permitted provided that the following conditions are met:
|
nuclear@2
|
8 *
|
nuclear@2
|
9 * 1. Redistributions of source code must retain the above copyright notice, this
|
nuclear@2
|
10 * list of conditions and the following disclaimer.
|
nuclear@2
|
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
|
nuclear@2
|
12 * this list of conditions and the following disclaimer in the documentation
|
nuclear@2
|
13 * and/or other materials provided with the distribution.
|
nuclear@2
|
14 *
|
nuclear@2
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
nuclear@2
|
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
nuclear@2
|
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
nuclear@2
|
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
nuclear@2
|
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
nuclear@2
|
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
nuclear@2
|
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
nuclear@2
|
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
nuclear@2
|
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
nuclear@2
|
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
nuclear@2
|
25 */
|
nuclear@2
|
26
|
nuclear@0
|
27 #include <stdio.h>
|
nuclear@0
|
28 #include <stdlib.h>
|
nuclear@0
|
29 #include <string.h>
|
nuclear@0
|
30 #include <stdarg.h>
|
nuclear@0
|
31 #include <assert.h>
|
nuclear@0
|
32 #include "fake_glib.h"
|
nuclear@0
|
33
|
nuclear@0
|
34 FakeGPtrArray *fg_ptr_array_new(void)
|
nuclear@0
|
35 {
|
nuclear@0
|
36 FakeGPtrArray *arr;
|
nuclear@0
|
37
|
nuclear@0
|
38 if(!(arr = malloc(sizeof *arr))) {
|
nuclear@0
|
39 return 0;
|
nuclear@0
|
40 }
|
nuclear@0
|
41 arr->pdata = 0;
|
nuclear@0
|
42 arr->len = 0;
|
nuclear@0
|
43 return arr;
|
nuclear@0
|
44 }
|
nuclear@0
|
45
|
nuclear@0
|
46 void **fg_ptr_array_free(FakeGPtrArray *arr, int free_seg)
|
nuclear@0
|
47 {
|
nuclear@0
|
48 void **res;
|
nuclear@0
|
49
|
nuclear@0
|
50 if(!arr) {
|
nuclear@0
|
51 return 0;
|
nuclear@0
|
52 }
|
nuclear@0
|
53
|
nuclear@0
|
54 if(free_seg) {
|
nuclear@0
|
55 free(arr->pdata);
|
nuclear@0
|
56 res = 0;
|
nuclear@0
|
57 } else {
|
nuclear@0
|
58 res = arr->pdata;
|
nuclear@0
|
59 }
|
nuclear@0
|
60 free(arr);
|
nuclear@0
|
61 return res;
|
nuclear@0
|
62 }
|
nuclear@0
|
63
|
nuclear@0
|
64 void fg_ptr_array_add(FakeGPtrArray *arr, void *data)
|
nuclear@0
|
65 {
|
nuclear@0
|
66 int idx = arr->len++;
|
nuclear@0
|
67
|
nuclear@0
|
68 arr->pdata = realloc(arr->pdata, arr->len * sizeof *arr->pdata);
|
nuclear@0
|
69 assert(arr->pdata);
|
nuclear@0
|
70
|
nuclear@0
|
71 arr->pdata[idx] = data;
|
nuclear@0
|
72 }
|
nuclear@0
|
73
|
nuclear@0
|
74 int fg_ptr_array_remove(FakeGPtrArray *arr, void *data)
|
nuclear@0
|
75 {
|
nuclear@0
|
76 int i;
|
nuclear@0
|
77
|
nuclear@0
|
78 for(i=0; i<arr->len; i++) {
|
nuclear@0
|
79 if(arr->pdata[i] == data) {
|
nuclear@0
|
80 fg_ptr_array_remove_index(arr, i);
|
nuclear@0
|
81 return 1;
|
nuclear@0
|
82 }
|
nuclear@0
|
83 }
|
nuclear@0
|
84
|
nuclear@0
|
85 return 0;
|
nuclear@0
|
86 }
|
nuclear@0
|
87
|
nuclear@0
|
88 void *fg_ptr_array_remove_index(FakeGPtrArray *arr, unsigned int idx)
|
nuclear@0
|
89 {
|
nuclear@0
|
90 void *data = arr->pdata[idx];
|
nuclear@0
|
91 int rest = --arr->len - idx;
|
nuclear@0
|
92 if(rest > 0) {
|
nuclear@0
|
93 memmove(arr->pdata + idx, arr->pdata + idx + 1, rest * sizeof *arr->pdata);
|
nuclear@0
|
94 }
|
nuclear@0
|
95 return data;
|
nuclear@0
|
96 }
|
nuclear@0
|
97
|
nuclear@0
|
98 void fg_ptr_array_sort(FakeGPtrArray *arr, FakeGCompareFunc cmp)
|
nuclear@0
|
99 {
|
nuclear@0
|
100 qsort(arr->pdata, arr->len, sizeof *arr->pdata, cmp);
|
nuclear@0
|
101 }
|
nuclear@0
|
102
|
nuclear@0
|
103 /* -- logging -- */
|
nuclear@0
|
104
|
nuclear@1
|
105 void fg_message(const char *fmt, ...)
|
nuclear@1
|
106 {
|
nuclear@1
|
107 va_list ap;
|
nuclear@1
|
108
|
nuclear@1
|
109 va_start(ap, fmt);
|
nuclear@1
|
110 vprintf(fmt, ap);
|
nuclear@1
|
111 va_end(ap);
|
nuclear@1
|
112 putchar('\n');
|
nuclear@1
|
113 }
|
nuclear@1
|
114
|
nuclear@0
|
115 void fg_warning(const char *fmt, ...)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 va_list ap;
|
nuclear@0
|
118
|
nuclear@0
|
119 printf("warning: ");
|
nuclear@0
|
120
|
nuclear@0
|
121 va_start(ap, fmt);
|
nuclear@0
|
122 vprintf(fmt, ap);
|
nuclear@0
|
123 va_end(ap);
|
nuclear@1
|
124 putchar('\n');
|
nuclear@0
|
125 }
|
nuclear@0
|
126
|
nuclear@0
|
127 void fg_critical(const char *fmt, ...)
|
nuclear@0
|
128 {
|
nuclear@0
|
129 va_list ap;
|
nuclear@0
|
130
|
nuclear@0
|
131 printf("critical: ");
|
nuclear@0
|
132
|
nuclear@0
|
133 va_start(ap, fmt);
|
nuclear@0
|
134 vfprintf(stderr, fmt, ap);
|
nuclear@0
|
135 va_end(ap);
|
nuclear@1
|
136 putchar('\n');
|
nuclear@0
|
137
|
nuclear@0
|
138 if(getenv("G_DEBUG")) {
|
nuclear@0
|
139 abort();
|
nuclear@0
|
140 }
|
nuclear@0
|
141 }
|
nuclear@0
|
142
|
nuclear@0
|
143 void fg_error(const char *fmt, ...)
|
nuclear@0
|
144 {
|
nuclear@0
|
145 va_list ap;
|
nuclear@0
|
146
|
nuclear@0
|
147 printf("error: ");
|
nuclear@0
|
148
|
nuclear@0
|
149 va_start(ap, fmt);
|
nuclear@0
|
150 vfprintf(stderr, fmt, ap);
|
nuclear@0
|
151 va_end(ap);
|
nuclear@1
|
152 putchar('\n');
|
nuclear@0
|
153 abort();
|
nuclear@0
|
154 }
|
nuclear@0
|
155
|
nuclear@0
|
156 void fg_debug(const char *fmt, ...)
|
nuclear@0
|
157 {
|
nuclear@0
|
158 va_list ap;
|
nuclear@0
|
159
|
nuclear@0
|
160 printf("debug: ");
|
nuclear@0
|
161
|
nuclear@0
|
162 va_start(ap, fmt);
|
nuclear@0
|
163 vprintf(fmt, ap);
|
nuclear@0
|
164 va_end(ap);
|
nuclear@1
|
165 putchar('\n');
|
nuclear@0
|
166 }
|