esp32 firmware for a toaster reflow oven WIP!!!!!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
reflower/components/common_utils/src/common_utils.c

85 lines
2.0 KiB

#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include "common_utils/utils.h"
char *append(char *buf, const char *appended, size_t *pcap)
{
char c;
char *buf0 = buf;
size_t cap = *pcap;
if (buf0 == NULL) return NULL;
if (appended == NULL || appended[0] == 0) return buf0;
if (*pcap < strlen(appended)+1) {
return NULL;
}
while (cap > 1 && 0 != (c = *appended++)) {
*buf++ = c;
cap--;
}
assert(cap > 0);
*pcap = cap;
*buf = 0;
return buf;
}
bool append_realloc(char **head, size_t *cap, const char *appended) {
if (!head) return NULL;
if (!*head) return NULL;
if (!cap) return NULL;
if (!appended) return NULL;
size_t cursize = strlen(*head);
size_t needed = strlen(appended) + 1;
size_t remains = *cap - cursize;
if (remains < needed) {
size_t need_extra = needed - remains;
size_t newsize = *cap + need_extra;
char *new = realloc(*head, newsize);
if (!new) return false;
*head = new;
*cap = newsize;
}
strcpy(*head + cursize, appended);
return true;
}
bool fd_is_valid(int fd)
{
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
}
bool parse_boolean_arg(const char *str)
{
if (0 == strcasecmp(str, "on")) return true;
if (0 == strcmp(str, "1")) return true;
if (0 == strcasecmp(str, "yes")) return true;
if (0 == strcasecmp(str, "enable")) return true;
if (0 == strcasecmp(str, "en")) return true;
if (0 == strcasecmp(str, "y")) return true;
if (0 == strcasecmp(str, "a")) return true;
return false;
}
bool parse_boolean_arg_false(const char *str)
{
if (0 == strcasecmp(str, "off")) return true;
if (0 == strcmp(str, "0")) return true;
if (0 == strcasecmp(str, "no")) return true;
if (0 == strcasecmp(str, "disable")) return true;
if (0 == strcasecmp(str, "dis")) return true;
if (0 == strcasecmp(str, "n")) return true;
return false;
}