00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "h5String.h"
00017 #include "hdf5.h"
00018 #include <assert.h>
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022
00023
00024 void h5str_new(h5str_t *str, size_t len)
00025 {
00026 if (str && len > 0)
00027 {
00028 str->s = (char *) malloc(len);
00029 str->max = len;
00030 str->s[0] = '\0';
00031 }
00032 }
00033
00034
00035 void h5str_free(h5str_t *str)
00036 {
00037 if (str && str->max>0)
00038 {
00039 free(str->s);
00040 memset(str, 0, sizeof(h5str_t));
00041 }
00042 }
00043
00044
00045 void h5str_empty(h5str_t *str)
00046 {
00047 if (str)
00048 str->s[0] = '\0';
00049 }
00050
00051
00052 void h5str_resize (h5str_t *str, size_t new_len)
00053 {
00054 char *new_str;
00055
00056 if (!str || new_len<=0 || str->max == new_len)
00057 return;
00058
00059 new_str = (char *)malloc(new_len);
00060 if (new_len > str->max)
00061 strcpy(new_str, str->s);
00062 else
00063 strncpy(new_str, str->s, new_len-1);
00064
00065 free(str->s);
00066 str->s = new_str;
00067 str->max = new_len;
00068 }
00069
00070
00071
00072
00073
00074 char* h5str_append (h5str_t *str, const char* cstr)
00075 {
00076 size_t len;
00077
00078 if (!str)
00079 return NULL;
00080 else if (!cstr)
00081 return str->s;
00082
00083 len = strlen(str->s) + strlen(cstr);
00084 while (len >= str->max)
00085 {
00086 h5str_resize(str, str->max*2);
00087 }
00088
00089 if (strlen(str->s) == 0)
00090 strcpy(str->s, cstr);
00091 else
00092 strcat(str->s, cstr);
00093
00094 return str->s;
00095 }
00096
00097
00098
00099
00100
00101
00102 int h5str_sprintf(h5str_t *str, hid_t tid, void *ptr, const char* compound_delim)
00103 {
00104 char tmp_char = 0;
00105 short tmp_short = 0;
00106 int tmp_int = 0;
00107 long tmp_long = 0;
00108 long long tmp_long_long = 0;
00109 unsigned char tmp_uchar = 0;
00110 unsigned short tmp_ushort = 0;
00111 unsigned int tmp_uint = 0;
00112 unsigned long tmp_ulong = 0;
00113 unsigned long long tmp_ulong_long = 0;
00114 float tmp_float = 0;
00115 double tmp_double = 0.0;
00116 long double tmp_long_double=0.0;
00117
00118 size_t offset, size;
00119 char *cptr = (char*)ptr;
00120 unsigned char *ucptr = (unsigned char*)ptr;
00121 char *this_str;
00122 int i, n;
00123 hid_t mtid = -1;
00124 H5T_class_t tclass = H5Tget_class(tid);
00125 hvl_t *vlptr;
00126
00127 if (!str || !ptr)
00128 return -1;
00129
00130 this_str = NULL;
00131 if (H5Tequal(tid, H5T_NATIVE_CHAR))
00132 {
00133 this_str = (char*)malloc(7);
00134 memcpy(&tmp_char, ptr, 1);
00135 sprintf(this_str, "%d", tmp_char);
00136 } else if (H5Tequal(tid, H5T_NATIVE_UCHAR))
00137 {
00138 this_str = (char*)malloc(7);
00139 memcpy(&tmp_uchar, ptr, 1);
00140 sprintf(this_str, "%u", tmp_uchar);
00141 } else if (H5Tequal(tid, H5T_NATIVE_SHORT))
00142 {
00143 this_str = (char*)malloc(9);
00144 memcpy(&tmp_short, ptr, 2);
00145 sprintf(this_str, "%d", tmp_short);
00146 } else if (H5Tequal(tid, H5T_NATIVE_USHORT))
00147 {
00148 this_str = (char*)malloc(9);
00149 memcpy(&tmp_ushort, ptr, 2);
00150 sprintf(this_str, "%u", tmp_ushort);
00151 } else if (H5Tequal(tid, H5T_NATIVE_INT))
00152 {
00153 this_str = (char*)malloc(14);
00154 memcpy(&tmp_int, ptr, 4);
00155 sprintf(this_str, "%d", tmp_int);
00156 } else if (H5Tequal(tid, H5T_NATIVE_UINT))
00157 {
00158 this_str = (char*)malloc(14);
00159 memcpy(&tmp_uint, ptr, 4);
00160 sprintf(this_str, "%u", tmp_uint);
00161 } else if (H5Tequal(tid, H5T_NATIVE_LONG)) {
00162 this_str = (char*)malloc(23);
00163 memcpy(&tmp_long, ptr, sizeof(long));
00164 sprintf(this_str, "%ld", tmp_long);
00165 } else if (H5Tequal(tid, H5T_NATIVE_ULONG))
00166 {
00167 this_str = (char*)malloc(23);
00168 memcpy(&tmp_ulong, ptr, sizeof(unsigned long));
00169 sprintf(this_str, "%lu", tmp_ulong);
00170 } else if (H5Tequal(tid, H5T_NATIVE_LLONG)) {
00171 this_str = (char*)malloc(23);
00172 memcpy(&tmp_long_long, ptr, sizeof(long long));
00173 sprintf(this_str, "%lld", tmp_long_long);
00174 } else if (H5Tequal(tid, H5T_NATIVE_ULLONG))
00175 {
00176 this_str = (char*)malloc(23);
00177 memcpy(&tmp_ulong_long, ptr, sizeof(unsigned long long));
00178 sprintf(this_str, "%llu", tmp_ulong_long);
00179 } else if (H5Tequal(tid, H5T_NATIVE_FLOAT))
00180 {
00181 this_str = (char*)malloc(25);
00182 memcpy(&tmp_float, ptr, sizeof(float));
00183 sprintf(this_str, "%f", tmp_float);
00184 } else if (H5Tequal(tid, H5T_NATIVE_DOUBLE)) {
00185 this_str = (char*)malloc(25);
00186 memcpy(&tmp_double, ptr, sizeof(double));
00187 sprintf(this_str, "%f", tmp_double);
00188 } else if (H5Tequal(tid, H5T_NATIVE_LDOUBLE)) {
00189 this_str = (char*)malloc(25);
00190 memcpy(&tmp_long_double, ptr, sizeof(long double));
00191 sprintf(this_str, "%Lf", tmp_long_double);
00192 } else if (tclass == H5T_STRING)
00193 {
00194 char *tmp_str;
00195 size = 0;
00196
00197 if(H5Tis_variable_str(tid))
00198 {
00199 tmp_str = *(char**)ptr;
00200 if(tmp_str) size = strlen(tmp_str);
00201 } else
00202 {
00203 tmp_str = cptr;
00204 size = H5Tget_size(tid);
00205 }
00206
00207 if (size > 0)
00208 {
00209 this_str = (char *)malloc(size+1);
00210 strncpy(this_str, tmp_str, size);
00211 this_str[size] = '\0';
00212 }
00213 } else if (tclass == H5T_COMPOUND)
00214 {
00215 n = H5Tget_nmembers(tid);
00216 h5str_append(str, " {");
00217
00218 for (i = 0; i < n; i++)
00219 {
00220 offset = H5Tget_member_offset(tid, (unsigned)i);
00221 mtid = H5Tget_member_type(tid, (unsigned)i);
00222 h5str_sprintf(str, mtid, cptr+offset, ", ");
00223 if (i < n-1) h5str_append(str, compound_delim);
00224 H5Tclose(mtid);
00225 }
00226 h5str_append(str, "}");
00227 } else if (tclass == H5T_ARRAY)
00228 {
00229 int rank=0;
00230 hsize_t dims[H5S_MAX_RANK];
00231 int total_elmts;
00232
00233 h5str_append(str, "[");
00234
00235 mtid = H5Tget_super(tid);
00236 size = H5Tget_size(mtid);
00237 rank = H5Tget_array_ndims(tid);
00238 H5Tget_array_dims(tid, dims, NULL);
00239
00240 total_elmts = 1;
00241 for (i=0; i<rank; i++)
00242 total_elmts *= dims[i];
00243
00244 for (i = 0; i < total_elmts; i++)
00245 {
00246 h5str_sprintf(str, mtid, cptr + i * size, compound_delim);
00247 if (i<total_elmts-1)
00248 strcat(str->s, ", ");
00249 }
00250 H5Tclose(mtid);
00251 h5str_append(str, "]");
00252 } else if (tclass == H5T_VLEN)
00253 {
00254 mtid = H5Tget_super(tid);
00255 size = H5Tget_size(mtid);
00256
00257 vlptr = (hvl_t *)cptr;
00258
00259 n = vlptr->len;
00260 for (i = 0; i < n; i++)
00261 {
00262 h5str_sprintf(str, mtid, ((char *)(vlptr->p)) + i * size, compound_delim);
00263 if (i<n-1)
00264 strcat(str->s, ", ");
00265 }
00266 H5Tclose(mtid);
00267 } else
00268 {
00269 n = H5Tget_size(tid);
00270 this_str = (char*)malloc((size_t)4*(n+1));
00271
00272 if (1==n)
00273 {
00274 sprintf(this_str, "0x%02x", ucptr[0]);
00275 } else
00276 {
00277 for (i = 0; i < n; i++)
00278 sprintf(this_str, "%s%02x", i?":":"", ucptr[i]);
00279 }
00280
00281 }
00282
00283 if (this_str)
00284 {
00285 h5str_append(str, this_str);
00286 free (this_str);
00287 }
00288
00289 return strlen(str->s);
00290 }
00291