[cig-commits] r6896 - cs/cigma/trunk/tools

luis at geodynamics.org luis at geodynamics.org
Wed May 16 10:19:07 PDT 2007


Author: luis
Date: 2007-05-16 10:19:07 -0700 (Wed, 16 May 2007)
New Revision: 6896

Added:
   cs/cigma/trunk/tools/init-cmd.c
Log:
Added init command


Added: cs/cigma/trunk/tools/init-cmd.c
===================================================================
--- cs/cigma/trunk/tools/init-cmd.c	2007-05-16 17:18:54 UTC (rev 6895)
+++ cs/cigma/trunk/tools/init-cmd.c	2007-05-16 17:19:07 UTC (rev 6896)
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <hdf5.h>
+
+
+int init_main(int argc, char *argv[])
+{
+    int i;
+    hid_t file_id;
+    hid_t group_id;
+    hid_t subgroup_id;
+    herr_t status;
+
+    char model[100];
+    char subgroup[100];
+
+
+    if (argc < 2)
+    {
+        printf("Usage: cigma init file.h5 [model [subgroup1 subgroup2 ...]]\n");
+        return EXIT_SUCCESS;
+    }
+
+    // try to create argv[1]
+    file_id = H5Fcreate(argv[1], H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
+    if (file_id < 0)
+    {
+        // creation failed...does it already exist?
+        // try opening the file instead
+        file_id = H5Fopen(argv[1], H5F_ACC_RDWR, H5P_DEFAULT);
+        if (file_id < 0)
+        {
+            fprintf(stderr, "Error: Could not access file %s\n", argv[1]);
+            return EXIT_FAILURE;
+        }
+    }
+
+    //
+    // carry on!
+    //
+
+    // create the top level group
+    if (argc < 3)
+        strcpy(model, "/model");
+    else
+        strcpy(model, argv[2]);
+
+    group_id = H5Gcreate(file_id, model, 0);
+    if (group_id < 0)
+    {
+        group_id = H5Gopen(file_id, model);
+        if (group_id < 0)
+        {
+            fprintf(stderr, "Error: Could not access group %s\n", model);
+            H5Fclose(file_id);
+            return EXIT_FAILURE;
+        }
+    }
+    
+    // create the rest of the groups
+    for (i = 2; i < argc; i++)
+    {
+        strcpy(subgroup, argv[i]);
+        subgroup_id = H5Gcreate(group_id, subgroup, 0);
+        if (subgroup_id < 0)
+        {
+            subgroup_id = H5Gopen(group_id, subgroup);
+            if (subgroup_id < 0)
+            {
+                fprintf(stderr, "Error: Could not acess subgroup %s\n", subgroup);
+                H5Fclose(file_id);
+                return EXIT_FAILURE;
+            }
+        }
+        status = H5Gclose(subgroup_id);
+    }
+
+    status = H5Gclose(group_id);
+    status = H5Fclose(file_id);
+
+    printf("Updated \"%s\"\n", argv[1]);
+
+    return EXIT_SUCCESS;
+}
+



More information about the cig-commits mailing list