chiark / gitweb /
patch from Adam Borowski to fix FTBFS on x32
[bible-kjv.git] / makeindex.c
1 /* -*-C-*-
2 *******************************************************************************
3 *
4 * File:         makeindex.c
5 * RCS:          $Header: /home/matthew/cvs/bible-kjv-4.10/makeindex.c,v 2.6 2005/01/23 11:23:15 matthew Exp $
6 * Description:  Create Line Index to Text File
7 * Author:       Chip Chapin, Hewlett Packard Company
8 * Created:      Jan 14 1989
9 * Modified:     Thu Sep 14 12:37:15 1989 (Chip Chapin) chip@hpcllcc
10 * Language:     C
11 * Package:      Bible Retrieval System
12 * Status:       Experimental (Do Not Distribute)
13 *
14 * $Log: makeindex.c,v $
15 * Revision 2.6  2005/01/23 11:23:15  matthew
16 * include standard header files
17 *
18 * Revision 2.5  2005/01/22 17:48:08  matthew
19 * prototype main correctly
20 *
21 * Revision 2.4  2005/01/21 19:39:36  matthew
22 * remove unused variables
23 *
24 * Revision 2.3  2003/07/26 09:22:12  matthew
25 * Correct arguments to printf
26 *
27 * Revision 2.2  2003/02/01 02:40:43  matthew
28 * Make main() return an int, and prototype accordingly.
29 *
30 * Revision 2.1  2003/01/08 15:52:50  matthew
31 * line_locator[] is int not long (from debian patch)
32 *
33 * Revision 2.0  2003/01/08 15:29:52  matthew
34 * versions collected from the net
35 *
36  * Revision 1.2  89/09/14  20:34:03  20:34:03  chip (Chip Chapin)
37  * Release 1-2.  Supports -f and -l options for formatting the output.
38  * Updates primarily brl.c, bible.c, and bible.1.
39  * 
40  * Revision 1.1  89/09/05  17:49:34  17:49:34  chip (Chip Chapin)
41  * Initial revision
42  * 
43 *
44 *******************************************************************************
45 */
46
47 /*----------------------------------------------------------------------
48 |   NAME:
49 |       makeindex.c
50 |
51 |   PURPOSE:
52 |       Create an index to each line of a text file.
53 |       
54 |       The table thus produced may be indexed by line number to
55 |       produce the absolute byte number of the file at which that
56 |       line begins.
57 |       
58 |       An extra entry is guaranteed at the end, so that
59 |       table[n+1], where n is the total number of lines in the
60 |       file, yields the byte number just *after* the last byte of
61 |       the last line (i.e. the total number of bytes in the
62 |       file).
63 |       
64 |       By convention, I assume that "line 0" of the text file is
65 |       an informational string describing the data.  So the first
66 |       line of data is actually "line 1".  But there's really
67 |       nothing in the construction of the index that mandates
68 |       that convention.
69 |       
70 |       For the Bible Retrieval System, each verse starts on a new
71 |       line.  So the index produced by this program is an index
72 |       to each Bible verse.
73 |
74 |   FUNCTIONS:
75 |       N/A
76 |
77 |   HISTORY:
78 |       890114 cc Creation.
79 |       890904 cc Tidy it up, and read filenames from command line.
80 |
81 \*----------------------------------------------------------------------*/
82
83
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <time.h>
88
89 /* Size of text buffer.  Better be enough for one line */
90 #define  TBSIZE         2048
91
92
93
94 int main(int argc,char **argv)
95 {
96     FILE        *fp;
97     time_t      t;
98     long        cur_line;
99     long        offset;
100     char        textbuff[TBSIZE];
101
102     if (--argc < 1) {
103         fprintf( stderr, "%s: Missing input file name\n", *argv );
104         fprintf( stderr, "Usage: %s textfile\n", *argv );
105         exit( 1 );
106     }
107     
108     fp = fopen( argv[1], "r" );
109     if (fp == NULL) {
110         fprintf( stderr, "%s: Cannot open file %s\n", *argv, argv[1] );
111         return 1;
112     }
113
114     time(&t);
115     printf( "/* Text index to file: %s\n", argv[1] );
116     printf( "   Created %s\n", ctime(&t) );
117     printf( "*/\n" );
118     printf( "int line_locator[] = {" );
119     
120     offset = cur_line = 0L;
121     while (!feof(fp)) {
122         if (cur_line % 8 == 0) printf( "\n\t" );        /* pretty */
123         /* print offset of current line */
124         printf( "%ld, ", offset );
125
126         /* Determine offset of next line */
127         fgets( textbuff, TBSIZE, fp);
128         offset += strlen(textbuff);
129         cur_line++;
130     }
131     /* print offset of last line */
132     printf( "%ld\n\t};\n", offset );    /* end of array initialization */
133     printf( "\t/* textfile: %s, %ld lines. */\n", argv[1], cur_line );
134     printf( "int max_line = %ld;\n", cur_line-1 );
135
136     fclose(fp);
137     return(0);
138 } /* main */