vendor/rlite/src/util.c in hirlite-0.0.2.2 vs vendor/rlite/src/util.c in hirlite-0.1.0

- old
+ new

@@ -1,18 +1,18 @@ #include <stdlib.h> #include <string.h> #include <stdio.h> #include <math.h> -#include "sha1.h" +#include "rlite/sha1.h" #ifdef RL_DEBUG #include <unistd.h> #include <sys/stat.h> #include <execinfo.h> #include <valgrind/valgrind.h> #endif -#include "status.h" -#include "util.h" +#include "rlite/status.h" +#include "rlite/util.h" #include <sys/time.h> int _sha1_formatter(unsigned char *data, char formatted[40]) { static const char *hex_lookup = "0123456789ABCDEF"; @@ -168,25 +168,27 @@ } *size = snprintf(*formatted, 22, "%ld", *(long *)v2); return RL_OK; } +#endif + int sha1_formatter(void *v2, char **formatted, int *size) { unsigned char *data = (unsigned char *)v2; - *formatted = rl_malloc(sizeof(char) * 40); + *formatted = rl_malloc(sizeof(char) * 41); if (*formatted == NULL) { return RL_OUT_OF_MEMORY; } + (*formatted)[40] = 0; _sha1_formatter(data, *formatted); if (size) { *size = 40; } return RL_OK; } -#endif // Code for serialize/deserialize double comes from // http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#serialization #define pack754_32(f) (pack754((f), 32, 8)) @@ -329,6 +331,27 @@ d = strtod(str, &eptr); if (_eptr) { *_eptr = _str + (eptr - str); } return d; +} + +char *rl_get_filename_with_suffix(const char *filename, char *suffix) { + int retval = RL_OK; + char *new_path = NULL; + size_t i, last_slash = 0, filenamelen = strlen(filename); + size_t suffixlen = strlen(suffix); + // Adding "." to the beginning of the filename, and null termination + RL_MALLOC(new_path, sizeof(char) * (filenamelen + suffixlen + 2)); + for (i = filenamelen - 1; i > 0; i--) { + if (filename[i] == '/') { + last_slash = i + 1; + break; + } + } + memcpy(new_path, filename, last_slash); + new_path[last_slash] = '.'; + memcpy(&new_path[last_slash + 1], &filename[last_slash], filenamelen - last_slash); + memcpy(&new_path[filenamelen + 1], suffix, suffixlen + 1); +cleanup: + return new_path; }