smflite

view src/fake_glib.c @ 2:d9e0d0500a78

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