]> git.sur5r.net Git - cc65/blob - src/ld65/fileinfo.c
7bf4e1d5588d79fd566505f0910379ced3a22222
[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
44
45
46 /*****************************************************************************/
47 /*                                   Data                                    */
48 /*****************************************************************************/
49
50
51
52 /* A list of all file infos without duplicates */
53 static Collection FileInfos = STATIC_COLLECTION_INITIALIZER;
54
55
56
57 /*****************************************************************************/
58 /*                                   Code                                    */
59 /*****************************************************************************/
60
61
62
63 static int FindFileInfo (unsigned Name, unsigned* Index)
64 /* Find the FileInfo for a given file name. The function returns true if the
65  * name was found. In this case, Index contains the index of the first item
66  * that matches. If the item wasn't found, the function returns false and
67  * Index contains the insert position for FileName.
68  */
69 {
70     /* Do a binary search */
71     int Lo = 0;
72     int Hi = (int) CollCount (&FileInfos) - 1;
73     int Found = 0;
74     while (Lo <= Hi) {
75
76         /* Mid of range */
77         int Cur = (Lo + Hi) / 2;
78
79         /* Get item */
80         FileInfo* CurItem = CollAt (&FileInfos, Cur);
81
82         /* Found? */
83         if (CurItem->Name < Name) {
84             Lo = Cur + 1;
85         } else {
86             Hi = Cur - 1;
87             /* Since we may have duplicates, repeat the search until we've
88              * the first item that has a match.
89              */
90             if (CurItem->Name == Name) {
91                 Found = 1;
92             }
93         }
94     }
95
96     /* Pass back the index. This is also the insert position */
97     *Index = Lo;
98     return Found;
99 }
100
101
102
103 static FileInfo* NewFileInfo (void)
104 /* Allocate and initialize a new FileInfo struct and return it */
105 {
106     /* We will assign file info ids in increasing order of creation */
107     static unsigned Id = 0;
108
109     /* Allocate memory */
110     FileInfo* FI = xmalloc (sizeof (FileInfo));
111
112     /* Initialize stuff */
113     FI->Id     = Id++; 
114     FI->Dumped = 0;
115
116     /* Return the new struct */
117     return FI;
118 }
119
120
121
122 FileInfo* ReadFileInfo (FILE* F, ObjData* O)
123 /* Read a file info from a file and return it */
124 {
125     FileInfo* FI;
126
127     /* Read the fields from the file */
128     unsigned      Name  = MakeGlobalStringId (O, ReadVar (F));
129     unsigned long MTime = Read32 (F);
130     unsigned long Size  = ReadVar (F);
131
132     /* Search for the first entry with this name */
133     unsigned Index;
134     if (FindFileInfo (Name, &Index)) {
135
136         /* We have at least one such entry. Try all of them and, if size and
137          * modification time matches, return the first match. When the loop
138          * is terminated without finding an entry, Index points one behind
139          * the last entry with the name, which is the perfect insert position.
140          */
141         FI = CollAt (&FileInfos, Index);
142         while (1) {
143
144             /* Check size and modification time stamp */
145             if (FI->Size == Size && FI->MTime == MTime) {
146                 /* Return this one */
147                 return FI;
148             }
149
150             /* Check the next one */
151             if (++Index >= CollCount (&FileInfos)) {
152                 /* Nothing left */
153                 break;
154             }
155             FI = CollAt (&FileInfos, Index);
156
157             /* Done if the name differs */
158             if (FI->Name != Name) {
159                 break;
160             }
161         }
162     }
163
164     /* Not found. Allocate a new FileInfo structure */
165     FI = NewFileInfo ();
166
167     /* Set the fields */
168     FI->Name  = Name;
169     FI->MTime = MTime;
170     FI->Size  = Size;
171
172     /* Insert the file info in our global list. Index points to the insert
173      * position.
174      */
175     CollInsert (&FileInfos, FI, Index);
176
177     /* Return the new struct */
178     return FI;
179 }
180
181
182