-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqindex.py
More file actions
82 lines (57 loc) · 2.26 KB
/
qindex.py
File metadata and controls
82 lines (57 loc) · 2.26 KB
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
#!/usr/bin/env python
import argparse
import codecs
import random
import struct
import sys
#//**********************************************************************
#//
#// globals/constants
#//
#//**********************************************************************
PROGRAM_NAME = "qindex"
VERSION = "3.0.2"
COPYRIGHT_MESSAGE = "copyright (c) 2025 (1990), Rick Gutleber (rickg@his.com)"
#//**********************************************************************
#//
#// main
#//
#//**********************************************************************
def main( ):
#print( PROGRAM_NAME + ' ' + VERSION, end='\n', file=sys.stderr )
#print( " Quote file must consist of ASCII quotes, each separated by a line containing", end='\n', file=sys.stderr )
#print( " only \"%\".", end='\n', file=sys.stderr )
parser = argparse.ArgumentParser( prog=PROGRAM_NAME, description=PROGRAM_NAME + ' - ' + VERSION + ' - ' + COPYRIGHT_MESSAGE )
#parser.add_argument( '-f', '--fortune', action='store_true', help="output fortune(6) format" )
parser.add_argument( 'quoteFile', default='quote.txt' )
parser.add_argument( 'indexFile', default='quote.idx' )
args = parser.parse_args( )
quoteFile = args.quoteFile
indexFile = args.indexFile
#fortuneFormat = args.fortune
outputFile = open( indexFile, 'wb' )
offset = 0
quoteCount = 1
outputFile.write( struct.pack( 'i', quoteCount ) ) # the final index count will go here when we are finished
outputFile.write( struct.pack( 'i', offset ) ) # the first quote
for line in codecs.open( quoteFile, 'r', 'ascii', 'replace' ):
offset += len( line )
if line == '%\r\n':
outputFile.write( struct.pack( 'i', offset ) )
quoteCount += 1
if quoteCount % 400 == 0:
print( quoteCount, end='\r', file=sys.stderr )
outputFile.seek( 0 )
outputFile.write( struct.pack( 'i', quoteCount ) )
outputFile.close( )
print( quoteCount - 1, end='\r', file=sys.stderr )
#//**********************************************************************
#//
#// __main__
#//
#//**********************************************************************
if __name__ == '__main__':
#try:
main( )
#except:
# pass