]> git.sur5r.net Git - cc65/blob - src/ld65/fileinfo.c
d48280447bd0c29deb2fda4610302e97b90e18a9
[cc65] / src / ld65 / fileinfo.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                fileinfo.c                                 */
4 /*                                                                           */
5 /*                        Source file info structure                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 /* common */
37 #include "coll.h"
38 #include "xmalloc.h"
39
40 /* ld65 */
41 #include "fileio.h"
42 #include "fileinfo.h"
43 #include "objdata.h"
44 #include "spool.h"
45
46
47
48 /*****************************************************************************/
49 /*                                   Data                                    */
50 /*****************************************************************************/
51
52
53
54 /* A list of all file infos without duplicates */
55 static Collection FileInfos = STATIC_COLLECTION_INITIALIZER;
56
57
58
59 /*****************************************************************************/
60 /*                                   Code                                    */
61 /*****************************************************************************/
62
63
64
65 static int FindFileInfo (unsigned Name, unsigned* Index)
66 /* Find the FileInfo for a given file name. The function returns true if the
67  * name was found. In this case, Index contains the index of the first item
68  * that matches. If the item wasn't found, the function returns false and
69  * Index contains the insert position for FileName.
70  */
71 {
72     /* Do a binary search */
73     int Lo = 0;
74     int Hi = (int) CollCount (&FileInfos) - 1;
75     int Found = 0;
76     while (Lo <= Hi) {
77
78         /* Mid of range */
79         int Cur = (Lo + Hi) / 2;
80
81         /* Get item */
82         FileInfo* CurItem = CollAt (&FileInfos, Cur);
83
84         /* Found? */
85         if (CurItem->Name < Name) {
86             Lo = Cur + 1;
87         } else {
88             Hi = Cur - 1;
89             /* Since we may have duplicates, repeat the search until we've
90              * the first item that has a match.
91              */
92             if (CurItem->Name == Name) {
93                 Found = 1;
94             }
95         }
96     }
97
98     /* Pass back the index. This is also the insert position */
99     *Index = Lo;
100     return Found;
101 }
102
103
104
105 static FileInfo* NewFileInfo (void)
106 /* Allocate and initialize a new FileInfo struct and return it */
107 {
108     /* We will assign file info ids in increasing order of creation */
109     static unsigned Id = 0;
110
111     /* Allocate memory */
112     FileInfo* FI = xmalloc (sizeof (FileInfo));
113
114     /* Initialize stuff */
115     FI->Id     = Id++;
116     FI->Dumped = 0;
117
118     /* Return the new struct */
119     return FI;
120 }
121
122
123
124 FileInfo* ReadFileInfo (FILE* F, ObjData* O)
125 /* Read a file info from a file and return it */
126 {
127     FileInfo* FI;
128
129     /* Read the fields from the file */
130     unsigned      Name  = MakeGlobalStringId (O, ReadVar (F));
131     unsigned long MTime = Read32 (F);
132     unsigned long Size  = ReadVar (F);
133
134     /* Search for the first entry with this name */
135     unsigned Index;
136     if (FindFileInfo (Name, &Index)) {
137
138         /* We have at least one such entry. Try all of them and, if size and
139          * modification time matches, return the first match. When the loop
140          * is terminated without finding an entry, Index points one behind
141          * the last entry with the name, which is the perfect insert position.
142          */
143         FI = CollAt (&FileInfos, Index);
144         while (1) {
145
146             /* Check size and modification time stamp */
147             if (FI->Size == Size && FI->MTime == MTime) {
148                 /* Return this one */
149                 return FI;
150             }
151
152             /* Check the next one */
153             if (++Index >= CollCount (&FileInfos)) {
154                 /* Nothing left */
155                 break;
156             }
157             FI = CollAt (&FileInfos, Index);
158
159             /* Done if the name differs */
160             if (FI->Name != Name) {
161                 break;
162             }
163         }
164     }
165
166     /* Not found. Allocate a new FileInfo structure */
167     FI = NewFileInfo ();
168
169     /* Set the fields */
170     FI->Name  = Name;
171     FI->MTime = MTime;
172     FI->Size  = Size;
173
174     /* Insert the file info in our global list. Index points to the insert
175      * position.
176      */
177     CollInsert (&FileInfos, FI, Index);
178
179     /* Return the new struct */
180     return FI;
181 }
182
183
184
185 void PrintDbgFileInfo (FILE* F)
186 /* Output the file info to a debug info file */
187 {
188     unsigned I, J;
189
190     /* Print file infos from all modules we have linked into the output file */
191     for (I = 0; I < CollCount (&ObjDataList); ++I) {
192
193         /* Get the object file */
194         ObjData* O = CollAtUnchecked (&ObjDataList, I);
195
196         /* Output the files section */
197         for (J = 0; J < CollCount (&O->Files); ++J) {
198             FileInfo* FI = CollAt (&O->Files, J);
199             if (!FI->Dumped) {
200                 fprintf (F,
201                          "file\tid=%u,name=\"%s\",size=%lu,mtime=0x%08lX\n",
202                          FI->Id, GetString (FI->Name), FI->Size, FI->MTime);
203                 FI->Dumped = 1;
204             }
205         }
206     }
207 }
208
209
210