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

  1. /* Prompt file indexer for XBBS */
  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 (void) {
  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 (findfirst ("XBBS.TXT",&f,0)) {
  32.         fputs("\nCan't find XBBS.TXT\n",stdout);
  33.         exit(1);
  34.     }
  35.  
  36.     if (f.ff_fsize<1) {
  37.         fputs("\nXBBS.TXT is null length\n",stdout);
  38.         exit(1);
  39.     }
  40.  
  41.     if ((fp=_open("XBBS.TXT",O_RDONLY | O_TEXT | O_DENYNONE))==-1) {
  42.         fputs("\nCan't open XBBS.TXT\n",stdout);
  43.         exit(1);
  44.     }
  45.  
  46.     unlink("XBBS.IDX");
  47.  
  48.     if ((idxfp=_open("XBBS.IDX",O_RDWR | O_TEXT | O_DENYWRITE))==-1)
  49.         if ((idxfp=creat("XBBS.IDX",S_IWRITE))==-1) {
  50.                 fputs("\nCan't open XBBS.IDX\n",stdout);
  51.                 exit(1);
  52.     }
  53.  
  54.     fputs("\nIndexing XBBS.TXT...\n",stdout);
  55.  
  56.     getline(lineh);
  57.     x=0;
  58.     while (1) {
  59.         if (strncmp(lineh,"\x1 END",5)==0) break;
  60.         if (*lineh!='\x1') {
  61.             continue;
  62.         }
  63.         pos=tell(fp);
  64.         _write(idxfp,&pos,sizeof(long));
  65.         getline(lineh);
  66.         while(*lineh!='\x1') {
  67.             getline(lineh);
  68.         }
  69.         x++;
  70.         fputs(itoa(x,s,10),stdout);
  71.         fputs(" \r",stdout);
  72.     }
  73.     _close(fp);
  74.     _close(idxfp);
  75.  
  76.     if (findfirst ("XBBS.GXT",&f,0)) {
  77.         fputs("\nCan't find XBBS.GXT\n",stdout);
  78.         exit(0);
  79.     }
  80.  
  81.     if (f.ff_fsize<1) {
  82.         fputs("\nXBBS.GXT is null length\n",stdout);
  83.         exit(0);
  84.     }
  85.  
  86.     if ((fp=_open("XBBS.GXT",O_RDONLY | O_TEXT | O_DENYNONE))==-1) {
  87.         fputs("\nCan't open XBBS.GXT\n",stdout);
  88.         exit(0);
  89.     }
  90.  
  91.     unlink("XBBS.GDX");
  92.  
  93.     if ((idxfp=_open("XBBS.GDX",O_RDWR | O_TEXT | O_DENYWRITE))==-1)
  94.         if ((idxfp=creat("XBBS.GDX",S_IWRITE))==-1) {
  95.         fputs("\nCan't open XBBS.GDX\n",stdout);
  96.         exit(0);
  97.     }
  98.  
  99.     fputs("\nIndexing XBBS.GXT...\n",stdout);
  100.  
  101.     getline(lineh);
  102.     x=0;
  103.     while (1) {
  104.         if (strncmp(lineh,"\x1 END",5)==0) break;
  105.         if (*lineh!='\x1') {
  106.             continue;
  107.         }
  108.         pos=tell(fp);
  109.         _write(idxfp,&pos,sizeof(long));
  110.         getline(lineh);
  111.         while(*lineh!='\x1') {
  112.             getline(lineh);
  113.         }
  114.         x++;
  115.         fputs(itoa(x,s,10),stdout);
  116.         fputs(" \r",stdout);
  117.     }
  118.     _close(fp);
  119.     _close(idxfp);
  120. }
  121.  
  122.  
  123. static void pascal getline (char *lineh) {
  124.  
  125.     if (fgetsx(lineh,256,fp)==NULL) strcpy(lineh,"\x1 END");
  126. }
  127.  
  128.  
  129.  
  130. char * pascal fgetsx (char *str,int num,int handle) {
  131.  
  132.     static char *p;
  133.     static long pos;
  134.     static long len;
  135.     static int x;
  136.  
  137.     if (eof(handle)) {
  138.         *str=0;
  139.         return NULL;
  140.     }
  141.     pos=tell(handle);
  142.     x=_read(handle,str,num-1);
  143.     if (x<1) {
  144.         *str=0;
  145.         return NULL;
  146.     }
  147.     str[x]=0;
  148.     if (!(p=(char *)strchr(str,'\r'))) return str;
  149.     *p='\n';
  150.     if (!p[1]) lseek(handle,1L,SEEK_CUR);
  151.     else {
  152.         p[1]=0;
  153.         len=(long)((int)p-(int)str);
  154.         lseek(handle,pos+len+2L,SEEK_SET);
  155.     }
  156.     return str;
  157. }
  158.  
  159.