Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include <cerrno>
11 : #include <iostream>
12 : #include <fstream>
13 : #include <string>
14 : #include <map>
15 : #include <stdlib.h>
16 : #include <string.h>
17 :
18 : static const int MAXLINE = 1024*64;
19 :
20 : using namespace std;
21 :
22 0 : int main(int argc, char *argv[])
23 : {
24 0 : if (argc != 3 || strcmp(argv[1],"-o"))
25 : {
26 0 : cout << "Usage: idxdict -o outputfile < input\n";
27 0 : ::exit(99);
28 : }
29 : // This call improves performance by approx 5x
30 0 : std::ios_base::sync_with_stdio(false);
31 :
32 0 : const char * outputFile(argv[2]);
33 : char inputBuffer[MAXLINE];
34 0 : multimap<string, size_t> entries;
35 0 : multimap<string,size_t>::iterator ret(entries.begin());
36 :
37 0 : int line(1);
38 0 : cin.getline(inputBuffer, MAXLINE);
39 0 : const string encoding(inputBuffer);
40 0 : size_t currentOffset(encoding.size()+1);
41 : while (true)
42 : {
43 : // Extract the next word, but not the entry count
44 0 : cin.getline(inputBuffer, MAXLINE, '|');
45 :
46 0 : if (cin.eof()) break;
47 :
48 0 : string word(inputBuffer);
49 0 : ret = entries.insert(ret, pair<string, size_t>(word, currentOffset));
50 0 : currentOffset += word.size() + 1;
51 : // Next is the entry count
52 0 : cin.getline(inputBuffer, MAXLINE);
53 0 : if (!cin.good())
54 : {
55 0 : cerr << "Unable to read entry - insufficient buffer?.\n";
56 0 : exit(99);
57 : }
58 0 : currentOffset += strlen(inputBuffer)+1;
59 : char * endptr;
60 0 : errno = 0;
61 0 : int entryCount(strtol(inputBuffer, &endptr, 10));
62 0 : if (errno != 0 || endptr == inputBuffer || *endptr != '\0')
63 : {
64 : cerr
65 0 : << "Unable to read count from \"" << inputBuffer
66 0 : << "\" input.\n";
67 0 : exit(99);
68 : }
69 0 : for (int i(0); i < entryCount; ++i)
70 : {
71 0 : cin.getline(inputBuffer, MAXLINE);
72 0 : currentOffset += strlen(inputBuffer)+1;
73 0 : ++line;
74 : }
75 0 : }
76 :
77 : // Use binary mode to prevent any translation of LF to CRLF on Windows
78 0 : ofstream outputStream(outputFile, ios_base::binary| ios_base::trunc|ios_base::out);
79 0 : if (!outputStream.is_open())
80 : {
81 0 : cerr << "Unable to open output file " << outputFile << endl;
82 0 : ::exit(99);
83 : }
84 :
85 0 : outputStream << encoding << '\n' << entries.size() << '\n';
86 :
87 0 : for (multimap<string, size_t>::const_iterator ii(entries.begin());
88 0 : ii != entries.end();
89 : ++ii
90 : )
91 : {
92 0 : outputStream << ii->first << '|' << ii->second << '\n';
93 0 : }
94 0 : }
95 :
96 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|