add timespec_clear_spec() and timespec_get_spec()

This commit is contained in:
Pim van Pelt
2018-11-04 16:17:35 +01:00
parent 0ad33f5fbc
commit a01e548249
3 changed files with 74 additions and 11 deletions

View File

@ -124,17 +124,7 @@ bool timespec_destroy(struct mgos_timespec **ts) {
if (!ts) {
return false;
}
while (!SLIST_EMPTY(&(*ts)->specs)) {
struct mgos_timespec_spec *t_spec;
t_spec = SLIST_FIRST(&(*ts)->specs);
SLIST_REMOVE_HEAD(&(*ts)->specs, entries);
if (t_spec) {
// LOG(LL_DEBUG, ("Removed mgos_timespec_spec"));
free(t_spec);
}
}
timespec_clear_spec(*ts);
free(*ts);
*ts = NULL;
return true;
@ -206,3 +196,57 @@ bool timespec_match_now(const struct mgos_timespec *ts) {
}
return timespec_match(ts, tm);
}
// Clear the timespec linked list
// Returns true on success, false otherwise.
bool timespec_clear_spec(struct mgos_timespec *ts) {
if (!ts) {
return false;
}
while (!SLIST_EMPTY(&ts->specs)) {
struct mgos_timespec_spec *t_spec;
t_spec = SLIST_FIRST(&ts->specs);
SLIST_REMOVE_HEAD(&ts->specs, entries);
if (t_spec) {
free(t_spec);
}
}
return true;
}
// Return a null terminated string in 'ret' of max retlen-1 which is a
// comma separated set of timespec elements from the linked list.
// Returns true on success, false otherwise.
bool timespec_get_spec(struct mgos_timespec *ts, char *ret, int retlen) {
struct mgos_timespec_spec *t_spec;
if (!ts) {
return false;
}
if (!ts) {
return false;
}
if (!ret) {
return false;
}
*ret = '\0';
SLIST_FOREACH(t_spec, &ts->specs, entries) {
char spec_str[20];
snprintf(spec_str, sizeof(spec_str) - 1, "%02d:%02d:%02d-%02d:%02d:%02d", t_spec->start_h, t_spec->start_m, t_spec->start_s,
t_spec->stop_h, t_spec->stop_m, t_spec->stop_s);
if (strlen(spec_str) + strlen(ret) > retlen - 1) {
return false;
}
if (strlen(ret) > 0) {
strncat(ret, ",", retlen);
}
strncat(ret, spec_str, retlen);
}
return true;
}