Carp/core/carp_io.h

106 lines
2.4 KiB
C
Raw Normal View History

2018-03-18 16:53:03 +03:00
void IO_println(String *s) { puts(*s); }
void IO_print(String *s) { printf("%s", *s); }
2018-01-24 17:53:18 +03:00
2018-10-23 00:47:40 +03:00
void IO_errorln(String *s) { fprintf(stderr, "%s\n", *s); }
void IO_error(String *s) { fprintf(stderr, "%s", *s); }
2018-03-23 18:24:32 +03:00
char IO_EOF = (char) EOF;
2019-03-05 18:37:27 +03:00
#ifdef _WIN32
// getline isn't a C standard library function so it's missing on windows
// This implementation is stolen from StackOverflow, not sure if it's optimal...
size_t getline(char **lineptr, size_t *n, FILE *stream) {
size_t pos;
int c;
if (lineptr == NULL || stream == NULL || n == NULL) {
errno = EINVAL;
return -1;
}
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (*lineptr == NULL) {
*lineptr = malloc(128);
if (*lineptr == NULL) {
return -1;
}
*n = 128;
}
pos = 0;
while(c != EOF) {
if (pos + 1 >= *n) {
size_t new_size = *n + (*n >> 2);
if (new_size < 128) {
new_size = 128;
}
char *new_ptr = CARP_REALLOC(*lineptr, new_size);
2019-03-05 18:37:27 +03:00
if (new_ptr == NULL) {
return -1;
}
*n = new_size;
*lineptr = new_ptr;
}
((unsigned char *)(*lineptr))[pos ++] = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
(*lineptr)[pos] = '\0';
return pos;
}
#endif
2018-03-18 16:53:03 +03:00
String IO_get_MINUS_line() {
2018-01-24 17:53:18 +03:00
size_t size = 1024;
2018-03-18 16:53:03 +03:00
String buffer = CARP_MALLOC(size);
2018-01-24 17:53:18 +03:00
getline(&buffer, &size, stdin);
return buffer;
}
String IO_read_MINUS_file(const String *filename) {
2018-03-18 16:53:03 +03:00
String buffer = 0;
2018-01-24 17:53:18 +03:00
long length;
FILE *f = fopen(*filename, "rb");
if(f) {
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = CARP_MALLOC (length + 1);
if (buffer) {
fread (buffer, 1, length, f);
buffer[length] = '\0';
} else {
printf("Failed to open buffer from file: %s\n", *filename);
buffer = String_empty();
}
fclose (f);
} else {
printf("Failed to open file: %s\n", *filename);
buffer = String_empty();
}
return buffer;
}
2018-03-23 18:24:32 +03:00
char IO_fgetc(FILE *f) {
return (char) fgetc(f);
}
2018-03-23 18:24:32 +03:00
void IO_fclose(FILE *f) {
fclose(f);
}
FILE *IO_fopen(const String *filename, const String *mode) {
2018-03-18 19:51:23 +03:00
return fopen(*filename, *mode);
}