~ubuntu-branches/ubuntu/karmic/fweb/karmic

« back to all changes in this revision

Viewing changes to Web/idxmerge.web

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-01-04 23:20:22 UTC
  • Revision ID: james.westby@ubuntu.com-20020104232022-330ad4iyzpvb5bm4
Tags: upstream-1.62
ImportĀ upstreamĀ versionĀ 1.62

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
@c
 
2
 
 
3
\Title{idxmerge}
 
4
 
 
5
@* INTRODUCTION.
 
6
This is skeleton code for the \.{idxmerge} utility.  For detailed usage
 
7
instructions, please see the \.{texinfo} documentation.
 
8
 
 
9
Apologies for the lack of comments.  Time is short, and this particular
 
10
subproject isn't really finished.
 
11
 
 
12
@a
 
13
#include <stdlib.h>
 
14
#include <stdio.h>
 
15
#include <string.h>
 
16
 
 
17
@ The following routine works with |qsort| for sorting file names.
 
18
@a
 
19
int 
 
20
fcmp(const void *arg1, const void *arg2)
 
21
{
 
22
return strcmp(*(char **)arg1, *(char **)arg2);
 
23
}
 
24
 
 
25
@
 
26
 
 
27
@d BUF_LEN 500
 
28
@d CALLOC(n, type) (type *)calloc(n, sizeof(type))
 
29
 
 
30
@a
 
31
main(int num_args, char *args[])
 
32
{
 
33
char **file_name, *root_name = NULL, *out_name = NULL;
 
34
char *pg = "|pg";
 
35
char *name_list = NULL;
 
36
int pg_len, root_len;
 
37
int sort_names = 1;
 
38
char buf[BUF_LEN];
 
39
int i, k, nfiles, nlines;
 
40
FILE *fp_in, *fp_out, *fp_names;
 
41
 
 
42
if(num_args == 1)
 
43
        {
 
44
        puts("Usage:  idxmerge [-o output_file_name] [-s] file1 [file2] ...");
 
45
        exit(0);
 
46
        }
 
47
 
 
48
nfiles = 0;
 
49
 
 
50
/* Allocate the file-name list.  There can't be more file names than the
 
51
total number of command-line arguments. */
 
52
file_name = CALLOC(num_args, char *);
 
53
 
 
54
for(i=1; i<num_args; i++)
 
55
        {
 
56
        char *pa = args[i];
 
57
 
 
58
        if(*pa == '-')
 
59
                @<Process command-line option@>@;
 
60
        else
 
61
                file_name[nfiles++] = pa;
 
62
        }
 
63
 
 
64
if(nfiles == 0)
 
65
        {
 
66
        puts("! No input files; nothing done.");
 
67
        exit(0);
 
68
        }
 
69
 
 
70
if(!name_list)
 
71
        name_list = "idx-names.tex";
 
72
 
 
73
pg_len = strlen(pg);
 
74
 
 
75
if(!out_name)
 
76
        fp_out = stdout; // Unix convention.
 
77
else
 
78
        fp_out = fopen(out_name, "w");
 
79
 
 
80
if(sort_names)
 
81
        qsort(file_name, nfiles, sizeof(char *), fcmp);
 
82
 
 
83
printf("Writing merged output to `%s', file names to `%s'...",
 
84
        out_name, name_list);
 
85
fflush(stdout);
 
86
 
 
87
fp_names = fopen(name_list, "w");
 
88
 
 
89
nlines = 0;
 
90
 
 
91
for(k=0; k<nfiles; k++)
 
92
        @<Process one file@>@;
 
93
 
 
94
printf("done.\n"
 
95
        "IDXMERGE:  %i files, %i index entries.\n", nfiles, nlines);
 
96
 
 
97
exit(0);
 
98
}
 
99
 
 
100
@
 
101
@<Process com...@>=
 
102
{
 
103
pa++;
 
104
 
 
105
switch(*pa++)
 
106
        {
 
107
   case 'n':
 
108
        /* Specify name of output file-name list. */
 
109
        name_list = CALLOC(strlen(pa)+1, char);
 
110
        strcpy(name_list, pa);
 
111
        break;
 
112
 
 
113
   case 'o':
 
114
        /* Set name of output file (no \.{.idx}). */
 
115
        root_name = CALLOC((root_len=strlen(pa))+1, char);
 
116
        strcpy(root_name, pa);
 
117
        out_name = CALLOC(root_len+5, char);
 
118
        strcpy(out_name, root_name);
 
119
        strcpy(out_name+root_len, ".idx");
 
120
 
 
121
        if(!name_list)
 
122
                {
 
123
                name_list = CALLOC(root_len + 12, char);
 
124
                strcpy(name_list, root_name);
 
125
                strcpy(name_list + root_len, "-names.tex");
 
126
                }
 
127
        break;
 
128
 
 
129
   case 'p':
 
130
        /* Give nondefault value of \.{makeindex.pg}. */
 
131
        pg = CALLOC(strlen(pa)+2, char);
 
132
        pg[0] = '|';
 
133
        strcpy(pg+1, pa);
 
134
        break;
 
135
 
 
136
   case 's':
 
137
        /* Turn off sorting of file names. */
 
138
        sort_names = !sort_names;
 
139
        break;
 
140
 
 
141
   default:
 
142
        printf("! Invalid command-line option `-%s' ignored.", pa-1);
 
143
        break;
 
144
        }
 
145
}
 
146
 
 
147
@
 
148
@<Process one...@>=
 
149
{
 
150
char *p, c;
 
151
 
 
152
fp_in = fopen(file_name[k], "r");
 
153
 
 
154
if(!fp_in)
 
155
        {
 
156
        printf("! Can't open input file #%i, `%s'.\n", k, file_name[k]);
 
157
        continue;
 
158
        }
 
159
 
 
160
while(fgets(buf, BUF_LEN, fp_in))
 
161
        {
 
162
        p = strstr(buf, pg);
 
163
        
 
164
        if(!p)
 
165
                continue;
 
166
 
 
167
        p += pg_len + 1; // Address of the right brace in \.{|pg\{\}\dots}.
 
168
        c = *p;
 
169
        *p = '\0';
 
170
        fprintf(fp_out, "%s%i%c%s", buf, k+1, c, p+1);
 
171
        nlines++;
 
172
        }
 
173
 
 
174
fclose(fp_in);
 
175
 
 
176
fprintf(fp_names, "\\idxname{%i}{%s}\n", k+1, file_name[k]);
 
177
}