1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 int
 saf_put_attribute(SAF_ParMode pmode,    /* One of the parallel modes. */
                   ss_pers_t *obj,       /* The handle to the object the attribute is to be associated with. */
                   const char *name,     /* The name of the attribute. */
                   hid_t type,           /* The datatype of the attribute. */
                   int count,            /* The number of items of type TYPE pointed to by *VALUE. */
                   const void *value     /* The attribute value(s) (an array of COUNT value(s) of type TYPE). */
                   )
 {
    SAF_ENTER(saf_put_attribute, SAF_PRECONDITION_ERROR);
    ss_attr_t    attr;
    hid_t        type_here=-1;

    SAF_REQUIRE(_saf_valid_pmode(pmode), SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                _saf_errmsg("PMODE must be valid"));
    if (!_saf_is_participating_proc(pmode)) SAF_RETURN(-1);

    SAF_REQUIRE(obj, SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                _saf_errmsg("OBJ must not be null"));
    SAF_REQUIRE(name != NULL, SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                _saf_errmsg("NAME must not be null"));
    SAF_REQUIRE(count >= 0, SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                _saf_errmsg("COUNT must be non-negative"));
    SAF_REQUIRE(count==0 || value!=NULL, SAF_LOW_CHK_COST, SAF_PRECONDITION_ERROR,
                _saf_errmsg("if count is non-zero, VALUE must not be null"));
    SAF_REQUIRE(true, SAF_NO_CHK_COST, SAF_PRECONDITION_ERROR,
                _saf_errmsg("database in which object exists must not be open for read-only access"));

    /* As a special case, if TYPE is H5T_C_S1 then create a temporary datatype which is exactly the same length as the
     * NUL-terminated string passed in as the VALUE argument. */
    if (H5Tequal(type, H5T_C_S1)) {
        if (count>1)
            SAF_ERROR(SAF_PARAMETER_ERROR, _saf_errmsg("H5T_C_S1 type can only be used if COUNT is zero or one"));
        if ((type = type_here = H5Tcopy(H5T_C_S1))<0)
            SAF_ERROR(SAF_MISC_ERROR, _saf_errmsg("H5Tcopy() failed"));
        if (H5Tset_size(type, strlen(value)+1)<0)
            SAF_ERROR(SAF_MISC_ERROR, _saf_errmsg("H5Tset_size() failed"));
    }

    /* Create a new attribute object */
    if (NULL==ss_attr_new(obj, name, type, (size_t)count, value, SAF_ALL==pmode?SS_ALLSAME:0U, &attr, NULL))
        SAF_ERROR(SAF_SSLIB_ERROR, _saf_errmsg("unable to create attribute \"%s\"", name));

    /* Cleanup */
    if (type_here>0) H5Tclose(type_here);

    SAF_LEAVE(SAF_SUCCESS);
 }