Changeset 0499235 in mainline for uspace/lib/c


Ignore:
Timestamp:
2012-03-02T17:20:23Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5608deba, 98cb0495, c30a015
Parents:
3113d47
Message:

add basic support for encoding name/value properties in sysinfo

Location:
uspace/lib/c
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/sysinfo.c

    r3113d47 r0499235  
    134134/** Get sysinfo binary data size
    135135 *
    136  * @param path  Sysinfo path.
    137  * @param value Pointer to store the binary data size.
     136 * @param path Sysinfo path.
     137 * @param size Pointer to store the binary data size.
    138138 *
    139139 * @return EOK if the value was successfully read and
     
    149149/** Get sysinfo binary data
    150150 *
    151  * @param path  Sysinfo path.
    152  * @param value Pointer to store the binary data size.
     151 * @param path Sysinfo path.
     152 * @param size Pointer to store the binary data size.
    153153 *
    154154 * @return Binary data read from sysinfo or NULL if the
     
    198198}
    199199
     200/** Get sysinfo property
     201 *
     202 * @param path Sysinfo path.
     203 * @param name Property name.
     204 * @param size Pointer to store the binary data size.
     205 *
     206 * @return Property value read from sysinfo or NULL if the
     207 *         sysinfo item value type is not binary data.
     208 *         The returned non-NULL pointer should be
     209 *         freed by free().
     210 *
     211 */
     212void *sysinfo_get_property(const char *path, const char *name, size_t *size)
     213{
     214        size_t total_size;
     215        void *data = sysinfo_get_data(path, &total_size);
     216        if ((data == NULL) || (total_size == 0)) {
     217                *size = 0;
     218                return NULL;
     219        }
     220       
     221        size_t pos = 0;
     222        while (pos < total_size) {
     223                /* Process each property with sanity checks */
     224                size_t cur_size = str_nsize(data + pos, total_size - pos);
     225                if (((char *) data)[pos + cur_size] != 0)
     226                        break;
     227               
     228                bool found = (str_cmp(data + pos, name) == 0);
     229               
     230                pos += cur_size + 1;
     231                if (pos >= total_size)
     232                        break;
     233               
     234                /* Process value size */
     235                size_t value_size;
     236                memcpy(&value_size, data + pos, sizeof(value_size));
     237               
     238                pos += sizeof(value_size);
     239                if ((pos >= total_size) || (pos + value_size > total_size))
     240                        break;
     241               
     242                if (found) {
     243                        void *value = malloc(value_size);
     244                        if (value == NULL)
     245                                break;
     246                       
     247                        memcpy(value, data + pos, value_size);
     248                        free(data);
     249                       
     250                        *size = value_size;
     251                        return value;
     252                }
     253               
     254                pos += value_size;
     255        }
     256       
     257        free(data);
     258       
     259        *size = 0;
     260        return NULL;
     261}
     262
    200263/** @}
    201264 */
  • uspace/lib/c/include/sysinfo.h

    r3113d47 r0499235  
    4444extern int sysinfo_get_value(const char *, sysarg_t *);
    4545extern void *sysinfo_get_data(const char *, size_t *);
     46extern void *sysinfo_get_property(const char *, const char *, size_t *);
    4647
    4748#endif
Note: See TracChangeset for help on using the changeset viewer.