Use o3tl::convert in Math
[LibreOffice.git] / l10ntools / source / idxdict / idxdict.cxx
blob6d2a22b3d04812dafc14e3bde286f49777aa54f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <cerrno>
11 #include <iostream>
12 #include <fstream>
13 #include <string>
14 #include <map>
15 #include <stdlib.h>
16 #include <string.h>
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";
25 ::exit(99);
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);
38 while (true)
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);
50 if (!std::cin.good())
52 std::cerr << "Unable to read entry - insufficient buffer?.\n";
53 exit(99);
55 currentOffset += strlen(inputBuffer)+1;
56 char * endptr;
57 errno = 0;
58 int entryCount(strtol(inputBuffer, &endptr, 10));
59 if (errno != 0 || endptr == inputBuffer || *endptr != '\0')
61 std::cerr
62 << "Unable to read count from \"" << inputBuffer
63 << "\" input.\n";
64 exit(99);
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;
78 ::exit(99);
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: */