1 /*****************************************************************************/
5 /* Input file table for ca65 */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 /* An entry in the file table */
55 typedef struct FileEntry FileEntry;
57 FileEntry* Next; /* Next in hash list */
58 unsigned Index; /* Index of entry */
59 unsigned long Size; /* Size of file */
60 unsigned long MTime; /* Time of last modification */
61 char Name[1]; /* Name, dynamically allocated */
64 /* Array of all entries, listed by index */
65 static FileEntry** FileTab = 0;
66 static unsigned FileCount = 0;
67 static unsigned FileMax = 0;
69 /* Hash table, hashed by name */
70 #define HASHTAB_SIZE 31
71 static FileEntry* HashTab[HASHTAB_SIZE];
75 /*****************************************************************************/
77 /*****************************************************************************/
81 static FileEntry* NewFileEntry (const char* Name, unsigned long Size, unsigned long MTime)
82 /* Create a new FileEntry, insert it into the tables and return it */
84 /* Get the length of the name */
85 unsigned Len = strlen (Name);
87 /* Get the hash over the name */
88 unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
90 /* Allocate memory for the entry */
91 FileEntry* F = xmalloc (sizeof (FileEntry) + Len);
93 /* Initialize the fields */
94 F->Index = FileCount+1;
97 memcpy (F->Name, Name, Len+1);
99 /* Count the entries and grow the file table if needed */
100 if (FileCount >= FileMax) {
101 /* We need to grow the table. Create a new one. */
102 unsigned NewFileMax = (FileMax == 0)? 32 : FileMax * 2;
103 FileEntry** NewFileTab = xmalloc (sizeof (FileEntry*) * NewFileMax);
105 /* Copy the old entries */
106 memcpy (NewFileTab, FileTab, sizeof (FileEntry*) * FileCount);
108 /* Use the new table */
110 FileTab = NewFileTab;
111 FileMax = NewFileMax;
114 /* Insert the file into the file table */
115 FileTab [FileCount++] = F;
117 /* Insert the entry into the hash table */
118 F->Next = HashTab[Hash];
121 /* Return the new entry */
127 const char* GetFileName (unsigned Name)
128 /* Get the name of a file where the name index is known */
130 PRECONDITION (Name <= FileCount);
132 /* Name was defined outside any file scope, use the name of the first
133 * file instead. Errors are then reported with a file position of
134 * line zero in the first file.
136 if (FileCount == 0) {
137 /* No files defined until now */
138 return "(outside file scope)";
140 return FileTab [0]->Name;
143 return FileTab [Name-1]->Name;
149 unsigned GetFileIndex (const char* Name)
150 /* Return the file index for the given file name. */
152 /* Get the hash over the name */
153 unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
155 /* Search the linear hash list */
156 FileEntry* F = HashTab[Hash];
158 /* Is it this one? */
159 if (strcmp (Name, F->Name) == 0) {
160 /* Found, return the index */
167 /* Not found, use main file */
168 Error (ERR_FILENAME_NOT_FOUND, Name);
174 unsigned AddFile (const char* Name, unsigned long Size, unsigned long MTime)
175 /* Add a new file to the list of input files. Return the index of the file in
179 /* Create a new file entry and insert it into the tables */
180 FileEntry* F = NewFileEntry (Name, Size, MTime);
182 /* Return the index */
188 void WriteFiles (void)
189 /* Write the list of input files to the object file */
193 /* Tell the obj file module that we're about to start the file list */
196 /* Write the file count */
197 ObjWriteVar (FileCount);
199 /* Write the file data */
200 for (I = 0; I < FileCount; ++I) {
201 /* Get a pointer to the entry */
202 FileEntry* F = FileTab[I];
203 /* Write the fields */
204 ObjWrite32 (F->MTime);
205 ObjWrite32 (F->Size);
206 ObjWriteStr (F->Name);
209 /* Done writing files */