diff --git a/Makefile b/Makefile index 9e1a7ad..ed0108b 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,7 @@ SRC_SRCS_1 += apu.c audio.c SRC_SRCS_1 += file.c save.c SRC_SRCS_1 += overlay.c menu.c SRC_SRCS_1 += state.c ini.c +SRC_SRCS_1 += compat.c SRC_SRCS_1 += sdl_render.c sdl_input.c sdl_audio.c sdl_timer.c SRC_SRCS_1 += sdl_overlay.c diff --git a/src/compat.c b/src/compat.c new file mode 100644 index 0000000..d15457b --- /dev/null +++ b/src/compat.c @@ -0,0 +1,54 @@ +#include "compat.h" + + +#if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__)) + +#include +#include + +/* +static inline size_t strnlen(const char* str, size_t size) { + int len = 0, remain = size; + while (remain > 0 && *str++) --remain; + return (size - remain); +} +*/ + +char* strndup(const char *str, size_t size) { + size_t len = strnlen(str, size); + char* dup = malloc(len + 1); + dup[len] = '\0'; + return memcpy(dup, str, len); +} + +ssize_t getline(char **restrict lineptr, size_t *restrict n, + FILE *restrict stream) { + if (NULL == *lineptr || 0 == *n) { + *n = 120; + *lineptr = (char*)malloc(*n); + if (NULL == *lineptr) return -1; + } + + int chr = 0; + ssize_t len = 0; + char* ptr = *lineptr; + while ((chr = fgetc(stream)) >= 0) { + ++len; + *ptr++ = chr; + if (*n < len + 1) { + char* more = realloc(*lineptr, *n * 2); + if (NULL == more) return -1; + *lineptr = more; + *n *= 2; + ptr = *lineptr + len; + } + if ('\n' == chr) break; + } + + ptr[len] = '\0'; + + return (len == 0 ? -1 : len); +} + + +#endif diff --git a/src/compat.h b/src/compat.h new file mode 100644 index 0000000..08b1ada --- /dev/null +++ b/src/compat.h @@ -0,0 +1,18 @@ +#ifndef NESE_COMPAT_H_ +#define NESE_COMPAT_H_ + +#if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__)) + + +#include + + +char* strndup(const char *str, size_t size); + +ssize_t getline(char **restrict lineptr, size_t *restrict n, + FILE *restrict stream); + + +#endif + +#endif // NESE_COMPAT_H_ diff --git a/src/ini.c b/src/ini.c index ccbbcda..12d0493 100644 --- a/src/ini.c +++ b/src/ini.c @@ -5,6 +5,7 @@ #include #include "ini.h" +#include "compat.h" static inline int stracmp(const char* zstr,