home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / MISC / XDEV_117.ZIP / INDEXIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-28  |  2.5 KB  |  126 lines

  1. /* Prompt file indexer for XBASIC */
  2.  
  3. #include "stdio.h"
  4. #include "io.h"
  5. #include "fcntl.h"
  6. #include "stdlib.h"
  7. #include "dir.h"
  8.  
  9. #define S_IFMT      0170000         /* file type mask */
  10. #define S_IFDIR     0040000         /* directory */
  11. #define S_IFCHR     0020000         /* character special */
  12. #define S_IFREG     0100000         /* regular */
  13. #define S_IREAD     0000400         /* read permission, owner */
  14. #define S_IWRITE    0000200         /* write permission, owner */
  15. #define S_IEXEC     0000100         /* execute/search permission, owner */
  16.  
  17. static void pascal getline (char *);
  18. char *         pascal fgetsx  (char *,int,int);
  19.  
  20. int fp;
  21.  
  22. void main (int argc,char *argv[]) {
  23.  
  24.     int idxfp;
  25.     struct ffblk f;
  26.     char lineh[256];
  27.     char s[18];
  28.     register unsigned int x;
  29.     long pos;
  30.  
  31.     if(argc<2) {
  32.         fputs("\nNeed a root filename to index (ROOT for ROOT.TXT)...\n",stdout);
  33.         exit(0);
  34.     }
  35.  
  36.     strcpy(lineh,argv[1]);
  37.     strncat(lineh,".TXT");
  38.  
  39.     if (findfirst (lineh,&f,0)) {
  40.         fputs("\nCan't find file `",stdout);
  41.         fputs(lineh,stdout);
  42.         fputs("'\n",stdout);
  43.         exit(1);
  44.     }
  45.  
  46.     if (f.ff_fsize<1) {
  47.         fputs("\nFile is null length\n",stdout);
  48.         exit(1);
  49.     }
  50.  
  51.     if ((fp=_open(lineh,O_RDONLY | O_TEXT | O_DENYNONE))==-1) {
  52.         fputs("\nCan't open file\n",stdout);
  53.         exit(1);
  54.     }
  55.  
  56.     strcpy(lineh,argv[1]);
  57.     strncat(lineh,".IDX");
  58.     unlink(lineh);
  59.  
  60.     if ((idxfp=_open(lineh,O_RDWR | O_TEXT | O_DENYWRITE))==-1)
  61.         if ((idxfp=creat(lineh,S_IWRITE))==-1) {
  62.                 fputs("\nCan't open index\n",stdout);
  63.                 exit(1);
  64.     }
  65.  
  66.     fputs("\nIndexing...\n",stdout);
  67.  
  68.     getline(lineh);
  69.     x=0;
  70.     while (1) {
  71.         if (strncmp(lineh,"\x1 END",5)==0) break;
  72.         if (*lineh!='\x1') {
  73.             continue;
  74.         }
  75.         pos=tell(fp);
  76.         _write(idxfp,&pos,sizeof(long));
  77.         getline(lineh);
  78.         while(*lineh!='\x1') {
  79.             getline(lineh);
  80.         }
  81.         x++;
  82.         fputs(itoa(x,s,10),stdout);
  83.         fputs(" \r",stdout);
  84.     }
  85.     _close(fp);
  86.     _close(idxfp);
  87. }
  88.  
  89.  
  90. static void pascal getline (char *lineh) {
  91.  
  92.     if (fgetsx(lineh,256,fp)==NULL) strcpy(lineh,"\x1 END");
  93. }
  94.  
  95.  
  96.  
  97. char * pascal fgetsx (char *str,int num,int handle) {
  98.  
  99.     static char *p;
  100.     static long pos;
  101.     static long len;
  102.     static int x;
  103.  
  104.     if (eof(handle)) {
  105.         *str=0;
  106.         return NULL;
  107.     }
  108.     pos=tell(handle);
  109.     x=_read(handle,str,num-1);
  110.     if (x<1) {
  111.         *str=0;
  112.         return NULL;
  113.     }
  114.     str[x]=0;
  115.     if (!(p=(char *)strchr(str,'\r'))) return str;
  116.     *p='\n';
  117.     if (!p[1]) lseek(handle,1L,SEEK_CUR);
  118.     else {
  119.         p[1]=0;
  120.         len=(long)((int)p-(int)str);
  121.         lseek(handle,pos+len+2L,SEEK_SET);
  122.     }
  123.     return str;
  124. }
  125.  
  126.