annotate lwar/util.c @ 174:cc41ccee8f64

added --merge to lwar
author lost
date Tue, 03 Mar 2009 00:42:47 +0000
parents d610b8aef91b
children bae1e3ecdce1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
1 /*
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
2 util.c
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
4
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
5 This file is part of LWAR.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
6
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
7 LWAR is free software: you can redistribute it and/or modify it under the
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
10 version.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
11
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
15 more details.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
16
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
19 */
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
20
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
21 /*
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
22 Utility functions
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
23 */
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
24
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
25 #define __util_c_seen__
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
26
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
27 #include <malloc.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
28 #include <stdio.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
29 #include <stdlib.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
30 #include <string.h>
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
31
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
32 #include "util.h"
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
33
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
34 void *lw_malloc(int size)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
35 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
36 void *ptr;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
37
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
38 ptr = malloc(size);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
39 if (!ptr)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
40 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
41 // bail out; memory allocation error
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
42 fprintf(stderr, "lw_malloc(): Memory allocation error\n");
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
43 exit(1);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
44 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
45 return ptr;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
46 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
47
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
48 void *lw_realloc(void *optr, int size)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
49 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
50 void *ptr;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
51
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
52 if (size == 0)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
53 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
54 lw_free(optr);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
55 return;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
56 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
57
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
58 ptr = realloc(optr, size);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
59 if (!ptr)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
60 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
61 fprintf(stderr, "lw_realloc(): memory allocation error\n");
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
62 exit(1);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
63 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
64 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
65
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
66 void lw_free(void *ptr)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
67 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
68 if (ptr)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
69 free(ptr);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
70 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
71
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
72 char *lw_strdup(const char *s)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
73 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
74 char *d;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
75
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
76 if (!s)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
77 return NULL;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
78
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
79 d = strdup(s);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
80 if (!d)
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
81 {
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
82 fprintf(stderr, "lw_strdup(): memory allocation error\n");
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
83 exit(1);
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
84 }
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
85
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
86 return d;
d610b8aef91b Added LWAR skeleton
lost
parents:
diff changeset
87 }