]> git.sur5r.net Git - cc65/blob - src/ca65/filetab.c
Replace error/warning numbers by strings.
[cc65] / src / ca65 / filetab.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 filetab.h                                 */
4 /*                                                                           */
5 /*                         Input file table for ca65                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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 char* GetFileName (unsigned Name)
184 /* Get the name of a file where the name index is known */
185 {
186     const FileEntry* F;
187
188     if (Name == 0) {
189         /* Name was defined outside any file scope, use the name of the first
190          * file instead. Errors are then reported with a file position of
191          * line zero in the first file.
192          */
193         if (CollCount (&FileTab) == 0) {
194             /* No files defined until now */
195             return "(outside file scope)";
196         } else {
197             F = CollConstAt (&FileTab, 0);
198         }
199     } else {
200         F = CollConstAt (&FileTab, Name-1);
201     }
202     return GetString (F->Name);
203 }
204
205
206
207 unsigned GetFileIndex (const char* Name)
208 /* Return the file index for the given file name. */
209 {
210     /* Get the string pool index from the name */
211     unsigned NameIdx = GetStringId (Name);
212
213     /* Search in the hash table for the name */
214     FileEntry* F = HT_FindEntry (&HashTab, &NameIdx);
215
216     /* If we don't have this index, print a diagnostic and use the main file */
217     if (F == 0) {
218         Error ("File name `%s' not found in file table", Name);
219         return 0;                                      
220     } else {
221         return F->Index;
222     }
223 }
224
225
226
227 unsigned AddFile (const char* Name, unsigned long Size, unsigned long MTime)
228 /* Add a new file to the list of input files. Return the index of the file in
229  * the table.
230  */
231 {
232     /* Create a new file entry and insert it into the tables */
233     FileEntry* F = NewFileEntry (GetStringId (Name), Size, MTime);
234
235     /* Return the index */
236     return F->Index;
237 }
238
239
240
241 void WriteFiles (void)
242 /* Write the list of input files to the object file */
243 {
244     unsigned I;
245
246     /* Tell the obj file module that we're about to start the file list */
247     ObjStartFiles ();
248
249     /* Write the file count */
250     ObjWriteVar (CollCount (&FileTab));
251
252     /* Write the file data */
253     for (I = 0; I < CollCount (&FileTab); ++I) {
254         /* Get a pointer to the entry */
255         const FileEntry* F = CollConstAt (&FileTab, I);
256         /* Write the fields */
257         ObjWriteVar (F->Name);
258         ObjWrite32 (F->MTime);
259         ObjWrite32 (F->Size);
260     }
261
262     /* Done writing files */
263     ObjEndFiles ();
264 }
265
266
267