summaryrefslogtreecommitdiffstats
path: root/misc/strongsidx.c
blob: 61bcda3554d2ca66974dc94b5d27b927505f0daa (plain) (blame)
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
/*****************************************************************************
 * Bible dictionary index utility
 */


#ifndef __GNUC__
#include <io.h>
#else
#include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


char findbreak(int fd, long *offset, short *size)
{
	char buf[3];
	char rc = 1;
	long offset2;

	memset(buf, 0, sizeof(buf));

	while (read(fd, &buf[sizeof(buf)-1], 1) == 1) {
		if ((buf[0] == 10) && (isdigit(buf[1])) && (isdigit(buf[2]))) {
			if (read(fd, buf, 1) == 1) {
				*offset = lseek(fd, 0, SEEK_CUR) - 3;
				rc = 0;
				if (size) {
					if (!findbreak(fd, &offset2, 0))
						*size = offset2 - *offset;
					else *size = lseek(fd, 0, SEEK_END) - *offset;
					lseek(fd, *offset, SEEK_SET);
				}
				break;
			}
			break;
		}
		memmove(buf, &buf[1], sizeof(buf)-1);
	}
	return rc;
}


void main(int argc, char **argv)
{
	int   fd, ifd;
	long  offset;
	short size;
	char  *buf;
	char  entbuf[6];

	if (argc < 2) {
		fprintf(stderr, "usage: %s <file to process (no .dat)>\n", argv[0]);
		exit(1);
	}

	buf = (char *) calloc(strlen(argv[1]) + 5, 1);

	sprintf(buf, "%s.dat", argv[1]);
	fd = open(buf, O_RDONLY);

	sprintf(buf, "%s.idx", argv[1]);
	ifd = open(buf, O_CREAT|O_WRONLY);

	offset = 0;				/* write offset for intro */
	write(ifd, &offset, 4);
	findbreak(fd, &offset, 0);
	lseek(fd, 0L, SEEK_SET);
	size = offset - 12;
	write(ifd, &size, 2);

	entbuf[5] = 0;				/* delimit string for read below */

	while(!findbreak(fd, &offset, &size)) {
		write(ifd, &offset, 4);
		write(ifd, &size, 2);
		read(fd, entbuf, 5);
		printf("Found: %s...(%ld:%d)\n", entbuf, offset, size);
	}

	free(buf);

	close(ifd);
	close(fd);
}