Xcos MVC: use var2vec/vec2var encoding for 'exprs'
[scilab.git] / scilab / modules / scicos / src / cpp / view_scilab / GraphicsAdapter.cpp
index ba0beb8..31c69a5 100644 (file)
@@ -14,7 +14,6 @@
 
 #include <string>
 #include <vector>
-#include <sstream>
 
 #include "list.hxx"
 #include "tlist.hxx"
@@ -26,6 +25,9 @@
 #include "GraphicsAdapter.hxx"
 #include "ports_management.hxx"
 
+#include "var2vec.hxx"
+#include "vec2var.hxx"
+
 extern "C" {
 #include "sci_malloc.h"
 #include "charEncoding.h"
@@ -223,416 +225,25 @@ struct exprs
     {
         ScicosID adaptee = adaptor.getAdaptee()->id();
 
-        std::vector<std::string> exprs;
+        std::vector<double> exprs;
         controller.getObjectProperty(adaptee, BLOCK, EXPRS, exprs);
 
-        if (exprs.empty())
-        {
-            return 0;
-        }
-        else if (exprs[0] == "List_fortran")
-        {
-            // Get and return a list
-            types::List* o = new types::List();
-
-            // Element #1: Block parameters
-            std::istringstream nParamsStr (exprs[1]);
-            int nParams;
-            nParamsStr >> nParams;
-            if (nParams == 0)
-            {
-                types::Double* paramsField = types::Double::Empty();
-                o->append(paramsField);
-            }
-            else
-            {
-                types::String* paramsField = new types::String(nParams, 1);
-                for (int i = 0; i < nParams; ++i)
-                {
-                    paramsField->set(i, exprs[2 + i].c_str());
-                }
-                o->append(paramsField);
-            }
-
-            // Element #2: List containing an empty matrix (default value)
-            types::List* emptyMatrixList = new types::List();
-            types::Double* emptyMatrix = types::Double::Empty();
-            emptyMatrixList->append(emptyMatrix);
-            o->append(emptyMatrixList);
-
-            return o;
-        }
-        else if (exprs[0] == "List_scifunc")
-        {
-            // Get and return a list
-            types::List* o = new types::List();
-
-            // Element #1: Block parameters
-            std::istringstream nParamsStr (exprs[1]);
-            int nParams;
-            nParamsStr >> nParams;
-            if (nParams == 0)
-            {
-                types::Double* paramsField = types::Double::Empty();
-                o->append(paramsField);
-            }
-            else
-            {
-                types::String* paramsField = new types::String(nParams, 1);
-                for (int i = 0; i < nParams; ++i)
-                {
-                    paramsField->set(i, exprs[2 + i].c_str());
-                }
-                o->append(paramsField);
-            }
-
-            // Element #2: Code parts
-            types::List* codePartsField = new types::List();
-
-            for (int i = 0; i < 7; ++i)
-            {
-                std::istringstream nLinesStr (exprs[2 + nParams]);
-                int nLines;
-                nLinesStr >> nLines;
-                if (nLines == 0)
-                {
-                    // Null 'nLines' signals an empty matrix
-                    types::Double* empty_matrix = types::Double::Empty();
-                    codePartsField->append(empty_matrix);
-                }
-                else
-                {
-                    types::String* part = new types::String(nLines, 1);
-                    for (int j = 0; j < nLines; ++j)
-                    {
-                        part->set(j, exprs[2 + nParams + 1 + j].c_str());
-                    }
-                    codePartsField->append(part);
-                }
-
-                nParams += nLines + 1;
-            }
-            o->append(codePartsField);
-
-            return o;
-        }
-        else if (exprs[0] == "List_code" || exprs[0] == "List_super")
-        {
-            // Get and return a list
-            types::List* o = new types::List();
-
-            // Element #1: Default values
-            std::istringstream nParamsStr (exprs[1]);
-            int nParams;
-            nParamsStr >> nParams;
-            if (nParams == 0)
-            {
-                types::Double* defaultValuesField = types::Double::Empty();
-                o->append(defaultValuesField);
-            }
-            else
-            {
-                types::String* defaultValuesField = new types::String(nParams, 1);
-                for (int i = 0; i < nParams; ++i)
-                {
-                    defaultValuesField->set(i, exprs[2 + i].c_str());
-                }
-                o->append(defaultValuesField);
-            }
-
-            // Element #2 depends on the block kind
-            if (exprs[0] == "List_code")
-            {
-                std::istringstream nLinesStr (exprs[2 + nParams]);
-                int nLines;
-                nLinesStr >> nLines;
-                if (nLines == 0)
-                {
-                    types::Double* functionBodyField = types::Double::Empty();
-                    o->append(functionBodyField);
-                }
-                else
-                {
-                    types::String* functionBodyField = new types::String(nLines, 1);
-                    for (int i = 0; i < nLines; ++i)
-                    {
-                        functionBodyField->set(i, exprs[2 + nParams + 1 + i].c_str());
-                    }
-                    o->append(functionBodyField);
-                }
-            }
-            else // List_super
-            {
-                types::List* secondElement = new types::List();
-
-                // Paramaters names
-                if (nParams == 0)
-                {
-                    types::Double* namesField = types::Double::Empty();
-                    secondElement->append(namesField);
-                }
-                else
-                {
-                    types::String* namesField = new types::String(nParams, 1);
-                    for (int i = 0; i < nParams; ++i)
-                    {
-                        namesField->set(i, exprs[2 + nParams + i].c_str());
-                    }
-                    secondElement->append(namesField);
-                }
-
-                // Title message and paramaters decriptions
-                types::String* titleField = new types::String(nParams + 1, 1);
-                for (int i = 0; i < nParams + 1; ++i)
-                {
-                    titleField->set(i, exprs[2 + 2 * nParams + i].c_str());
-                }
-                secondElement->append(titleField);
-
-                // Parameters types and sizes
-                types::List* typesAndSizes = new types::List();
-
-                if (nParams == 0)
-                {
-                    types::Double* typeSizeField = types::Double::Empty();
-                    typesAndSizes->append(typeSizeField);
-                }
-                else
-                {
-                    for (int i = 0; i < nParams; ++i)
-                    {
-                        // Read type
-                        types::String* Type = new types::String(exprs[2 + 3 * nParams + 1 + i].c_str());
-                        typesAndSizes->append(Type);
-
-                        // Read size
-                        std::istringstream sizeStr (exprs[2 + 4 * nParams + 1 + i]);
-                        int size;
-                        sizeStr >> size;
-                        types::Double* Size = new types::Double(size);
-                        typesAndSizes->append(Size);
-                    }
-                }
-                secondElement->append(typesAndSizes);
-
-                o->append(secondElement);
-            }
-
-            return o;
-        }
-        else if (exprs[0] == "List_MBLOCK" || exprs[0] == "List_MPBLOCK")
-        {
-            // Get and return a Modelica tlist
-            types::TList* o = new types::TList();
-
-            // Header, starting with "List_MBLOCK"
-            types::String* header = new types::String(1, 10);
-            if (exprs[0] == "List_MBLOCK")
-            {
-                header->set(0, MBLOCK.c_str());
-            }
-            else
-            {
-                header->set(0, MPBLOCK.c_str());
-            }
-            header->set(1, in.c_str());
-            header->set(2, intype.c_str());
-            header->set(3, out.c_str());
-            header->set(4, outtype.c_str());
-            header->set(5, param.c_str());
-            header->set(6, paramv.c_str());
-            header->set(7, pprop.c_str());
-            header->set(8, nameF.c_str());
-            header->set(9, funtxt.c_str());
-            o->append(header);
-
-            // 'in'
-            types::String* inField = new types::String(exprs[1].c_str());
-            o->append(inField);
-
-            // 'intype'
-            types::String* intypeField = new types::String(exprs[2].c_str());
-            o->append(intypeField);
-
-            // 'out'
-            types::String* outField = new types::String(exprs[3].c_str());
-            o->append(outField);
-
-            // 'outtype'
-            types::String* outtypeField = new types::String(exprs[4].c_str());
-            o->append(outtypeField);
-
-            // 'param'
-            types::String* paramField = new types::String(exprs[5].c_str());
-            o->append(paramField);
-
-            // 'paramv'
-            types::List* paramvField = new types::List();
-            std::istringstream paramvSizeStr (exprs[6]);
-            int paramvSize;
-            paramvSizeStr >> paramvSize;
-            for (int i = 0; i < paramvSize; ++i)
-            {
-                types::String* paramvElement = new types::String(exprs[7 + i].c_str());
-                paramvField->set(i, paramvElement);
-            }
-            o->append(paramvField);
-
-            // 'pprop'
-            types::String* ppropField = new types::String(exprs[7 + paramvSize].c_str());
-            o->append(ppropField);
-
-            // 'nameF'
-            types::String* nameFField = new types::String(exprs[7 + paramvSize + 1].c_str());
-            o->append(nameFField);
-
-            // 'funtxt'
-            std::istringstream funtxtSizeStr (exprs[7 + paramvSize + 2]);
-            int funtxtSize;
-            funtxtSizeStr >> funtxtSize;
-
-            if (funtxtSize == 0)
-            {
-                // An empty 'funtxt' field returns an empty matrix
-                o->append(types::Double::Empty());
-                return o;
-            }
-            types::String* funtxtField = new types::String(funtxtSize, 1);
-            for (int i = 0; i < funtxtSize; ++i)
-            {
-                funtxtField->set(i, exprs[7 + paramvSize + 3 + i].c_str());
-            }
-            o->append(funtxtField);
-
-            return o;
-        }
-        else if (exprs[0] == "List_PDE")
-        {
-            // Get and return a PDE list
-            types::List* o = new types::List();
-
-            // Parameters
-            types::TList* params = new types::TList();
-
-            // First, the header
-            std::istringstream nParamsStr (exprs[1]);
-            int nParams;
-            nParamsStr >> nParams;
-            types::String* header = new types::String(nParams + 1, 1); // The first element of the header is the TList type
-            for (int i = 0; i < nParams + 1; ++i)
-            {
-                header->set(i, exprs[2 + i].c_str());
-            }
-            params->append(header);
-
-            // Then, the content
-            for (int i = 0; i < nParams - 1; ++i) // Stop at 'nParams-1' to get the last element as a string matrix
-            {
-                types::String* paramsElement = new types::String(exprs[3 + nParams + i].c_str());
-                params->append(paramsElement);
-            }
-            // Last element of the content (string matrix)
-            std::istringstream nColsStr (exprs[3 + 2 * nParams]);
-            int nCols;
-            nColsStr >> nCols;
-            types::String* last_string = new types::String(nCols, 1);
-            for (int i = 0; i < nCols; ++i)
-            {
-                last_string->set(i, exprs[4 + 2 * nParams + i].c_str());
-            }
-            params->append(last_string);
-
-            o->append(params);
-
-            // Next, the code
-            std::istringstream nLinesStr (exprs[4 + 2 * nParams + nCols]);
-            int nLines;
-            nLinesStr >> nLines;
-            if (nLines == 0)
-            {
-                types::Double* empty_matrix = types::Double::Empty();
-                o->append(empty_matrix);
-            }
-            else
-            {
-                types::String* code = new types::String(nLines, 1);
-                for (int i = 0; i < nLines + 1; ++i)
-                {
-                    code->set(i, exprs[5 + 2 * nParams + nCols + i].c_str());
-                }
-                o->append(code);
-            }
-
-            // Finally, the block name
-            types::String* name = new types::String(exprs[5 + 2 * nParams + nCols + nLines].c_str());
-            o->append(name);
-
-            return o;
-        }
-        else if (exprs[0] == "List_CONSTRAINT")
-        {
-            // Get and return a CONSTRAINT2_c list
-            types::List* o = new types::List();
-
-            // A "CONSTRAINT2_c" block has 3 strings in 'exprs'
-            for (int i = 1; i < 4; ++i)
-            {
-                types::String* ithElement = new types::String(exprs[i].c_str());
-                o->append(ithElement);
-            }
-            return o;
-        }
-        else if (exprs[0] == "List_LOOKUP2D")
-        {
-            // Get and return a LOOKUP2D list
-            types::List* o = new types::List();
-
-            // A "LOOKUP2D" block has 5 strings in 'exprs'
-            for (int i = 1; i < 6; ++i)
-            {
-                types::String* ithElement = new types::String(exprs[i].c_str());
-                o->append(ithElement);
-            }
-            return o;
-        }
-        else if (exprs[0] == "String")
-        {
-            // Simple case, 'exprs' is a string matrix (can be empty)
-            if (exprs.size() == 1)
-            {
-                return types::Double::Empty();
-            }
-            types::String* o = new types::String((int)exprs.size() - 1, 1);
-            for (int i = 0; i < (int)exprs.size() - 1; ++i)
-            {
-                o->set(i, exprs[i + 1].data());
-            }
-            return o;
-        }
-        else
+        types::InternalType* res;
+        if (!vec2var(exprs, res))
         {
             return 0;
         }
+        return res;
     }
 
     static bool set(GraphicsAdapter& adaptor, types::InternalType* v, Controller& controller)
     {
         ScicosID adaptee = adaptor.getAdaptee()->id();
 
-        std::vector<std::string> exprs (1); // First element will contain "String", "List_code", "List_super, "List_scifunc", "List_fortran", "List_MBLOCK" or "List_PDE"
-
+        // From here on, only perform check on 'v'. If it is correct, encode it in the model via 'var2vec'
         if (v->getType() == types::InternalType::ScilabString)
         {
             types::String* current = v->getAs<types::String>();
-
-            exprs[0] = "String";
-            exprs.resize(1 + current->getSize());
-            for (int i = 1; i < (int)exprs.size(); ++i)
-            {
-                char* c_str = wide_string_to_UTF8(current->get(i - 1));
-                exprs[i] = std::string(c_str);
-                FREE(c_str);
-            }
         }
         else if (v->getType() == types::InternalType::ScilabDouble)
         {
@@ -642,16 +253,13 @@ struct exprs
                 return false;
             }
 
-            exprs[0] = "String";
         }
+        // All the following cases are meant for blocks:
+        // CBLOCK, fortran_block, EXPRESSION, scifunc_block_m, PDE, CONSTRAINT2_c, LOOKUP2D, MBLOCK and MPBLOCK
         else if (v->getType() == types::InternalType::ScilabList)
         {
             types::List* initial_list = v->getAs<types::List>();
 
-            // Leave 'exprs[0]' empty for the moment, because we don't know yet what we are going to deal with
-            char* c_str; // Buffer
-
-            // All these blocks have two elements in 'exprs', except for blocks "PDE", "CONSTRAINT2_c" and "LOOKUP2D"
             if (initial_list->getSize() == 2)
             {
                 size_t nParams = 0;
@@ -663,27 +271,11 @@ struct exprs
                     {
                         return false;
                     }
-                    exprs.resize(2);
-                    exprs[1] = "0"; // Indicating empty matrix
                 }
                 else if (initial_list->get(0)->getType() == types::InternalType::ScilabString)
                 {
                     types::String* initial_string = initial_list->get(0)->getAs<types::String>();
-
                     nParams = initial_string->getSize();
-                    exprs.resize(exprs.size() + 1 + nParams); // Allocation for the first string
-                    std::ostringstream strSize;
-                    strSize << nParams;
-                    std::string sizeStr = strSize.str();
-                    exprs[1] = sizeStr; // Saving the size of the initial string
-
-                    for (size_t i = 0; i < nParams; ++i)
-                    {
-                        c_str = wide_string_to_UTF8(initial_string->get(i));
-                        std::string stringElement(c_str);
-                        FREE(c_str);
-                        exprs[2 + i] = stringElement;
-                    }
                 }
                 else
                 {
@@ -698,30 +290,12 @@ struct exprs
                     {
                         return false;
                     }
-
-                    exprs[0] = "List_code"; // List coming from a C block
-                    exprs.push_back("0"); // Indicating empty matrix
+                    // List coming from a C block
                 }
                 else if (initial_list->get(1)->getType() == types::InternalType::ScilabString)
                 {
                     types::String* second_string = initial_list->get(1)->getAs<types::String>();
-
-                    exprs[0] = "List_code"; // List coming from a "user-defined function" block
-                    size_t second_size = second_string->getSize();
-                    int exprsSize = exprs.size(); // Saving last index before resizing
-                    exprs.resize(exprs.size() + 1 + second_size); // Allocation for the first string
-                    std::ostringstream strSize2;
-                    strSize2 << second_size;
-                    std::string sizeStr2 = strSize2.str();
-                    exprs[exprsSize] = sizeStr2; // Saving the size of the second string
-
-                    for (size_t i = 0; i < second_size; ++i)
-                    {
-                        c_str = wide_string_to_UTF8(second_string->get(i));
-                        std::string stringElement(c_str);
-                        FREE(c_str);
-                        exprs[exprsSize + 1 + i] = stringElement;
-                    }
+                    // List coming from a "user-defined function" block
                 }
                 else if (initial_list->get(1)->getType() == types::InternalType::ScilabList)
                 {
@@ -739,14 +313,14 @@ struct exprs
                         {
                             return false;
                         }
-                        exprs[0] = "List_fortran"; // List coming from a fortran_block
+                        // List coming from a fortran_block
                     }
                     else if (second_list->getSize() == 3)
                     {
-                        // Parameter names, Title message and Parameters types & sizes
-                        exprs[0] = "List_super"; // List coming from a SuperBlock (masked or not)
+                        // List coming from a SuperBlock (masked or not)
+                        // The three elements are: parameter names, title message and parameters types & sizes
 
-                        // Parameters Names (string matrix, can be empty)
+                        // Parameters names (string matrix, can be empty)
                         if (second_list->get(0)->getType() == types::InternalType::ScilabDouble)
                         {
                             types::Double* empty_matrix_expected = second_list->get(0)->getAs<types::Double>();
@@ -764,17 +338,6 @@ struct exprs
                             {
                                 return false;
                             }
-
-                            int exprsSize = exprs.size(); // Saving last index before resizing
-                            exprs.resize(exprs.size() + nParams); // Allocation for the first string
-
-                            for (size_t i = 0; i < nParams; ++i)
-                            {
-                                c_str = wide_string_to_UTF8(second_string->get(i));
-                                std::string stringElement(c_str);
-                                FREE(c_str);
-                                exprs[exprsSize + i] = stringElement;
-                            }
                         }
                         else
                         {
@@ -799,17 +362,6 @@ struct exprs
                                 // There must be as many parameter descriptions as there are parameters, plus the title message
                                 return false;
                             }
-
-                            int exprsSize = exprs.size(); // Saving last index before resizing
-                            exprs.resize(exprs.size() + nParams + 1); // Allocation for the first string, minding the title message
-
-                            for (size_t i = 0; i < nParams + 1; ++i)
-                            {
-                                c_str = wide_string_to_UTF8(title_message->get(i));
-                                std::string stringElement(c_str);
-                                FREE(c_str);
-                                exprs[exprsSize + i] = stringElement;
-                            }
                         }
                         else
                         {
@@ -832,47 +384,10 @@ struct exprs
                                 return false;
                             }
                         }
-
-                        // First, read all types
-                        for (size_t i = 0; i < nParams; ++i)
-                        {
-                            if (third_list->get(2 * i)->getType() != types::InternalType::ScilabString)
-                            {
-                                return false;
-                            }
-                            types::String* paramType = third_list->get(2 * i)->getAs<types::String>();
-                            if (!paramType->isScalar())
-                            {
-                                return false;
-                            }
-                            c_str = wide_string_to_UTF8(paramType->get(0));
-                            std::string paramTypeStored(c_str);
-                            FREE(c_str);
-
-                            exprs.push_back(paramTypeStored);
-                        }
-                        // Then, read all sizes
-                        for (size_t i = 0; i < nParams; ++i)
-                        {
-                            if (third_list->get(2 * i + 1)->getType() != types::InternalType::ScilabDouble)
-                            {
-                                return false;
-                            }
-                            types::Double* paramSize = third_list->get(2 * i + 1)->getAs<types::Double>();
-                            if (!paramSize->isScalar())
-                            {
-                                return false;
-                            }
-
-                            std::ostringstream paramSizeStored;
-                            paramSizeStored << paramSize->get(0);
-                            std::string paramSizeStr = paramSizeStored.str();
-                            exprs.push_back(paramSizeStr);
-                        }
                     }
                     else if (second_list->getSize() == 7)
                     {
-                        exprs[0] = "List_scifunc"; // List coming from a scifunc block
+                        // List coming from a scifunc block
 
                         // Code parts (string matrices)
                         for (int i = 0; i < 7; ++i)
@@ -884,28 +399,8 @@ struct exprs
                                 {
                                     return false;
                                 }
-                                exprs.push_back(std::string("0"));
-                            }
-                            else if (second_list->get(i)->getType() == types::InternalType::ScilabString)
-                            {
-                                types::String* ith_string = second_list->get(i)->getAs<types::String>();
-                                size_t stringSize = ith_string->getSize();
-                                int exprsSize = exprs.size(); // Saving last index before resizing
-                                exprs.resize(exprs.size() + 1 + stringSize); // Allocation for the first string
-                                std::ostringstream strSizeIth;
-                                strSizeIth << stringSize;
-                                std::string sizeStrIth = strSizeIth.str();
-                                exprs[exprsSize] = sizeStrIth; // Saving the size of the ith string
-
-                                for (size_t j = 0; j < stringSize; ++j)
-                                {
-                                    c_str = wide_string_to_UTF8(ith_string->get(j));
-                                    std::string ithStringElement(c_str);
-                                    FREE(c_str);
-                                    exprs[exprsSize + 1 + j] = ithStringElement;
-                                }
                             }
-                            else
+                            else if (second_list->get(i)->getType() != types::InternalType::ScilabString)
                             {
                                 return false;
                             }
@@ -923,34 +418,16 @@ struct exprs
             }
             else if (initial_list->getSize() == 3)
             {
+                // List coming from a PDE block
                 if (initial_list->get(0)->getType() == types::InternalType::ScilabTList)
                 {
-                    exprs[0] = "List_PDE"; // List coming from a PDE block
                     types::TList* ParamsPDE = initial_list->get(0)->getAs<types::TList>();
 
                     types::String* header = ParamsPDE->getFieldNames();
                     int nParams = header->getSize() - 1; // The first element is the TList type
-                    exprs.resize(3 + 2 * nParams); // Allocation for the header size, the TList type and the header
-                    // Save the header size
-                    std::ostringstream strNParams;
-                    strNParams << nParams;
-                    std::string nParamsStr = strNParams.str();
-                    exprs[1] = nParamsStr; // Saving the header size
-                    // Save the TList type
-                    c_str = wide_string_to_UTF8(header->get(0));
-                    std::string tlistType(c_str);
-                    FREE(c_str);
-                    exprs[2] = tlistType;
-
-                    // Save the header
+                    // Check the header
                     for (int i = 1; i < nParams; ++i) // Stop at 'nParams-1' to treat the last element differently (string matrix)
                     {
-                        // Header element
-                        c_str = wide_string_to_UTF8(header->get(i));
-                        std::string headerElement(c_str);
-                        FREE(c_str);
-                        exprs[2 + i] = headerElement;
-
                         // Its corresponding element in the TList
                         if (ParamsPDE->get(i)->getType() != types::InternalType::ScilabString)
                         {
@@ -961,38 +438,14 @@ struct exprs
                         {
                             return false;
                         }
-                        c_str = wide_string_to_UTF8(ith_string->get(0));
-                        std::string stringElement(c_str);
-                        FREE(c_str);
-                        exprs[2 + nParams + i] = stringElement;
                     }
                     // Header element for the last element of ParamsPDE
-                    c_str = wide_string_to_UTF8(header->get(nParams));
-                    std::string lastHeaderElement(c_str);
-                    FREE(c_str);
-                    exprs[2 + nParams] = lastHeaderElement;
                     if (ParamsPDE->get(nParams)->getType() != types::InternalType::ScilabString)
                     {
                         return false;
                     }
                     types::String* last_string = ParamsPDE->get(nParams)->getAs<types::String>();
 
-                    int nCols = last_string->getSize();
-                    int exprsSize = exprs.size(); // Saving last index before resizing
-                    exprs.resize(exprs.size() + 1 + nCols); // Store the size of the last string because it can be a matrix
-                    std::ostringstream strSize;
-                    strSize << nCols;
-                    std::string sizeStr = strSize.str();
-                    exprs[exprsSize] = sizeStr;
-
-                    for (int i = 0; i < nCols; ++i)
-                    {
-                        c_str = wide_string_to_UTF8(last_string->get(i));
-                        std::string lastStringElement(c_str);
-                        FREE(c_str);
-                        exprs[exprsSize + 1 + i] = lastStringElement;
-                    }
-
                     // Next comes some code
                     if (initial_list->get(1)->getType() == types::InternalType::ScilabDouble)
                     {
@@ -1001,27 +454,10 @@ struct exprs
                         {
                             return false;
                         }
-                        exprs.push_back("0"); // Indicating empty matrix
                     }
                     else if (initial_list->get(1)->getType() == types::InternalType::ScilabString)
                     {
                         types::String* code = initial_list->get(1)->getAs<types::String>();
-
-                        int nLines = code->getSize();
-                        exprsSize = exprs.size(); // Saving last index before resizing
-                        exprs.resize(exprs.size() + 1 + nLines); // Store the size of the last string because it can be a matrix
-                        std::ostringstream strLines;
-                        strLines << nLines;
-                        std::string linesStr = strLines.str();
-                        exprs[exprsSize] = linesStr;
-
-                        for (int i = 0; i < nLines; ++i)
-                        {
-                            c_str = wide_string_to_UTF8(code->get(i));
-                            std::string codeElement(c_str);
-                            FREE(c_str);
-                            exprs[exprsSize + 1 + i] = codeElement;
-                        }
                     }
                     else
                     {
@@ -1038,24 +474,15 @@ struct exprs
                     {
                         return false;
                     }
-                    c_str = wide_string_to_UTF8(name->get(0));
-                    std::string nameStr(c_str);
-                    FREE(c_str);
-                    exprs.push_back(nameStr);
                 }
                 else if (initial_list->get(0)->getType() == types::InternalType::ScilabString)
                 {
-                    exprs[0] = "List_CONSTRAINT"; // List coming from a CONSTRAINT2_c block
-
+                    // List coming from a CONSTRAINT2_c block
                     types::String* x = initial_list->get(0)->getAs<types::String>();
                     if (!x->isScalar())
                     {
                         return false;
                     }
-                    c_str = wide_string_to_UTF8(x->get(0));
-                    std::string xStr(c_str);
-                    FREE(c_str);
-                    exprs.push_back(xStr);
 
                     if (initial_list->get(1)->getType() != types::InternalType::ScilabString)
                     {
@@ -1066,10 +493,6 @@ struct exprs
                     {
                         return false;
                     }
-                    c_str = wide_string_to_UTF8(xd->get(0));
-                    std::string xdStr(c_str);
-                    FREE(c_str);
-                    exprs.push_back(xdStr);
 
                     if (initial_list->get(2)->getType() != types::InternalType::ScilabString)
                     {
@@ -1080,10 +503,6 @@ struct exprs
                     {
                         return false;
                     }
-                    c_str = wide_string_to_UTF8(id->get(0));
-                    std::string idStr(c_str);
-                    FREE(c_str);
-                    exprs.push_back(idStr);
                 }
                 else
                 {
@@ -1092,17 +511,12 @@ struct exprs
             }
             else if (initial_list->getSize() == 5)
             {
-                exprs[0] = "List_LOOKUP2D"; // List coming from a LOOKUP2D block
-
+                // List coming from a LOOKUP2D block
                 types::String* xx = initial_list->get(0)->getAs<types::String>();
                 if (!xx->isScalar())
                 {
                     return false;
                 }
-                c_str = wide_string_to_UTF8(xx->get(0));
-                std::string xxStr(c_str);
-                FREE(c_str);
-                exprs.push_back(xxStr);
 
                 if (initial_list->get(1)->getType() != types::InternalType::ScilabString)
                 {
@@ -1113,10 +527,6 @@ struct exprs
                 {
                     return false;
                 }
-                c_str = wide_string_to_UTF8(yy->get(0));
-                std::string yyStr(c_str);
-                FREE(c_str);
-                exprs.push_back(yyStr);
 
                 if (initial_list->get(2)->getType() != types::InternalType::ScilabString)
                 {
@@ -1127,10 +537,6 @@ struct exprs
                 {
                     return false;
                 }
-                c_str = wide_string_to_UTF8(zz->get(0));
-                std::string zzStr(c_str);
-                FREE(c_str);
-                exprs.push_back(zzStr);
 
                 if (initial_list->get(3)->getType() != types::InternalType::ScilabString)
                 {
@@ -1141,10 +547,6 @@ struct exprs
                 {
                     return false;
                 }
-                c_str = wide_string_to_UTF8(Method->get(0));
-                std::string MethodStr(c_str);
-                FREE(c_str);
-                exprs.push_back(MethodStr);
 
                 if (initial_list->get(4)->getType() != types::InternalType::ScilabString)
                 {
@@ -1155,10 +557,6 @@ struct exprs
                 {
                     return false;
                 }
-                c_str = wide_string_to_UTF8(Graf->get(0));
-                std::string GrafStr(c_str);
-                FREE(c_str);
-                exprs.push_back(GrafStr);
             }
             else
             {
@@ -1175,15 +573,7 @@ struct exprs
             {
                 return false;
             }
-            if (header->get(0) == MBLOCK)
-            {
-                exprs[0] = "List_MBLOCK"; // TList coming from an MBLOCK
-            }
-            else if (header->get(0) == MPBLOCK)
-            {
-                exprs[0] = "List_MPBLOCK"; // TList coming from an MPBLOCK
-            }
-            else
+            if (header->get(0) != MBLOCK && header->get(0) != MPBLOCK)
             {
                 return false;
             }
@@ -1224,8 +614,6 @@ struct exprs
                 return false;
             }
 
-            char* c_str; // Buffer
-
             // 'in'
             if (current->get(1)->getType() != types::InternalType::ScilabString)
             {
@@ -1236,10 +624,6 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(inField->get(0));
-            std::string inFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(inFieldStored);
 
             // 'intype'
             if (current->get(2)->getType() != types::InternalType::ScilabString)
@@ -1251,10 +635,6 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(intypeField->get(0));
-            std::string intypeFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(intypeFieldStored);
 
             // 'out'
             if (current->get(3)->getType() != types::InternalType::ScilabString)
@@ -1266,10 +646,6 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(outField->get(0));
-            std::string outFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(outFieldStored);
 
             // 'outtype'
             if (current->get(4)->getType() != types::InternalType::ScilabString)
@@ -1281,10 +657,6 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(outtypeField->get(0));
-            std::string outtypeFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(outtypeFieldStored);
 
             // 'param'
             if (current->get(5)->getType() != types::InternalType::ScilabString)
@@ -1296,10 +668,6 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(paramField->get(0));
-            std::string paramFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(paramFieldStored);
 
             // 'paramv'
             if (current->get(6)->getType() != types::InternalType::ScilabList)
@@ -1308,24 +676,12 @@ struct exprs
             }
             types::List* list = current->get(6)->getAs<types::List>();
 
-            size_t paramvSize = list->getSize();
-            exprs.resize(exprs.size() + 1 + paramvSize); // Allocation for the 'paramv' strings
-            std::ostringstream strParamv;
-            strParamv << paramvSize;
-            std::string paramvSizeStr = strParamv.str();
-            exprs[6] = paramvSizeStr; // Saving the size of the 'paramv' field'
-
-            for (size_t i = 0; i < paramvSize; ++i)
+            for (size_t i = 0; i < list->getSize(); ++i)
             {
                 if (list->get(static_cast<int>(i))->getType() != types::InternalType::ScilabString)
                 {
                     return false;
                 }
-                types::String* listElement = list->get(static_cast<int>(i))->getAs<types::String>();
-                c_str = wide_string_to_UTF8(listElement->get(0));
-                std::string paramvElement(c_str);
-                FREE(c_str);
-                exprs[7 + i] = paramvElement;
             }
 
             // 'pprop'
@@ -1338,10 +694,6 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(ppropField->get(0));
-            std::string ppropFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(ppropFieldStored);
 
             // 'nameF'
             if (current->get(8)->getType() != types::InternalType::ScilabString)
@@ -1353,14 +705,8 @@ struct exprs
             {
                 return false;
             }
-            c_str = wide_string_to_UTF8(nameFField->get(0));
-            std::string nameFFieldStored(c_str);
-            FREE(c_str);
-            exprs.push_back(nameFFieldStored);
 
             // 'funtxt'
-            size_t funtxtSize;
-            std::ostringstream strFuntxt;
             if (current->get(9)->getType() == types::InternalType::ScilabDouble)
             {
                 types::Double* funtxtFieldDouble = current->get(9)->getAs<types::Double>();
@@ -1368,11 +714,6 @@ struct exprs
                 {
                     return false;
                 }
-                // Allow empty matrix for 'funtxt', storing a size "0"
-                funtxtSize = 0;
-                strFuntxt << funtxtSize;
-                std::string funtxtSizeStr = strFuntxt.str();
-                exprs.push_back(funtxtSizeStr);
             }
             else
             {
@@ -1381,21 +722,6 @@ struct exprs
                     return false;
                 }
                 types::String* funtxtField = current->get(9)->getAs<types::String>();
-
-                funtxtSize = funtxtField->getSize();
-                size_t exprsSize = exprs.size(); // Saving last index before resizing
-                exprs.resize(exprs.size() + 1 + funtxtSize); // Allocation for the 'funtxt' strings
-                strFuntxt << funtxtSize;
-                std::string funtxtSizeStr = strFuntxt.str();
-                exprs[exprsSize] = funtxtSizeStr; // Saving the size of the 'funtxt' field'
-
-                for (size_t i = 0; i < funtxtSize; ++i)
-                {
-                    c_str = wide_string_to_UTF8(funtxtField->get(static_cast<int>(i)));
-                    std::string funtxtElement(c_str);
-                    FREE(c_str);
-                    exprs[exprsSize + 1 + i] = funtxtElement;
-                }
             }
         }
         else
@@ -1403,6 +729,12 @@ struct exprs
             return false;
         }
 
+        // 'v' is OK, now perform the actual encoding
+        std::vector<double> exprs;
+        if (!var2vec(v, exprs))
+        {
+            return false;
+        }
         controller.setObjectProperty(adaptee, BLOCK, EXPRS, exprs);
         return true;
     }