Changes in uspace/lib/c/generic/capa.c [cfd04c4:de65624] in mainline
- File:
-
- 1 edited
-
uspace/lib/c/generic/capa.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/capa.c
rcfd04c4 rde65624 1 1 /* 2 * Copyright (c) 20 25 Jiri Svoboda2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 34 34 */ 35 35 36 #include <assert.h>37 36 #include <capa.h> 38 37 #include <errno.h> 39 38 #include <imath.h> 40 #include <stddef.h>41 39 #include <stdio.h> 42 40 #include <str.h> … … 199 197 } 200 198 201 /** Format capacity as string into a newly allocated buffer.202 *203 * @param capa Capacity204 * @param rstr Place to store pointer to newly allocated string205 * @return EOK on success or an error code206 */207 199 errno_t capa_format(capa_spec_t *capa, char **rstr) 208 200 { … … 239 231 } 240 232 241 /** Format capacity as string into an existing buffer.242 *243 * @param capa Capacity244 * @param buf Buffer for storing string245 * @param bufsize Size of buffer in bytes246 * @return EOK on success or an error code247 */248 errno_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 it281 * and formats it as string.282 *283 * @param nblocks Number of blocks284 * @param block_size Size of each block in bytes285 * @param rstr Place to store pointer to newly allocated string286 * @return EOK on success or an error code287 */288 errno_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 it301 * 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 blocks308 * @param block_size Size of each block in bytes309 * @param buf Buffer for storing string310 * @param bufsize Size of buffer in bytes311 */312 void 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 327 233 static errno_t capa_digit_val(char c, int *val) 328 234 { … … 365 271 } 366 272 367 /** Parse string as capacity specification.368 *369 * @param str String (e.g. "100 kB")370 * @param capa Place to store capacity371 * @return EOK on success or an error code372 */373 273 errno_t capa_parse(const char *str, capa_spec_t *capa) 374 274 {
Note:
See TracChangeset
for help on using the changeset viewer.
