summaryrefslogtreecommitdiff
path: root/macros/inrstex/inrsdoc/indsort.c
blob: 93b9f4dd24d504a515a7c45d1b50409613c9a4d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
This reads a an unsorted index file *.ind and writes a sorted, and converted
*.sor index file
File Names are specialized to the INRSTeX reference book
... DOS version uses strncmpi()
*/ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloc.h> 
#include <ctype.h>

#define NUL             ((char)0)
#define BSLASH          ((char)0x5c) 
#define CR              ((char)0x0d)
#define LF              ((char)0x0a) 
#define MAX             900
#define CS              20
#define SS              510
#define PL              8
/* type def */

typedef struct {
    char cmp_str[CS];
    char *str;
} ind;

/* the variables */

  FILE	*outfile;        /* out file. */
  FILE   *infile;       /* input file */
  char   in_buf[512];   /* input buffer */
  ind    index[MAX]; 
  char   work[512];
  int    j, k, l, cp, in_len ;        /*  dummy vars */
  int    mem_over, op;

/* Comparison Function */ 
int sortfcn(const void *a, const void *b)
  { 
    return (strcmpi(a,b));
   }
    



/*****
* The main function.
input file name is masth.ind output is masth.sor ... for inrstex ref book
*****/
void main()
    {printf("\n INRSTeX index massage and sort. \n     Input file is 'masth.ind'. \n     Output file is 'masth.sor'.\n");
     infile = fopen( "masth.ind", "r");
     if (infile==0) printf("\n Sorry ... I can't find the index file \n `masth.ind` \n"); 
      else {
       outfile = fopen( "masth.sor", "w" ); 
       j=0; while (j<MAX) {k=0; while (k<CS) {index[j].cmp_str[k]=' '; k++;}
                           j++; index[j].cmp_str[CS-1]=0;
                          }
       mem_over=0;
       cp=0;
       while ((fgets(in_buf, 510, infile))&&(cp<MAX))
         { in_len=strlen(in_buf);
           index[cp].str=malloc(in_len); 
           if (index[cp].str==0) {mem_over=1; 
                                printf("\n Memory Overflow");
                                break;
                            }
           strncpy(index[cp].str, in_buf, SS-1);
           j=0; k=0; 
           while ((j<CS-PL-1)&&(in_buf[k]!=LF))
             { if (isalpha(in_buf[k])) {
                                     index[cp].cmp_str[j]=in_buf[k]; 
                                        j++;
                                       }
               k++;
             }
           if (j==5) {j=0; while (j<5) {index[cp].cmp_str[j]=' '; j++; } }
                /* fix for single non-alphabetic commands */ 
           cp++;
         }
        qsort((void *) index, cp, sizeof(ind), sortfcn); 
        printf("\n %d index records were read and sorted. \n", cp);
         op=0;
       while (op<cp) 
        { 
        strncpy(work, index[op].str, SS-1);
        if (work[0]=='!') 
         {fputc(BSLASH,outfile);fputc('2',outfile);
          k=1; while  (work[k]!='!')
                      { if ((work[k]=='*')&&(work[k+1]=='/')&&(work[k+2]=='*'))
                           {fputc(LF,outfile); k++;k++;}
                          else 
                           {fputc(work[k],outfile); 
                            if (work[k]==BSLASH) fputc('1',outfile);
                           }
                       k++;
                      }
             
          k++;  while ((work[k]!=LF)&&(work[k]!=0))
                      {fputc(work[k],outfile); k++;}
          fputc(LF,outfile);
         }
          op++;
        }

     fclose(infile); fclose(outfile); 
      }
   }