common.cxx 4.89 KB
Newer Older
Zolnai Tamás's avatar
Zolnai Tamás committed
1 2 3 4 5 6 7 8 9
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

10
#include <common.hxx>
Zolnai Tamás's avatar
Zolnai Tamás committed
11 12

//flags for handleArguments()
13 14 15
enum class State {
    NONE, Input, Output, MergeSrc, Languages
};
Zolnai Tamás's avatar
Zolnai Tamás committed
16 17 18 19 20 21 22

namespace common {

bool handleArguments(
    int argc, char * argv[], HandledArgs& o_aHandledArgs)
{
    o_aHandledArgs = HandledArgs();
23
    State nState = State::NONE;
Zolnai Tamás's avatar
Zolnai Tamás committed
24 25 26 27 28

    for( int i = 1; i < argc; i++ )
    {
        if ( OString( argv[ i ] ).toAsciiUpperCase() == "-I" )
        {
29
            nState = State::Input; // next token specifies source file
Zolnai Tamás's avatar
Zolnai Tamás committed
30 31 32
        }
        else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-O" )
        {
33
            nState = State::Output; // next token specifies the dest file
Zolnai Tamás's avatar
Zolnai Tamás committed
34 35 36
        }
        else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-M" )
        {
37
            nState = State::MergeSrc; // next token specifies the merge database
38
            o_aHandledArgs.m_bMergeMode = true;
Zolnai Tamás's avatar
Zolnai Tamás committed
39 40 41
        }
        else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-L" )
        {
42
            nState = State::Languages;
Zolnai Tamás's avatar
Zolnai Tamás committed
43
        }
44 45 46 47
        else if ( OString( argv[ i ] ).toAsciiUpperCase() == "-B" )
        {
            o_aHandledArgs.m_bUTF8BOM = true;
        }
Zolnai Tamás's avatar
Zolnai Tamás committed
48 49 50 51
        else
        {
            switch ( nState )
            {
52
                case State::NONE:
Zolnai Tamás's avatar
Zolnai Tamás committed
53 54 55
                {
                    return false;    // no valid command line
                }
56
                case State::Input:
Zolnai Tamás's avatar
Zolnai Tamás committed
57 58 59 60
                {
                    o_aHandledArgs.m_sInputFile = OString( argv[i] );
                }
                break;
61
                case State::Output:
Zolnai Tamás's avatar
Zolnai Tamás committed
62 63 64 65
                {
                    o_aHandledArgs.m_sOutputFile = OString( argv[i] );
                }
                break;
66
                case State::MergeSrc:
Zolnai Tamás's avatar
Zolnai Tamás committed
67 68 69 70
                {
                    o_aHandledArgs.m_sMergeSrc = OString( argv[i] );
                }
                break;
71
                case State::Languages:
Zolnai Tamás's avatar
Zolnai Tamás committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                {
                    o_aHandledArgs.m_sLanguage = OString( argv[i] );
                }
                break;
            }
        }
    }
    if( !o_aHandledArgs.m_sInputFile.isEmpty() &&
        !o_aHandledArgs.m_sOutputFile.isEmpty() )
    {
        return true;
    }
    else
    {
        o_aHandledArgs = HandledArgs();
        return false;
    }
}

void writeUsage(const OString& rName, const OString& rFileType)
{
    std::cout
94
        << " Syntax: " << rName
95
        << " -i FileIn -o FileOut [-m DataBase] [-l Lang] [-b]\n"
96
           " FileIn:   Source files (" << rFileType << ")\n"
97 98 99 100 101
           " FileOut:  Destination file (*.*)\n"
           " DataBase: Mergedata (*.po)\n"
           " Lang: Restrict the handled language; one element of\n"
           " (de, en-US, ...) or all\n"
           " -b:   Add UTF-8 Byte Order Mark to FileOut(use with -m option)\n";
Zolnai Tamás's avatar
Zolnai Tamás committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

void writePoEntry(
    const OString& rExecutable, PoOfstream& rPoStream, const OString& rSourceFile,
    const OString& rResType, const OString& rGroupId, const OString& rLocalId,
    const OString& rHelpText, const OString& rText, const PoEntry::TYPE eType )
{
    try
    {
        PoEntry aPO(rSourceFile, rResType, rGroupId, rLocalId, rHelpText, rText, eType);
        rPoStream.writeEntry( aPO );
    }
    catch( PoEntry::Exception& aException )
    {
        if(aException == PoEntry::NOSOURCFILE)
        {
            std::cerr << rExecutable << " warning: no sourcefile specified for po entry\n";
        }
        else
        {
122
            std::cerr << rExecutable << " warning: invalid po attributes extracted from " <<  rSourceFile << "\n";
Zolnai Tamás's avatar
Zolnai Tamás committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            if(aException == PoEntry::NOGROUPID)
            {
                std::cerr << "No groupID specified!\n";
                std::cerr << "String: " << rText << "\n";
            }
            else if (aException == PoEntry::NOSTRING)
            {
                std::cerr << "No string specified!\n";
                std::cerr << "GroupID: " << rGroupId << "\n";
                if( !rLocalId.isEmpty() ) std::cerr << "LocalID: " << rLocalId << "\n";
            }
            else
            {
                if (aException == PoEntry::NORESTYPE)
                {
                    std::cerr << "No resource type specified!\n";
                }
                else if (aException == PoEntry::WRONGHELPTEXT)
                {
                    std::cerr << "x-comment length is 5 characters:" << rHelpText << "\n";
                }

                std::cerr << "GroupID: " << rGroupId << "\n";
                if( !rLocalId.isEmpty() ) std::cerr << "LocalID: " << rLocalId << "\n";
                std::cerr << "String: " << rText << "\n";
            }
        }
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */