save and load keep fields order of struct 52/16852/3
Antoine ELIAS [Thu, 16 Jul 2015 15:54:21 +0000 (17:54 +0200)]
Change-Id: If628adcb2679a3deeea16788b0f5e4bde7536424

scilab/modules/ast/includes/types/singlestruct.hxx
scilab/modules/ast/src/cpp/types/singlestruct.cpp
scilab/modules/hdf5/sci_gateway/cpp/sci_hdf5_load_v3.cpp
scilab/modules/hdf5/sci_gateway/cpp/sci_hdf5_save.cpp

index 1376a29..a32d314 100644 (file)
@@ -64,7 +64,6 @@ public :
     bool                                    addField(const std::wstring& _sKey);
     bool                                    addFieldFront(const std::wstring& _sKey);
     std::vector<InternalType *> &           getData();
-    std::vector<std::wstring>               getFieldsName();
     std::unordered_map<std::wstring, int> & getFields();
     int                                     getFieldIndex(const std::wstring& _field);
     bool                                    removeField(const std::wstring& _sKey);
index 3d741fb..2ab6930 100644 (file)
@@ -64,18 +64,6 @@ std::unordered_map<std::wstring, int> & SingleStruct::getFields()
     return m_wstFields;
 }
 
-std::vector<std::wstring> SingleStruct::getFieldsName()
-{
-    std::vector<std::wstring> names;
-    names.reserve(m_wstFields.size());
-    for (const auto & p : m_wstFields)
-    {
-        names.emplace_back(p.first);
-    }
-
-    return names;
-}
-
 int SingleStruct::getFieldIndex(const std::wstring & _field)
 {
     const auto i = m_wstFields.find(_field);
@@ -188,17 +176,10 @@ std::vector<InternalType*> SingleStruct::extract(std::vector<std::wstring> & _st
 
 String* SingleStruct::getFieldNames()
 {
-    std::set<std::wstring> names;
-    for (const auto & p : m_wstFields)
-    {
-        names.emplace(p.first);
-    }
-
-    String* pOut = new String((int)names.size(), 1);
-    int i = 0;
-    for (const auto & name : names)
+    String* pOut = new String((int)m_wstFields.size(), 1);
+    for (const auto & field : m_wstFields)
     {
-        pOut->set(i++, name.c_str());
+        pOut->set(field.second, field.first.data());
     }
     return pOut;
 }
index 07958eb..28a285f 100644 (file)
@@ -581,72 +581,72 @@ static types::InternalType* import_struct(int dataset)
         return str;
     }
 
+    //get fields name
+    int dfield = getDataSetIdFromName(dataset, "__fields__");
+    int dim = 0;
+    getDatasetInfo(dfield, &complex, &dim, NULL);
+    std::vector<int> d(dim);
+    size = getDatasetInfo(dfield, &complex, &dim, d.data());
+
+    //get dims value
+    std::vector<char*> fields(size);
+    readStringMatrix(dfield, fields.data());
+
     //open __refs__ node
     int refs = getDataSetIdFromName(dataset, "__refs__");
 
-    H5O_info_t oinfo;
-    for (int i = 0; i < fieldCount; ++i)
+    for (const auto& name : fields)
     {
-        H5Oget_info_by_idx(dataset, ".", H5_INDEX_NAME, H5_ITER_NATIVE, i, &oinfo, H5P_DEFAULT);
-        ssize_t len = H5Lget_name_by_idx(dataset, ".", H5_INDEX_NAME, H5_ITER_INC, i, 0, 0, H5P_DEFAULT) + 1;
-        char* name = (char*)MALLOC(sizeof(char) * len);
-        H5Lget_name_by_idx(dataset, ".", H5_INDEX_NAME, H5_ITER_INC, i, name, len, H5P_DEFAULT);
+        wchar_t* field = to_wide_string(name);
+        str->addField(field);
 
-        if (strcmp(name, "__dims__") != 0 && strcmp(name, "__refs__") != 0)
+        int dataref = getDataSetIdFromName(dataset, name);
+        if (dataref < 0)
         {
-            wchar_t* field = to_wide_string(name);
-            str->addField(field);
+            closeList6(dataset);
+            freeStringMatrix(dfield, fields.data());
+            return nullptr;
+        }
+
+        int refdim = 0;
+        getDatasetInfo(dataref, &complex, &refdim, NULL);
+        std::vector<int> refdims(refdim);
+        int refcount = getDatasetInfo(dataref, &complex, &refdim, refdims.data());
+        std::vector<hobj_ref_t> vrefs(refcount);
+        ret = H5Dread(dataref, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, vrefs.data());
+        if (ret < 0)
+        {
+            freeStringMatrix(dfield, fields.data());
+            return nullptr;
+        }
 
-            int dataref = getDataSetIdFromName(dataset, name);
-            if (dataref < 0)
+
+        //import field
+        for (int j = 0; j < refcount; ++j)
+        {
+            int data = H5Rdereference(refs, H5R_OBJECT, &vrefs[j]);
+            if (data < 0)
             {
-                closeList6(dataset);
-                FREE(name);
+                freeStringMatrix(dfield, fields.data());
                 return nullptr;
             }
 
-            int refdim = 0;
-            getDatasetInfo(dataref, &complex, &refdim, NULL);
-            std::vector<int> refdims(refdim);
-            int refcount = getDatasetInfo(dataref, &complex, &refdim, refdims.data());
-            std::vector<hobj_ref_t> vrefs(refcount);
-            ret = H5Dread(dataref, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, vrefs.data());
-            if (ret < 0)
+            types::InternalType* val = import_data(data);
+            if (val == nullptr)
             {
-                FREE(name);
+                freeStringMatrix(dfield, fields.data());
                 return nullptr;
             }
 
+            sstr[j]->set(field, val);
 
-            //import field
-            for (int j = 0; j < refcount; ++j)
-            {
-                int data = H5Rdereference(refs, H5R_OBJECT, &vrefs[j]);
-                if (data < 0)
-                {
-                    FREE(name);
-                    return nullptr;
-                }
-
-                types::InternalType* val = import_data(data);
-                if (val == nullptr)
-                {
-                    FREE(name);
-                    return nullptr;
-                }
-
-                sstr[j]->set(field, val);
-
-            }
-            FREE(field);
-
-            closeDataSet(dataref);
         }
 
-        FREE(name);
+        FREE(field);
+        closeDataSet(dataref);
     }
-    //str->addField();
 
+    freeStringMatrix(dfield, fields.data());
     closeList6(refs);
     closeList6(dataset);
     return str;
index fc17666..dbfcf25 100644 (file)
@@ -462,6 +462,9 @@ static int export_struct(int parent, const std::string& name, types::Struct* dat
     int fieldCount = fields->getSize();
     wchar_t** pfields = fields->get();
 
+    //save fields list in vector to keep order
+    export_string(dset, "__fields__", fields);
+
 
     std::vector<hobj_ref_t> vrefs(size);
     //fill main group with struct field name