utf: scicos 2
[scilab.git] / scilab / modules / scicos / sci_gateway / cpp / sci_scicos_setfield.cpp
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2014-2014 - Scilab Enterprises - Clement DAVID
4  *
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 #include <string>
14
15 #include "../../includes/view_scilab/Adapters.hxx"
16 #include "gw_scicos.hxx"
17
18 #include "types.hxx"
19 #include "string.hxx"
20 #include "list.hxx"
21 #include "function.hxx"
22
23 #include "view_scilab/BaseAdapter.hxx"
24 #include "view_scilab/BlockAdapter.hxx"
25 #include "view_scilab/CprAdapter.hxx"
26 #include "view_scilab/DiagramAdapter.hxx"
27 #include "view_scilab/GraphicsAdapter.hxx"
28 #include "view_scilab/LinkAdapter.hxx"
29 #include "view_scilab/ModelAdapter.hxx"
30 #include "view_scilab/ParamsAdapter.hxx"
31 #include "view_scilab/ScsAdapter.hxx"
32 #include "view_scilab/StateAdapter.hxx"
33 #include "view_scilab/TextAdapter.hxx"
34
35 #include "utilities.hxx"
36 #include "Controller.hxx"
37 #include "model/Annotation.hxx"
38 #include "model/Block.hxx"
39 #include "model/Diagram.hxx"
40 #include "model/Link.hxx"
41
42 extern "C"
43 {
44 #include "Scierror.h"
45 #include "localization.h"
46 }
47
48 using namespace org_scilab_modules_scicos;
49
50 static const std::string funame = "scicos_setfield";
51
52 template<class Adaptor, class Adaptee>
53 types::InternalType* set(types::InternalType* adaptor_type, const std::string& field, types::InternalType* value)
54 {
55     Adaptor* adaptor = adaptor_type->getAs<Adaptor>();
56
57     if (!adaptor->setProperty(field, value, Controller()))
58     {
59         Scierror(999, _("%s: Wrong value for input argument #%d: unable to set \"%s\".\n"), funame.c_str(), 2, field.c_str());
60         return adaptor_type;
61     }
62
63     return adaptor_type->clone();
64 }
65
66 types::Function::ReturnValue sci_scicos_setfield(types::typed_list &in, int _iRetCount, types::typed_list &out)
67 {
68     if (in.size() != 3)
69     {
70         Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), funame.data(), 3);
71         return types::Function::Error;
72     }
73     if (_iRetCount != 1)
74     {
75         Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), funame.data(), 1);
76         return types::Function::Error;
77     }
78
79     types::InternalType* field_type = in[0];
80     if (field_type->getType() != types::InternalType::ScilabString)
81     {
82         Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), funame.data(), 1);
83         return types::Function::Error;
84     }
85
86     types::String* field_name = field_type->getAs<types::String>();
87     if (field_name->getSize() > 1)
88     {
89         Scierror(999, _("%s: Wrong size for input argument #%d: String expected.\n"), funame.data(), 1);
90         return types::Function::Error;
91     }
92
93     std::string field = field_name->get(0);
94     types::InternalType* value = in[1];
95     types::InternalType* adaptor = in[2];
96
97     /*
98      * allocate the right adapter then try to set fields values
99      */
100
101     const view_scilab::Adapters::adapters_index_t adapter_index = view_scilab::Adapters::instance().lookup_by_typename(adaptor->getShortTypeStr());
102     types::InternalType* returnType;
103     switch (adapter_index)
104     {
105         case view_scilab::Adapters::BLOCK_ADAPTER:
106             returnType = set<view_scilab::BlockAdapter, model::Block>(adaptor, field, value);
107             if (returnType == 0)
108             {
109                 return types::Function::Error;
110             }
111             out.push_back(returnType);
112             break;
113         case view_scilab::Adapters::CPR_ADAPTER:
114             returnType = set<view_scilab::CprAdapter, model::Diagram>(adaptor, field, value);
115             if (returnType == 0)
116             {
117                 return types::Function::Error;
118             }
119             out.push_back(returnType);
120             break;
121         case view_scilab::Adapters::DIAGRAM_ADAPTER:
122             returnType = set<view_scilab::DiagramAdapter, model::Diagram>(adaptor, field, value);
123             if (returnType == 0)
124             {
125                 return types::Function::Error;
126             }
127             out.push_back(returnType);
128             break;
129         case view_scilab::Adapters::GRAPHIC_ADAPTER:
130             returnType = set<view_scilab::GraphicsAdapter, model::Block>(adaptor, field, value);
131             if (returnType == 0)
132             {
133                 return types::Function::Error;
134             }
135             out.push_back(returnType);
136             break;
137         case view_scilab::Adapters::LINK_ADAPTER:
138             returnType = set<view_scilab::LinkAdapter, model::Link>(adaptor, field, value);
139             if (returnType == 0)
140             {
141                 return types::Function::Error;
142             }
143             out.push_back(returnType);
144             break;
145         case view_scilab::Adapters::MODEL_ADAPTER:
146             returnType = set<view_scilab::ModelAdapter, model::Block>(adaptor, field, value);
147             if (returnType == 0)
148             {
149                 return types::Function::Error;
150             }
151             out.push_back(returnType);
152             break;
153         case view_scilab::Adapters::PARAMS_ADAPTER:
154             returnType = set<view_scilab::ParamsAdapter, model::Diagram>(adaptor, field, value);
155             if (returnType == 0)
156             {
157                 return types::Function::Error;
158             }
159             out.push_back(returnType);
160             break;
161         case view_scilab::Adapters::SCS_ADAPTER:
162             returnType = set<view_scilab::ScsAdapter, model::Diagram>(adaptor, field, value);
163             if (returnType == 0)
164             {
165                 return types::Function::Error;
166             }
167             out.push_back(returnType);
168             break;
169         case view_scilab::Adapters::STATE_ADAPTER:
170             returnType = set<view_scilab::StateAdapter, model::Diagram>(adaptor, field, value);
171             if (returnType == 0)
172             {
173                 return types::Function::Error;
174             }
175             out.push_back(returnType);
176             break;
177         case view_scilab::Adapters::TEXT_ADAPTER:
178             returnType = set<view_scilab::TextAdapter, model::Annotation>(adaptor, field, value);
179             if (returnType == 0)
180             {
181                 return types::Function::Error;
182             }
183             out.push_back(returnType);
184             break;
185         default:
186             Scierror(999, _("%s: Wrong value for input argument #%d:  \"%s\" type is not managed.\n"), funame.data(), 2, adaptor->getTypeStr().c_str());
187             return types::Function::Error;
188             break;
189     }
190
191     return types::Function::OK;
192 }