#include #include #include #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; }