Ignore:
File:
1 edited

Legend:

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

    r5fc8244 rcfd04c4  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2025 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    199199}
    200200
     201/** Format capacity as string into a newly allocated buffer.
     202 *
     203 * @param capa Capacity
     204 * @param rstr Place to store pointer to newly allocated string
     205 * @return EOK on success or an error code
     206 */
    201207errno_t capa_format(capa_spec_t *capa, char **rstr)
    202208{
     
    233239}
    234240
     241/** Format capacity as string into an existing buffer.
     242 *
     243 * @param capa Capacity
     244 * @param buf Buffer for storing string
     245 * @param bufsize Size of buffer in bytes
     246 * @return EOK on success or an error code
     247 */
     248errno_t capa_format_buf(capa_spec_t *capa, char *buf, size_t bufsize)
     249{
     250        errno_t rc;
     251        const char *sunit;
     252        uint64_t ipart;
     253        uint64_t fpart;
     254        uint64_t div;
     255
     256        sunit = NULL;
     257
     258        assert(capa->cunit < CU_LIMIT);
     259
     260        rc = ipow10_u64(capa->dp, &div);
     261        if (rc != EOK)
     262                return rc;
     263
     264        ipart = capa->m / div;
     265        fpart = capa->m % div;
     266
     267        sunit = cu_str[capa->cunit];
     268        if (capa->dp > 0) {
     269                snprintf(buf, bufsize, "%" PRIu64 ".%0*" PRIu64 " %s", ipart,
     270                    (int)capa->dp, fpart, sunit);
     271        } else {
     272                snprintf(buf, bufsize, "%" PRIu64 " %s", ipart, sunit);
     273        }
     274
     275        return EOK;
     276}
     277
     278/** Format capacity of n blocks as string into a newly allocated buffer.
     279 *
     280 * This computes the total capacity of the blocks, simplifies it
     281 * and formats it as string.
     282 *
     283 * @param nblocks Number of blocks
     284 * @param block_size Size of each block in bytes
     285 * @param rstr Place to store pointer to newly allocated string
     286 * @return EOK on success or an error code
     287 */
     288errno_t capa_blocks_format(uint64_t nblocks, size_t block_size,
     289    char **rptr)
     290{
     291        capa_spec_t capa;
     292
     293        capa_from_blocks(nblocks, block_size, &capa);
     294        capa_simplify(&capa);
     295        return capa_format(&capa, rptr);
     296}
     297
     298/** Format capacity of n blocks as string into an existing buffer.
     299 *
     300 * This computes the total capacity of the blocks, simplifies it
     301 * and formats it as string.
     302 *
     303 * This function does not return error. If the buffer is too small,
     304 * the string will be truncated. To make sure it is not truncated,
     305 * bufsize should be at least CAPA_BLOCKS_BUFSIZE.
     306 *
     307 * @param nblocks Number of blocks
     308 * @param block_size Size of each block in bytes
     309 * @param buf Buffer for storing string
     310 * @param bufsize Size of buffer in bytes
     311 */
     312void capa_blocks_format_buf(uint64_t nblocks, size_t block_size,
     313    char *buf, size_t bufsize)
     314{
     315        capa_spec_t capa;
     316        errno_t rc;
     317
     318        capa_from_blocks(nblocks, block_size, &capa);
     319        capa_simplify(&capa);
     320
     321        /* Should not get range error because of nblocks * block_size limits */
     322        rc = capa_format_buf(&capa, buf, bufsize);
     323        assert(rc == EOK);
     324        (void)rc;
     325}
     326
    235327static errno_t capa_digit_val(char c, int *val)
    236328{
     
    273365}
    274366
     367/** Parse string as capacity specification.
     368 *
     369 * @param str String (e.g. "100 kB")
     370 * @param capa Place to store capacity
     371 * @return EOK on success or an error code
     372 */
    275373errno_t capa_parse(const char *str, capa_spec_t *capa)
    276374{
Note: See TracChangeset for help on using the changeset viewer.