initial commit

This commit is contained in:
2025-03-31 04:12:41 -05:00
commit 961e0472f5
13 changed files with 614 additions and 0 deletions

72
include/utils.c Normal file
View File

@@ -0,0 +1,72 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "utils.h"
#include "log.h"
/**
* @brief get a substring of a string
*
* @param s string
* @param pos first pos
* @param l length
* @return the substring
*/
char
*get_substring(char *s, int pos, int l)
{
int i = 0;
char *out;
if (l <= 0) {
return NULL;
}
out = malloc((l + 1) * sizeof(char));
if (out == NULL) {
log_fatal("failed to get substring malloc: %s", strerror(errno));
exit(1);
}
/* Copy substring into ss */
while (i < l) {
out[i] = s[pos + i];
i++;
}
/* Null terminate the substring */
out[i] = '\0';
return out;
}
/**
* @brief concatinate two strings
*
* @param s1 first
* @param s2 second
* @return the concatinated string
*/
char
*concat(const char *s1, const char *s2)
{
int len1, len2;
char *result;
len1 = strlen(s1);
len2 = strlen(s2);
/* attempt to allocate size for the new string */
result = malloc(len1 + len2 + 1);
if (result == NULL) {
log_fatal("malloc: %s", strerror(errno));
exit(1);
}
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1);
// + 1 copies the null terminator
return result;
}