smflite

annotate 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
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <stdarg.h>
nuclear@0 5 #include <assert.h>
nuclear@0 6 #include "fake_glib.h"
nuclear@0 7
nuclear@0 8 FakeGPtrArray *fg_ptr_array_new(void)
nuclear@0 9 {
nuclear@0 10 FakeGPtrArray *arr;
nuclear@0 11
nuclear@0 12 if(!(arr = malloc(sizeof *arr))) {
nuclear@0 13 return 0;
nuclear@0 14 }
nuclear@0 15 arr->pdata = 0;
nuclear@0 16 arr->len = 0;
nuclear@0 17 return arr;
nuclear@0 18 }
nuclear@0 19
nuclear@0 20 void **fg_ptr_array_free(FakeGPtrArray *arr, int free_seg)
nuclear@0 21 {
nuclear@0 22 void **res;
nuclear@0 23
nuclear@0 24 if(!arr) {
nuclear@0 25 return 0;
nuclear@0 26 }
nuclear@0 27
nuclear@0 28 if(free_seg) {
nuclear@0 29 free(arr->pdata);
nuclear@0 30 res = 0;
nuclear@0 31 } else {
nuclear@0 32 res = arr->pdata;
nuclear@0 33 }
nuclear@0 34 free(arr);
nuclear@0 35 return res;
nuclear@0 36 }
nuclear@0 37
nuclear@0 38 void fg_ptr_array_add(FakeGPtrArray *arr, void *data)
nuclear@0 39 {
nuclear@0 40 int idx = arr->len++;
nuclear@0 41
nuclear@0 42 arr->pdata = realloc(arr->pdata, arr->len * sizeof *arr->pdata);
nuclear@0 43 assert(arr->pdata);
nuclear@0 44
nuclear@0 45 arr->pdata[idx] = data;
nuclear@0 46 }
nuclear@0 47
nuclear@0 48 int fg_ptr_array_remove(FakeGPtrArray *arr, void *data)
nuclear@0 49 {
nuclear@0 50 int i;
nuclear@0 51
nuclear@0 52 for(i=0; i<arr->len; i++) {
nuclear@0 53 if(arr->pdata[i] == data) {
nuclear@0 54 fg_ptr_array_remove_index(arr, i);
nuclear@0 55 return 1;
nuclear@0 56 }
nuclear@0 57 }
nuclear@0 58
nuclear@0 59 return 0;
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 void *fg_ptr_array_remove_index(FakeGPtrArray *arr, unsigned int idx)
nuclear@0 63 {
nuclear@0 64 void *data = arr->pdata[idx];
nuclear@0 65 int rest = --arr->len - idx;
nuclear@0 66 if(rest > 0) {
nuclear@0 67 memmove(arr->pdata + idx, arr->pdata + idx + 1, rest * sizeof *arr->pdata);
nuclear@0 68 }
nuclear@0 69 return data;
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 void fg_ptr_array_sort(FakeGPtrArray *arr, FakeGCompareFunc cmp)
nuclear@0 73 {
nuclear@0 74 qsort(arr->pdata, arr->len, sizeof *arr->pdata, cmp);
nuclear@0 75 }
nuclear@0 76
nuclear@0 77 /* -- logging -- */
nuclear@0 78
nuclear@1 79 void fg_message(const char *fmt, ...)
nuclear@1 80 {
nuclear@1 81 va_list ap;
nuclear@1 82
nuclear@1 83 va_start(ap, fmt);
nuclear@1 84 vprintf(fmt, ap);
nuclear@1 85 va_end(ap);
nuclear@1 86 putchar('\n');
nuclear@1 87 }
nuclear@1 88
nuclear@0 89 void fg_warning(const char *fmt, ...)
nuclear@0 90 {
nuclear@0 91 va_list ap;
nuclear@0 92
nuclear@0 93 printf("warning: ");
nuclear@0 94
nuclear@0 95 va_start(ap, fmt);
nuclear@0 96 vprintf(fmt, ap);
nuclear@0 97 va_end(ap);
nuclear@1 98 putchar('\n');
nuclear@0 99 }
nuclear@0 100
nuclear@0 101 void fg_critical(const char *fmt, ...)
nuclear@0 102 {
nuclear@0 103 va_list ap;
nuclear@0 104
nuclear@0 105 printf("critical: ");
nuclear@0 106
nuclear@0 107 va_start(ap, fmt);
nuclear@0 108 vfprintf(stderr, fmt, ap);
nuclear@0 109 va_end(ap);
nuclear@1 110 putchar('\n');
nuclear@0 111
nuclear@0 112 if(getenv("G_DEBUG")) {
nuclear@0 113 abort();
nuclear@0 114 }
nuclear@0 115 }
nuclear@0 116
nuclear@0 117 void fg_error(const char *fmt, ...)
nuclear@0 118 {
nuclear@0 119 va_list ap;
nuclear@0 120
nuclear@0 121 printf("error: ");
nuclear@0 122
nuclear@0 123 va_start(ap, fmt);
nuclear@0 124 vfprintf(stderr, fmt, ap);
nuclear@0 125 va_end(ap);
nuclear@1 126 putchar('\n');
nuclear@0 127 abort();
nuclear@0 128 }
nuclear@0 129
nuclear@0 130 void fg_debug(const char *fmt, ...)
nuclear@0 131 {
nuclear@0 132 va_list ap;
nuclear@0 133
nuclear@0 134 printf("debug: ");
nuclear@0 135
nuclear@0 136 va_start(ap, fmt);
nuclear@0 137 vprintf(fmt, ap);
nuclear@0 138 va_end(ap);
nuclear@1 139 putchar('\n');
nuclear@0 140 }