smflite
view src/fake_glib.c @ 1:8e535ca4bb86
added smfsh
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Jan 2012 15:35:18 +0200 |
parents | 4264abea8b06 |
children | d9e0d0500a78 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <assert.h>
6 #include "fake_glib.h"
8 FakeGPtrArray *fg_ptr_array_new(void)
9 {
10 FakeGPtrArray *arr;
12 if(!(arr = malloc(sizeof *arr))) {
13 return 0;
14 }
15 arr->pdata = 0;
16 arr->len = 0;
17 return arr;
18 }
20 void **fg_ptr_array_free(FakeGPtrArray *arr, int free_seg)
21 {
22 void **res;
24 if(!arr) {
25 return 0;
26 }
28 if(free_seg) {
29 free(arr->pdata);
30 res = 0;
31 } else {
32 res = arr->pdata;
33 }
34 free(arr);
35 return res;
36 }
38 void fg_ptr_array_add(FakeGPtrArray *arr, void *data)
39 {
40 int idx = arr->len++;
42 arr->pdata = realloc(arr->pdata, arr->len * sizeof *arr->pdata);
43 assert(arr->pdata);
45 arr->pdata[idx] = data;
46 }
48 int fg_ptr_array_remove(FakeGPtrArray *arr, void *data)
49 {
50 int i;
52 for(i=0; i<arr->len; i++) {
53 if(arr->pdata[i] == data) {
54 fg_ptr_array_remove_index(arr, i);
55 return 1;
56 }
57 }
59 return 0;
60 }
62 void *fg_ptr_array_remove_index(FakeGPtrArray *arr, unsigned int idx)
63 {
64 void *data = arr->pdata[idx];
65 int rest = --arr->len - idx;
66 if(rest > 0) {
67 memmove(arr->pdata + idx, arr->pdata + idx + 1, rest * sizeof *arr->pdata);
68 }
69 return data;
70 }
72 void fg_ptr_array_sort(FakeGPtrArray *arr, FakeGCompareFunc cmp)
73 {
74 qsort(arr->pdata, arr->len, sizeof *arr->pdata, cmp);
75 }
77 /* -- logging -- */
79 void fg_message(const char *fmt, ...)
80 {
81 va_list ap;
83 va_start(ap, fmt);
84 vprintf(fmt, ap);
85 va_end(ap);
86 putchar('\n');
87 }
89 void fg_warning(const char *fmt, ...)
90 {
91 va_list ap;
93 printf("warning: ");
95 va_start(ap, fmt);
96 vprintf(fmt, ap);
97 va_end(ap);
98 putchar('\n');
99 }
101 void fg_critical(const char *fmt, ...)
102 {
103 va_list ap;
105 printf("critical: ");
107 va_start(ap, fmt);
108 vfprintf(stderr, fmt, ap);
109 va_end(ap);
110 putchar('\n');
112 if(getenv("G_DEBUG")) {
113 abort();
114 }
115 }
117 void fg_error(const char *fmt, ...)
118 {
119 va_list ap;
121 printf("error: ");
123 va_start(ap, fmt);
124 vfprintf(stderr, fmt, ap);
125 va_end(ap);
126 putchar('\n');
127 abort();
128 }
130 void fg_debug(const char *fmt, ...)
131 {
132 va_list ap;
134 printf("debug: ");
136 va_start(ap, fmt);
137 vprintf(fmt, ap);
138 va_end(ap);
139 putchar('\n');
140 }