smflite
view src/fake_glib.c @ 0:4264abea8b06
smf-lite initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Jan 2012 11:25:11 +0200 |
parents | |
children | 8e535ca4bb86 |
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_warning(const char *fmt, ...)
80 {
81 va_list ap;
83 printf("warning: ");
85 va_start(ap, fmt);
86 vprintf(fmt, ap);
87 va_end(ap);
88 }
90 void fg_critical(const char *fmt, ...)
91 {
92 va_list ap;
94 printf("critical: ");
96 va_start(ap, fmt);
97 vfprintf(stderr, fmt, ap);
98 va_end(ap);
100 if(getenv("G_DEBUG")) {
101 abort();
102 }
103 }
105 void fg_error(const char *fmt, ...)
106 {
107 va_list ap;
109 printf("error: ");
111 va_start(ap, fmt);
112 vfprintf(stderr, fmt, ap);
113 va_end(ap);
114 abort();
115 }
117 void fg_debug(const char *fmt, ...)
118 {
119 va_list ap;
121 printf("debug: ");
123 va_start(ap, fmt);
124 vprintf(fmt, ap);
125 va_end(ap);
126 }