aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-01-02 16:23:28 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-01-02 16:23:28 -0500
commit9be1567765803341e252e87262dc43d790d8e770 (patch)
tree6c28bdb0ede85c629027d1079df319390edfee95 /src
parent1d071010c2bec860e62371cfb70f2e12a7d00563 (diff)
downloadstasis-9be1567765803341e252e87262dc43d790d8e770.tar.gz
Move utility functions to utils.c
Diffstat (limited to 'src')
-rw-r--r--src/cli/stasis/args.c26
-rw-r--r--src/cli/stasis/include/args.h4
-rw-r--r--src/lib/core/include/utils.h6
-rw-r--r--src/lib/core/multiprocessing.c18
-rw-r--r--src/lib/core/utils.c44
5 files changed, 50 insertions, 48 deletions
diff --git a/src/cli/stasis/args.c b/src/cli/stasis/args.c
index 9410958..172981a 100644
--- a/src/cli/stasis/args.c
+++ b/src/cli/stasis/args.c
@@ -106,29 +106,3 @@ void usage(char *progname) {
puts(output);
}
}
-
-int str_to_timeout(char *s) {
- if (!s) {
- return 0; // no timeout
- }
-
- char *scale = NULL;
- int value = (int) strtol(s, &scale, 10);
- if (scale) {
- if (*scale == 's') {
- value *= 1; // seconds, no-op
- } else if (*scale == 'm') {
- value *= 60; // minutes
- } else if (*scale == 'h') {
- value *= 3200; // hours
- } else {
- return STR_TO_TIMEOUT_INVALID_TIME_SCALE; // invalid time scale
- }
- }
-
- if (value < 0) {
- return STR_TO_TIMEOUT_NEGATIVE; // cannot be negative
- }
- return value;
-}
-
diff --git a/src/cli/stasis/include/args.h b/src/cli/stasis/include/args.h
index d75fe29..5536735 100644
--- a/src/cli/stasis/include/args.h
+++ b/src/cli/stasis/include/args.h
@@ -23,8 +23,4 @@
extern struct option long_options[];
void usage(char *progname);
-#define STR_TO_TIMEOUT_NEGATIVE (-1)
-#define STR_TO_TIMEOUT_INVALID_TIME_SCALE (-2)
-int str_to_timeout(char *s);
-
#endif //STASIS_ARGS_H
diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h
index a9bcd2f..2476d4e 100644
--- a/src/lib/core/include/utils.h
+++ b/src/lib/core/include/utils.h
@@ -464,4 +464,10 @@ int is_git_sha(char const *hash);
int check_python_package_dependencies(const char *srcdir);
+char *seconds_to_human_readable(int v);
+
+#define STR_TO_TIMEOUT_NEGATIVE (-1)
+#define STR_TO_TIMEOUT_INVALID_TIME_SCALE (-2)
+int str_to_timeout(char *s);
+
#endif //STASIS_UTILS_H
diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c
index 43f3e07..91b668a 100644
--- a/src/lib/core/multiprocessing.c
+++ b/src/lib/core/multiprocessing.c
@@ -4,24 +4,6 @@
/// The sum of all tasks started by mp_task()
size_t mp_global_task_count = 0;
-static char *seconds_to_human_readable(const int v) {
- static char result[255] = {0};
- const int hours = v / 3600;
- const int minutes = (v % 3600) / 60;
- const int seconds = v % 60;
-
- memset(result, '\0', sizeof(result));
- if (hours) {
- snprintf(result + strlen(result), sizeof(result), "%dh ", hours);
- }
- if (minutes) {
- snprintf(result + strlen(result), sizeof(result), "%dm ", minutes);
- }
- snprintf(result + strlen(result), sizeof(result), "%ds", seconds);
-
- return result;
-}
-
static double get_duration(const struct timespec stop, const struct timespec start) {
const struct timespec result = timespec_sub(stop, start);
return timespec_to_double(result);
diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c
index 62f3bec..0871787 100644
--- a/src/lib/core/utils.c
+++ b/src/lib/core/utils.c
@@ -1078,3 +1078,47 @@ int check_python_package_dependencies(const char *srcdir) {
}
return 0;
}
+
+int str_to_timeout(char *s) {
+ if (!s) {
+ return 0; // no timeout
+ }
+
+ char *scale = NULL;
+ int value = (int) strtol(s, &scale, 10);
+ if (scale) {
+ if (*scale == 's') {
+ value *= 1; // seconds, no-op
+ } else if (*scale == 'm') {
+ value *= 60; // minutes
+ } else if (*scale == 'h') {
+ value *= 3200; // hours
+ } else {
+ return STR_TO_TIMEOUT_INVALID_TIME_SCALE; // invalid time scale
+ }
+ }
+
+ if (value < 0) {
+ return STR_TO_TIMEOUT_NEGATIVE; // cannot be negative
+ }
+ return value;
+}
+
+char *seconds_to_human_readable(const int v) {
+ static char result[255] = {0};
+ const int hours = v / 3600;
+ const int minutes = (v % 3600) / 60;
+ const int seconds = v % 60;
+
+ memset(result, '\0', sizeof(result));
+ if (hours) {
+ snprintf(result + strlen(result), sizeof(result), "%dh ", hours);
+ }
+ if (hours || minutes) {
+ snprintf(result + strlen(result), sizeof(result), "%dm ", minutes);
+ }
+ snprintf(result + strlen(result), sizeof(result), "%ds", seconds);
+
+ return result;
+}
+