summaryrefslogtreecommitdiffstats
path: root/scilab/modules/slint/sci_gateway/cpp/sci_slint.cpp
blob: f18299b5c20e1a1057e3d75a1a3053942108f52b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2006 - INRIA - Antoine ELIAS
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 */

#include <iostream>
#include <string>

#include "SLint.hxx"
#include "output/SLintScilabResult.hxx"
#include "output/SLintXmlResult.hxx"
#include "output/cnes/CNESXmlResult.hxx"
#include "output/cnes/CNESCsvResult.hxx"
#include "config/cnes/ToolConfiguration.hxx"
#include "config/XMLConfig.hxx"

#include "struct.hxx"
#include "string.hxx"
#include "slint_gw.hxx"

extern "C"
{
#include "Scierror.h"
#include "sciprint.h"
#include "localization.h"
}

static bool contributionMsg = true;
/**
 * slint(String[a,b] files, String[1,c] conf, String[1,1] out)
 * slint(String[a,b] files, String[1,1] out)                   : default conf is etc/slint.xml
 * slint(String[a,b] files)                                    : default conf is etc/slint.xml & out is outputstream
 * slint(String[a,b] files, bool[1,1] printResult)             : default conf is etc/slint.xml
 * slint(String[a,b] files, String[1,c] conf, bool[1,1] printResult)
 */

/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_slint(types::typed_list & in, int _iRetCount, types::typed_list & out)
{
    slint::SLintResult * results = nullptr;
    bool printResults = false;
    const int size = (int)in.size();
    types::String * conf = nullptr;
    types::String * outFile = nullptr;

    if (size == 0 || size >= 4)
    {
        Scierror(999, _("%s: Wrong number of input arguments: at least %d expected.\n"), "slint", 1);
        return types::Function::Error;
    }

    if (!in[0]->isString())
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "slint", 1);
        return types::Function::Error;
    }

    switch (size)
    {
        case 1:
        {
            printResults = true;
            break;
        }
        case 2:
        {
            if (in[1]->isBool())
            {
                if (in[1]->getAs<types::Bool>()->getSize() == 1)
                {
                    printResults = in[1]->getAs<types::Bool>()->get(0) == 0 ? false : true;
                }
                else
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A single boolean expected.\n"), "slint", 2);
                    return types::Function::Error;
                }
            }
            else if (in[1]->isString())
            {
                outFile = in[1]->getAs<types::String>();
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A string or a boolean expected.\n"), "slint", 2);
                return types::Function::Error;
            }
            break;
        }
        case 3:
        {
            if (in[2]->isBool())
            {
                if (in[2]->getAs<types::Bool>()->getSize() == 1)
                {
                    printResults = in[2]->getAs<types::Bool>()->get(0) == 0 ? false : true;
                }
                else
                {
                    Scierror(999, _("%s: Wrong type for input argument #%d: A single boolean expected.\n"), "slint", 3);
                    return types::Function::Error;
                }
            }
            else if (in[2]->isString())
            {
                outFile = in[2]->getAs<types::String>();
            }
            else
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A string or a boolean expected.\n"), "slint", 3);
                return types::Function::Error;
            }

            if (!in[1]->isString())
            {
                Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "slint", 3);
                return types::Function::Error;
            }
            conf = in[1]->getAs<types::String>();
            break;
        }
    }

    try
    {
        slint::SLintOptions options;
        if (conf)
        {
            if (conf->getSize() == 1)
            {
                slint::XMLConfig::getOptions(conf->get(0), options);
            }
            else
            {
                slint::XMLConfig::getOptions(*conf, options);
            }
        }
        else
        {
            slint::XMLConfig::getOptions(L"SCI/modules/slint/etc/slint.xml", options);
        }

        if (outFile)
        {
            if (conf && conf->getSize() >= 2 && (std::wstring(conf->get(0)) == L"cnes"))
            {
                const slint::CNES::ToolConfiguration tc = slint::CNES::ToolConfiguration::createFromXml(conf->get(1));
                const std::wstring out(outFile->get(0));
                const std::size_t pos = out.find_last_of(L'.');
                if (pos != std::string::npos && out.substr(pos) == L".csv")
                {
                    results = new slint::CNES::CNESCsvResult(tc, conf, options.getId(), outFile->get(0));
                }
                else
                {
                    results = new slint::CNES::CNESXmlResult(tc, conf, options.getId(), outFile->get(0));
                }
            }
            else
            {
                results = new slint::SLintXmlResult(outFile->get(0));
            }
        }
        else
        {
            if (printResults)
            {
                results = new slint::SLintScilabResult();
            }
            else
            {
                results = new slint::SLintScilabOut();
            }
        }

        slint::SLint slint(options, *results);
        slint.setFiles(in[0]->getAs<types::String>());
        slint.check();
        results->finalize();

        if (!outFile && !printResults)
        {
            out.emplace_back(static_cast<slint::SLintScilabOut *>(results)->getStruct());
        }
    }
    catch (slint::PCREException & e)
    {
        delete results;
        Scierror(999, _("%s: %s\n"), "slint", e.what(), 1);
        return types::Function::Error;
    }
    catch (slint::FileException & e)
    {
        delete results;
        Scierror(999, _("%s: %s\n"), "slint", e.what(), 1);
        return types::Function::Error;
    }
    catch (slint::SLintXMLException & e)
    {
        delete results;
        Scierror(999, _("%s: %s\n"), "slint", e.what(), 1);
        return types::Function::Error;
    }

    delete results;

    if (contributionMsg)
    {
        sciprint("%s\n", _("Module developed with the contribution of CNES."));
        contributionMsg = false;
    }

    return types::Function::OK;
}