/************************************************* ** ** ** dBASE.c ** ** ** ** SoftC (tm) Dbase Library ** ** Diskette TOC Demo Program ** ** ** ** Copyright (C) 1989, 1990 by ** ** SoftC, Ltd. ** ** 16820 3rd St NE ** ** Ham Lake, MN 55304 ** ** (612) 434-6968 ** ** ** ** All rights reserved. ** *************************************************/ /* The Diskette TOC Program is a simple program which will create a database (TOC.DBF) and three index files. The database record contains the following fields: field name type length description ---------- ---- ------ ----------- NAME C 64 file name LENGTH N 10.0 file size in bytes DATE D 8 file creation date TIME C 8 file creation time ATTRIBUTE C 3 file attribute (READ ONLY, HIDDEN) The index files created are: TOCNAME.NDX (file name), TOCLNGTH.NDX (file length), and TOCDATE.NDX (file creation date and time). The program uses the DOS functions "findfirst" and "findnext" to step through the files in the directory. It reformats the compressed file date ("mm/dd/yy") and time ("hh:mm:ss") and places the resultant data into the output buffer. After all fields have been entered it will append a record to the end of the database and add keys for each file found. After all the files have been cataloged, the name will be displayed in alphabetical order using TOCNAME.NDX. The program only works on the current directory. It does not support any command line arguments, although it would be easy for the user to add such support. Very little error checking is performed. The program as it stands has value only in the demonstration of certain library functions. However, it could form the basis of a diskette cataloger, or ... */ #include #include #include #if defined(__TURBOC__) #include #else #include #endif #include #include SC_FIELD fields[] = { "name",'c',64,0, "length",'n',10,0, "date",'d',8,0, "time",'c',8,0, "attribute",'n',3,0 }; void main () { #if defined(__TURBOC__) struct ffblk ffblk; #else struct find_t ffblk; #endif int r, w1, w2, w3, dbf, ndx1, ndx2, ndx3; char work[9], *key, name[65], fname[13], fattrib; long recno; unsigned fdate, ftime; double d, fsize; atexit(scdterm); /* make sure that the terminate function is called */ scdinit(20,0); /* initialize the database manager */ /* create the database file */ scddcreate("toc.dbf",5,fields,SC_DB3); scddopenx(&dbf,"toc.dbf",0); /* open database file */ /* create empty index file on file name field */ scdncreate("tocname.ndx",'c',"name",64); scdnopenx(&ndx1,"tocname.ndx",0); /* open index file */ /* create empty index file on file length field */ scdncreate("toclngth.ndx",'n',"length",10); scdnopenx(&ndx2,"toclngth.ndx",0); /* open index file */ /* create empty index file using expression "dtoc(date) + time" */ scdncreate("tocdate.ndx",'c',"dtoc(date) + time",16); scdnopenx(&ndx3,"tocdate.ndx",0); /* open index file */ /* begin DOS file search */ #if defined(__TURBOC__) findfirst("*.*",&ffblk,0); #else _dos_findfirst("*.*",0,&ffblk); #endif do { #if defined(__TURBOC__) strcpy(fname,ffblk.ff_name); /* file name */ fsize = (double) ffblk.ff_fsize; /* file size */ fdate = ffblk.ff_fdate; /* file creation date */ ftime = ffblk.ff_ftime; /* file creation time */ fattrib = ffblk.ff_attrib; /* file attributes */ #else strcpy(fname,ffblk.name); /* file name */ fsize = (double) ffblk.size; /* file size */ fdate = ffblk.wr_date; /* file creation date */ ftime = ffblk.wr_time; /* file creation time */ fattrib = ffblk.attrib; /* file attributes */ #endif /* ignore the files I am using */ if ((strcmp(fname,"TOC.DBF") != 0) && (strcmp(fname,"TOCNAME.NDX") != 0) && (strcmp(fname,"TOCLNGTH.NDX") != 0) && (strcmp(fname,"TOCDATE.NDX") != 0)) { scddfput(dbf,0,fname); /* put file name in record */ scddfput(dbf,1,&fsize); /* put file size in record */ w1 = ((fdate & 0xFE00) >> 9) + 80; /* add date to record */ w2 = (fdate & 0x01E0) >> 5; w3 = fdate & 0x001F; sprintf(work,"%02d/%02d/%02d",w2,w3,w1); scddfput(dbf,2,work); w1 = (ftime & 0xF800) >> 11; /* add file create time to record */ w2 = (ftime & 0x07E0) >> 5; w3 = (ftime & 0x001F) << 1; sprintf(work,"%02d:%02d:%02d",w1,w2,w3); scddfput(dbf,3,work); d = (double) fattrib; /* put file attributes in record */ scddfput(dbf,4,&d); if (scddrput(dbf,&recno,SC_ADD) == SC_SUCCESS) { /* write record */ scdnkadd(ndx1,fname,recno); /* add key to "name" index */ scdnkadd(ndx2,&fsize,recno); /* add key to "length" index */ scdnkmake(dbf,ndx3,(void **) &key); /* add key to "dtoc(date) + time" index */ scdnkadd(ndx3,key,recno); free(key); } } #if defined(__TURBOC__) r = findnext(&ffblk); /* get next file */ } while (r != -1); #else r = _dos_findnext(&ffblk); } while (r == 0); #endif scdnktop(ndx1,name,&recno); /* get first record in "name" index */ do { scddrget(dbf,recno); /* get corresponding record */ scddfget(dbf,0,name); /* get file name field */ scddfget(dbf,1,&d); /* get file size */ printf("%s %10.0lf\n",name,d); scdnknext(ndx1,name,&recno); /* get next key */ } while (sc_code != SC_END); /* keep doing this until no more keys */ scddclose(dbf); /* close data file */ scdnclose(ndx1); /* close "name" index file */ scdnclose(ndx2); /* close "length" index file */ scdnclose(ndx3); /* close other index file */ }