]> git.sur5r.net Git - cc65/blob - src/ca65/filetab.c
8448d44df9a3146b54a99f9e929d828e54c5e461
[cc65] / src / ca65 / filetab.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 filetab.h                                 */
4 /*                                                                           */
5 /*                         Input file table for ca65                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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 /* common */
37 #include "check.h"
38 #include "hashstr.h"
39 #include "xmalloc.h"
40
41 /* ca65 */
42 #include "error.h"
43 #include "objfile.h"
44 #include "filetab.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* An entry in the file table */
55 typedef struct FileEntry FileEntry;
56 struct 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 */
62 };
63
64 /* Array of all entries, listed by index */
65 static FileEntry**      FileTab   = 0;
66 static unsigned         FileCount = 0;
67 static unsigned         FileMax   = 0;
68
69 /* Hash table, hashed by name */
70 #define HASHTAB_SIZE    31
71 static FileEntry*       HashTab[HASHTAB_SIZE];
72
73
74
75 /*****************************************************************************/
76 /*                                   Code                                    */
77 /*****************************************************************************/
78
79
80
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 */
83 {
84     /* Get the length of the name */
85     unsigned Len = strlen (Name);
86
87     /* Get the hash over the name */
88     unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
89
90     /* Allocate memory for the entry */
91     FileEntry* F = xmalloc (sizeof (FileEntry) + Len);
92
93     /* Initialize the fields */
94     F->Index    = FileCount+1;
95     F->Size     = Size;
96     F->MTime    = MTime;
97     memcpy (F->Name, Name, Len+1);
98
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);
104
105         /* Copy the old entries */
106         memcpy (NewFileTab, FileTab, sizeof (FileEntry*) * FileCount);
107
108         /* Use the new table */
109         xfree (FileTab);
110         FileTab = NewFileTab;
111         FileMax = NewFileMax;
112     }
113
114     /* Insert the file into the file table */
115     FileTab [FileCount++] = F;
116
117     /* Insert the entry into the hash table */
118     F->Next = HashTab[Hash];
119     HashTab[Hash] = F;
120
121     /* Return the new entry */
122     return F;
123 }
124
125
126
127 const char* GetFileName (unsigned Name)
128 /* Get the name of a file where the name index is known */
129 {
130     PRECONDITION (Name <= FileCount);
131     if (Name == 0) {
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.
135          */
136         if (FileCount == 0) {
137             /* No files defined until now */
138             return "(outside file scope)";
139         } else {
140             return FileTab [0]->Name;
141         }
142     } else {
143         return FileTab [Name-1]->Name;
144     }
145 }
146
147
148
149 unsigned GetFileIndex (const char* Name)
150 /* Return the file index for the given file name. */
151 {
152     /* Get the hash over the name */
153     unsigned Hash = HashStr (Name) % HASHTAB_SIZE;
154
155     /* Search the linear hash list */
156     FileEntry* F = HashTab[Hash];
157     while (F) {
158         /* Is it this one? */
159         if (strcmp (Name, F->Name) == 0) {
160             /* Found, return the index */
161             return F->Index;
162         }
163         /* No, check next */
164         F = F->Next;
165     }
166
167     /* Not found, use main file */
168     Error (ERR_FILENAME_NOT_FOUND, Name);
169     return 0;
170 }
171
172
173
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
176  * the table.
177  */
178 {
179     /* Create a new file entry and insert it into the tables */
180     FileEntry* F = NewFileEntry (Name, Size, MTime);
181
182     /* Return the index */
183     return F->Index;
184 }
185
186
187
188 void WriteFiles (void)
189 /* Write the list of input files to the object file */
190 {
191     unsigned I;
192
193     /* Tell the obj file module that we're about to start the file list */
194     ObjStartFiles ();
195
196     /* Write the file count */
197     ObjWriteVar (FileCount);
198
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);
207     }
208
209     /* Done writing files */
210     ObjEndFiles ();
211 }
212
213
214