[cig-commits] r13826 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Mon Jan 12 14:09:49 PST 2009


Author: luis
Date: 2009-01-12 14:09:48 -0800 (Mon, 12 Jan 2009)
New Revision: 13826

Modified:
   cs/cigma/trunk/src/h5attr.cpp
Log:
Iterate over all groups & datasets for a given location

Modified: cs/cigma/trunk/src/h5attr.cpp
===================================================================
--- cs/cigma/trunk/src/h5attr.cpp	2009-01-12 22:09:47 UTC (rev 13825)
+++ cs/cigma/trunk/src/h5attr.cpp	2009-01-12 22:09:48 UTC (rev 13826)
@@ -12,6 +12,7 @@
 #include "tri_logger.hpp"
 
 #include "H5Cpp.h"
+#include "hdf5.h"
 
 
 using namespace std;
@@ -256,13 +257,50 @@
     return 0;
 }
 
-void listOp(H5::H5Object& loc, const string name, void *data)
+/*
+void listAttrOp(H5::H5Object& loc, const string name, void *data)
 {
+    TRI_LOG_STR("listAttrOp()");
     string dtype, value;
     getattr(&loc, name, dtype, value);
     cout << name << " = " << value << endl;
+} */
+
+herr_t listAttrOp(hid_t loc_id, const char *attr_name, const H5A_info_t *ainfo, void *op_data)
+{
+    //TRI_LOG_STR("listAttrOp()");
+    string name(attr_name);
+    string dtype, value;
+
+    cout << "\t" << name << " = ";
+    if (op_data != 0)
+    {
+        H5::H5Object *obj = (H5::H5Object *)op_data;
+        getattr(obj, name, dtype, value);
+        if (dtype != "string")
+        {
+            cout << value;
+        }
+        else
+        {
+            boost::algorithm::replace_all(value, "\\", "\\\\");
+            boost::algorithm::replace_all(value, "\r", "\\r");
+            boost::algorithm::replace_all(value, "\n", "\\n");
+            boost::algorithm::replace_all(value, "\t", "\\t");
+            boost::algorithm::replace_all(value, "'", "\\'");
+            cout << "'" << value << "'";
+        }
+    }
+    cout << endl;
 }
 
+herr_t listGroupOp(hid_t group, const char *name, void *data)
+{
+    //TRI_LOG_STR("listOp()");
+    cout << "\t" << name << endl;
+}
+
+
 // ----------------------------------------------------------------------------
 
 int main(int argc, char *argv[])
@@ -390,32 +428,101 @@
         cout << "Error: Could not open location '" << location << "'" << endl;
         return -1;
     }
+    cout << "File " << filename << endl;
+    cout << "  Location " << location << endl;
+    string indent("    ");
 
-    // Check that attribute name is not empty
-    if (vm.count("name"))
+    try
     {
-        if (vm.count("value"))
+        // Check that attribute name is not empty
+        if (vm.count("name"))
         {
-            setattr(obj, attr_name, attr_dtype, attr_value);
+            if (vm.count("value"))
+            {
+                setattr(obj, attr_name, attr_dtype, attr_value);
+            }
+            else
+            {
+                getattr(obj, attr_name, attr_dtype, attr_value);
+            }
         }
         else
         {
-            getattr(obj, attr_name, attr_dtype, attr_value);
+            int idx;
+            int status;
+
+            /*
+            hobj_ref_t ref;
+            obj->reference(&ref, location.c_str());
+            H5G_obj_t objtype = file->getObjType(&ref);
+            */
+
+            H5I_type_t objtype = H5::IdComponent::getHDFObjType(obj->getId());
+            TRI_LOG(obj->getId());
+            TRI_LOG(objtype);
+            if (objtype == H5I_GROUP)
+            {
+                /*
+                // NOTE: This requires HDF5 1.8.1
+                H5G_info_t ginfo;
+                herr_t ret = H5Gget_info(obj->getId(), &ginfo);
+                if (ret < 0)
+                {
+                    throw H5::Exception("main", "H5Gget_info failed");
+                }
+                TRI_LOG(ginfo.nlinks);
+                cout << "Found " << ginfo.nlinks << " groups under " << location  << endl;
+                */
+
+                const char *prefix = location.c_str();
+                cout << indent << "Groups" << endl;
+                try
+                {
+                    idx = 0;
+                    while ((status = file->iterateElems(location.c_str(), &idx, listGroupOp, (void *)prefix)) > 0)
+                    {
+                        //TRI_LOG(status);
+                    }
+                } catch(H5::Exception e)
+                {
+                }
+            }
+            else if (objtype == H5I_DATASET)
+            {
+                // XXX: report dimensions and datatype?
+            }
+
+            H5O_info_t oinfo;
+            hid_t oid = obj->getId();
+            status = H5Oget_info(oid, &oinfo);
+            if (status < 0)
+            {
+                throw H5::Exception("main", "H5Oget_info failed");
+            }
+
+            int num_attrs = oinfo.num_attrs;
+            TRI_LOG(num_attrs);
+            cout << indent << "Attributes" << endl;
+            if (num_attrs > 0)
+            {
+                hsize_t _idx = 0;
+                while ((status = H5Aiterate2(oid, H5_INDEX_NAME, H5_ITER_INC, &_idx, listAttrOp, obj)) > 0)
+                {
+                    TRI_LOG(status);
+                }
+            }
+            else
+            {
+                cout << "\t" << "No attributes found" << endl;
+            }
         }
     }
-    else
+    catch (H5::Exception error)
     {
-        if (obj->getNumAttrs() > 0)
-        {
-            // print all attributes at location
-            obj->iterateAttrs(listOp);
-        }
-        else
-        {
-            cout << "No attributes are attached to '"
-                 << location << "' in file '"
-                 << filename << "'" << endl;
-        }
+        cout << "Error: " << error.getDetailMsg() << " (in " << error.getFuncName() << ")" << endl;
+        if (obj) { delete obj; }
+        if (file) { delete file; }
+        return -1;
     }
 
     // Clean up
@@ -424,3 +531,4 @@
 
     return 0;
 }
+



More information about the CIG-COMMITS mailing list