Пример #1
0
char* test_utf_8() {
  hid_t file, node, unode, dset;
  size_t len;
  char res, *path, *attr, **dset_val;

  file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
  mu_assert("H5Fopen() open file...", file >= 0);

  mu_assert("Valid node path...", AH5_path_valid(file, NODE_PATH));
  node = H5Oopen(file, NODE_PATH, H5P_DEFAULT);
  mu_assert("H5Oopen() open node...", node >= 0);

  mu_assert("Valid unode path...", AH5_path_valid(node, UNODE_PATH));
  unode = H5Oopen(node, UNODE_PATH, H5P_DEFAULT);
  mu_assert("H5Oopen() open unode...", unode >= 0);

  H5Oclose(unode);

  path = (char*) malloc(sizeof(char) * (1 + strlen(NODE_PATH) + 1 + strlen(UNODE_PATH) + 1));
  path[0] = '/';
  path[1] = '\0';
  strcat(path, NODE_PATH);
  strcat(path, "/");
  strcat(path, UNODE_PATH);
  mu_assert("Valid unode absolute path...", AH5_path_valid(file, path));
  unode = H5Oopen(file, path, H5P_DEFAULT);
  mu_assert("H5Oopen() open unode...", unode >= 0);
  free(path);

  len = AH5_read_str_attr_len(unode, ".", "attribut du sous-nœud accentué");
  mu_assert("AH5_read_str_attr_len() read utf-8 attr len...", len > 0);

  attr = (char*) malloc(sizeof(char) * (len + 1));
  res = AH5_read_str_attr(unode, ".", "attribut du sous-nœud accentué", &attr);
  mu_assert("AH5_read_str_attr() status...", res == AH5_TRUE);
  mu_assert("AH5_read_str_attr() str length...", strlen(attr) == strlen("é è à œ"));
  mu_assert("AH5_read_str_attr() str value...", strcmp(attr, "é è à œ") == 0);
  free(attr);

  mu_assert("Valid dset path...", AH5_path_valid(unode, DSET_PATH));
  dset = H5Oopen(unode, DSET_PATH, H5P_DEFAULT);
  mu_assert("H5Oopen() open dset...", dset >= 0);
  len = 11;
  dset_val = (char**) malloc(sizeof(char*));
  dset_val[0] = (char*) malloc(sizeof(char) * (len + 1));
  res = AH5_read_str_dataset(dset, ".", 1, len, &dset_val);
  mu_assert("AH5_read_str_dataset() status...", res == AH5_TRUE);
  mu_assert("AH5_read_str_dataset() str length...", strlen(dset_val[0]) == strlen("é è à œ"));
  mu_assert("AH5_read_str_dataset() str value...", strcmp(dset_val[0], "é è à œ") == 0);
  free(dset_val[0]);
  free(dset_val);

  H5Oclose(dset);
  H5Oclose(unode);
  H5Oclose(node);
  H5Fclose(file);

  return MU_FINISHED_WITHOUT_ERRORS;
}
Пример #2
0
// Read label category (all datasets)
char AH5_read_label(hid_t file_id, AH5_label_t *label)
{
    char path[AH5_ABSOLUTE_PATH_LENGTH], rdata = AH5_TRUE;
    AH5_children_t children;
    hsize_t i;

    AH5_init_label(label);

    if (AH5_path_valid(file_id, AH5_C_LABEL))
    {
        children = AH5_read_children_name(file_id, AH5_C_LABEL);
        label->nb_datasets = children.nb_children;
        if (children.nb_children > 0)
        {
			label->datasets = (AH5_lbl_dataset_t *) malloc((size_t) children.nb_children * sizeof(AH5_lbl_dataset_t));
            for (i = 0; i < children.nb_children; i++)
            {
                strcpy(path, AH5_C_LABEL);
                strcat(path, children.childnames[i]);
                if(!AH5_read_lbl_dataset(file_id, path, label->datasets + i))
                    rdata = AH5_FALSE;
                free(children.childnames[i]);
            }
            free(children.childnames);
        }
    }
    else
    {
        AH5_print_err_path(AH5_C_LABEL, AH5_C_LABEL);
        rdata = AH5_FALSE;
    }
    return rdata;
}
Пример #3
0
// Read label dataset
char AH5_read_lbl_dataset(hid_t file_id, const char *path, AH5_lbl_dataset_t *lbl_dataset)
{
    H5T_class_t type_class;
    char rdata = AH5_FALSE;
    size_t length;
    int nb_dims;

    AH5_init_lbl_dataset(lbl_dataset);
    lbl_dataset->path = strdup(path);

    lbl_dataset->nb_items = 1;  // in case of single value
    if (AH5_path_valid(file_id, path))
        if (H5LTget_dataset_ndims(file_id, path, &nb_dims) >= 0)
            if (nb_dims <= 1)
                if (H5LTget_dataset_info(file_id, path, &(lbl_dataset->nb_items), &type_class, &length) >= 0)
                    if (type_class == H5T_STRING)
                        if(AH5_read_str_dataset(file_id, path, lbl_dataset->nb_items, length, &(lbl_dataset->items)))
                            rdata = AH5_TRUE;
    if (!rdata)
    {
        AH5_print_err_dset(AH5_C_LABEL, path);
        lbl_dataset->nb_items = 0;
    }
    return rdata;
}
Пример #4
0
// Read externalElement category (all datasets)
char AH5_read_external_element (hid_t file_id, AH5_external_element_t *external_element)
{
    char path[AH5_ABSOLUTE_PATH_LENGTH], rdata = AH5_TRUE;
    AH5_children_t children;
    hsize_t i;

    external_element->datasets = NULL;

    if (AH5_path_valid(file_id, AH5_C_EXTERNAL_ELEMENT))
    {
        children = AH5_read_children_name(file_id, AH5_C_EXTERNAL_ELEMENT);
        external_element->nb_datasets = children.nb_children;
        if (children.nb_children > 0)
        {
            external_element->datasets = (AH5_eet_dataset_t *) malloc(children.nb_children * sizeof(AH5_eet_dataset_t));
            for (i = 0; i < children.nb_children; i++)
            {
                strcpy(path, AH5_C_EXTERNAL_ELEMENT);
                strcat(path, children.childnames[i]);
                if (!AH5_read_eet_dataset(file_id, path, external_element->datasets + i))
                    rdata = AH5_FALSE;
                free(children.childnames[i]);
            }
            free(children.childnames);
        }
    }
    else
    {
        AH5_print_err_path(AH5_C_EXTERNAL_ELEMENT, AH5_C_EXTERNAL_ELEMENT);
        rdata = AH5_FALSE;
    }
    return rdata;
}
Пример #5
0
// Read dataset in externalElement and open external files
char AH5_read_eet_dataset (hid_t file_id, const char *path, AH5_eet_dataset_t *eet_dataset)
{
    H5T_class_t type_class;
    char rdata = AH5_FALSE;
    hsize_t dims[2], i;
    size_t length;
    int nb_dims;

    eet_dataset->path = strdup(path);

    if (AH5_path_valid(file_id, path))
        if (H5LTget_dataset_ndims(file_id, path, &nb_dims) >= 0)
            if (nb_dims == 2)
                if (H5LTget_dataset_info(file_id, path, dims, &type_class, &length) >= 0)
                    if (dims[0] > 0 && dims[1] == 3 && type_class == H5T_STRING)
                        if(AH5_read_str_dataset(file_id, path, dims[0] * dims[1], length, &(eet_dataset->eed_items)))
                        {
                            eet_dataset->nb_eed_items = dims[0];
                            rdata = AH5_TRUE;
                            eet_dataset->file_id = (hid_t *) malloc(eet_dataset->nb_eed_items * sizeof(hid_t));
                            for (i = 0; i < eet_dataset->nb_eed_items; i++)
                                eet_dataset->file_id[i] = -1;
                        }
    if (!rdata)
    {
        AH5_print_err_dset(AH5_C_EXTERNAL_ELEMENT, path);
        eet_dataset->nb_eed_items = 0;
        eet_dataset->file_id = NULL;
        eet_dataset->eed_items = NULL;
    }
    else
        if (!AH5_open_external_files(eet_dataset))
            rdata = AH5_FALSE;
    return rdata;
}
Пример #6
0
// Read string attribute <attr_name> given by address <path>
char AH5_read_str_attr(hid_t loc_id, const char *path, char *attr_name, char **rdata)
{
    hid_t attr_id, filetype, memtype;
    size_t sdim;
    char success = AH5_FALSE;

    if (AH5_path_valid(loc_id, path) || strcmp(path, ".") == 0)
        if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0)
        {
            attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT);
            filetype = H5Aget_type(attr_id);
            sdim = H5Tget_size(filetype);
            sdim++;  // make a space for null terminator
            *rdata = (char *) malloc(sdim * sizeof(char));
            memtype = H5Tcopy(H5T_C_S1);
            H5Tset_size(memtype, sdim);
            if (H5Aread(attr_id, memtype, *rdata) >= 0)
                success = AH5_TRUE;
            else
                free(*rdata);
            H5Tclose(memtype);
            H5Tclose(filetype);
            H5Aclose(attr_id);
        }
    if (!success)
        *rdata = NULL;
    return success;
}
Пример #7
0
// Read localizationSystem transformation
char AH5_read_lsm_transformation (hid_t file_id, const char *path,
                                  AH5_lsm_transf_t *lsm_transformation)
{
  char *type, rdata = AH5_TRUE;

  lsm_transformation->path = strdup(path);
  lsm_transformation->type = TRF_INVALID;

  if (AH5_path_valid(file_id, path))
  {
    if (AH5_read_str_attr(file_id, path, AH5_A_TYPE, &type))
    {
      if (strcmp(type, AH5_V_SCALE) == 0)
        lsm_transformation->type = TRF_SCALE;
      else if (strcmp(type, AH5_V_ROTATION) == 0)
        lsm_transformation->type = TRF_ROTATION;
      else if (strcmp(type, AH5_V_TRANSLATION) == 0)
        lsm_transformation->type = TRF_TRANSLATION;
      free(type);
    }
    else
      rdata = AH5_FALSE;
    if(!AH5_read_int_attr(file_id, path, AH5_A_RANK, &(lsm_transformation->rank)))
      rdata = AH5_FALSE;
  }
  else
  {
    AH5_print_err_path(AH5_C_LOCALIZATION_SYSTEM, path);
    rdata = AH5_FALSE;
  }
  return rdata;
}
Пример #8
0
// Write complex attribute <attr_name> given by address <path>
char AH5_write_cpx_attr(hid_t loc_id, const char* path, char* attr_name, const AH5_complex_t wdata)
{
    char success = AH5_FALSE;
    hid_t attr_id = 0, cpx_filetype, cpx_memtype, object_id, space;
    hsize_t dims[1] = {1};

    cpx_filetype = AH5_H5Tcreate_cpx_filetype();
    cpx_memtype = AH5_H5Tcreate_cpx_memtype();
    space = H5Screate_simple (1, dims, NULL);

    if (AH5_path_valid(loc_id, path))
    {
        if ((object_id = H5Oopen(loc_id, path, H5P_DEFAULT)) >= 0)
        {
            if ((attr_id = H5Acreate(object_id, attr_name, cpx_filetype, space, H5P_DEFAULT, H5P_DEFAULT)) >= 0)
            {
                if (H5Awrite(attr_id, cpx_memtype, &wdata) >= 0)
                {
                    success = AH5_TRUE;
                }
                H5Aclose(attr_id);
            }
            H5Oclose(object_id);
        }
    }

    H5Sclose(space);
    H5Tclose(cpx_memtype);
    H5Tclose(cpx_filetype);

    return success;
}
Пример #9
0
// Read children names of an object
AH5_children_t AH5_read_children_name(hid_t file_id, const char* path)
{
    H5G_info_t ginfo;
    AH5_children_t children;
    hsize_t i, j = 0;
    ssize_t size;
    hid_t group_id;
    char temp[AH5_ELEMENT_NAME_LENGTH], *temp2;

    children.childnames = NULL;
    /*
        - path must exist
        - number of children must be greater than zero
        - element name
            - must be readable
            - must be shorter than AH5_ELEMENT_NAME_LENGTH
            - must NOT be same as "_param"
    */
    if (AH5_path_valid(file_id, path) || strcmp(path, "/") == 0)
    {
        group_id = H5Gopen1(file_id, path);
        H5Gget_info(group_id, &ginfo);
        if (ginfo.nlinks > 0)
        {
            children.childnames = (char **) malloc((size_t) ginfo.nlinks * sizeof(char *));
            for (i = 0; i < ginfo.nlinks; i++)
            {
                size = H5Lget_name_by_idx(group_id, ".", H5_INDEX_NAME, H5_ITER_INC, i, NULL, 0, H5P_DEFAULT);
                if (size < 0)
                    printf("***** ERROR: Cannot read all children of \"%s\". *****\n\n", path);
                else if (size >= AH5_ELEMENT_NAME_LENGTH)
                {
                    temp2 = (char *) malloc ((size + 1) * sizeof(char));
                    H5Lget_name_by_idx(group_id, ".", H5_INDEX_NAME, H5_ITER_INC, i, temp2, size + 1, H5P_DEFAULT);
                    printf("***** ERROR: Maximum name length (%i) exceeded in \"%s/%s\". *****\n\n", AH5_ELEMENT_NAME_LENGTH - 1, path, temp2);
                    free(temp2);
                }
                else
                {
                    H5Lget_name_by_idx(group_id, ".", H5_INDEX_NAME, H5_ITER_INC, i, temp, size + 1, H5P_DEFAULT);
                    if (strcmp(temp, "_param") != 0)  // exclude parameterized attributes
                    {
                        children.childnames[j] = (char *) malloc((size + 2) * sizeof(char));
                        strcpy(children.childnames[j], "/");
                        strcat(children.childnames[j++], temp);
                    }
                }
            }
            if (j == 0)
                free(children.childnames);
        }
        H5Gclose(group_id);
    }
    children.nb_children = j;
    return children;
}
Пример #10
0
// Write string attribute <attr_name> given by address <path>
char AH5_write_str_attr(hid_t loc_id, const char *path, char *attr_name, const char *wdata)
{
    char success = AH5_FALSE;

    if (AH5_path_valid(loc_id, path))
        if (H5LTset_attribute_string(loc_id, path, attr_name, wdata) >= 0)
            success = AH5_TRUE;

    return success;
}
Пример #11
0
// Read integer attribute <attr_name> given by address <path>
char AH5_read_int_attr(hid_t loc_id, const char *path, char *attr, int *rdata)
{
    char success = AH5_FALSE;

    if (AH5_path_valid(loc_id, path))
        if (H5Aexists_by_name(loc_id, path, attr, H5P_DEFAULT) > 0)
            if (H5LTget_attribute_int(loc_id, path, attr, rdata) >= 0)
                success = AH5_TRUE;
    if(!success)
        *rdata = 0;
    return success;
}
Пример #12
0
// Read children names of an object
AH5_children_t AH5_read_children_name(hid_t file_id, const char *path)
{
  H5G_info_t ginfo;
  AH5_children_t children;
  hsize_t i, j = 0;
  ssize_t size;
  hid_t group_id;
  char *temp;

  children.childnames = NULL;
  /*
      - path must exist
      - number of children must be greater than zero
      - element name
          - must be readable
          - must NOT be same as "_param"
  */
  if (AH5_path_valid(file_id, path) || strcmp(path, "/") == 0)
  {
    group_id = H5Gopen(file_id, path, H5P_DEFAULT);
    H5Gget_info(group_id, &ginfo);
    if (ginfo.nlinks > 0)
    {
      temp = malloc(sizeof(*temp));
      children.childnames = (char **) malloc((size_t) ginfo.nlinks * sizeof(char *));
      for (i = 0; i < ginfo.nlinks; i++)
      {
        size = H5Lget_name_by_idx(group_id, ".", H5_INDEX_NAME, H5_ITER_INC, i, NULL, 0, H5P_DEFAULT);
        if (size < 0)
          AH5_log_error("Cannot read all children of \"%s\". *****\n\n", path);
        else
        {
          temp = realloc(temp, (size + 1) * sizeof(*temp));
          H5Lget_name_by_idx(group_id, ".", H5_INDEX_NAME, H5_ITER_INC, i, temp, size + 1, H5P_DEFAULT);
          if (strcmp(temp, "_param") != 0)  // exclude parameterized attributes
          {
            children.childnames[j] = (char *) malloc((size + 2) * sizeof(char));
            strcpy(children.childnames[j], "/");
            strcat(children.childnames[j++], temp);
          }
        }
      }
      free(temp);
      if (j == 0)
        free(children.childnames);
    }
    H5Gclose(group_id);
  }
  children.nb_children = j;
  return children;
}
Пример #13
0
// Read localizationSystem instance
char AH5_read_lsm_instance (hid_t file_id, const char *path, AH5_lsm_instance_t *lsm_instance)
{
  char mandatory[][AH5_ATTR_LENGTH] = {AH5_A_DIMENSION}, rdata = AH5_TRUE;
  char *path2;
  AH5_children_t children;
  hsize_t i;

  lsm_instance->path = strdup(path);
  lsm_instance->transformations = NULL;
  lsm_instance->opt_attrs.instances = NULL;

  if (AH5_path_valid(file_id, path))
  {
    AH5_read_opt_attrs(file_id, path, &(lsm_instance->opt_attrs), mandatory,
                       sizeof(mandatory)/AH5_ATTR_LENGTH);
    AH5_read_int_attr(file_id, path, AH5_A_DIMENSION, &(lsm_instance->dimension));

    children = AH5_read_children_name(file_id, path);
    lsm_instance->nb_transformations = children.nb_children;
    if (children.nb_children > 0)
    {
      lsm_instance->transformations = (AH5_lsm_transf_t *) malloc((size_t) children.nb_children * sizeof(
                                        AH5_lsm_transf_t));
      for (i = 0; i < children.nb_children; i++)
      {
        path2 = malloc((strlen(path) + strlen(children.childnames[i]) + 1) * sizeof(*path2));
        strcpy(path2, path);
        strcat(path2, children.childnames[i]);
        if (!AH5_read_lsm_transformation(file_id, path2, lsm_instance->transformations + i))
          rdata = AH5_FALSE;
        free(children.childnames[i]);
        free(path2);
      }
      free(children.childnames);
    }
  }
  else
  {
    AH5_print_err_path(AH5_C_LOCALIZATION_SYSTEM, path);
    rdata = AH5_FALSE;
  }
  return rdata;
}
Пример #14
0
// Read dataset in externalElement and open external files
char AH5_read_eet_dataset (hid_t file_id, const char *path, AH5_eet_dataset_t *eet_dataset)
{
  H5T_class_t type_class;
  char rdata = AH5_FALSE;
  hsize_t dims[2], i;
  size_t length;
  int nb_dims;
  ssize_t fpath_size;

  eet_dataset->path = strdup(path);

  fpath_size = H5Fget_name(file_id, NULL, 0);
  // Strange behavior of H5Fget_name: it seems to return to small length.
  eet_dataset->principle_file_path = malloc(fpath_size + 2);
  eet_dataset->principle_file_path[fpath_size + 1] = '\0';
  eet_dataset->principle_file_path[fpath_size] = '\0';
  H5Fget_name(file_id, eet_dataset->principle_file_path, fpath_size + 1);

  if (AH5_path_valid(file_id, path))
    if (H5LTget_dataset_ndims(file_id, path, &nb_dims) >= 0)
      if (nb_dims == 2)
        if (H5LTget_dataset_info(file_id, path, dims, &type_class, &length) >= 0)
          if (dims[0] > 0 && dims[1] == 3 && type_class == H5T_STRING)
            if(AH5_read_str_dataset(file_id, path, dims[0] * dims[1], length, &(eet_dataset->eed_items)))
            {
              eet_dataset->nb_eed_items = dims[0];
              rdata = AH5_TRUE;
              eet_dataset->file_id = (hid_t *) malloc((size_t) eet_dataset->nb_eed_items * sizeof(hid_t));
              for (i = 0; i < eet_dataset->nb_eed_items; i++)
                eet_dataset->file_id[i] = -1;
            }
  if (!rdata)
  {
    AH5_print_err_dset(AH5_C_EXTERNAL_ELEMENT, path);
    eet_dataset->nb_eed_items = 0;
    eet_dataset->file_id = NULL;
    eet_dataset->eed_items = NULL;
  }
  else if (!AH5_open_external_files(eet_dataset))
    rdata = AH5_FALSE;
  return rdata;
}
Пример #15
0
// Read complex attribute <attr_name> given by address <path>
char AH5_read_cpx_attr(hid_t loc_id, const char* path, char* attr_name, AH5_complex_t *rdata)
{
    hid_t attr_id, type_id;
    float buf[2];
    char success = AH5_FALSE;

    if (AH5_path_valid(loc_id, path))
        if (H5Aexists_by_name(loc_id, path, attr_name, H5P_DEFAULT) > 0)
        {
            attr_id = H5Aopen_by_name(loc_id, path, attr_name, H5P_DEFAULT, H5P_DEFAULT);
            type_id = AH5_H5Tcreate_cpx_memtype();
            if (H5Aread(attr_id, type_id, buf) >= 0)
            {
                *rdata = AH5_set_complex(buf[0], buf[1]);
                success = AH5_TRUE;
            }
            H5Tclose(type_id);
            H5Aclose(attr_id);
        }
    if (!success)
        *rdata = AH5_set_complex(0, 0);
    return success;
}
Пример #16
0
// Read localizationSystem category
char AH5_read_localization_system (hid_t file_id, AH5_localization_system_t *localization_system)
{
  char *path;
  char rdata = AH5_TRUE;
  AH5_children_t children;
  hsize_t i;

  localization_system->instances = NULL;

  if (AH5_path_valid(file_id, AH5_C_LOCALIZATION_SYSTEM))
  {
    children = AH5_read_children_name(file_id, AH5_C_LOCALIZATION_SYSTEM);
    localization_system->nb_instances = children.nb_children;
    if (children.nb_children > 0)
    {
      localization_system->instances = (AH5_lsm_instance_t *) malloc((size_t) children.nb_children *
                                       sizeof(AH5_lsm_instance_t));
      for (i = 0; i < children.nb_children; i++)
      {
        path = malloc((strlen(AH5_C_LOCALIZATION_SYSTEM) + strlen(children.childnames[i]) + 1) * sizeof(*path));
        strcpy(path, AH5_C_LOCALIZATION_SYSTEM);
        strcat(path, children.childnames[i]);
        if(!AH5_read_lsm_instance(file_id, path, localization_system->instances + i))
          rdata = AH5_FALSE;
        free(children.childnames[i]);
        free(path);
      }
      free(children.childnames);
    }
  }
  else
  {
    AH5_print_err_path(AH5_C_LOCALIZATION_SYSTEM, AH5_C_LOCALIZATION_SYSTEM);
    rdata = AH5_FALSE;
  }
  return rdata;
}
Пример #17
0
// Read simulation category (all instances)
char AH5_read_simulation (hid_t file_id, AH5_simulation_t *simulation)
{
  char *path, rdata = AH5_TRUE;
  AH5_children_t children;
  hsize_t i;

  simulation->instances = NULL;

  if (AH5_path_valid(file_id, AH5_C_SIMULATION))
  {
    children = AH5_read_children_name(file_id, AH5_C_SIMULATION);
    simulation->nb_instances = children.nb_children;
    if (children.nb_children > 0)
    {
      simulation->instances = (AH5_sim_instance_t *) malloc((size_t) children.nb_children * sizeof(
                                AH5_sim_instance_t));
      for (i = 0; i < children.nb_children; i++)
      {
        path = malloc((strlen(AH5_C_SIMULATION) + strlen(children.childnames[i]) + 1)* sizeof(*path));
        strcpy(path, AH5_C_SIMULATION);
        strcat(path, children.childnames[i]);
        if (!AH5_read_sim_instance(file_id, path, simulation->instances + i))
          rdata = AH5_FALSE;
        free(children.childnames[i]);
        free(path);
      }
      free(children.childnames);
    }
  }
  else
  {
    AH5_print_err_path(AH5_C_SIMULATION, AH5_C_SIMULATION);
    rdata = AH5_FALSE;
  }
  return rdata;
}
Пример #18
0
// Read all optional attributes
char AH5_read_opt_attrs(hid_t loc_id, const char *path, AH5_opt_attrs_t *opt_attrs, char mandatory_attrs[][AH5_ATTR_LENGTH], size_t nb_mandatory_attrs)
{
    char success = AH5_FALSE, is_mandatory;
    H5O_info_t object_info;
    hsize_t i, j, k = 0;
    hid_t attr_id, type_id, memtype;
    float buf[2];
    hsize_t nb_present_mandatory_attrs = 0;
    char temp_name[AH5_ATTR_LENGTH];

    if (AH5_path_valid(loc_id, path))
    {
        // Check presence of all mandatory attributes
        for (i = 0; i < (hsize_t) nb_mandatory_attrs; i++)
            if (H5Aexists_by_name(loc_id, path, mandatory_attrs[i], H5P_DEFAULT) > 0)
                nb_present_mandatory_attrs++;
        H5Oget_info_by_name(loc_id, path, &object_info, H5P_DEFAULT);
        opt_attrs->nb_instances = object_info.num_attrs - nb_present_mandatory_attrs;
        if (opt_attrs->nb_instances > 0)
            opt_attrs->instances = (AH5_attr_instance_t *) malloc ((size_t) opt_attrs->nb_instances * sizeof(AH5_attr_instance_t));
        for (i = 0; i < object_info.num_attrs; i++)
        {
            is_mandatory = AH5_FALSE;
            attr_id = H5Aopen_by_idx(loc_id, path, H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, i, H5P_DEFAULT, H5P_DEFAULT);
            H5Aget_name(attr_id, AH5_ATTR_LENGTH, temp_name);
            for (j = 0; j < nb_mandatory_attrs; j++)
                if (strcmp(temp_name, mandatory_attrs[j]) == 0)
                    is_mandatory = AH5_TRUE;
            if (!is_mandatory)
            {
                opt_attrs->instances[k].name = strdup(temp_name);
                type_id = H5Aget_type(attr_id);
                opt_attrs->instances[k].type = H5Tget_class(type_id);
                H5Tclose(type_id);
                switch (opt_attrs->instances[k].type)
                {
                case H5T_INTEGER:
                    opt_attrs->instances[k].value.i = 0;
                    if (H5Aread(attr_id, H5T_NATIVE_INT, &(opt_attrs->instances[k].value.i)) >= 0)
                        success = AH5_TRUE;
                    break;
                case H5T_FLOAT:
                    opt_attrs->instances[k].value.f = 0;
                    if (H5Aread(attr_id, H5T_NATIVE_FLOAT, &(opt_attrs->instances[k].value.f)) >= 0)
                        success = AH5_TRUE;
                    break;
                case H5T_COMPOUND:
                    opt_attrs->instances[k].value.c = AH5_set_complex(0, 0);
                    type_id = AH5_H5Tcreate_cpx_memtype();
                    if (H5Aread(attr_id, type_id, buf) >= 0)
                    {
                        opt_attrs->instances[k].value.c = AH5_set_complex(buf[0], buf[1]);
                        success = AH5_TRUE;
                    }
                    H5Tclose(type_id);
                    break;
                case H5T_STRING:
                    opt_attrs->instances[k].value.s = NULL;
                    memtype = H5Tcopy(H5T_C_S1);
                    H5Tset_size(memtype, AH5_ATTR_LENGTH);
                    opt_attrs->instances[k].value.s = (char *) malloc(AH5_ATTR_LENGTH * sizeof(char));
                    if (H5Aread(attr_id, memtype, opt_attrs->instances[k].value.s) >= 0)
                        success = AH5_TRUE;
                    H5Tclose(memtype);
                    break;
                default:
                    opt_attrs->instances[k].type = H5T_NO_CLASS;
                    printf("***** WARNING: Unsupported type of attribute \"%s@%s\". *****\n\n", path, opt_attrs->instances[k].name);
                    break;
                }
                k++;
            }
            H5Aclose(attr_id);
        }
    }
    if (!success)
    {
        opt_attrs->instances = NULL;
        opt_attrs->nb_instances = 0;
    }
    return success;
}
Пример #19
0
// Read simulation instance
char AH5_read_sim_instance (hid_t file_id, const char *path, AH5_sim_instance_t *sim_instance)
{
  char mandatory[][AH5_ATTR_LENGTH] = {AH5_A_MODULE, AH5_A_VERSION};
  /*    char mandatory2[][AH5_ATTR_LENGTH] = {}; */
  char *path1;
  char rdata = AH5_TRUE;
  H5T_class_t type_class;
  char success1 = AH5_FALSE, success2 = AH5_FALSE;
  size_t length;
  int nb_dims;

  sim_instance->path = strdup(path);
  sim_instance->opt_attrs.instances = NULL;
  sim_instance->module = NULL;
  sim_instance->version = NULL;
  sim_instance->inputs = NULL;
  sim_instance->outputs = NULL;

  if (AH5_path_valid(file_id, path))
  {
    AH5_read_opt_attrs(file_id, path, &(sim_instance->opt_attrs), mandatory,
                       sizeof(mandatory)/AH5_ATTR_LENGTH);
    if (!AH5_read_str_attr(file_id, path, AH5_A_MODULE, &(sim_instance->module)))
      AH5_print_err_attr(AH5_C_SIMULATION, path, AH5_A_MODULE);
    if (!AH5_read_str_attr(file_id, path, AH5_A_VERSION, &(sim_instance->version)))
      AH5_print_err_attr(AH5_C_SIMULATION, path, AH5_A_VERSION);

    path1 = malloc((strlen(path) + strlen(AH5_G_PARAMETER) + 1) * sizeof(*path1));
    if (!path1)
    {
      AH5_print_err_dset(AH5_C_SIMULATION, path);
      return AH5_FALSE;
    }
    strncpy(path1, path, strlen(path) + 1);
    strncat(path1, AH5_G_PARAMETER, strlen(AH5_G_PARAMETER));
    AH5_read_opt_attrs(file_id, path1, &(sim_instance->parameter), NULL, 0);

    // inputs
    sim_instance->nb_inputs = 1;  // in case of single value
    path1 = realloc(path1, (strlen(path) + strlen(AH5_G_INPUTS) + 1) * sizeof(*path1));
    if (!path1)
    {
      AH5_print_err_dset(AH5_C_SIMULATION, path);
      return AH5_FALSE;
    }
    strncpy(path1, path, strlen(path) + 1);
    strncat(path1, AH5_G_INPUTS, strlen(AH5_G_INPUTS));
    if (AH5_path_valid(file_id, path1))
      if (H5LTget_dataset_ndims(file_id, path1, &nb_dims) >= 0)
        if (nb_dims <= 1)
          if (H5LTget_dataset_info(file_id, path1, &(sim_instance->nb_inputs), &type_class, &length) >= 0)
            if (type_class == H5T_STRING)
              if(AH5_read_str_dataset(file_id, path1, sim_instance->nb_inputs, length, &(sim_instance->inputs)))
                success1 = AH5_TRUE;
    if (!success1)
    {
      AH5_print_err_dset(AH5_C_SIMULATION, path1);
      sim_instance->nb_inputs = 0;
      rdata = AH5_FALSE;
    }

    // outputs
    sim_instance->nb_outputs = 1;  // in case of single value

    path1 = realloc(path1, (strlen(path) + strlen(AH5_G_OUTPUTS) + 1) * sizeof(*path1));
    if (!path1)
    {
      AH5_print_err_dset(AH5_C_SIMULATION, path);
      return AH5_FALSE;
    }
    strncpy(path1, path, strlen(path) + 1);
    strncat(path1, AH5_G_OUTPUTS, strlen(AH5_G_OUTPUTS));
    if (AH5_path_valid(file_id, path1))
      if (H5LTget_dataset_ndims(file_id, path1, &nb_dims) >= 0)
        if (nb_dims <= 1)
          if (H5LTget_dataset_info(file_id, path1, &(sim_instance->nb_outputs), &type_class, &length) >= 0)
            if (type_class == H5T_STRING)
              if(AH5_read_str_dataset(file_id, path1, sim_instance->nb_outputs, length, &(sim_instance->outputs)))
                success2 = AH5_TRUE;
    if (!success2)
    {
      AH5_print_wrn_outputs(path);
      sim_instance->nb_outputs = 0;
    }

    free(path1);
  }
  else
  {
    rdata = AH5_FALSE;
  }

  return rdata;
}