summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPedro Vicente Nunes <pvn@hdfgroup.org>2008-02-19 16:50:13 (GMT)
committerPedro Vicente Nunes <pvn@hdfgroup.org>2008-02-19 16:50:13 (GMT)
commit7f9cdd4ca24432c373a26c1c657a0993a8b0b0e0 (patch)
treeb06d55ffdbea213c962e960d5a0a2187a2c7a7e4
parente2012038843dcd2418af8db1b852cc419d3e4047 (diff)
downloadhdf5-7f9cdd4ca24432c373a26c1c657a0993a8b0b0e0.zip
hdf5-7f9cdd4ca24432c373a26c1c657a0993a8b0b0e0.tar.gz
hdf5-7f9cdd4ca24432c373a26c1c657a0993a8b0b0e0.tar.bz2
[svn-r14610] #368 (E1) h5import: add ability to import strings
DONE NOW for 1.6, done previously for 1.8 RFC here http://bugzilla.hdfgroup.uiuc.edu/show_bug.cgi?id=368 tested: linux, solaris
-rwxr-xr-xtools/h5import/h5import.c209
-rwxr-xr-xtools/h5import/h5import.h4
-rwxr-xr-xtools/h5import/h5importtestutil.sh3
-rw-r--r--tools/h5import/testfiles/txtstr.conf6
-rw-r--r--tools/h5import/testfiles/txtstr.h5bin0 -> 10240 bytes
-rw-r--r--tools/h5import/testfiles/txtstr.txt2
6 files changed, 217 insertions, 7 deletions
diff --git a/tools/h5import/h5import.c b/tools/h5import/h5import.c
index 2bb7e24..22e54d3 100755
--- a/tools/h5import/h5import.c
+++ b/tools/h5import/h5import.c
@@ -12,6 +12,7 @@
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
#include "hdf5.h"
#include "H5private.h"
#include <stdio.h>
@@ -19,6 +20,8 @@
#include <string.h>
#include <ctype.h>
#include "h5import.h"
+#include "h5tools_utils.h"
+
int main(int argc, char *argv[])
{
@@ -42,6 +45,13 @@ int main(int argc, char *argv[])
(void) HDsetvbuf(stderr, (char *) NULL, _IOLBF, 0);
(void) HDsetvbuf(stdout, (char *) NULL, _IOLBF, 0);
+ if ( argv[1] && (strcmp("-V",argv[1])==0) )
+ {
+ print_version("h5import");
+ exit(EXIT_SUCCESS);
+
+ }
+
/*
* validate the number of command line arguments
*/
@@ -249,8 +259,23 @@ gtoken(char *s)
return (token);
}
+/*-------------------------------------------------------------------------
+ * Function: processDataFile
+ *
+ * Purpose: allocate memory and read data file
+ *
+ * Return: 0, success, -1, error
+ *
+ * Programmer: pkmat
+ *
+ * Modifications: pvn
+ * 2/19/2008. Added support for STR type, extra parameter FILE_ID
+ *
+ *-------------------------------------------------------------------------
+ */
+
static int
-processDataFile(char *infile, struct Input *in, FILE **strm)
+processDataFile(char *infile, struct Input *in, FILE **strm, hid_t file_id)
{
const char *err1 = "Unable to open the input file %s for reading.\n";
const char *err2 = "Error in allocating integer data storage.\n";
@@ -260,6 +285,7 @@ processDataFile(char *infile, struct Input *in, FILE **strm)
const char *err6 = "Error in allocating unsigned integer data storage.\n";
const char *err7 = "Error in reading unsigned integer data.\n";
const char *err10 = "Unrecognized input class type.\n";
+ const char *err11 = "Error in reading string data.\n";
/*-------------------------------------------------------------------------
* special case for opening binary classes in WIN32
@@ -341,7 +367,16 @@ processDataFile(char *infile, struct Input *in, FILE **strm)
break;
case 5: /* STR */
- break;
+
+ if (processStrData(strm, in, file_id) == -1)
+ {
+ (void) fprintf(stderr, err11, infile);
+ return(-1);
+ }
+
+
+
+ break;
case 6: /* TEXTUIN */
case 7: /* UIN */
@@ -379,7 +414,7 @@ readIntegerData(FILE **strm, struct Input *in)
int j;
const char *err1 = "Unable to get integer value from file.\n";
- const char *err2 = "Unrecongnized input class type.\n";
+ const char *err2 = "Unrecognized input class type.\n";
const char *err3 = "Invalid input size.\n";
for (j=0; j<in->rank;j++)
@@ -542,7 +577,7 @@ readUIntegerData(FILE **strm, struct Input *in)
hsize_t i;
int j;
const char *err1 = "Unable to get unsigned integer value from file.\n";
- const char *err2 = "Unrecongnized input class type.\n";
+ const char *err2 = "Unrecognized input class type.\n";
const char *err3 = "Invalid input size.\n";
for (j=0; j<in->rank;j++)
@@ -700,7 +735,7 @@ readFloatData(FILE **strm, struct Input *in)
hsize_t i;
int j;
const char *err1 = "Unable to get integer value from file.\n";
- const char *err2 = "Unrecongnized input class type.\n";
+ const char *err2 = "Unrecognized input class type.\n";
const char *err3 = "Invalid input size type.\n";
for (j=0; j<in->rank;j++)
@@ -789,6 +824,157 @@ readFloatData(FILE **strm, struct Input *in)
return(0);
}
+
+/*-------------------------------------------------------------------------
+ * Function: processStrData
+ *
+ * Purpose: read an ASCII file with string data and generate an HDF5 dataset
+ * with a variable length type
+ *
+ * Return: 0, ok, -1 no
+ *
+ * Programmer: Pedro Vicente, pvn@hdfgroup.org
+ *
+ * Date: July, 26, 2007
+ *
+ *-------------------------------------------------------------------------
+ */
+static int
+processStrData(FILE **strm, struct Input *in, hid_t file_id)
+{
+ hid_t group_id, dset_id, space_id, mspace_id, type_id, handle;
+ hsize_t dims[1];
+ char str[1024];
+ char c;
+ int i = 0, j, nlines = 0, line;
+
+/*-------------------------------------------------------------------------
+ * get number of lines in the input file
+ *-------------------------------------------------------------------------
+ */
+
+ while ( !feof( *strm ) )
+ {
+ c = fgetc( *strm );
+
+ if ( c == 10 ) /* eol */
+ {
+ nlines++;
+
+ }
+ }
+
+ if ( !nlines )
+ return 0;
+
+ /* number of records */
+ dims[0] = nlines;
+
+ /* rewind */
+ fseek(*strm,0L,0);
+
+/*-------------------------------------------------------------------------
+ * read file again and generate an HDF5 dataset
+ *-------------------------------------------------------------------------
+ */
+
+ if (( type_id = H5Tcopy(H5T_C_S1)) < 0 )
+ goto out;
+
+ if ( H5Tset_size (type_id,H5T_VARIABLE) < 0 )
+ goto out;
+
+ /* disable error reporting */
+ H5E_BEGIN_TRY
+ {
+
+ /* create parent groups */
+ if(in->path.count > 1) {
+ j = 0;
+ handle = file_id;
+ while(j < in->path.count - 1) {
+ if((group_id = H5Gopen(handle, in->path.group[j])) < 0) {
+ group_id = H5Gcreate(handle, in->path.group[j++], 0);
+ for(; j < in->path.count - 1; j++)
+ group_id = H5Gcreate(group_id, in->path.group[j], 0);
+ handle = group_id;
+ break;
+ }
+ handle = group_id;
+ j++;
+ }
+ }
+ else {
+ handle = file_id;
+ j = 0;
+ }
+
+ /*enable error reporting */
+ } H5E_END_TRY;
+
+ if((space_id = H5Screate_simple(1, dims, NULL)) < 0)
+ goto out;
+
+ if((mspace_id = H5Screate(H5S_SCALAR)) < 0)
+ goto out;
+
+ if((dset_id = H5Dcreate(handle, in->path.group[j], type_id, space_id, H5P_DEFAULT)) < 0)
+ goto out;
+
+ line = 0;
+
+ while(!feof(*strm)) {
+ c = fgetc(*strm);
+
+ str[i] = c;
+
+ i++;
+
+ if(c == 10) /* eol */
+ {
+ char *str2 = str;
+ hid_t fspace_id;
+ hsize_t start[1];
+ hsize_t count[1] = { 1 };
+
+ str[ i-1 ] = '\0'; /* terminate string */
+
+ if (( fspace_id = H5Dget_space (dset_id)) < 0 )
+ goto out;
+
+ start[0] = line ++ ;
+
+ if ( H5Sselect_hyperslab(fspace_id,H5S_SELECT_SET,start,NULL,count,NULL) < 0 )
+ goto out;
+
+ if ( H5Dwrite(dset_id,type_id,mspace_id,fspace_id,H5P_DEFAULT, &str2 ) < 0 )
+ goto out;
+
+ if ( H5Sclose(fspace_id) < 0 )
+ goto out;
+
+ i = 0;
+ str[ 0 ] = '\0';
+
+ }
+ }
+
+
+ /* close */
+ H5Dclose(dset_id);
+ H5Sclose(space_id);
+ H5Sclose(mspace_id);
+ H5Tclose(type_id);
+
+ return(0);
+
+out:
+
+ return (-1);
+}
+
+
+
static int
allocateIntegerStorage(struct Input *in)
{
@@ -1292,6 +1478,10 @@ validateConfigurationParameters(struct Input * in)
const char *err6 = "No support for reading 64-bit integer (INPUT-CLASS: IN, TEXTIN, UIN, TEXTUIN files\n";
#endif
+ /* for class STR other parameters are ignored */
+ if (in->inputClass == 5) /* STR */
+ return (0);
+
if (
(in->configOptionVector[DIM] != 1) ||
(in->configOptionVector[RANK] != 1))
@@ -2276,12 +2466,15 @@ process(struct Options *opt)
}
}
- if (processDataFile(opt->infiles[k].datafile, in, &strm) == -1)
+ if (processDataFile(opt->infiles[k].datafile, in, &strm, file_id ) == -1)
{
(void) fprintf(stderr, err3, opt->infiles[k].datafile);
return (-1);
}
+ if (in->inputClass != 5) /* STR */
+ {
+
for (j=0; j<in->rank;j++)
numOfElements *= in->sizeOfDimension[j];
@@ -2389,6 +2582,10 @@ process(struct Options *opt)
H5Pclose(proplist);
H5Sclose(dataspace);
}
+
+ } /* STR */
+
+
H5Fclose(file_id);
return (0);
}
diff --git a/tools/h5import/h5import.h b/tools/h5import/h5import.h
index 4485a89..f692fd6 100755
--- a/tools/h5import/h5import.h
+++ b/tools/h5import/h5import.h
@@ -13,6 +13,7 @@
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
/*
*
* Data and structure definitions for h5import
@@ -214,7 +215,7 @@ static int CompressionTypeStrToInt(char *temp);
static int getCompressionParameter(struct Input *in, FILE** strm);
static int getExternalFilename(struct Input *in, FILE** strm);
static int getMaximumDimensionSizes(struct Input *in, FILE **strm);
-static int processDataFile(char *infile, struct Input *in, FILE **strm);
+static int processDataFile(char *infile, struct Input *in, FILE **strm, hid_t file_id);
static int readIntegerData(FILE **strm, struct Input *in);
static int readFloatData(FILE **strm, struct Input *in);
static int allocateIntegerStorage(struct Input *in);
@@ -224,6 +225,7 @@ hid_t createInputDataType(struct Input *in);
static int readUIntegerData(FILE **strm, struct Input *in);
static int allocateUIntegerStorage(struct Input *in);
static int validateConfigurationParameters(struct Input * in);
+static int processStrData(FILE **strm, struct Input *in, hid_t file_id);
#endif /* H5IMPORT_H__ */
diff --git a/tools/h5import/h5importtestutil.sh b/tools/h5import/h5importtestutil.sh
index 2183abf..7516ea6 100755
--- a/tools/h5import/h5importtestutil.sh
+++ b/tools/h5import/h5importtestutil.sh
@@ -99,6 +99,9 @@ TOOLTEST binuin16.bin -c $srcdir/testfiles/binuin16.conf -o binuin16.h5
TESTING "BINARY UI32 - rank 3 - Output LE + CHUNKED "
TOOLTEST binuin32.bin -c $srcdir/testfiles/binuin32.conf -o binuin32.h5
+TESTING "STR"
+TOOLTEST $srcdir/testfiles/txtstr.txt -c $srcdir/testfiles/txtstr.conf -o txtstr.h5
+
TESTING "BINARY I8 CR LF EOF"
TOOLTEST binin8w.bin -c $srcdir/testfiles/binin8w.conf -o binin8w.h5
diff --git a/tools/h5import/testfiles/txtstr.conf b/tools/h5import/testfiles/txtstr.conf
new file mode 100644
index 0000000..85079e0
--- /dev/null
+++ b/tools/h5import/testfiles/txtstr.conf
@@ -0,0 +1,6 @@
+PATH /mytext/data
+INPUT-CLASS STR
+
+
+
+
diff --git a/tools/h5import/testfiles/txtstr.h5 b/tools/h5import/testfiles/txtstr.h5
new file mode 100644
index 0000000..bc909aa
--- /dev/null
+++ b/tools/h5import/testfiles/txtstr.h5
Binary files differ
diff --git a/tools/h5import/testfiles/txtstr.txt b/tools/h5import/testfiles/txtstr.txt
new file mode 100644
index 0000000..25be0a6
--- /dev/null
+++ b/tools/h5import/testfiles/txtstr.txt
@@ -0,0 +1,2 @@
+ hello world
+ hello world again