]> git.sur5r.net Git - cc65/blob - src/ca65/filetab.c
5b31fff3dc8475bc7fcdadc6789cdf18bb5498b3
[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 "xmalloc.h"
39
40 /* ca65 */
41 #include "error.h"
42 #include "objfile.h"
43 #include "filetab.h"
44
45
46
47 /*****************************************************************************/
48 /*                                   Data                                    */
49 /*****************************************************************************/
50
51
52
53 /* List of input files */
54 static struct {
55     unsigned long  MTime;               /* Time of last modification */
56     unsigned long  Size;                /* Size of file */
57     const char*    Name;                /* Name of file */
58 } Files [MAX_INPUT_FILES];
59 static unsigned    FileCount = 0;
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 const char* GetFileName (unsigned Name)
70 /* Get the name of a file where the name index is known */
71 {
72     PRECONDITION (Name <= FileCount);
73     if (Name == 0) {
74         /* Name was defined outside any file scope, use the name of the first
75          * file instead. Errors are then reported with a file position of
76          * line zero in the first file.
77          */
78         if (FileCount == 0) {
79             /* No files defined until now */
80             return "(outside file scope)";
81         } else {
82             return Files [0].Name;
83         }
84     } else {
85         return Files [Name-1].Name;
86     }
87 }
88
89
90
91 unsigned AddFile (const char* Name, unsigned long Size, unsigned long MTime)
92 /* Add a new file to the list of input files. Return the index of the file in
93  * the table.
94  */
95 {
96     /* Check for a table overflow */
97     if (FileCount >= MAX_INPUT_FILES) {
98         /* Table overflow */
99         Fatal (FAT_MAX_INPUT_FILES);
100     }
101
102     /* Add the file to the table */
103     Files [FileCount].Name  = xstrdup (Name);
104     Files [FileCount].Size  = Size;
105     Files [FileCount].MTime = MTime;
106
107     /* One more file */
108     return ++FileCount;
109 }
110
111
112
113 void WriteFiles (void)
114 /* Write the list of input files to the object file */
115 {
116     unsigned I;
117
118     /* Tell the obj file module that we're about to start the file list */
119     ObjStartFiles ();
120
121     /* Write the file count */
122     ObjWriteVar (FileCount);
123
124     /* Write the file data */
125     for (I = 0; I < FileCount; ++I) {
126         ObjWrite32 (Files [I].MTime);
127         ObjWrite32 (Files [I].Size);
128         ObjWriteStr (Files [I].Name);
129     }
130
131     /* Done writing files */
132     ObjEndFiles ();
133 }
134
135
136