PDL-IO-HDF5

 view release on metacpan or  search on metacpan

hdf5.pd  view on Meta::CPAN

#  calls that are defined in PDL::IO::HDF5 (like PDL::IO::HDF5::H5T_NATIVE_CHAR() )
#  Doing a 'use' would make use of the calls before they are defined.
#
require PDL::IO::HDF5::Group;
require PDL::IO::HDF5::Dataset;


use Carp;

sub AUTOLOAD {
    # This AUTOLOAD is used to 'autoload' constants from the constant()
    # XS function.  If a constant is not found then control is passed
    # to the AUTOLOAD in AutoLoader.

    my $constname;
    ($constname = $AUTOLOAD) =~ s/.*:://;
    croak "& not defined" if $constname eq 'constant';
    my $val = constant($constname, @_ ? $_[0] : 0);
    if ($! != 0) {
	if ($! =~ /Invalid/) {
	    $AutoLoader::AUTOLOAD = $AUTOLOAD;
	    goto &AutoLoader::AUTOLOAD;
	}
	else {
		croak "Your vendor has not defined hdf5 macro $constname";
	}
    }
    *$AUTOLOAD = sub { $val };
    goto &$AUTOLOAD;
}
EOPM

# Code that implements the dataset count and dataset name functions
pp_addxs('',<<'EOXS');

# Code to get the number of datasets in a group

int
H5GgetDatasetCount( groupID, groupName )
	hid_t	groupID
	char * 	groupName
CODE:
	int	dsetCount = 0;
        H5Giterate(groupID, groupName, NULL, incIfDset, &dsetCount);
	RETVAL = dsetCount;
OUTPUT:
	RETVAL


# Code to get the names of the datasets in a group

void
H5GgetDatasetNames( groupID, groupName )
	hid_t	groupID
	char * 	groupName
PREINIT:
	int	dsetCount = 0;
	char ** datasetNames;  /* Array of dataset names */
	char ** datasetPtr;    /* temp pointer to datasetNames */
	int i; 	               /* Index variable */
PPCODE:
	/* Get the number of datasets */
        H5Giterate(groupID, groupName, NULL, incIfDset, &dsetCount);
	
	if( dsetCount > 0){ /* Datasets found */
		
		/* Allocate Space  for array of strings */
		datasetNames = (char **) malloc( dsetCount * sizeof(char *));
		if( datasetNames == NULL){
			printf("PDL::IO::HDF5; out of Memory in H5GgetDatasetNames\n");
			exit(1);
		}
 
		datasetPtr = datasetNames;

		H5Giterate(groupID, groupName, NULL, getName_if_Dset, &datasetPtr);

		EXTEND(SP, dsetCount); /* Make room for results on the return stack */
		for( i = 0; i< dsetCount; i++){ /* Push Names onto return stack */
		       /*  printf("Name found = '%s'\n",datasetNames[i]); */
		       PUSHs(sv_2mortal(newSVpv(datasetNames[i],0)));
		       free(datasetNames[i]); /* Release Memory */
		}

		free(datasetNames);
	}


# Code to get the number of groups in a group/file

int
H5GgetGroupCount( groupID, groupName )
	hid_t	groupID
	char * 	groupName
CODE:
	int	groupCount = 0;
        H5Giterate(groupID, groupName, NULL, incIfGroup, &groupCount);
	RETVAL = groupCount;
OUTPUT:
	RETVAL


# Code to get the names of the groups in a group/file

void
H5GgetGroupNames( groupID, groupName )
	hid_t	groupID
	char * 	groupName
PREINIT:
	int	groupCount = 0;
	char ** groupNames;     /* Array of group names */
	char ** groupPtr;    /* temp pointer to groupnames */
	int i; 	               /* Index variable */
PPCODE:
	/* Get the number of datasets */
        H5Giterate(groupID, groupName, NULL, incIfGroup, &groupCount);
	
	if( groupCount > 0){ /* Groups found */
		
		/* Allocate Space  for array of strings */
		groupNames = (char **) malloc( groupCount * sizeof(char *));
		if( groupNames == NULL){
			printf("PDL::IO::HDF5; out of Memory in H5GgetGroupNames\n");
			exit(1);
		}
 
		groupPtr = groupNames;

		H5Giterate(groupID, groupName, NULL, getName_if_Group, &groupPtr);

		EXTEND(SP, groupCount); /* Make room for results on the return stack */
		for( i = 0; i< groupCount; i++){ /* Push Names onto return stack */
		       /*  printf("Name found = '%s'\n",datasetNames[i]); */
		       PUSHs(sv_2mortal(newSVpv(groupNames[i],0)));
		       free(groupNames[i]); /* Release Memory */
		}

		free(groupNames);
	}


# Code to get the maximum length of strings in a ragged character array
int
findMaxVarLenSize( buf, numelem )
	I8 *	buf
	int 	numelem
CODE:
	int     i;
	int     maxStrSize;
	int     len;
	char**  rdata; 
	/* Convert input generic pointer to character array */
	rdata = (char **) buf;

        /* Find max string length */
        maxStrSize = 0;
        for(i=0; i<numelem; i++) {
		if( rdata[i] ){ /* Ignore null entries */
                	/* printf("String %d = '%s'\n", i, rdata[i]); */
                	len = strlen(rdata[i]);
                	if( len > maxStrSize ) maxStrSize = len;
		}
        } /* end for */
        RETVAL = maxStrSize;
OUTPUT:
	RETVAL

	
# Function to copy the variable length strings from an input buffer varlenbuff to a supplied
#   fixed-length string buffer fixedbuf.
#    Number of elements (numelem) and maximum length of any variable length string (maxVarlensize)
#    must be supplied.
#  Output is the number of elements converted
int



( run in 1.309 second using v1.01-cache-2.11-cpan-5511b514fd6 )