Browse Source

Add missing functions for non-POSIX targets

master
Nathaniel Walizer 10 months ago
parent
commit
58ee59d297
4 changed files with 74 additions and 0 deletions
  1. +1
    -0
      Makefile
  2. +54
    -0
      src/compat.c
  3. +18
    -0
      src/compat.h
  4. +1
    -0
      src/ini.c

+ 1
- 0
Makefile View File

@@ -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



+ 54
- 0
src/compat.c View File

@@ -0,0 +1,54 @@
#include "compat.h"


#if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__))

#include <stdlib.h>
#include <string.h>

/*
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

+ 18
- 0
src/compat.h View File

@@ -0,0 +1,18 @@
#ifndef NESE_COMPAT_H_
#define NESE_COMPAT_H_

#if !defined (__unix__) && !(defined (__APPLE__) && defined (__MACH__))


#include <stdio.h>


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_

+ 1
- 0
src/ini.c View File

@@ -5,6 +5,7 @@
#include <string.h>

#include "ini.h"
#include "compat.h"


static inline int stracmp(const char* zstr,


Loading…
Cancel
Save