|
20 | 20 | #include <configuration.h> |
21 | 21 | #include <devices.h> |
22 | 22 | #include <logging.h> |
| 23 | +#include <ringbuffer.h> |
23 | 24 | #include <shmem.h> |
24 | 25 | #include <utils.h> |
25 | 26 |
|
@@ -47,6 +48,7 @@ static int as_logging_type(char* str); |
47 | 48 | static int as_logging_level(char* str); |
48 | 49 | static int as_logging_mode(char* str); |
49 | 50 | static int as_volume(char* str); |
| 51 | +static int as_size(char* str, size_t def, size_t* size); |
50 | 52 |
|
51 | 53 | static unsigned int as_update_process_title(char* str, unsigned int* policy, unsigned int default_policy); |
52 | 54 |
|
@@ -93,6 +95,8 @@ hrmp_init_configuration(void* shm) |
93 | 95 | config->prev_volume = -1; |
94 | 96 | config->is_muted = false; |
95 | 97 |
|
| 98 | + config->cache_size = HRMP_RINGBUFFER_MAX_BYTES; |
| 99 | + |
96 | 100 | config->metadata = false; |
97 | 101 |
|
98 | 102 | config->dop = false; |
@@ -308,6 +312,13 @@ hrmp_read_configuration(void* shm, char* filename, bool emitWarnings) |
308 | 312 | { |
309 | 313 | drv.volume = as_volume(value); |
310 | 314 | } |
| 315 | + else if (key_in_section("cache", section, key, true, &unknown)) |
| 316 | + { |
| 317 | + if (as_size(value, HRMP_RINGBUFFER_MAX_BYTES, &config->cache_size)) |
| 318 | + { |
| 319 | + unknown = true; |
| 320 | + } |
| 321 | + } |
311 | 322 | else |
312 | 323 | { |
313 | 324 | unknown = true; |
@@ -432,6 +443,11 @@ hrmp_validate_configuration(void* shm) |
432 | 443 | } |
433 | 444 | } |
434 | 445 |
|
| 446 | + if (config->cache_size < HRMP_RINGBUFFER_MIN_BYTES) |
| 447 | + { |
| 448 | + config->cache_size = HRMP_RINGBUFFER_MIN_BYTES; |
| 449 | + } |
| 450 | + |
435 | 451 | return 0; |
436 | 452 | } |
437 | 453 |
|
@@ -1383,3 +1399,90 @@ to_log_type(char* where, int value) |
1383 | 1399 |
|
1384 | 1400 | return 0; |
1385 | 1401 | } |
| 1402 | + |
| 1403 | +static int |
| 1404 | +as_size(char* str, size_t def, size_t* size) |
| 1405 | +{ |
| 1406 | + int multiplier = 1; |
| 1407 | + int index; |
| 1408 | + char value[MISC_LENGTH]; |
| 1409 | + bool multiplier_set = false; |
| 1410 | + int i_value = def; |
| 1411 | + |
| 1412 | + if (is_empty_string(str)) |
| 1413 | + { |
| 1414 | + *size = def; |
| 1415 | + return 0; |
| 1416 | + } |
| 1417 | + |
| 1418 | + index = 0; |
| 1419 | + for (size_t i = 0; i < strlen(str); i++) |
| 1420 | + { |
| 1421 | + if (isdigit(str[i])) |
| 1422 | + { |
| 1423 | + value[index++] = str[i]; |
| 1424 | + } |
| 1425 | + else if (isalpha(str[i]) && multiplier_set) |
| 1426 | + { |
| 1427 | + // allow a 'B' suffix on a multiplier |
| 1428 | + // like for instance 'MB', but don't allow it |
| 1429 | + // for bytes themselves ('BB') |
| 1430 | + if (multiplier == 1 || (str[i] != 'b' && str[i] != 'B')) |
| 1431 | + { |
| 1432 | + // another non-digit char not allowed |
| 1433 | + goto error; |
| 1434 | + } |
| 1435 | + } |
| 1436 | + else if (isalpha(str[i]) && !multiplier_set) |
| 1437 | + { |
| 1438 | + if (str[i] == 'M' || str[i] == 'm') |
| 1439 | + { |
| 1440 | + multiplier = 1024 * 1024; |
| 1441 | + multiplier_set = true; |
| 1442 | + } |
| 1443 | + else if (str[i] == 'G' || str[i] == 'g') |
| 1444 | + { |
| 1445 | + multiplier = 1024 * 1024 * 1024; |
| 1446 | + multiplier_set = true; |
| 1447 | + } |
| 1448 | + else if (str[i] == 'K' || str[i] == 'k') |
| 1449 | + { |
| 1450 | + multiplier = 1024; |
| 1451 | + multiplier_set = true; |
| 1452 | + } |
| 1453 | + else if (str[i] == 'B' || str[i] == 'b') |
| 1454 | + { |
| 1455 | + multiplier = 1; |
| 1456 | + multiplier_set = true; |
| 1457 | + } |
| 1458 | + } |
| 1459 | + else |
| 1460 | + { |
| 1461 | + // do not allow alien chars |
| 1462 | + goto error; |
| 1463 | + } |
| 1464 | + } |
| 1465 | + |
| 1466 | + value[index] = '\0'; |
| 1467 | + if (!as_int(value, &i_value)) |
| 1468 | + { |
| 1469 | + // sanity check: the value |
| 1470 | + // must be a positive number! |
| 1471 | + if (i_value >= 0) |
| 1472 | + { |
| 1473 | + *size = i_value * multiplier; |
| 1474 | + } |
| 1475 | + else |
| 1476 | + { |
| 1477 | + goto error; |
| 1478 | + } |
| 1479 | + |
| 1480 | + return 0; |
| 1481 | + } |
| 1482 | + else |
| 1483 | + { |
| 1484 | +error: |
| 1485 | + *size = def; |
| 1486 | + return 1; |
| 1487 | + } |
| 1488 | +} |
0 commit comments