home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 2
/
DATAFILE_PDCD2.iso
/
utilities3
/
discindex
/
DiscIndex
/
Source
/
C
/
FileUtils
next >
Wrap
Text File
|
1994-08-03
|
6KB
|
226 lines
/* DiscIndex 1.00
* File and scanning code - FileUtils.c
* By Neil A Carson 1994 A.D.
*/
#include <string.h>
#include "dbox.h"
#include "discindex.h"
#include "heap.h"
#include "kernel.h"
#include "visdelay.h"
#include "werr.h"
#include "wimp.h"
#define OS_FSControl 0x29
#define OS_GBPB 0x0C
extern scan_win_type scan_win_info;
extern dbox scan_dbox;
extern BOOL scan_open;
void fname_count(char *, os_gbpb_block *);
void fname_process(char *, os_gbpb_block *);
int files_to_process;
disc_type *dest_disc;
void do_scan(disc_type *disc)
{
_kernel_swi_regs r;
os_error *error;
BOOL filling = TRUE, all_go = FALSE;
char *buffer;
if (!scan_open)
{
scan_dbox = dbox_new("Scan");
dbox_show(scan_dbox);
}
if (scan_win_info.type & directories) dbox_setnumeric(scan_dbox, 5, 1);
else dbox_setnumeric(scan_dbox, 5, 0);
if (scan_win_info.type & applications) dbox_setnumeric(scan_dbox, 4, 1);
else dbox_setnumeric(scan_dbox, 4, 0);
if (scan_win_info.type & files_apps) dbox_setnumeric(scan_dbox, 3, 1);
else dbox_setnumeric(scan_dbox, 3, 0);
if (scan_win_info.type & files) dbox_setnumeric(scan_dbox, 1, 1);
else dbox_setnumeric(scan_dbox, 1, 0);
if (scan_win_info.type & root_only) dbox_setnumeric(scan_dbox, 6, 1);
else dbox_setnumeric(scan_dbox, 6, 0);
dbox_setnumeric(scan_dbox, 8, scan_win_info.dismount);
dbox_setfield(scan_dbox, 0, scan_win_info.drive);
if (scan_open) return;
scan_open = TRUE;
while (filling)
{
switch(dbox_fillin(scan_dbox))
{
case 7:
if (!dbox_persist())
{
all_go = TRUE;
filling = FALSE;
}
break;
case dbox_CLOSE:
filling = FALSE;
break;
}
}
scan_win_info.type = 0;
if (dbox_getnumeric(scan_dbox, 5)) scan_win_info.type |= directories;
if (dbox_getnumeric(scan_dbox, 4)) scan_win_info.type |= applications;
if (dbox_getnumeric(scan_dbox, 3)) scan_win_info.type |= files_apps;
if (dbox_getnumeric(scan_dbox, 1)) scan_win_info.type |= files;
if (dbox_getnumeric(scan_dbox, 6)) scan_win_info.type |= root_only;
scan_win_info.dismount = dbox_getnumeric(scan_dbox, 8);
dbox_getfield(scan_dbox, 0, scan_win_info.drive, 32);
dbox_dispose(&scan_dbox);
scan_open = FALSE;
if (!all_go) return;
files_to_process = 0;
dest_disc = disc;
if (dest_disc->files != 0) heap_free(dest_disc->files);
dest_disc->no = 0;
dest_disc->used = 0;
if (scan_win_info.type == 0)
{
werr(0, "These settings will not find anything!");
return;
}
/* Check desired drive/directory is valid */
if (scan_win_info.drive[0] == 0)
{
werr(0, "Can't deal with infinitely short paths!");
return;
}
r.r[0] = 0;
r.r[1] = (int) scan_win_info.drive;
error = (os_error *) _kernel_swi(OS_FSControl, &r, &r);
if (error != 0)
{
wimp_reporterror(error, wimp_EOK, "Disc Index");
return;
}
/* Grab disc name */
if ((buffer = heap_alloc(256)) == 0)
{
werr(0, "Not enough RAM for this operation!");
return;
}
r.r[0] = 5;
r.r[2] = (int) buffer;
error = (os_error *) _kernel_swi(OS_GBPB, &r, &r);
if (error != 0)
{
wimp_reporterror(error, wimp_EOK, "Disc Index");
return;
}
strncpy(dest_disc->disc_name, &buffer[1],buffer[0]);
dest_disc->disc_name[buffer[0]] = 0;
heap_free(buffer);
/* Count files needed.... ! */
visdelay_begin();
recurse(scan_win_info.drive, fname_count);
/* Grab RAM */
if (files_to_process == 0)
{
visdelay_end();
werr(0, "No files match search criteria!");
return;
}
dest_disc->files = heap_alloc((files_to_process + 1) * sizeof(file_type));
if (dest_disc->files == 0)
{
visdelay_end();
werr(0, "Not enough free memory to store files on this disc!");
return;
}
memset(dest_disc->files, 0, (files_to_process + 1) * sizeof(file_type));
recurse(scan_win_info.drive, fname_process); /* Now fetch and process ! */
if (scan_win_info.dismount)
_kernel_system("DISMOUNT", FALSE);
visdelay_end();
}
BOOL wanted(char *path, os_gbpb_block *file)
{
char *ptr;
BOOL is_dir = FALSE, is_app = FALSE, is_file_in_app = FALSE;
BOOL is_file = FALSE, is_root = FALSE;
if ((file->type == 2) && (file->name[0] != '!')) is_dir = TRUE;
if ((file->name[0] == '!') && (file->type == 2)) is_app = TRUE;
ptr = strchr(path, '!');
if ((ptr != 0) && (*(ptr - 1) == '.') && (file->type != 2))
is_file_in_app = TRUE;
if ((!is_file_in_app) && (file->type == 1)) is_file = TRUE;
if (strcmp(path, scan_win_info.drive) == 0) is_root = TRUE;
/* Now see if we actually want this entry or not...
*/
if ((scan_win_info.type & root_only) && (!is_root)) return FALSE;
if ((scan_win_info.type & directories) && (is_dir)) return TRUE;
if ((scan_win_info.type & applications) && (is_app)) return TRUE;
if ((scan_win_info.type & files_apps) && (is_file_in_app)) return TRUE;
if ((scan_win_info.type & files) && (is_file)) return TRUE;
return FALSE;
}
void fname_count(char *path, os_gbpb_block *file)
{
if (wanted(path, file)) files_to_process ++;
}
void fname_process(char *path, os_gbpb_block *file)
{
/* For filetype: >=0 = filetype, -1 = not known, -2 = dir, -3 = app */
dest_disc->used += file->len;
if (!wanted(path, file)) return; /* Quick exit clause if needed... */
strncpy(dest_disc->files[dest_disc->no].name, file->name, 31);
dest_disc->files[dest_disc->no].name[31] = 0;
dest_disc->files[dest_disc->no].size = file->len;
if (file->type == 2)
{
dest_disc->files[dest_disc->no].type = -2;
if (file->name[0] == '!') dest_disc->files[dest_disc->no].type = -3;
dest_disc->no ++;
return;
}
if ((file->load & 0xFFF00000) != 0xFFF00000)
{
dest_disc->files[dest_disc->no].type = -1;
dest_disc->no ++;
return;
}
dest_disc->files[dest_disc->no].type = (file->load >> 8) & 0xFFF;
dest_disc->no ++;
}