]> git.sur5r.net Git - cc65/blob - src/ca65/filetab.c
2ad8257c0e4ca03b8809fe9429330c81aa90733b
[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 "hashstr.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 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* An entry in the file table */
59 typedef struct FileEntry FileEntry;
60 struct FileEntry {
61     unsigned            Name;           /* File name */
62     FileEntry*          Next;           /* Next in hash list */
63     unsigned            Index;          /* Index of entry */
64     unsigned long       Size;           /* Size of file */
65     unsigned long       MTime;          /* Time of last modification */
66 };
67
68 /* Array of all entries, listed by index */
69 static Collection FileTab = STATIC_COLLECTION_INITIALIZER;
70
71 /* Hash table, hashed by name */
72 #define HASHTAB_MASK    0x1FU
73 #define HASHTAB_SIZE    (HASHTAB_MASK + 1)
74 static FileEntry*       HashTab[HASHTAB_SIZE];
75
76
77
78 /*****************************************************************************/
79 /*                                   Code                                    */
80 /*****************************************************************************/
81
82
83
84 static FileEntry* NewFileEntry (unsigned Name, unsigned long Size, unsigned long MTime)
85 /* Create a new FileEntry, insert it into the tables and return it */
86 {
87     /* Get the hash over the name */
88     unsigned Hash = (Name & HASHTAB_MASK);
89
90     /* Allocate memory for the entry */
91     FileEntry* F = xmalloc (sizeof (FileEntry));
92
93     /* Initialize the fields */
94     F->Name     = Name;
95     F->Index    = CollCount (&FileTab) + 1;     /* First file has index #1 */
96     F->Size     = Size;
97     F->MTime    = MTime;
98
99     /* Insert the file into the file table */
100     CollAppend (&FileTab, F);
101
102     /* Insert the entry into the hash table */
103     F->Next = HashTab[Hash];
104     HashTab[Hash] = F;
105
106     /* Return the new entry */
107     return F;
108 }
109
110
111
112 const char* GetFileName (unsigned Name)
113 /* Get the name of a file where the name index is known */
114 {
115     const FileEntry* F;
116
117     if (Name == 0) {
118         /* Name was defined outside any file scope, use the name of the first
119          * file instead. Errors are then reported with a file position of
120          * line zero in the first file.
121          */
122         if (CollCount (&FileTab) == 0) {
123             /* No files defined until now */
124             return "(outside file scope)";
125         } else {
126             F = CollConstAt (&FileTab, 0);
127         }
128     } else {
129         F = CollConstAt (&FileTab, Name-1);
130     }
131     return GetString (F->Name);
132 }
133
134
135
136 unsigned GetFileIndex (const char* Name)
137 /* Return the file index for the given file name. */
138 {
139     /* Get the string pool index from the name */
140     unsigned NameIdx = GetStringId (Name);
141
142     /* Get the hash over the name */
143     unsigned Hash = (NameIdx & HASHTAB_MASK);
144
145     /* Search the linear hash list */
146     FileEntry* F = HashTab[Hash];
147     while (F) {
148         /* Is it this one? */
149         if (NameIdx == F->Name) {
150             /* Found, return the index */
151             return F->Index;
152         }
153         /* No, check next */
154         F = F->Next;
155     }
156
157     /* Not found, use main file */
158     Error (ERR_FILENAME_NOT_FOUND, Name);
159     return 0;
160 }
161
162
163
164 unsigned AddFile (const char* Name, unsigned long Size, unsigned long MTime)
165 /* Add a new file to the list of input files. Return the index of the file in
166  * the table.
167  */
168 {
169     /* Create a new file entry and insert it into the tables */
170     FileEntry* F = NewFileEntry (GetStringId (Name), Size, MTime);
171
172     /* Return the index */
173     return F->Index;
174 }
175
176
177
178 void WriteFiles (void)
179 /* Write the list of input files to the object file */
180 {
181     unsigned I;
182
183     /* Tell the obj file module that we're about to start the file list */
184     ObjStartFiles ();
185
186     /* Write the file count */
187     ObjWriteVar (CollCount (&FileTab));
188
189     /* Write the file data */
190     for (I = 0; I < CollCount (&FileTab); ++I) {
191         /* Get a pointer to the entry */
192         const FileEntry* F = CollConstAt (&FileTab, I);
193         /* Write the fields */
194         ObjWriteVar (F->Name);
195         ObjWrite32 (F->MTime);
196         ObjWrite32 (F->Size);
197     }
198
199     /* Done writing files */
200     ObjEndFiles ();
201 }
202
203
204