diff options
Diffstat (limited to 'hl/src/H5LT.c')
-rw-r--r-- | hl/src/H5LT.c | 581 |
1 files changed, 400 insertions, 181 deletions
diff --git a/hl/src/H5LT.c b/hl/src/H5LT.c index 22bb97f..eeaceeb 100644 --- a/hl/src/H5LT.c +++ b/hl/src/H5LT.c @@ -18,16 +18,17 @@ #include <assert.h> #include <stdio.h> #include "H5LTprivate.h" - +#include "H5private.h" /* For Lex and Yacc */ #define COL 3 #define LIMIT 512 #define INCREMENT 1024 +#define TMP_LEN 256 #define MAX(a,b) (((a)>(b)) ? (a) : (b)) int input_len; char *myinput; -int indent = 0; +size_t indent = 0; @@ -1572,9 +1573,6 @@ out: } - - - /*------------------------------------------------------------------------- * Function: H5LTtext_to_dtype * @@ -1621,6 +1619,47 @@ out: } /*------------------------------------------------------------------------- +* Function: realloc_and_append +* +* Purpose: Expand the buffer and append a string to it. +* +* Return: void +* +* Programmer: Raymond Lu, songyulu@hdfgroup.org +* +* Date: 29 September 2011 +* +* Modifications: +* +*------------------------------------------------------------------------- +*/ +static char* +realloc_and_append(hbool_t _no_user_buf, size_t *len, char *buf, char *str_to_add) +{ + if(_no_user_buf) { + /* If the buffer isn't big enough, reallocate it. Otherwise, go to do strcat. */ + if(str_to_add && ((ssize_t)(*len - (strlen(buf) + strlen(str_to_add) + 1)) < LIMIT)) { + *len += ((strlen(buf) + strlen(str_to_add) + 1) / INCREMENT + 1) * INCREMENT; + buf = (char*)realloc(buf, *len); + } else if(!str_to_add && ((ssize_t)(*len - strlen(buf) - 1) < LIMIT)) { + *len += INCREMENT; + buf = (char*)realloc(buf, *len); + } + } + + if(!buf) + goto out; + + if(str_to_add) + strcat(buf, str_to_add); + + return buf; + +out: + return NULL; +} + +/*------------------------------------------------------------------------- * Function: indentation * * Purpose: Print spaces for indentation @@ -1635,16 +1674,24 @@ out: * *------------------------------------------------------------------------- */ -static void -indentation(int x, char* str) +static char* +indentation(size_t x, char* str, hbool_t no_u_buf, size_t *s_len) { + char tmp_str[TMP_LEN]; + if (x < 80) { - while (x-- > 0) - strcat(str, " "); - } else { - strcat(str, "error: the indentation exceeds the number of cols."); - exit(1); - } + memset(tmp_str, ' ', x); + tmp_str[x]='\0'; + } else + HDsnprintf(tmp_str, TMP_LEN, "error: the indentation exceeds the number of cols."); + + if(!(str = realloc_and_append(no_u_buf, s_len, str, tmp_str))) + goto out; + + return str; + +out: + return NULL; } /*------------------------------------------------------------------------- @@ -1659,21 +1706,20 @@ indentation(int x, char* str) * Modifications: * *-----------------------------------------------------------------------*/ -static herr_t -print_enum(hid_t type, char* str, int indt) +static char* +print_enum(hid_t type, char* str, size_t *str_len, hbool_t no_ubuf, size_t indt) { char **name = NULL; /*member names */ unsigned char *value = NULL; /*value array */ unsigned char *copy = NULL; /*a pointer to value array */ int nmembs; /*number of members */ - char tmp_str[256]; + char tmp_str[TMP_LEN]; int nchars; /*number of output characters */ hid_t super = -1; /*enum base integer type */ hid_t native = -1; /*native integer data type */ size_t super_size; /*enum base type size */ size_t dst_size; /*destination value type size */ int i; - herr_t ret = SUCCEED; if((nmembs = H5Tget_nmembers(type))==0) goto out; @@ -1718,27 +1764,28 @@ print_enum(hid_t type, char* str, int indt) /* Print members */ for (i = 0; i < nmembs; i++) { - indentation(indt + COL, str); - nchars = sprintf(tmp_str, "\"%s\"", name[i]); - strcat(str, tmp_str); - sprintf(tmp_str, "%*s ", MAX(0, 16 - nchars), ""); - strcat(str, tmp_str); - - if (H5T_SGN_NONE == H5Tget_sign(native)) { - /*On SGI Altix(cobalt), wrong values were printed out with "value+i*dst_size" - *strangely, unless use another pointer "copy".*/ - copy = value+i*dst_size; - sprintf(tmp_str,"%u", *((unsigned int*)((void *)copy))); - strcat(str, tmp_str); - } else { - /*On SGI Altix(cobalt), wrong values were printed out with "value+i*dst_size" - *strangely, unless use another pointer "copy".*/ - copy = value+i*dst_size; - sprintf(tmp_str,"%d", *((int*)((void *)copy))); - strcat(str, tmp_str); - } + if(!(str = indentation(indt + COL, str, no_ubuf, str_len))) + goto out; + nchars = HDsnprintf(tmp_str, TMP_LEN, "\"%s\"", name[i]); + if(!(str = realloc_and_append(no_ubuf, str_len, str, tmp_str))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "%*s ", MAX(0, 16 - nchars), ""); + if(!(str = realloc_and_append(no_ubuf, str_len, str, tmp_str))) + goto out; + + /*On SGI Altix(cobalt), wrong values were printed out with "value+i*dst_size" + *strangely, unless use another pointer "copy".*/ + copy = value+i*dst_size; + if (H5T_SGN_NONE == H5Tget_sign(native)) + HDsnprintf(tmp_str, TMP_LEN, "%u", *((unsigned int*)((void *)copy))); + else + HDsnprintf(tmp_str, TMP_LEN, "%d", *((int*)((void *)copy))); + if(!(str = realloc_and_append(no_ubuf, str_len, str, tmp_str))) + goto out; - strcat(str, ";\n"); + HDsnprintf(tmp_str, TMP_LEN, ";\n"); + if(!(str = realloc_and_append(no_ubuf, str_len, str, tmp_str))) + goto out; } /* Release resources */ @@ -1749,13 +1796,13 @@ print_enum(hid_t type, char* str, int indt) free(value); H5Tclose(super); - return ret; + return str; out: if(0 == nmembs) { - sprintf(tmp_str, "\n%*s <empty>", indt + 4, ""); - strcat(str, tmp_str); + HDsnprintf(tmp_str, TMP_LEN, "\n%*s <empty>", indt + 4, ""); + str = realloc_and_append(no_ubuf, str_len, str, tmp_str); } /* end if */ /* Release resources */ @@ -1772,7 +1819,7 @@ out: if(super >= 0) H5Tclose(super); - return FAIL; + return NULL; } /*------------------------------------------------------------------------- @@ -1796,7 +1843,7 @@ herr_t H5LTdtype_to_text(hid_t dtype, char *str, H5LT_lang_t lang_type, size_t * { size_t str_len = INCREMENT; char *text_str; - herr_t ret = -1; + herr_t ret = SUCCEED; if(lang_type <= H5LT_LANG_ERR || lang_type >= H5LT_NO_LANG) goto out; @@ -1804,12 +1851,14 @@ herr_t H5LTdtype_to_text(hid_t dtype, char *str, H5LT_lang_t lang_type, size_t * if(len && !str) { text_str = (char*)calloc(str_len, sizeof(char)); text_str[0]='\0'; - if((ret = H5LT_dtype_to_text(dtype, &text_str, lang_type, &str_len, 1)) < 0) + if(!(text_str = H5LT_dtype_to_text(dtype, text_str, lang_type, &str_len, 1))) goto out; *len = strlen(text_str) + 1; - free(text_str); + if(text_str) + free(text_str); + text_str = NULL; } else if(len && str) { - if((ret = H5LT_dtype_to_text(dtype, &str, lang_type, len, 0)) < 0) + if(!(H5LT_dtype_to_text(dtype, str, lang_type, len, 0))) goto out; str[*len-1] = '\0'; } @@ -1837,26 +1886,18 @@ out: * *------------------------------------------------------------------------- */ -herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t *slen, +char* H5LT_dtype_to_text(hid_t dtype, char *dt_str, H5LT_lang_t lang, size_t *slen, hbool_t no_user_buf) { H5T_class_t tcls; - char tmp_str[256]; - char *tmp; + char tmp_str[TMP_LEN]; int i; - herr_t ret = SUCCEED; - - if(no_user_buf && ((*slen - strlen(*dt_str)) < LIMIT)) { - *slen += INCREMENT; - tmp = (char*)realloc(*dt_str, *slen); - if(tmp != *dt_str) { - free(*dt_str); - *dt_str = tmp; - } - } + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, NULL))) + goto out; + if(lang != H5LT_DDL) { - sprintf(*dt_str, "only DDL is supported for now"); + HDsnprintf(dt_str, *slen, "only DDL is supported for now"); goto out; } @@ -1866,81 +1907,81 @@ herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t * switch (tcls) { case H5T_INTEGER: if (H5Tequal(dtype, H5T_STD_I8BE)) { - sprintf(*dt_str, "H5T_STD_I8BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I8BE"); } else if (H5Tequal(dtype, H5T_STD_I8LE)) { - sprintf(*dt_str, "H5T_STD_I8LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I8LE"); } else if (H5Tequal(dtype, H5T_STD_I16BE)) { - sprintf(*dt_str, "H5T_STD_I16BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I16BE"); } else if (H5Tequal(dtype, H5T_STD_I16LE)) { - sprintf(*dt_str, "H5T_STD_I16LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I16LE"); } else if (H5Tequal(dtype, H5T_STD_I32BE)) { - sprintf(*dt_str, "H5T_STD_I32BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I32BE"); } else if (H5Tequal(dtype, H5T_STD_I32LE)) { - sprintf(*dt_str, "H5T_STD_I32LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I32LE"); } else if (H5Tequal(dtype, H5T_STD_I64BE)) { - sprintf(*dt_str, "H5T_STD_I64BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I64BE"); } else if (H5Tequal(dtype, H5T_STD_I64LE)) { - sprintf(*dt_str, "H5T_STD_I64LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_I64LE"); } else if (H5Tequal(dtype, H5T_STD_U8BE)) { - sprintf(*dt_str, "H5T_STD_U8BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U8BE"); } else if (H5Tequal(dtype, H5T_STD_U8LE)) { - sprintf(*dt_str, "H5T_STD_U8LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U8LE"); } else if (H5Tequal(dtype, H5T_STD_U16BE)) { - sprintf(*dt_str, "H5T_STD_U16BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U16BE"); } else if (H5Tequal(dtype, H5T_STD_U16LE)) { - sprintf(*dt_str, "H5T_STD_U16LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U16LE"); } else if (H5Tequal(dtype, H5T_STD_U32BE)) { - sprintf(*dt_str, "H5T_STD_U32BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U32BE"); } else if (H5Tequal(dtype, H5T_STD_U32LE)) { - sprintf(*dt_str, "H5T_STD_U32LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U32LE"); } else if (H5Tequal(dtype, H5T_STD_U64BE)) { - sprintf(*dt_str, "H5T_STD_U64BE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U64BE"); } else if (H5Tequal(dtype, H5T_STD_U64LE)) { - sprintf(*dt_str, "H5T_STD_U64LE"); + HDsnprintf(dt_str, *slen, "H5T_STD_U64LE"); } else if (H5Tequal(dtype, H5T_NATIVE_SCHAR)) { - sprintf(*dt_str, "H5T_NATIVE_SCHAR"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_SCHAR"); } else if (H5Tequal(dtype, H5T_NATIVE_UCHAR)) { - sprintf(*dt_str, "H5T_NATIVE_UCHAR"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_UCHAR"); } else if (H5Tequal(dtype, H5T_NATIVE_SHORT)) { - sprintf(*dt_str, "H5T_NATIVE_SHORT"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_SHORT"); } else if (H5Tequal(dtype, H5T_NATIVE_USHORT)) { - sprintf(*dt_str, "H5T_NATIVE_USHORT"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_USHORT"); } else if (H5Tequal(dtype, H5T_NATIVE_INT)) { - sprintf(*dt_str, "H5T_NATIVE_INT"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_INT"); } else if (H5Tequal(dtype, H5T_NATIVE_UINT)) { - sprintf(*dt_str, "H5T_NATIVE_UINT"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_UINT"); } else if (H5Tequal(dtype, H5T_NATIVE_LONG)) { - sprintf(*dt_str, "H5T_NATIVE_LONG"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_LONG"); } else if (H5Tequal(dtype, H5T_NATIVE_ULONG)) { - sprintf(*dt_str, "H5T_NATIVE_ULONG"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_ULONG"); } else if (H5Tequal(dtype, H5T_NATIVE_LLONG)) { - sprintf(*dt_str, "H5T_NATIVE_LLONG"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_LLONG"); } else if (H5Tequal(dtype, H5T_NATIVE_ULLONG)) { - sprintf(*dt_str, "H5T_NATIVE_ULLONG"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_ULLONG"); } else { - sprintf(*dt_str, "undefined integer"); + HDsnprintf(dt_str, *slen, "undefined integer"); } break; case H5T_FLOAT: if (H5Tequal(dtype, H5T_IEEE_F32BE)) { - sprintf(*dt_str, "H5T_IEEE_F32BE"); + HDsnprintf(dt_str, *slen, "H5T_IEEE_F32BE"); } else if (H5Tequal(dtype, H5T_IEEE_F32LE)) { - sprintf(*dt_str, "H5T_IEEE_F32LE"); + HDsnprintf(dt_str, *slen, "H5T_IEEE_F32LE"); } else if (H5Tequal(dtype, H5T_IEEE_F64BE)) { - sprintf(*dt_str, "H5T_IEEE_F64BE"); + HDsnprintf(dt_str, *slen, "H5T_IEEE_F64BE"); } else if (H5Tequal(dtype, H5T_IEEE_F64LE)) { - sprintf(*dt_str, "H5T_IEEE_F64LE"); + HDsnprintf(dt_str, *slen, "H5T_IEEE_F64LE"); } else if (H5Tequal(dtype, H5T_NATIVE_FLOAT)) { - sprintf(*dt_str, "H5T_NATIVE_FLOAT"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_FLOAT"); } else if (H5Tequal(dtype, H5T_NATIVE_DOUBLE)) { - sprintf(*dt_str, "H5T_NATIVE_DOUBLE"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_DOUBLE"); #if H5_SIZEOF_LONG_DOUBLE !=0 } else if (H5Tequal(dtype, H5T_NATIVE_LDOUBLE)) { - sprintf(*dt_str, "H5T_NATIVE_LDOUBLE"); + HDsnprintf(dt_str, *slen, "H5T_NATIVE_LDOUBLE"); #endif } else { - sprintf(*dt_str, "undefined float"); + HDsnprintf(dt_str, *slen, "undefined float"); } break; @@ -1969,38 +2010,47 @@ herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t * goto out; /* Print lead-in */ - sprintf(*dt_str, "H5T_STRING {\n"); + HDsnprintf(dt_str, *slen, "H5T_STRING {\n"); indent += COL; - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; if(is_vlstr) - strcat(*dt_str, "STRSIZE H5T_VARIABLE;\n"); - else { - sprintf(tmp_str, "STRSIZE %d;\n", (int)size); - strcat(*dt_str, tmp_str); - } + HDsnprintf(tmp_str, TMP_LEN, "STRSIZE H5T_VARIABLE;\n"); + else + HDsnprintf(tmp_str, TMP_LEN, "STRSIZE %d;\n", (int)size); - indentation(indent + COL, *dt_str); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; + + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; if (str_pad == H5T_STR_NULLTERM) - strcat(*dt_str, "STRPAD H5T_STR_NULLTERM;\n"); + HDsnprintf(tmp_str, TMP_LEN, "STRPAD H5T_STR_NULLTERM;\n"); else if (str_pad == H5T_STR_NULLPAD) - strcat(*dt_str, "STRPAD H5T_STR_NULLPAD;\n"); + HDsnprintf(tmp_str, TMP_LEN, "STRPAD H5T_STR_NULLPAD;\n"); else if (str_pad == H5T_STR_SPACEPAD) - strcat(*dt_str, "STRPAD H5T_STR_SPACEPAD;\n"); + HDsnprintf(tmp_str, TMP_LEN, "STRPAD H5T_STR_SPACEPAD;\n"); else - strcat(*dt_str, "STRPAD H5T_STR_ERROR;\n"); + HDsnprintf(tmp_str, TMP_LEN, "STRPAD H5T_STR_ERROR;\n"); - indentation(indent + COL, *dt_str); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; + + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; if (cset == H5T_CSET_ASCII) - strcat(*dt_str, "CSET H5T_CSET_ASCII;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CSET H5T_CSET_ASCII;\n"); else if (cset == H5T_CSET_UTF8) - strcat(*dt_str, "CSET H5T_CSET_UTF8;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CSET H5T_CSET_UTF8;\n"); else - strcat(*dt_str, "CSET unknown;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CSET unknown;\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; /* Reproduce a C type string */ if((str_type = H5Tcopy(H5T_C_S1)) < 0) @@ -2017,11 +2067,14 @@ herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t * if(H5Tset_strpad(str_type, str_pad) < 0) goto out; - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; /* Check C variable-length string first. Are the two types equal? */ if (H5Tequal(tmp_type, str_type)) { - strcat(*dt_str, "CTYPE H5T_C_S1;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CTYPE H5T_C_S1;\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; goto next; } @@ -2037,7 +2090,9 @@ herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t * } if (H5Tequal(tmp_type, str_type)) { - strcat(*dt_str, "H5T_C_S1;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CTYPE H5T_C_S1;\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; goto next; } @@ -2056,7 +2111,9 @@ herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t * /* Are the two types equal? */ if (H5Tequal(tmp_type, str_type)) { - strcat(*dt_str, "CTYPE H5T_FORTRAN_S1;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CTYPE H5T_FORTRAN_S1;\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; goto next; } @@ -2073,12 +2130,16 @@ herr_t H5LT_dtype_to_text(hid_t dtype, char **dt_str, H5LT_lang_t lang, size_t * /* Are the two types equal? */ if (H5Tequal(tmp_type, str_type)) { - strcat(*dt_str, "CTYPE H5T_FORTRAN_S1;\n"); + HDsnprintf(tmp_str, TMP_LEN, "CTYPE H5T_FORTRAN_S1;\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; goto next; } /* Type doesn't match any of above. */ - strcat(*dt_str, "CTYPE unknown_one_character_type;\n "); + HDsnprintf(tmp_str, TMP_LEN, "CTYPE unknown_one_character_type;\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; next: H5Tclose(str_type); @@ -2086,36 +2147,48 @@ next: /* Print closing */ indent -= COL; - indentation(indent + COL, *dt_str); - strcat(*dt_str, "}"); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "}"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; break; } case H5T_OPAQUE: { - char *tag; + char *tag = NULL; /* Print lead-in */ - sprintf(*dt_str, "H5T_OPAQUE {\n"); + HDsnprintf(dt_str, *slen, "H5T_OPAQUE {\n"); indent += COL; - indentation(indent + COL, *dt_str); - sprintf(tmp_str, "OPQ_SIZE %lu;\n", (unsigned long)H5Tget_size(dtype)); - strcat(*dt_str, tmp_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "OPQ_SIZE %lu;\n", (unsigned long)H5Tget_size(dtype)); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; tag = H5Tget_tag(dtype); if(tag) { - sprintf(tmp_str, "OPQ_TAG \"%s\";\n", tag); - free(tag); + HDsnprintf(tmp_str, TMP_LEN, "OPQ_TAG \"%s\";\n", tag); + if(tag) + free(tag); + tag = NULL; } else - sprintf(tmp_str, "OPQ_TAG \"\";\n"); - strcat(*dt_str, tmp_str); + HDsnprintf(tmp_str, TMP_LEN, "OPQ_TAG \"\";\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; /* Print closing */ indent -= COL; - indentation(indent + COL, *dt_str); - strcat(*dt_str, "}"); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "}"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; break; } @@ -2123,12 +2196,13 @@ next: { hid_t super; size_t super_len; - char* stmp; + char* stmp = NULL; /* Print lead-in */ - sprintf(*dt_str, "H5T_ENUM {\n"); + HDsnprintf(dt_str, *slen, "H5T_ENUM {\n"); indent += COL; - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; if((super = H5Tget_super(dtype)) < 0) goto out; @@ -2136,19 +2210,29 @@ next: goto out; stmp = (char*)calloc(super_len, sizeof(char)); if(H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) + goto out; + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) + goto out; + + if(stmp) + free(stmp); + stmp = NULL; + + HDsnprintf(tmp_str, TMP_LEN, ";\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) goto out; - strcat(*dt_str, stmp); - free(stmp); - strcat(*dt_str, ";\n"); H5Tclose(super); - if(print_enum(dtype, *dt_str, indent) < 0) + if(!(dt_str = print_enum(dtype, dt_str, slen, no_user_buf, indent))) goto out; /* Print closing */ indent -= COL; - indentation(indent + COL, *dt_str); - strcat(*dt_str, "}"); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "}"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; break; } @@ -2156,12 +2240,13 @@ next: { hid_t super; size_t super_len; - char* stmp; + char* stmp = NULL; /* Print lead-in */ - sprintf(*dt_str, "H5T_VLEN {\n"); + HDsnprintf(dt_str, *slen, "H5T_VLEN {\n"); indent += COL; - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; if((super = H5Tget_super(dtype)) < 0) goto out; @@ -2170,15 +2255,24 @@ next: stmp = (char*)calloc(super_len, sizeof(char)); if(H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) goto out; - strcat(*dt_str, stmp); - free(stmp); - strcat(*dt_str, "\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) + goto out; + + if(stmp) + free(stmp); + stmp = NULL; + HDsnprintf(tmp_str, TMP_LEN, "\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; H5Tclose(super); /* Print closing */ indent -= COL; - indentation(indent + COL, *dt_str); - strcat(*dt_str, "}"); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "}"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; break; } @@ -2186,14 +2280,15 @@ next: { hid_t super; size_t super_len; - char* stmp; + char* stmp = NULL; hsize_t dims[H5S_MAX_RANK]; int ndims; /* Print lead-in */ - sprintf(*dt_str, "H5T_ARRAY {\n"); + HDsnprintf(dt_str, *slen, "H5T_ARRAY {\n"); indent += COL; - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; /* Get array information */ if((ndims = H5Tget_array_ndims(dtype)) < 0) @@ -2203,10 +2298,13 @@ next: /* Print array dimensions */ for (i = 0; i < ndims; i++) { - sprintf(tmp_str, "[%d]", (int) dims[i]); - strcat(*dt_str, tmp_str); + HDsnprintf(tmp_str, TMP_LEN, "[%d]", (int) dims[i]); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; } - strcat(*dt_str, " "); + HDsnprintf(tmp_str, TMP_LEN, " "); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; if((super = H5Tget_super(dtype)) < 0) goto out; @@ -2215,32 +2313,40 @@ next: stmp = (char*)calloc(super_len, sizeof(char)); if(H5LTdtype_to_text(super, stmp, lang, &super_len) < 0) goto out; - strcat(*dt_str, stmp); - free(stmp); - strcat(*dt_str, "\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, stmp))) + goto out; + if(stmp) + free(stmp); + stmp = NULL; + HDsnprintf(tmp_str, TMP_LEN, "\n"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; H5Tclose(super); /* Print closing */ indent -= COL; - indentation(indent + COL, *dt_str); - strcat(*dt_str, "}"); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "}"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; break; } case H5T_COMPOUND: { - char *mname; + char *mname = NULL; hid_t mtype; size_t moffset; H5T_class_t mclass; size_t mlen; - char* mtmp; + char* mtmp = NULL; int nmembs; if((nmembs = H5Tget_nmembers(dtype)) < 0) goto out; - sprintf(*dt_str, "H5T_COMPOUND {\n"); + HDsnprintf(dt_str, *slen, "H5T_COMPOUND {\n"); indent += COL; for (i = 0; i < nmembs; i++) { @@ -2249,7 +2355,8 @@ next: if((mtype = H5Tget_member_type(dtype, (unsigned)i)) < 0) goto out; moffset = H5Tget_member_offset(dtype, (unsigned)i); - indentation(indent + COL, *dt_str); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; if((mclass = H5Tget_class(mtype)) < 0) goto out; @@ -2261,47 +2368,53 @@ next: mtmp = (char*)calloc(mlen, sizeof(char)); if(H5LTdtype_to_text(mtype, mtmp, lang, &mlen) < 0) goto out; - strcat(*dt_str, mtmp); - free(mtmp); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, mtmp))) + goto out; + if(mtmp) + free(mtmp); + mtmp = NULL; if (H5T_COMPOUND == mclass) indent -= COL; - sprintf(tmp_str, " \"%s\"", mname); - strcat(*dt_str, tmp_str); - free(mname); + HDsnprintf(tmp_str, TMP_LEN, " \"%s\"", mname); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; + if(mname) + free(mname); + mname = NULL; - sprintf(tmp_str, " : %lu;\n", (unsigned long)moffset); - strcat(*dt_str, tmp_str); + HDsnprintf(tmp_str, TMP_LEN, " : %lu;\n", (unsigned long)moffset); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; } /* Print closing */ indent -= COL; - indentation(indent + COL, *dt_str); - strcat(*dt_str, "}"); + if(!(dt_str = indentation(indent + COL, dt_str, no_user_buf, slen))) + goto out; + HDsnprintf(tmp_str, TMP_LEN, "}"); + if(!(dt_str = realloc_and_append(no_user_buf, slen, dt_str, tmp_str))) + goto out; break; } case H5T_TIME: - sprintf(*dt_str, "H5T_TIME: not yet implemented"); + HDsnprintf(dt_str, *slen, "H5T_TIME: not yet implemented"); break; case H5T_BITFIELD: - sprintf(*dt_str, "H5T_BITFIELD: not yet implemented"); + HDsnprintf(dt_str, *slen, "H5T_BITFIELD: not yet implemented"); break; default: - sprintf(*dt_str, "unknown data type"); + HDsnprintf(dt_str, *slen, "unknown data type"); } - return ret; + return dt_str; out: - return FAIL; + return NULL; } - - - - /*------------------------------------------------------------------------- * * Get attribute functions @@ -2917,4 +3030,110 @@ out: } - +htri_t +H5LTpath_valid(hid_t loc_id, const char *path, hbool_t check_object_valid) + { + char *tmp_path = NULL; /* Temporary copy of the path */ + char *curr_name; /* Pointer to current component of path name */ + char *delimit; /* Pointer to path delimiter during traversal */ + H5I_type_t obj_type; + htri_t link_exists, obj_exists; + size_t path_length; + htri_t ret_value; + + /* Initialize */ + ret_value = FALSE; + + /* Find the type of loc_id */ + if((obj_type = H5Iget_type(loc_id)) == H5I_BADID) { + ret_value = FAIL; + goto done; + } + + /* Find the length of the path */ + path_length = HDstrlen(path); + + /* Check if the identifier is the object itself, i.e. path is '.' */ + if(HDstrncmp(path, ".", path_length) == 0) { + if(check_object_valid) { + obj_exists = H5Oexists_by_name(loc_id, path, H5P_DEFAULT); + ret_value = obj_exists; + goto done; + } else { + ret_value = TRUE; /* Since the object is the identifier itself, + * we can only check if loc_id is a valid type */ + goto done; + } + } + + /* Duplicate the path to use */ + if(NULL == (tmp_path = HDstrdup(path))) { + ret_value = FAIL; + goto done; + } + + curr_name = tmp_path; + + /* check if absolute pathname */ + if(HDstrncmp(path, "/", 1) == 0) curr_name++; + + /* check if relative path name starts with "./" */ + if(HDstrncmp(path, "./", 2) == 0) curr_name += 2; + + while((delimit=HDstrchr(curr_name,'/'))!=NULL) { + /* Change the delimiter to terminate the string */ + *delimit='\0'; + + obj_exists = FALSE; + if((link_exists = H5Lexists(loc_id, tmp_path, H5P_DEFAULT)) < 0) { + ret_value = FAIL; + goto done; + } + + /* If target link does not exist then no reason to + * continue checking the path */ + if(link_exists != TRUE) { + ret_value = FALSE; + goto done; + } + + /* Determine if link resolves to an actual object */ + if((obj_exists = H5Oexists_by_name(loc_id, tmp_path, H5P_DEFAULT)) < 0) { + ret_value = FAIL; + goto done; + } + + if(obj_exists != TRUE) + break; + + /* Change the delimiter back to '/' */ + *delimit='/'; + + /* Advance the pointer in the path to the start of the next component */ + curr_name = delimit + 1; + + } /* end while */ + + /* Should be pointing to the last component in the path name now... */ + + /* Check if link does not exist */ + if((link_exists = H5Lexists(loc_id, tmp_path, H5P_DEFAULT)) < 0) { + ret_value = FAIL; + } else { + ret_value = link_exists; + /* Determine if link resolves to an actual object for check_object_valid TRUE */ + if(check_object_valid == TRUE && link_exists == TRUE) { + if((obj_exists = H5Oexists_by_name(loc_id, tmp_path, H5P_DEFAULT)) < 0) { + ret_value = FAIL; + } else { + ret_value = obj_exists; + } + } + } + +done: + if(tmp_path != NULL) + HDfree(tmp_path); + + return ret_value; + } |