-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddToList.c
executable file
·361 lines (325 loc) · 8.7 KB
/
AddToList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* AddToList.c contains the functions to add the Download Counter to the
* filelist. Note that the FAME DoorPort is no longer valid, so there is
* no other chance to inform the user about any errors! All errors will
* be logged to 'FAME:ExternEnv/Doors/mEGA-DLC.errors' to give the SysOp
* a bit more overview
*/
#include <proto/utility.h>
#include "struct_ex.h"
#include "proto.h"
#include "global_Defines.h"
void AddToFileList(void);
STATIC void WriteError(char *);
STATIC void CutSpaces(char *s);
STATIC BOOL ReadTheDir(BPTR dp);
STATIC BOOL AddToFList(char *name, long offset,BOOL ins);
STATIC void FreeFileList(void);
STATIC long CheckAndAdd(BPTR dp,struct DLC_Prefs *);
STATIC void InsertBlankLine(BPTR dir,struct FileList *,char *counta);
/*
* List of Files inside the Dirlisting
*/
struct FileList
{
struct FileList *next; /* Pointer to next file */
char FileName[32]; /* The Filename */
long Offset; /* The offset in the next line */
BOOL Insert, /* If TRUE, mEGA-DLC has to insert a line for the counter ! */
Processed; /* If TRUE, the counter for that file was added already */
}*f1,*f2;
#define ADD_FAILED -1
#define ADD_RELOAD 1
#define ADD_DONE 0
/*
* Main Add Function. Here finally the dirlistings will be loaded in the
* order gained from the Preferences. After getting one dir, all files
* will be checked to find out if any of the downloaded files are from
* this dir. If this is the case, the directory will be loaded and all
* files which came from the according conf will be searched and if found,
* the counter will be added directly below the name+size+date line of
* the Filelisting.
*/
void AddToFileList(void)
{
long mycount=NULL;
BPTR dirlock=NULL;
struct DLC_Prefs *test=pr1;
BOOL readthatconf=FALSE,stop=FALSE;
char mytest[256];
STATIC char *err_nodir="Unable to open %sDir%ld !";
*mytemp1='\0';*mytest='\0';
test=pr1;
while(test)
{
for(mycount=0;mycount<files;mycount++)
{
if(FileArray[mycount].ConfNum==test->ConfNum) readthatconf=TRUE;
}
if(readthatconf==FALSE)
{
test=test->next;
continue;
}
readthatconf=FALSE;
SPrintf(mytest,"%sDir%ld",test->ConfLoc,test->ConfMaxDirs);
if(!(dirlock=Open(mytest,MODE_READWRITE)))
{
SPrintf(mytemp1,err_nodir,test->ConfLoc,test->ConfMaxDirs);
WriteError(mytemp1);
return;
}
if(ReadTheDir(dirlock)==FALSE)
{
Close(dirlock);
SPrintf(mytemp1,err_nodir,test->ConfLoc,test->ConfMaxDirs);
WriteError(mytemp1);
return;
}
while(stop==FALSE)
{
switch(CheckAndAdd(dirlock,test))
{
case ADD_RELOAD: Close(dirlock);dirlock=NULL;
DeleteFile(mytest);
*mytemp1='\0';
strcpy(mytemp1,mytest);
strcat(mytemp1,"_new");
Rename(mytemp1,mytest);
if(!(dirlock=Open(mytest,MODE_READWRITE)))
{
SPrintf(mytemp1,err_nodir,test->ConfLoc,test->ConfMaxDirs);
WriteError(mytemp1);
return;
}
continue;
default: stop=TRUE;
break;
}
}
Close(dirlock);dirlock=NULL;
test=test->next;
stop=FALSE;
FreeFileList();
}
}
/*
* Function to add the counter. If neccessary, a blank line will be added to
* the dirlisting to avoid overwritten filenames.
*/
STATIC long CheckAndAdd(BPTR dp,struct DLC_Prefs *dlcp)
{
struct FileList *myptr=f1;
long mycount=NULL,filecount=NULL;
for(mycount=0;mycount<files;mycount++)
{
if(FileArray[mycount].ConfNum==dlcp->ConfNum)
{
myptr=f1;
while(myptr)
{
if(!Stricmp(FileArray[mycount].FileName,myptr->FileName))
{
if(myptr->Processed==FALSE)
{
if(myptr->Insert==TRUE)
{
InsertBlankLine(dp,myptr,dlcp->Counter);
myptr->Processed=TRUE;
return(ADD_RELOAD);
}
else
{
*mytemp1='\0';
Seek(dp,myptr->Offset+dlcp->CounterPos,OFFSET_BEGINNING);
Read(dp,mytemp1,7);
mytemp1[6]='\0';
CutSpaces(mytemp1);
StrToLong(mytemp1,&filecount);
filecount++;
*mytemp1='\0';
sprintf(mytemp1,dlcp->Counter,filecount);
Seek(dp,myptr->Offset,OFFSET_BEGINNING);
FPuts(dp,mytemp1);
Flush(dp);
myptr->Processed=TRUE;
}
}
}
myptr=myptr->next;
}
}
}
return(ADD_DONE);
}
/*
* Function to read in the already opened filelisting and fill up the list of filenames
* and their seekpoints. Returns FALSE if anything fails.
*/
STATIC BOOL ReadTheDir(BPTR dp)
{
char localbuf[256],namebuf[32];
long coffset=NULL;
BOOL checkins=FALSE;
*localbuf='\0';*namebuf='\0';
while(FGets(dp,localbuf,255))
{
if(localbuf[25]=='-'&&localbuf[28]=='-')
{
coffset=Seek(dp,0L,OFFSET_CURRENT);
FAMEStrMid(localbuf,namebuf,1,12);
CutSpaces(namebuf);
FAMEMemSet(localbuf,'\0',255);
FGets(dp,localbuf,255);
if(localbuf[25]=='-'&&localbuf[28]=='-')
{
Seek(dp,coffset,OFFSET_BEGINNING);
checkins=TRUE;
}
else checkins=FALSE;
if(AddToFList(namebuf,coffset,checkins)==FALSE) return(FALSE);
}
}
return(TRUE);
}
/*
* Function to remove leading spaces inside the counter string
*/
STATIC void CutSpaces(char *s)
{
char *d=s;
while(*d==' ') *d++;
while(*d)
{
if(*d==' ') *d++;
else *s++=*d++;
}
*s='\0';
}
/*
* WriteError(char *) - Reports all strange things to PROGDIR:mEGA-DLC.errors
*/
STATIC void WriteError(char *errtext)
{
BPTR ptr=NULL;
#ifdef DEBUG
if(ptr=Open("RAM:TEST",MODE_READWRITE))
#else
if(ptr=Open("FAME:ExternEnv/Doors/mEGA-DLC.errors",MODE_READWRITE))
#endif
{
Seek(ptr,0L,OFFSET_END);
FPrintf(ptr,"%s\n",errtext);
Close(ptr);
}
}
/*
* List functions for the Filename-list, Allocation & Deallocation functions
*/
STATIC BOOL AddToFList(char *name, long offset,BOOL ins)
{
if(!f1)
{
if(!(f1=AllocPooled(dlc_pool,sizeof(struct FileList)))) return(FALSE);
f2=f1;
}
else
{
if(!(f2->next=AllocPooled(dlc_pool,sizeof(struct FileList)))) return(FALSE);
f2=f2->next;
}
f2->next=NULL;
strcpy(f2->FileName,name);
f2->Offset=offset;
f2->Insert=ins;
f2->Processed=FALSE; // TRUE only when counter was added
return(TRUE);
}
/*
* Removes the FileList from memory
*/
STATIC void FreeFileList(void)
{
struct FileList *help=f1;
while(help)
{
f1=help;
help=help->next;
FreePooled(dlc_pool,f1,sizeof(struct FileList));
}
f1=NULL;f2=NULL;
}
/*
* Function to add a blank line to the dirlisting to make room for the counter (V0.5)
* First the dirlisting will be copied from offset 0 until the offset given, than a
* blank line will be append to new copied file and after this the rest of the file
* will be append and the new created file will be renamed to the old filename.
*/
STATIC void InsertBlankLine(BPTR dir,struct FileList *fl,char *counta)
{
BPTR destptr=NULL;
char dirname[256];
long origlength=NULL,orgoffset=NULL,dummy=NULL,offset=NULL,diff=NULL;
struct FileInfoBlock __aligned fib;
APTR memblock=NULL;
struct FileList *mycheck=f1;
offset=fl->Offset;
*dirname='\0';
if(!NameFromFH(dir,dirname,249)) // Required for dest name + _new extension
{
WriteError("NameFromFH(): Buffer too short!");
return;
}
FAMEFillMem(&fib,'\0',sizeof(struct FileInfoBlock));
ExamineFH(dir,&fib);
origlength=fib.fib_Size;
*mytemp1='\0';
if(!(memblock=AllocPooled(dlc_pool,origlength)))
{
WriteError("CopyMemBlock(): Out of memory !!!");
return;
}
strcat(dirname,"_new");
if(!(destptr=Open(dirname,MODE_NEWFILE)))
{
WriteError("Unable to open directory for reading !");
FreePooled(dlc_pool,memblock,origlength);
return;
}
orgoffset=Seek(dir,0L,OFFSET_CURRENT);
Seek(dir,0L,OFFSET_BEGINNING);
Read(dir,memblock,offset);
if(Write(destptr,memblock,offset)!=offset)
{
Close(destptr);
FreePooled(dlc_pool,memblock,origlength);
WriteError("Error while writing destination directory !");
return;
}
*mytemp1='\0';
sprintf(mytemp1,counta,1); // First download of this file
strcat(mytemp1,"\n\0");
FPuts(destptr,mytemp1);
Flush(destptr);
dummy=Read(dir,memblock,origlength);
if(Write(destptr,memblock,dummy)!=dummy)
{
Close(destptr);
FreePooled(dlc_pool,memblock,origlength);
WriteError("Error while writing 2nd part of destination directory !");
return;
}
FAMEFillMem(&fib,'\0',sizeof(struct FileInfoBlock));
ExamineFH(destptr,&fib);
diff=fib.fib_Size-origlength;
Close(destptr);destptr=NULL;
FreePooled(dlc_pool,memblock,origlength);
while(mycheck)
{
if(mycheck->Processed==FALSE)
{
if(mycheck->Offset>offset) mycheck->Offset+=diff; // Correct offsets of all other offsets
}
mycheck=mycheck->next;
}
}