1 /*****************************************************************************/
5 /* sOURCE FILE INFO STRUCTURE */
9 /* (C) 2001-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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 /*****************************************************************************/
46 /*****************************************************************************/
48 /*****************************************************************************/
52 /* A list of all file infos without duplicates */
53 static Collection FileInfos = STATIC_COLLECTION_INITIALIZER;
57 /*****************************************************************************/
59 /*****************************************************************************/
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.
70 /* Do a binary search */
72 int Hi = (int) CollCount (&FileInfos) - 1;
77 int Cur = (Lo + Hi) / 2;
80 FileInfo* CurItem = CollAt (&FileInfos, Cur);
83 if (CurItem->Name < Name) {
87 /* Since we may have duplicates, repeat the search until we've
88 * the first item that has a match.
90 if (CurItem->Name == Name) {
96 /* Pass back the index. This is also the insert position */
103 static FileInfo* NewFileInfo (void)
104 /* Allocate and initialize a new FileInfo struct and return it */
106 /* We will assign file info ids in increasing order of creation */
107 static unsigned Id = 0;
109 /* Allocate memory */
110 FileInfo* FI = xmalloc (sizeof (FileInfo));
112 /* Initialize stuff */
116 /* Return the new struct */
122 FileInfo* ReadFileInfo (FILE* F, ObjData* O)
123 /* Read a file info from a file and return it */
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);
132 /* Search for the first entry with this name */
134 if (FindFileInfo (Name, &Index)) {
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.
141 FI = CollAt (&FileInfos, Index);
144 /* Check size and modification time stamp */
145 if (FI->Size == Size && FI->MTime == MTime) {
146 /* Return this one */
150 /* Check the next one */
151 if (++Index >= CollCount (&FileInfos)) {
155 FI = CollAt (&FileInfos, Index);
157 /* Done if the name differs */
158 if (FI->Name != Name) {
164 /* Not found. Allocate a new FileInfo structure */
172 /* Insert the file info in our global list. Index points to the insert
175 CollInsert (&FileInfos, FI, Index);
177 /* Return the new struct */