home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / UTILITY / SORTREGS.ZIP / SORTREGS.C < prev   
Text File  |  1995-12-20  |  3KB  |  111 lines

  1. The Black Dragon 1@1180
  2. Mon Dec 18 14:30:18 1995 PST
  3. This program is designed to sort and merge a regions file.  It will operate 
  4. on REGIONS.DAT and on the extended regions files.  Should it find two assignments 
  5. for the same area code (or prefix for the extended files), it will take the 
  6. LATER occurance.  When it writes the data back to the file, it is written 
  7. in alphabetical order for the locations and numerical order within the location 
  8. for the area codes or prefixes.
  9.  
  10. It is distributed in SOURCE code to demonstrate to certain paranoid individuals 
  11. on the various networks that there is nothing covert going on here.  Regardless, 
  12. I still retain the copyright and ask that anyone who modifies this code give me 
  13. the credit I am due for being the original author.
  14.  
  15. The program was compiled using Turbo C++ 1.0 to a COM file (Tiny model).  
  16. It should work with any other compilation environment.....
  17. --------------------------------------------
  18. #include <alloc.h>
  19. #include <conio.h>
  20. #include <ctype.h>
  21. #include <dir.h>
  22. #include <dos.h>
  23. #include <fcntl.h>
  24. #include <io.h>
  25. #include <math.h>
  26. #include <mem.h>
  27. #include <process.h>
  28. #include <stat.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stat.h>
  33. #define MAX_VALUE 1000
  34.  
  35. typedef struct prefix_rec {
  36.   struct prefix_rec *next;
  37.   unsigned int start;
  38.   unsigned int count;
  39.   unsigned char text;
  40. } prefix_rec;
  41.  
  42. prefix_rec *first=NULL;
  43. prefix_rec *exchange[MAX_VALUE];
  44.  
  45. void main(int argc, char *argv[])
  46. {
  47.   FILE *f;
  48.   prefix_rec *p2;
  49.   prefix_rec *p=NULL;
  50.   unsigned char *q, s[80];
  51.   signed int i;
  52.  
  53.   if (argc!=2) {
  54.     fprintf(stderr,"%s <regionsfile>\n",argv[0]);
  55.     exit(1);
  56.   }
  57.   if ((f=fopen(argv[1],"rt"))==NULL) {
  58.     fprintf(stderr,"%s: Could not read file \"%s\"\n",argv[0],argv[1]);
  59.     exit(2);
  60.   }
  61.   for(i=0;i<MAX_VALUE;++i) exchange[i]=NULL;
  62.   while(fgets(s,sizeof(s),f)!=NULL) {
  63.     s[sizeof(s)-1]=0;
  64.     if ((q=strchr(s,'\n'))!=NULL) *q=0;
  65.     if (strlen(s)<3) continue;
  66.     if (isdigit(s[0])) {
  67.       if ((i=atol(s))>=MAX_VALUE) continue;
  68.       if (exchange[i]!=NULL) --exchange[i]->count;
  69.       if ((exchange[i]=p)==NULL) continue;
  70.       if (i<p->start) p->start=i;
  71.       ++p->count;
  72.     } else {
  73.       p2=(prefix_rec *)(((p!=NULL)&&(stricmp(&p->text,s)<0))?p:&first);
  74.       for(;p2!=NULL;p2=p2->next) {
  75.         if ((p=p2->next)!=NULL) {
  76.           if (!(i=stricmp(&p->text,s))) break;
  77.           if (i<0) continue;
  78.         }
  79.         if ((p=(prefix_rec *)farmalloc(sizeof(prefix_rec)+strlen(s)))==NULL) {
  80.           fprintf(stderr,"%s: Out of memory\n",argv[0]);
  81.           exit(3);
  82.         }
  83.         strcpy(&p->text,s);
  84.         p->start=MAX_VALUE;
  85.         p->next=p2->next;
  86.         p2->next=p;
  87.         p->count=0;
  88.         break;
  89.       }
  90.     }
  91.   }
  92.   fclose(f);
  93.   if ((f=fopen(argv[1],"wt+"))==NULL) {
  94.     fprintf(stderr,"%s: Could not write file \"%s\"\n",argv[0],argv[1]);
  95.     exit(4);
  96.   }
  97.   for(p=first;p!=NULL;p=p->next) {
  98.     if (!p->count) continue;
  99.     fprintf(f,"%s\n",&p->text);
  100.     for(i=p->start;i<MAX_VALUE;++i) {
  101.       if (exchange[i]==p) {
  102.         exchange[i]=NULL;
  103.         fprintf(f,"%03u\n",i);
  104.         if (!--p->count) break;
  105.       }
  106.     }
  107.   }
  108.   fclose(f);
  109. }
  110. --------------------------------------------
  111.