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/.
17 static const int MAXLINE
= 1024*64;
21 int main(int argc
, char *argv
[])
23 if (argc
!= 3 || strcmp(argv
[1],"-o"))
25 cout
<< "Usage: idxdict -o outputfile < input\n";
28 // This call improves performance by approx 5x
29 cin
.sync_with_stdio(false);
31 const char * outputFile(argv
[2]);
32 char inputBuffer
[MAXLINE
];
33 multimap
<string
, size_t> entries
;
34 multimap
<string
,size_t>::iterator
ret(entries
.begin());
37 cin
.getline(inputBuffer
, MAXLINE
);
38 const string
encoding(inputBuffer
);
39 size_t currentOffset(encoding
.size()+1);
42 // Extract the next word, but not the entry count
43 cin
.getline(inputBuffer
, MAXLINE
, '|');
47 string
word(inputBuffer
);
48 ret
= entries
.insert(ret
, pair
<string
, size_t>(word
, currentOffset
));
49 currentOffset
+= word
.size() + 1;
50 // Next is the entry count
51 cin
.getline(inputBuffer
, MAXLINE
);
54 cerr
<< "Unable to read entry - insufficient buffer?.\n";
57 currentOffset
+= strlen(inputBuffer
)+1;
58 int entryCount(strtol(inputBuffer
, NULL
, 10));
59 for (int i(0); i
< entryCount
; ++i
)
61 cin
.getline(inputBuffer
, MAXLINE
);
62 currentOffset
+= strlen(inputBuffer
)+1;
67 // Use binary mode to prevent any translation of LF to CRLF on Windows
68 ofstream
outputStream(outputFile
, ios_base::binary
| ios_base::trunc
|ios_base::out
);
69 if (!outputStream
.is_open())
71 cerr
<< "Unable to open output file " << outputFile
<< endl
;
75 outputStream
<< encoding
<< '\n' << entries
.size() << '\n';
77 for (multimap
<string
, size_t>::const_iterator
ii(entries
.begin());
82 outputStream
<< ii
->first
<< '|' << ii
->second
<< '\n';
86 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */