summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Lu <songyulu@hdfgroup.org>2002-04-05 22:31:20 (GMT)
committerRaymond Lu <songyulu@hdfgroup.org>2002-04-05 22:31:20 (GMT)
commit242d36d2631e3e1f68216b75d462cb1f46ccd6ec (patch)
tree4625358b70f24227c9fd186d793eabf59e39740e
parent9606ebdfc7318b73860cea422d326334d44e288e (diff)
downloadhdf5-242d36d2631e3e1f68216b75d462cb1f46ccd6ec.zip
hdf5-242d36d2631e3e1f68216b75d462cb1f46ccd6ec.tar.gz
hdf5-242d36d2631e3e1f68216b75d462cb1f46ccd6ec.tar.bz2
[svn-r5147]
Purpose: New feature Description: Added a query function H5Tget_member_index for compound and enumeration data types, to retrieve member's index by its name. Platforms tested: Linux 2.2
-rw-r--r--release_docs/RELEASE.txt3
-rw-r--r--src/H5T.c59
-rw-r--r--src/H5Tpublic.h1
-rw-r--r--test/dtypes.c214
4 files changed, 277 insertions, 0 deletions
diff --git a/release_docs/RELEASE.txt b/release_docs/RELEASE.txt
index f477b99..fe5a14b 100644
--- a/release_docs/RELEASE.txt
+++ b/release_docs/RELEASE.txt
@@ -156,6 +156,9 @@ Documentation
New Features
============
+ * A new query function H5Tget_member_index has been added for compound
+ and enumeration data types, to retrieve member's index by name.
+ SLU - 2002/04/05
* Improved performance of "regular" hyperslab I/O when using MPI-IO and the
datatype conversion is unneccessary. QAK - 2002/04/02
* Improved performance of single hyperslab I/O when datatype conversion is
diff --git a/src/H5T.c b/src/H5T.c
index 50d67e4..c74cb29 100644
--- a/src/H5T.c
+++ b/src/H5T.c
@@ -4075,6 +4075,65 @@ H5Tget_member_name(hid_t type_id, int membno)
/*-------------------------------------------------------------------------
+ * Function: H5Tget_member_index
+ *
+ * Purpose: Returns the index of a member in a compound or enumeration
+ * data type by given name.Members are stored in no particular
+ * order with numbers 0 through N-1 where N is the value
+ * returned by H5Tget_nmembers().
+ *
+ * Return: Success: index of the member if exists.
+ * Failure: -1.
+ *
+ * Programmer: Raymond Lu
+ * Thursday, April 4, 2002
+ *
+ * Modifications:
+ *
+ *-------------------------------------------------------------------------
+ */
+int
+H5Tget_member_index(hid_t type_id, const char *name)
+{
+ H5T_t *dt = NULL;
+ int ret_value = FAIL;
+ int nmembs, i;
+
+ FUNC_ENTER(H5Tget_member_index, NULL);
+ H5TRACE2("Is","is",type_id,name);
+
+ /* Check arguments */
+ assert(name);
+ if(H5I_DATATYPE!=H5I_get_type(type_id) || NULL==(dt=H5I_object(type_id)))
+ HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");
+
+ /* Locate member by name */
+ switch (dt->type) {
+ case H5T_COMPOUND:
+ nmembs = dt->u.compnd.nmembs;
+ for(i=0; i<nmembs; i++) {
+ if(!HDstrcmp(dt->u.compnd.memb[i].name, name))
+ HGOTO_DONE(i);
+ }
+ break;
+ case H5T_ENUM:
+ nmembs = dt->u.enumer.nmembs;
+ for(i=0; i<nmembs; i++) {
+ if(!HDstrcmp(dt->u.enumer.name[i], name))
+ HGOTO_DONE(i);
+ }
+ break;
+ default:
+ HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL,
+ "operation not supported for this type");
+ }
+
+done:
+ FUNC_LEAVE(ret_value);
+}
+
+
+/*-------------------------------------------------------------------------
* Function: H5Tget_member_offset
*
* Purpose: Returns the byte offset of the beginning of a member with
diff --git a/src/H5Tpublic.h b/src/H5Tpublic.h
index 06ef8bf..458340c 100644
--- a/src/H5Tpublic.h
+++ b/src/H5Tpublic.h
@@ -500,6 +500,7 @@ __DLL__ H5T_pad_t H5Tget_inpad(hid_t type_id);
__DLL__ H5T_str_t H5Tget_strpad(hid_t type_id);
__DLL__ int H5Tget_nmembers(hid_t type_id);
__DLL__ char *H5Tget_member_name(hid_t type_id, int membno);
+__DLL__ int H5Tget_member_index(hid_t type_id, const char *name);
__DLL__ size_t H5Tget_member_offset(hid_t type_id, int membno);
__DLL__ H5T_class_t H5Tget_member_class(hid_t type_id, int membno);
__DLL__ hid_t H5Tget_member_type(hid_t type_id, int membno);
diff --git a/test/dtypes.c b/test/dtypes.c
index dd95439..083c581 100644
--- a/test/dtypes.c
+++ b/test/dtypes.c
@@ -43,6 +43,7 @@
const char *FILENAME[] = {
"dtypes1",
"dtypes2",
+ "dtypes3",
NULL
};
@@ -1084,6 +1085,218 @@ test_compound_7(void)
/*-------------------------------------------------------------------------
+ * Function: test_query
+ *
+ * Purpose: Tests query functions of compound and enumeration types.
+ *
+ * Return: Success: 0
+ *
+ * Failure: number of errors
+ *
+ * Programmer: Raymond Lu
+ * Thursday, April 4, 2002
+ *
+ * Modifications:
+ *
+ *-------------------------------------------------------------------------
+ */
+static int
+test_query(void)
+{
+ struct s1 {
+ int a;
+ float b;
+ long c;
+ double d;
+ };
+ hid_t file=-1, tid1=-1, tid2=-1;
+ char filename[1024];
+ char compnd_type[]="Compound_type", enum_type[]="Enum_type";
+ int nmembs, index;
+ short enum_val;
+
+ TESTING("query functions of compound and enumeration types");
+
+ /* Create File */
+ h5_fixname(FILENAME[2], H5P_DEFAULT, filename, sizeof filename);
+ if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT))<0)
+ goto error;
+
+ /* Create a compound datatype */
+ if((tid1=H5Tcreate(H5T_COMPOUND, sizeof(struct s1)))<0) {
+ H5_FAILED();
+ printf("Can't create datatype!\n");
+ goto error;
+ } /* end if */
+ if(H5Tinsert(tid1, "a", HOFFSET(struct s1, a), H5T_NATIVE_INT)<0) {
+ H5_FAILED();
+ printf("Can't insert field 'a'\n");
+ goto error;
+ } /* end if */
+ if(H5Tinsert(tid1, "b", HOFFSET(struct s1, b), H5T_NATIVE_FLOAT)<0) {
+ H5_FAILED();
+ printf("Can't insert field 'b'\n");
+ goto error;
+ } /* end if */
+ if(H5Tinsert(tid1, "c", HOFFSET(struct s1, c), H5T_NATIVE_LONG)<0) {
+ H5_FAILED();
+ printf("Can't insert field 'c'\n");
+ goto error;
+ } /* end if */
+ if(H5Tinsert(tid1, "d", HOFFSET(struct s1, d), H5T_NATIVE_DOUBLE)<0) {
+ H5_FAILED();
+ printf("Can't insert field 'd'\n");
+ goto error;
+ } /* end if */
+
+ /* Create a enumerate datatype */
+ if((tid2=H5Tcreate(H5T_ENUM, sizeof(short)))<0) {
+ H5_FAILED();
+ printf("Can't create enumerate type\n");
+ goto error;
+ } /* end if */
+ if(H5Tenum_insert(tid2, "RED", (enum_val=0,&enum_val))<0) {
+ H5_FAILED();
+ printf("Can't insert field into enumeration type\n");
+ goto error;
+ } /* end if */
+ if(H5Tenum_insert(tid2, "GREEN", (enum_val=1,&enum_val))<0) {
+ H5_FAILED();
+ printf("Can't insert field into enumeration type\n");
+ goto error;
+ } /* end if */
+ if(H5Tenum_insert(tid2, "BLUE", (enum_val=2,&enum_val))<0) {
+ H5_FAILED();
+ printf("Can't insert field into enumeration type\n");
+ goto error;
+ } /* end if */
+ if(H5Tenum_insert(tid2, "ORANGE", (enum_val=3,&enum_val))<0) {
+ H5_FAILED();
+ printf("Can't insert field into enumeration type\n");
+ goto error;
+ } /* end if */
+ if(H5Tenum_insert(tid2, "YELLOW", (enum_val=4,&enum_val))<0) {
+ H5_FAILED();
+ printf("Can't insert field into enumeration type\n");
+ goto error;
+ } /* end if */
+
+ /* Query member number and member index by name, for compound type. */
+ if((nmembs=H5Tget_nmembers(tid1))!=4) {
+ H5_FAILED();
+ printf("Can't get member number\n");
+ goto error;
+ } /* end if */
+ if((index=H5Tget_member_index(tid1, "c"))!=2) {
+ H5_FAILED();
+ printf("Can't get correct index number\n");
+ goto error;
+ } /* end if */
+
+ /* Query member number and member index by name, for enumeration type. */
+ if((nmembs=H5Tget_nmembers(tid2))!=5) {
+ H5_FAILED();
+ printf("Can't get member number\n");
+ goto error;
+ } /* end if */
+ if((index=H5Tget_member_index(tid2, "ORANGE"))!=3) {
+ H5_FAILED();
+ printf("Can't get correct index number\n");
+ goto error;
+ } /* end if */
+
+ /* Commit compound datatype and close it */
+ if(H5Tcommit(file, compnd_type, tid1)<0) {
+ H5_FAILED();
+ printf("Can't commit compound datatype\n");
+ goto error;
+ } /* end if */
+ if(H5Tclose(tid1)<0) {
+ H5_FAILED();
+ printf("Can't close datatype\n");
+ goto error;
+ } /* end if */
+
+ /* Commit enumeration datatype and close it */
+ if(H5Tcommit(file, enum_type, tid2)<0) {
+ H5_FAILED();
+ printf("Can't commit compound datatype\n");
+ goto error;
+ } /* end if */
+ if(H5Tclose(tid2)<0) {
+ H5_FAILED();
+ printf("Can't close datatype\n");
+ goto error;
+ } /* end if */
+
+ /* Open the dataytpe for query */
+ if((tid1=H5Topen(file, compnd_type))<0) {
+ H5_FAILED();
+ printf("Can't open datatype\n");
+ goto error;
+ } /* end if */
+ if((tid2=H5Topen(file, enum_type))<0) {
+ H5_FAILED();
+ printf("Can't open datatype\n");
+ goto error;
+ } /* end if */
+
+ /* Query member number and member index by name, for compound type */
+ if((nmembs=H5Tget_nmembers(tid1))!=4) {
+ H5_FAILED();
+ printf("Can't get member number\n");
+ goto error;
+ } /* end if */
+ if((index=H5Tget_member_index(tid1, "c"))!=2) {
+ H5_FAILED();
+ printf("Can't get correct index number\n");
+ goto error;
+ } /* end if */
+
+ /* Query member number and member index by name, for enumeration type */
+ if((nmembs=H5Tget_nmembers(tid2))!=5) {
+ H5_FAILED();
+ printf("Can't get member number\n");
+ goto error;
+ } /* end if */
+ if((index=H5Tget_member_index(tid2, "ORANGE"))!=3) {
+ H5_FAILED();
+ printf("Can't get correct index number\n");
+ goto error;
+ } /* end if */
+
+ /* Close data type and file */
+ if(H5Tclose(tid1)<0) {
+ H5_FAILED();
+ printf("Can't close datatype\n");
+ goto error;
+ } /* end if */
+ if(H5Tclose(tid2)<0) {
+ H5_FAILED();
+ printf("Can't close datatype\n");
+ goto error;
+ } /* end if */
+
+ if(H5Fclose(file)<0) {
+ H5_FAILED();
+ printf("Can't close file\n");
+ goto error;
+ } /* end if */
+
+ PASSED();
+ return 0;
+
+ error:
+ H5E_BEGIN_TRY {
+ H5Tclose (tid1);
+ H5Tclose (tid2);
+ H5Fclose (file);
+ } H5E_END_TRY;
+ return 1;
+}
+
+
+/*-------------------------------------------------------------------------
* Function: test_transient
*
* Purpose: Tests transient data types.
@@ -3892,6 +4105,7 @@ main(void)
nerrors += test_classes();
nerrors += test_copy();
nerrors += test_compound_1();
+ nerrors += test_query();
nerrors += test_transient (fapl);
nerrors += test_named (fapl);
h5_cleanup(FILENAME, fapl); /*must happen before first reset*/