]> git.sur5r.net Git - cc65/blob - src/ca65/filetab.c
Make much more usage of dynamic strings (StrBufs) instead of char* and
[cc65] / src / ca65 / filetab.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 filetab.h                                 */
4 /*                                                                           */
5 /*                         Input file table for ca65                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2008 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 "coll.h"
41 #include "hashtab.h"
42 #include "xmalloc.h"
43
44 /* ca65 */
45 #include "error.h"
46 #include "filetab.h"
47 #include "objfile.h"
48 #include "spool.h"
49
50
51
52 /*****************************************************************************/
53 /*                                 Forwards                                  */
54 /*****************************************************************************/
55
56
57
58 static unsigned HT_GenHash (const void* Key);
59 /* Generate the hash over a key. */
60
61 static const void* HT_GetKey (void* Entry);
62 /* Given a pointer to the user entry data, return a pointer to the key. */
63
64 static HashNode* HT_GetHashNode (void* Entry);
65 /* Given a pointer to the user entry data, return a pointer to the hash node */
66
67 static int HT_Compare (const void* Key1, const void* Key2);
68 /* Compare two keys. The function must return a value less than zero if
69  * Key1 is smaller than Key2, zero if both are equal, and a value greater
70  * than zero if Key1 is greater then Key2.
71  */
72
73
74
75 /*****************************************************************************/
76 /*                                   Data                                    */
77 /*****************************************************************************/
78
79
80
81 /* Number of entries in the table and the mask to generate the hash */
82 #define HASHTAB_MASK    0x1F
83 #define HASHTAB_COUNT   (HASHTAB_MASK + 1)
84
85 /* An entry in the file table */
86 typedef struct FileEntry FileEntry;
87 struct FileEntry {
88     HashNode            Node;
89     unsigned            Name;           /* File name */
90     unsigned            Index;          /* Index of entry */
91     unsigned long       Size;           /* Size of file */
92     unsigned long       MTime;          /* Time of last modification */
93 };
94
95 /* Array of all entries, listed by index */
96 static Collection FileTab = STATIC_COLLECTION_INITIALIZER;
97
98 /* Hash table functions */
99 static const HashFunctions HashFunc = {
100     HT_GenHash,
101     HT_GetKey,
102     HT_GetHashNode,
103     HT_Compare
104 };
105
106 /* Hash table, hashed by name */
107 static HashTable HashTab = STATIC_HASHTABLE_INITIALIZER (HASHTAB_COUNT, &HashFunc);
108
109
110
111 /*****************************************************************************/
112 /*                           Hash table functions                            */
113 /*****************************************************************************/
114
115
116
117 static unsigned HT_GenHash (const void* Key)
118 /* Generate the hash over a key. */
119 {
120     return (*(const unsigned*)Key & HASHTAB_MASK);
121 }
122
123
124
125 static const void* HT_GetKey (void* Entry)
126 /* Given a pointer to the user entry data, return a pointer to the index */
127 {
128     return &((FileEntry*) Entry)->Name;
129 }
130
131
132
133 static HashNode* HT_GetHashNode (void* Entry)
134 /* Given a pointer to the user entry data, return a pointer to the hash node */
135 {
136     return &((FileEntry*) Entry)->Node;
137 }
138
139
140
141 static int HT_Compare (const void* Key1, const void* Key2)
142 /* Compare two keys. The function must return a value less than zero if
143  * Key1 is smaller than Key2, zero if both are equal, and a value greater
144  * than zero if Key1 is greater then Key2.
145  */
146 {
147     return (int)*(const unsigned*)Key1 - (int)*(const unsigned*)Key2;
148 }
149
150
151
152 /*****************************************************************************/
153 /*                                   Code                                    */
154 /*****************************************************************************/
155
156
157
158 static FileEntry* NewFileEntry (unsigned Name, unsigned long Size, unsigned long MTime)
159 /* Create a new FileEntry, insert it into the tables and return it */
160 {
161     /* Allocate memory for the entry */
162     FileEntry* F = xmalloc (sizeof (FileEntry));
163
164     /* Initialize the fields */
165     InitHashNode (&F->Node, F);
166     F->Name     = Name;
167     F->Index    = CollCount (&FileTab) + 1;     /* First file has index #1 */
168     F->Size     = Size;
169     F->MTime    = MTime;
170
171     /* Insert the file into the file table */
172     CollAppend (&FileTab, F);
173
174     /* Insert the entry into the hash table */
175     HT_Insert (&HashTab, &F->Node);
176
177     /* Return the new entry */
178     return F;
179 }
180
181
182
183 const StrBuf* GetFileName (unsigned Name)
184 /* Get the name of a file where the name index is known */
185 {
186     static StrBuf ErrorMsg = LIT_STRBUF_INITIALIZER ("(outside file scope)");
187
188     const FileEntry* F;
189
190     if (Name == 0) {
191         /* Name was defined outside any file scope, use the name of the first
192          * file instead. Errors are then reported with a file position of
193          * line zero in the first file.
194          */
195         if (CollCount (&FileTab) == 0) {
196             /* No files defined until now */
197             return &ErrorMsg;
198         } else {
199             F = CollConstAt (&FileTab, 0);
200         }
201     } else {
202         F = CollConstAt (&FileTab, Name-1);
203     }
204     return GetStrBuf (F->Name);
205 }
206
207
208
209 unsigned GetFileIndex (const StrBuf* Name)
210 /* Return the file index for the given file name. */
211 {
212     /* Get the string pool index from the name */
213     unsigned NameIdx = GetStrBufId (Name);
214
215     /* Search in the hash table for the name */
216     FileEntry* F = HT_FindEntry (&HashTab, &NameIdx);
217
218     /* If we don't have this index, print a diagnostic and use the main file */
219     if (F == 0) {
220         Error ("File name `%m%p' not found in file table", Name);
221         return 0;
222     } else {
223         return F->Index;
224     }
225 }
226
227
228
229 unsigned AddFile (const StrBuf* Name, unsigned long Size, unsigned long MTime)
230 /* Add a new file to the list of input files. Return the index of the file in
231  * the table.
232  */
233 {
234     /* Create a new file entry and insert it into the tables */
235     FileEntry* F = NewFileEntry (GetStrBufId (Name), Size, MTime);
236
237     /* Return the index */
238     return F->Index;
239 }
240
241
242
243 void WriteFiles (void)
244 /* Write the list of input files to the object file */
245 {
246     unsigned I;
247
248     /* Tell the obj file module that we're about to start the file list */
249     ObjStartFiles ();
250
251     /* Write the file count */
252     ObjWriteVar (CollCount (&FileTab));
253
254     /* Write the file data */
255     for (I = 0; I < CollCount (&FileTab); ++I) {
256         /* Get a pointer to the entry */
257         const FileEntry* F = CollConstAt (&FileTab, I);
258         /* Write the fields */
259         ObjWriteVar (F->Name);
260         ObjWrite32 (F->MTime);
261         ObjWrite32 (F->Size);
262     }
263
264     /* Done writing files */
265     ObjEndFiles ();
266 }
267
268
269
270