-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.c
58 lines (48 loc) · 1.06 KB
/
object.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "object.h"
#include <string.h>
object *object_create(char *name, size_t len)
{
if (name == NULL) return NULL;
size_t name_len = strlen(name) + 1;
object *o = (object *)calloc(1, sizeof(object));
if (o == NULL) {
return NULL;
}
o->name = (char *)calloc(name_len, sizeof(char));
if (o->name == NULL) {
free(o);
return NULL;
}
o->name = strcpy(o->name, name);
o->len = len;
return o;
}
int object_compare(void *obj, void *obj_name)
{
object *o = (object *)obj;
char *objname = (char *)obj_name;
if (o == NULL && objname == NULL) return 0;
if (o == NULL) return 1;
if (objname == NULL) return -1;
return strcmp(o->name, objname);
}
void object_dump(void *obj, FILE *f)
{
if (obj == NULL) return;
object *o = (object *)obj;
if (o->name == NULL) {
fprintf(f, "0\n");
fprintf(f, "0\n");
} else {
fprintf(f, "%zu\n", strlen(o->name));
fprintf(f, "%s\n", o->name);
fprintf(f, "%zu\n", o->len);
}
}
void object_destroy(void *obj)
{
if (obj == NULL) return;
object *o = (object *)obj;
if (o->name != NULL) free(o->name);
free(o);
}