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  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <iostream>
23 #include <fstream>
24 #include <string>
25 #include <cstring>
26 
27 #include <sal/main.h>
28 
29 #include <helpmerge.hxx>
30 #include <common.hxx>
31 #include <memory>
32 
33 #ifndef TESTDRIVER
34 
WriteUsage()35 static void WriteUsage()
36 {
37     std::cout
38         << (" Syntax: Helpex -[m]i FileIn -o FileOut [-m DataBase] [-l Lang]\n"
39             " FileIn + i:   Source file (*.xhp)\n"
40             " FileIn + -mi: File including paths of source files"
41             " (only for merge)\n"
42             " FileOut:  Destination file (*.*) or files (in case of -mi)\n"
43             " DataBase: Mergedata (*.po)\n"
44             " Lang: Restrict the handled languages; one element of\n"
45             " (de, en-US, ...) or all\n");
46 }
47 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)48 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
49 {
50     bool hasNoError = true;
51     try
52     {
53         bool bMultiMerge = false;
54         for (int nIndex = 1; nIndex != argc; ++nIndex)
55         {
56             if (std::strcmp(argv[nIndex], "-mi") == 0)
57             {
58                 argv[nIndex][1] = 'i';
59                 argv[nIndex][2] = '\0';
60                 bMultiMerge = true;
61                 break;
62             }
63         }
64 
65         common::HandledArgs aArgs;
66         if ( !common::handleArguments( argc, argv, aArgs) )
67         {
68             WriteUsage();
69             return 1;
70         }
71 
72         if ( aArgs.m_bMergeMode )
73         {
74             if( bMultiMerge )
75             {
76                 std::ifstream aInput( aArgs.m_sInputFile.getStr() );
77                 if( !aInput.is_open() )
78                 {
79                     std::cerr << "Helpex error: cannot open input file\n";
80                     return 1;
81                 }
82                 std::unique_ptr<MergeDataFile> pMergeDataFile;
83                 if( aArgs.m_sLanguage != "qtz")
84                 {
85                     pMergeDataFile.reset(new MergeDataFile(aArgs.m_sMergeSrc, OString(), false, false ));
86                 }
87                 std::string sTemp;
88                 aInput >> sTemp;
89                 while( !aInput.eof() )
90                 {
91                     // coverity[tainted_data] - this is a build time tool
92                     const OString sXhpFile( sTemp.data(), static_cast<sal_Int32>(sTemp.length()) );
93                     HelpParser aParser( sXhpFile );
94                     const OString sOutput(
95                         aArgs.m_sOutputFile +
96                         sXhpFile.copy( sXhpFile.lastIndexOf('/') ));
97                     if( !aParser.Merge( sOutput,
98                         aArgs.m_sLanguage, pMergeDataFile.get() ))
99                     {
100                         hasNoError = false;
101                     }
102                     aInput >> sTemp;
103                 }
104                 aInput.close();
105             }
106             else
107             {
108                 HelpParser aParser( aArgs.m_sInputFile );
109                 std::unique_ptr<MergeDataFile> pMergeDataFile;
110                 if( aArgs.m_sLanguage != "qtz")
111                 {
112                     pMergeDataFile.reset(new MergeDataFile(aArgs.m_sMergeSrc, aArgs.m_sInputFile, false, false ));
113                 }
114                 hasNoError =
115                     aParser.Merge(
116                         aArgs.m_sOutputFile,
117                         aArgs.m_sLanguage, pMergeDataFile.get() );
118             }
119         }
120         else
121         {
122             HelpParser aParser( aArgs.m_sInputFile );
123             std::unique_ptr<XMLFile> xmlfile(new XMLFile( OString('0') ));
124             hasNoError =
125                 HelpParser::CreatePO(
126                     aArgs.m_sOutputFile, aArgs.m_sInputFile,
127                     xmlfile.get(), "help" );
128         }
129     }
130     catch (std::exception& e)
131     {
132         std::cerr << "Helpex exception: " << e.what() << std::endl;
133         hasNoError = true;
134     }
135 
136     if( hasNoError )
137         return 0;
138     else
139         return 1;
140 }
141 #endif
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
144