1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
18 static const int MAXLINE
= 1024*64;
22 int main(int argc
, char *argv
[])
24 if (argc
!= 3 || strcmp(argv
[1],"-o"))
26 cout
<< "Usage: idxdict -o outputfile < input\n";
29 // This call improves performance by approx 5x
30 std::ios_base::sync_with_stdio(false);
32 const char * outputFile(argv
[2]);
33 char inputBuffer
[MAXLINE
];
34 multimap
<string
, size_t> entries
;
35 multimap
<string
,size_t>::iterator
ret(entries
.begin());
38 cin
.getline(inputBuffer
, MAXLINE
);
39 const string
encoding(inputBuffer
);
40 size_t currentOffset(encoding
.size()+1);
43 // Extract the next word, but not the entry count
44 cin
.getline(inputBuffer
, MAXLINE
, '|');
48 string
word(inputBuffer
);
49 ret
= entries
.insert(ret
, pair
<string
, size_t>(word
, currentOffset
));
50 currentOffset
+= word
.size() + 1;
51 // Next is the entry count
52 cin
.getline(inputBuffer
, MAXLINE
);
55 cerr
<< "Unable to read entry - insufficient buffer?.\n";
58 currentOffset
+= strlen(inputBuffer
)+1;
61 int entryCount(strtol(inputBuffer
, &endptr
, 10));
62 if (errno
!= 0 || endptr
== inputBuffer
|| *endptr
!= '\0')
65 << "Unable to read count from \"" << inputBuffer
69 for (int i(0); i
< entryCount
; ++i
)
71 cin
.getline(inputBuffer
, MAXLINE
);
72 currentOffset
+= strlen(inputBuffer
)+1;
77 // Use binary mode to prevent any translation of LF to CRLF on Windows
78 ofstream
outputStream(outputFile
, ios_base::binary
| ios_base::trunc
|ios_base::out
);
79 if (!outputStream
.is_open())
81 cerr
<< "Unable to open output file " << outputFile
<< endl
;
85 outputStream
<< encoding
<< '\n' << entries
.size() << '\n';
87 for (multimap
<string
, size_t>::const_iterator
ii(entries
.begin());
92 outputStream
<< ii
->first
<< '|' << ii
->second
<< '\n';
96 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */