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 const int MAXLINE
= 1024*64;
20 int main(int argc
, char *argv
[])
22 if (argc
!= 3 || strcmp(argv
[1],"-o"))
24 std::cout
<< "Usage: idxdict -o outputfile < input\n";
27 // This call improves performance by approx 5x
28 std::ios_base::sync_with_stdio(false);
30 const char * outputFile(argv
[2]);
31 char inputBuffer
[MAXLINE
];
32 std::multimap
<std::string
, size_t> entries
;
33 std::multimap
<std::string
,size_t>::iterator
ret(entries
.begin());
35 std::cin
.getline(inputBuffer
, MAXLINE
);
36 const std::string
encoding(inputBuffer
);
37 size_t currentOffset(encoding
.size()+1);
40 // Extract the next word, but not the entry count
41 std::cin
.getline(inputBuffer
, MAXLINE
, '|');
43 if (std::cin
.eof()) break;
45 std::string
word(inputBuffer
);
46 ret
= entries
.insert(ret
, std::pair
<std::string
, size_t>(word
, currentOffset
));
47 currentOffset
+= word
.size() + 1;
48 // Next is the entry count
49 std::cin
.getline(inputBuffer
, MAXLINE
);
52 std::cerr
<< "Unable to read entry - insufficient buffer?.\n";
55 currentOffset
+= strlen(inputBuffer
)+1;
58 int entryCount(strtol(inputBuffer
, &endptr
, 10));
59 if (errno
!= 0 || endptr
== inputBuffer
|| *endptr
!= '\0')
62 << "Unable to read count from \"" << inputBuffer
66 for (int i(0); i
< entryCount
; ++i
)
68 std::cin
.getline(inputBuffer
, MAXLINE
);
69 currentOffset
+= strlen(inputBuffer
)+1;
73 // Use binary mode to prevent any translation of LF to CRLF on Windows
74 std::ofstream
outputStream(outputFile
, std::ios_base::binary
| std::ios_base::trunc
|std::ios_base::out
);
75 if (!outputStream
.is_open())
77 std::cerr
<< "Unable to open output file " << outputFile
<< std::endl
;
81 outputStream
<< encoding
<< '\n' << entries
.size() << '\n';
83 for (auto const& entry
: entries
)
85 outputStream
<< entry
.first
<< '|' << entry
.second
<< '\n';
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */