home *** CD-ROM | disk | FTP | other *** search
- /* Prompt file indexer for XBASIC */
-
- #include "stdio.h"
- #include "io.h"
- #include "fcntl.h"
- #include "stdlib.h"
- #include "dir.h"
-
- #define S_IFMT 0170000 /* file type mask */
- #define S_IFDIR 0040000 /* directory */
- #define S_IFCHR 0020000 /* character special */
- #define S_IFREG 0100000 /* regular */
- #define S_IREAD 0000400 /* read permission, owner */
- #define S_IWRITE 0000200 /* write permission, owner */
- #define S_IEXEC 0000100 /* execute/search permission, owner */
-
- static void pascal getline (char *);
- char * pascal fgetsx (char *,int,int);
-
- int fp;
-
- void main (int argc,char *argv[]) {
-
- int idxfp;
- struct ffblk f;
- char lineh[256];
- char s[18];
- register unsigned int x;
- long pos;
-
- if(argc<2) {
- fputs("\nNeed a root filename to index (ROOT for ROOT.TXT)...\n",stdout);
- exit(0);
- }
-
- strcpy(lineh,argv[1]);
- strncat(lineh,".TXT");
-
- if (findfirst (lineh,&f,0)) {
- fputs("\nCan't find file `",stdout);
- fputs(lineh,stdout);
- fputs("'\n",stdout);
- exit(1);
- }
-
- if (f.ff_fsize<1) {
- fputs("\nFile is null length\n",stdout);
- exit(1);
- }
-
- if ((fp=_open(lineh,O_RDONLY | O_TEXT | O_DENYNONE))==-1) {
- fputs("\nCan't open file\n",stdout);
- exit(1);
- }
-
- strcpy(lineh,argv[1]);
- strncat(lineh,".IDX");
- unlink(lineh);
-
- if ((idxfp=_open(lineh,O_RDWR | O_TEXT | O_DENYWRITE))==-1)
- if ((idxfp=creat(lineh,S_IWRITE))==-1) {
- fputs("\nCan't open index\n",stdout);
- exit(1);
- }
-
- fputs("\nIndexing...\n",stdout);
-
- getline(lineh);
- x=0;
- while (1) {
- if (strncmp(lineh,"\x1 END",5)==0) break;
- if (*lineh!='\x1') {
- continue;
- }
- pos=tell(fp);
- _write(idxfp,&pos,sizeof(long));
- getline(lineh);
- while(*lineh!='\x1') {
- getline(lineh);
- }
- x++;
- fputs(itoa(x,s,10),stdout);
- fputs(" \r",stdout);
- }
- _close(fp);
- _close(idxfp);
- }
-
-
- static void pascal getline (char *lineh) {
-
- if (fgetsx(lineh,256,fp)==NULL) strcpy(lineh,"\x1 END");
- }
-
-
-
- char * pascal fgetsx (char *str,int num,int handle) {
-
- static char *p;
- static long pos;
- static long len;
- static int x;
-
- if (eof(handle)) {
- *str=0;
- return NULL;
- }
- pos=tell(handle);
- x=_read(handle,str,num-1);
- if (x<1) {
- *str=0;
- return NULL;
- }
- str[x]=0;
- if (!(p=(char *)strchr(str,'\r'))) return str;
- *p='\n';
- if (!p[1]) lseek(handle,1L,SEEK_CUR);
- else {
- p[1]=0;
- len=(long)((int)p-(int)str);
- lseek(handle,pos+len+2L,SEEK_SET);
- }
- return str;
- }
-