OSDN Git Service

Support load/save of NBO info
[molby/Molby.git] / MolLib / Object.c
1 /*
2  *  Object.c
3  *
4  *  Created by Toshi Nagata on 06/03/11.
5  *  Copyright 2006-2008 Toshi Nagata. All rights reserved.
6  *
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation version 2 of the License.
10  
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15  */
16
17 #include "Object.h"
18 #include "Types.h"
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <ctype.h>
23
24 void
25 ObjectInit(Object *obj, Object **objRootPtr, const char *name)
26 {
27         obj->refCount = 1;
28         obj->next = *objRootPtr;
29         *objRootPtr = obj;
30         ObjectSetName(obj, name, *objRootPtr);
31 }
32
33 int
34 ObjectIncrRefCount(Object *obj)
35 {
36         if (obj != NULL)
37                 return ++(obj->refCount);
38         else return -1;
39 }
40
41 int
42 ObjectDecrRefCount(Object *obj)
43 {
44         if (obj != NULL)
45                 return --(obj->refCount);
46         else return -1;
47 }
48
49 void
50 ObjectDealloc(Object *obj, Object **objRootPtr)
51 {
52         Object **objp;
53         free((void *)(obj->name));
54         for (objp = objRootPtr; *objp != NULL; objp = &((*objp)->next)) {
55                 if (*objp == obj) {
56                         *objp = obj->next;
57                         break;
58                 }
59         }
60         free(obj);
61 }
62
63 void
64 ObjectSetName(Object *obj, const char *name, Object *objRoot)
65 {
66         if (obj->name != NULL)
67                 free((void *)(obj->name));
68         obj->name = strdup(name);
69         return;
70 }
71
72 const char *
73 ObjectGetName(Object *obj)
74 {
75         if (obj == NULL)
76                 return NULL;
77         else return obj->name;
78 }
79
80 Object *
81 ObjectWithName(const char *name, Object *objRoot)
82 {
83         Object *obj;
84         for (obj = objRoot; obj != NULL; obj = obj->next) {
85                 if (strcmp(obj->name, name) == 0)
86                         return obj;
87         }
88         return NULL;
89 }
90