LibreOffice Module l10ntools (master) 1
common.cxx
Go to the documentation of this file.
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
10#include <iostream>
11
12#include <common.hxx>
13
14namespace {
15
16//flags for handleArguments()
17enum class State {
18 NONE, Input, Output, MergeSrc, Languages
19};
20
21}
22
23namespace common {
24
26 int argc, char * argv[], HandledArgs& o_aHandledArgs)
27{
28 o_aHandledArgs = HandledArgs();
29 State nState = State::NONE;
30
31 for( int i = 1; i < argc; i++ )
32 {
33 if ( OString( argv[ i ] ).toAsciiUpperCase() == "-I" )
34 {
35 nState = State::Input; // next token specifies source file
36 }
37 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-O" )
38 {
39 nState = State::Output; // next token specifies the dest file
40 }
41 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-M" )
42 {
43 nState = State::MergeSrc; // next token specifies the merge database
44 o_aHandledArgs.m_bMergeMode = true;
45 }
46 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-L" )
47 {
48 nState = State::Languages;
49 }
50 else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-B" )
51 {
52 o_aHandledArgs.m_bUTF8BOM = true;
53 }
54 else
55 {
56 switch ( nState )
57 {
58 case State::NONE:
59 {
60 return false; // no valid command line
61 }
62 case State::Input:
63 {
64 o_aHandledArgs.m_sInputFile = OString( argv[i] );
65 }
66 break;
67 case State::Output:
68 {
69 o_aHandledArgs.m_sOutputFile = OString( argv[i] );
70 }
71 break;
72 case State::MergeSrc:
73 {
74 o_aHandledArgs.m_sMergeSrc = OString( argv[i] );
75 }
76 break;
77 case State::Languages:
78 {
79 o_aHandledArgs.m_sLanguage = OString( argv[i] );
80 }
81 break;
82 }
83 }
84 }
85 if( !o_aHandledArgs.m_sInputFile.isEmpty() &&
86 !o_aHandledArgs.m_sOutputFile.isEmpty() )
87 {
88 return true;
89 }
90 else
91 {
92 o_aHandledArgs = HandledArgs();
93 return false;
94 }
95}
96
97void writeUsage(const OString& rName, const OString& rFileType)
98{
99 std::cout
100 << " Syntax: " << rName
101 << " -i FileIn -o FileOut [-m DataBase] [-l Lang] [-b]\n"
102 " FileIn: Source files (" << rFileType << ")\n"
103 " FileOut: Destination file (*.*)\n"
104 " DataBase: Mergedata (*.po)\n"
105 " Lang: Restrict the handled language; one element of\n"
106 " (de, en-US, ...) or all\n"
107 " -b: Add UTF-8 Byte Order Mark to FileOut(use with -m option)\n";
108}
109
111 const OString& rExecutable, PoOfstream& rPoStream, const OString& rSourceFile,
112 std::string_view rResType, const OString& rGroupId, const OString& rLocalId,
113 const OString& rHelpText, const OString& rText, const PoEntry::TYPE eType )
114{
115 try
116 {
117 PoEntry aPO(rSourceFile, rResType, rGroupId, rLocalId, rHelpText, rText, eType);
118 rPoStream.writeEntry( aPO );
119 }
120 catch( PoEntry::Exception& aException )
121 {
122 if(aException == PoEntry::NOSOURCFILE)
123 {
124 std::cerr << rExecutable << " warning: no sourcefile specified for po entry\n";
125 }
126 else
127 {
128 std::cerr << rExecutable << " warning: invalid po attributes extracted from " << rSourceFile << "\n";
129 if(aException == PoEntry::NOGROUPID)
130 {
131 std::cerr << "No groupID specified!\n";
132 std::cerr << "String: " << rText << "\n";
133 }
134 else if (aException == PoEntry::NOSTRING)
135 {
136 std::cerr << "No string specified!\n";
137 std::cerr << "GroupID: " << rGroupId << "\n";
138 if( !rLocalId.isEmpty() ) std::cerr << "LocalID: " << rLocalId << "\n";
139 }
140 else
141 {
142 if (aException == PoEntry::NORESTYPE)
143 {
144 std::cerr << "No resource type specified!\n";
145 }
146 else if (aException == PoEntry::WRONGHELPTEXT)
147 {
148 std::cerr << "x-comment length is 5 characters:" << rHelpText << "\n";
149 }
150
151 std::cerr << "GroupID: " << rGroupId << "\n";
152 if( !rLocalId.isEmpty() ) std::cerr << "LocalID: " << rLocalId << "\n";
153 std::cerr << "String: " << rText << "\n";
154 }
155 }
156 }
157}
158
159}
160
161/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Interface to use po entries in localization.
Definition: po.hxx:34
TYPE
Definition: po.hxx:45
Exception
Definition: po.hxx:46
@ NOSTRING
Definition: po.hxx:46
@ NOGROUPID
Definition: po.hxx:46
@ NOSOURCFILE
Definition: po.hxx:46
@ NORESTYPE
Definition: po.hxx:46
@ WRONGHELPTEXT
Definition: po.hxx:46
Interface to write po entry to files as output streams.
Definition: po.hxx:101
void writeEntry(const PoEntry &rPo)
Definition: po.cxx:528
sal_Int32 nState
DocumentType eType
def Input(s)
NONE
void writeUsage(const OString &rName, const OString &rFileType)
Write out a help about usage.
Definition: common.cxx:97
void writePoEntry(const OString &rExecutable, PoOfstream &rPoStream, const OString &rSourceFile, std::string_view rResType, const OString &rGroupId, const OString &rLocalId, const OString &rHelpText, const OString &rText, const PoEntry::TYPE eType)
Write out a PoEntry with attention to exceptions.
Definition: common.cxx:110
bool handleArguments(int argc, char *argv[], HandledArgs &o_aHandledArgs)
Handle command line parameters.
Definition: common.cxx:25
int i
State
Result type of handleArguments()
Definition: common.hxx:26
OString m_sMergeSrc
Definition: common.hxx:29
OString m_sOutputFile
Definition: common.hxx:28
OString m_sLanguage
Definition: common.hxx:30
OString m_sInputFile
Definition: common.hxx:27