<commit_before>/*---------------------------------------------------------------------------
  NeXus - Neutron & X-ray Common Data Format
  
  Application Program Interface (HDF5) Routines
  
  Copyright (C) 1997-2014 NIAC
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.
 
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
             
  For further information, see <http://www.nexusformat.org>

----------------------------------------------------------------------------*/

#define H5Aiterate_vers 2

#include <nxconfig.h>

#ifdef WITH_HDF5

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>

#include "napi.h"
#include "napi_internal.h"
#include "napi5.h"

#ifdef H5_VERSION_GE
#if !H5_VERSION_GE(1,8,0)
#error HDF5 Version must be 1.8.0 or higher
#endif
#endif

#ifdef _MSC_VER
#define snprintf _snprintf
#endif				/* _MSC_VER */

#define NX_UNKNOWN_GROUP ""	/* for when no NX_class attr */

extern void *NXpData;

typedef struct __NexusFile5 {
	struct iStack5 {
		char irefn[1024];
		int iVref;
		hsize_t iCurrentIDX;
	} iStack5[NXMAXSTACK];
	struct iStack5 iAtt5;
	int iFID;
	int iCurrentG;
	int iCurrentD;
	int iCurrentS;
	int iCurrentT;
	int iCurrentA;
	int iNX;
	int iNXID;
	int iStackPtr;
	char *iCurrentLGG;
	char *iCurrentLD;
	char name_ref[1024];
	char name_tmp[1024];
	char iAccess[2];
} NexusFile5, *pNexusFile5;

/* forward declaration of NX5closegroup in order to get rid of a nasty warning */

NXstatus NX5closegroup(NXhandle fid);

/*-------------------------------------------------------------------*/

static pNexusFile5 NXI5assert(NXhandle fid)
{
	pNexusFile5 pRes;

	assert(fid != NULL);
	pRes = (pNexusFile5) fid;
	assert(pRes->iNXID == NX5SIGNATURE);
	return pRes;
}

/*--------------------------------------------------------------------*/

static void NXI5KillDir(pNexusFile5 self)
{
	self->iStack5[self->iStackPtr].iCurrentIDX = 0;
}

static herr_t readStringAttribute(hid_t attr, char **data)
{
	herr_t iRet = 0;
	hid_t atype = -1;
	hid_t space;
	int ndims;
	hsize_t thedims[H5S_MAX_RANK], sdim;

	atype = H5Aget_type(attr);
	sdim = H5Tget_size(atype);	
	space = H5Aget_space(attr);
	ndims = H5Sget_simple_extent_dims(space, thedims, NULL);

	if (ndims == 0) {
	       if (H5Tis_variable_str(atype)) {
	           hid_t btype = H5Tget_native_type(atype, H5T_DIR_ASCEND); 
	           iRet = H5Aread(attr, btype, data);
	           H5Tclose(btype);
	       } else {
			*data = malloc(sdim+1);
			iRet = H5Aread(attr, atype, *data);
			(*data)[sdim] = '\0';
		}
	} else if (ndims == 1) {
		int i;
		char **strings;
		
		strings = (char **) malloc(thedims[0] * sizeof(char*));

		if (! H5Tis_variable_str(atype)) {
			strings[0] = (char *) malloc(thedims[0] * sdim * sizeof(char));
			for(i=1; i<thedims[0]; i++) {
				strings[i] = strings[0] + i * sdim;
			}
		}

                iRet = H5Aread(attr, atype, strings[0]);
		*data = malloc((sdim + 2) * thedims[0] * sizeof(char));
		for(i=0; i<thedims[0]; i++) {
			if (i==0) {
				strncpy(*data, strings[i], sdim);	
			} else {
				strcat(*data, ", ");
				strncat(*data, strings[i], sdim);
			}
		}
		if (H5Tis_variable_str(atype)) {
			H5Dvlen_reclaim(atype, space, H5P_DEFAULT, strings);
		} else {
			free(strings[0]);
		}

		free(strings);
	} else {
		*data = strdup(" higher dimensional string array");
	} <commit_msg>make sure strings are null terminated.<commit_after>/*---------------------------------------------------------------------------
  NeXus - Neutron & X-ray Common Data Format
  
  Application Program Interface (HDF5) Routines
  
  Copyright (C) 1997-2014 NIAC
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.
 
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
             
  For further information, see <http://www.nexusformat.org>

----------------------------------------------------------------------------*/

#define H5Aiterate_vers 2

#include <nxconfig.h>

#ifdef WITH_HDF5

#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>

#include "napi.h"
#include "napi_internal.h"
#include "napi5.h"

#ifdef H5_VERSION_GE
#if !H5_VERSION_GE(1,8,0)
#error HDF5 Version must be 1.8.0 or higher
#endif
#endif

#ifdef _MSC_VER
#define snprintf _snprintf
#endif				/* _MSC_VER */

#define NX_UNKNOWN_GROUP ""	/* for when no NX_class attr */

extern void *NXpData;

typedef struct __NexusFile5 {
	struct iStack5 {
		char irefn[1024];
		int iVref;
		hsize_t iCurrentIDX;
	} iStack5[NXMAXSTACK];
	struct iStack5 iAtt5;
	int iFID;
	int iCurrentG;
	int iCurrentD;
	int iCurrentS;
	int iCurrentT;
	int iCurrentA;
	int iNX;
	int iNXID;
	int iStackPtr;
	char *iCurrentLGG;
	char *iCurrentLD;
	char name_ref[1024];
	char name_tmp[1024];
	char iAccess[2];
} NexusFile5, *pNexusFile5;

/* forward declaration of NX5closegroup in order to get rid of a nasty warning */

NXstatus NX5closegroup(NXhandle fid);

/*-------------------------------------------------------------------*/

static pNexusFile5 NXI5assert(NXhandle fid)
{
	pNexusFile5 pRes;

	assert(fid != NULL);
	pRes = (pNexusFile5) fid;
	assert(pRes->iNXID == NX5SIGNATURE);
	return pRes;
}

/*--------------------------------------------------------------------*/

static void NXI5KillDir(pNexusFile5 self)
{
	self->iStack5[self->iStackPtr].iCurrentIDX = 0;
}

static herr_t readStringAttribute(hid_t attr, char **data)
{
	herr_t iRet = 0;
	hid_t atype = -1;
	hid_t space;
	int ndims;
	hsize_t thedims[H5S_MAX_RANK], sdim;

	atype = H5Aget_type(attr);
	sdim = H5Tget_size(atype);	
	space = H5Aget_space(attr);
	ndims = H5Sget_simple_extent_dims(space, thedims, NULL);

	if (ndims == 0) {
	       if (H5Tis_variable_str(atype)) {
	           hid_t btype = H5Tget_native_type(atype, H5T_DIR_ASCEND); 
	           iRet = H5Aread(attr, btype, data);
	           H5Tclose(btype);
	       } else {
			*data = malloc(sdim+1);
			iRet = H5Aread(attr, atype, *data);
			(*data)[sdim] = '\0';
		}
	} else if (ndims == 1) {
		int i;
		char **strings;
		
		strings = (char **) malloc(thedims[0] * sizeof(char*));

		if (! H5Tis_variable_str(atype)) {
			strings[0] = (char *) malloc(thedims[0] * sdim * sizeof(char));
			for(i=1; i<thedims[0]; i++) {
				strings[i] = strings[0] + i * sdim;
			}
		}

                iRet = H5Aread(attr, atype, strings[0]);
		*data = calloc((sdim + 2) * thedims[0], sizeof(char));
		for(i=0; i<thedims[0]; i++) {
			if (i==0) {
				strncpy(*data, strings[i], sdim);	
			} else {
				strcat(*data, ", ");
				strncat(*data, strings[i], sdim);
			}
		}
		if (H5Tis_variable_str(atype)) {
			H5Dvlen_reclaim(atype, space, H5P_DEFAULT, strings);
		} else {
			free(strings[0]);
		}

		free(strings);
	} else {
		*data = strdup(" higher dimensional string array");
	} 
{'fraction_non_alphanumeric': 0.18364003024955886, 'fraction_numerical': 0.025964204688681623, 'mean_word_length': 2.680426716141002, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '286122', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2862, 'n_words': 779}