]> git.sur5r.net Git - cc65/blob - src/ar65/objdata.c
Simplify things using collections. Some more generic overhaul.
[cc65] / src / ar65 / objdata.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objdata.c                                 */
4 /*                                                                           */
5 /*              Handling object file data for the ar65 archiver              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* ar65 */
43 #include "error.h"
44 #include "objdata.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* Collection with object files */
55 Collection       ObjPool        = STATIC_COLLECTION_INITIALIZER;
56
57
58
59 /*****************************************************************************/
60 /*                                   Code                                    */
61 /*****************************************************************************/
62
63
64
65 ObjData* NewObjData (void)
66 /* Allocate a new structure on the heap, insert it into the list, return it */
67 {
68     /* Allocate memory */
69     ObjData* O = xmalloc (sizeof (ObjData));
70
71     /* Initialize the data */
72     O->Name        = 0;
73     O->Flags       = 0;
74     O->MTime       = 0;
75     O->Start       = 0;
76     O->Size        = 0;
77     O->StringCount = 0;
78     O->Strings     = 0;
79     O->ImportSize  = 0;
80     O->Imports     = 0;
81     O->ExportSize  = 0;
82     O->Exports     = 0;
83
84     /* Add it to the list */
85     CollAppend (&ObjPool, O);
86
87     /* Return the new entry */
88     return O;
89 }
90
91
92
93 void FreeObjData (ObjData* O)
94 /* Free a complete struct */
95 {
96     unsigned I;
97
98     xfree (O->Name);
99     xfree (O->Imports);
100     xfree (O->Exports);
101     for (I = 0; I < O->StringCount; ++I) {
102         xfree (O->Strings[I]);
103     }
104     xfree (O->Strings);
105     xfree (O);
106 }
107
108
109
110 ObjData* FindObjData (const char* Module)
111 /* Search for the module with the given name and return it. Return NULL if the
112  * module is not in the list.
113  */
114 {
115     unsigned I;
116
117     /* Hmm. Maybe we should hash the module names? */
118     for (I = 0; I < CollCount (&ObjPool); ++I) {
119
120         /* Get this object file */
121         ObjData* O = CollAtUnchecked (&ObjPool, I);
122
123         /* Did we find it? */
124         if (strcmp (O->Name, Module) == 0) {
125             return O;
126         }
127     }
128     return 0;
129 }
130
131
132
133 void DelObjData (const char* Module)
134 /* Delete the object module from the list */
135 {
136     unsigned I;
137     for (I = 0; I < CollCount (&ObjPool); ++I) {
138
139         /* Get this object file */
140         ObjData* O = CollAtUnchecked (&ObjPool, I);
141
142         /* Did we find it? */
143         if (strcmp (O->Name, Module) == 0) {
144
145             /* Free the entry */
146             CollDelete (&ObjPool, I);
147             FreeObjData (O);
148
149             /* Done */
150             return;
151         }
152     }
153
154     /* Not found! */
155     Warning ("Module `%s' not found in library", Module);
156 }
157
158
159
160 const char* GetObjString (const ObjData* O, unsigned Index)
161 /* Get a string from the string pool of an object file */
162 {
163     if (Index >= O->StringCount) {
164         Error ("Invalid string index (%u) in module `%s'",
165                Index, O->Name);
166     }
167     return O->Strings[Index];
168 }
169
170
171