Changeset c111da2 in mainline for uspace/lib/c
- Timestamp:
- 2025-10-09T15:44:52Z (2 months ago)
- Branches:
- master
- Children:
- cfd04c4
- Parents:
- 1a96db9
- Location:
- uspace/lib/c
- Files:
-
- 3 edited
-
generic/capa.c (modified) (2 diffs)
-
include/capa.h (modified) (2 diffs)
-
test/capa.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/capa.c
r1a96db9 rc111da2 1 1 /* 2 * Copyright (c) 20 15 Jiri Svoboda2 * Copyright (c) 2025 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 233 233 } 234 234 235 errno_t capa_format_buf(capa_spec_t *capa, char *buf, size_t bufsize) 236 { 237 errno_t rc; 238 const char *sunit; 239 uint64_t ipart; 240 uint64_t fpart; 241 uint64_t div; 242 243 sunit = NULL; 244 245 assert(capa->cunit < CU_LIMIT); 246 247 rc = ipow10_u64(capa->dp, &div); 248 if (rc != EOK) 249 return rc; 250 251 ipart = capa->m / div; 252 fpart = capa->m % div; 253 254 sunit = cu_str[capa->cunit]; 255 if (capa->dp > 0) { 256 snprintf(buf, bufsize, "%" PRIu64 ".%0*" PRIu64 " %s", ipart, 257 (int)capa->dp, fpart, sunit); 258 } else { 259 snprintf(buf, bufsize, "%" PRIu64 " %s", ipart, sunit); 260 } 261 262 return EOK; 263 } 264 265 errno_t capa_blocks_format(uint64_t nblocks, size_t block_size, 266 char **rptr) 267 { 268 capa_spec_t capa; 269 270 capa_from_blocks(nblocks, block_size, &capa); 271 capa_simplify(&capa); 272 return capa_format(&capa, rptr); 273 } 274 275 void capa_blocks_format_buf(uint64_t nblocks, size_t block_size, 276 char *buf, size_t bsize) 277 { 278 capa_spec_t capa; 279 errno_t rc; 280 281 capa_from_blocks(nblocks, block_size, &capa); 282 capa_simplify(&capa); 283 284 /* Should not get range error because of nblocks * block_size limits */ 285 rc = capa_format_buf(&capa, buf, bsize); 286 assert(rc == EOK); 287 (void)rc; 288 } 289 235 290 static errno_t capa_digit_val(char c, int *val) 236 291 { -
uspace/lib/c/include/capa.h
r1a96db9 rc111da2 1 1 /* 2 * Copyright (c) 20 15 Jiri Svoboda2 * Copyright (c) 2025 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 87 87 } capa_spec_t; 88 88 89 /** Size of buffer large enough for capa_blocks_format_buf */ 90 #define CAPA_BLOCKS_BUFSIZE 16 91 89 92 extern errno_t capa_format(capa_spec_t *, char **); 93 extern errno_t capa_format_buf(capa_spec_t *, char *, size_t); 94 extern errno_t capa_blocks_format(uint64_t, size_t, char **); 95 extern void capa_blocks_format_buf(uint64_t, size_t, char *, size_t); 90 96 extern errno_t capa_parse(const char *, capa_spec_t *); 91 97 extern void capa_simplify(capa_spec_t *); -
uspace/lib/c/test/capa.c
r1a96db9 rc111da2 1 1 /* 2 * Copyright (c) 2025 Jiri Svoboda 2 3 * Copyright (c) 2019 Matthieu Riolo 3 4 * All rights reserved. … … 285 286 } 286 287 288 PCUT_TEST(capa_blocks_format) 289 { 290 errno_t rc; 291 char *str; 292 293 rc = capa_blocks_format(42, 1, &str); 294 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 295 PCUT_ASSERT_STR_EQUALS("42 B", str); 296 free(str); 297 } 298 299 PCUT_TEST(capa_blocks_format_buf) 300 { 301 char str[CAPA_BLOCKS_BUFSIZE]; 302 303 capa_blocks_format_buf(42, 1, str, sizeof(str)); 304 PCUT_ASSERT_STR_EQUALS("42 B", str); 305 } 306 287 307 PCUT_EXPORT(capa);
Note:
See TracChangeset
for help on using the changeset viewer.
