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); }
|
|
|
|
|
2019-10-30 10:47:36 +03:00
|
|
|
char IO_EOF = (char)EOF;
|
2018-03-23 18:24:32 +03:00
|
|
|
|
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);
|
2019-10-30 10:47:36 +03:00
|
|
|
if (c == EOF) { return -1; }
|
2019-03-05 18:37:27 +03:00
|
|
|
|
|
|
|
if (*lineptr == NULL) {
|
|
|
|
*lineptr = malloc(128);
|
2019-10-30 10:47:36 +03:00
|
|
|
if (*lineptr == NULL) { return -1; }
|
2019-03-05 18:37:27 +03:00
|
|
|
*n = 128;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = 0;
|
2019-10-30 10:47:36 +03:00
|
|
|
while (c != EOF) {
|
2019-03-05 18:37:27 +03:00
|
|
|
if (pos + 1 >= *n) {
|
|
|
|
size_t new_size = *n + (*n >> 2);
|
2019-10-30 10:47:36 +03:00
|
|
|
if (new_size < 128) { new_size = 128; }
|
2019-10-13 11:40:54 +03:00
|
|
|
char *new_ptr = CARP_REALLOC(*lineptr, new_size);
|
2019-10-30 10:47:36 +03:00
|
|
|
if (new_ptr == NULL) { return -1; }
|
2019-03-05 18:37:27 +03:00
|
|
|
*n = new_size;
|
|
|
|
*lineptr = new_ptr;
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:47:36 +03:00
|
|
|
((unsigned char *)(*lineptr))[pos++] = c;
|
|
|
|
if (c == '\n') { break; }
|
2019-03-05 18:37:27 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:38:19 +03:00
|
|
|
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");
|
|
|
|
|
2019-10-30 10:47:36 +03:00
|
|
|
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);
|
2018-01-24 17:53:18 +03:00
|
|
|
buffer[length] = '\0';
|
|
|
|
} else {
|
|
|
|
printf("Failed to open buffer from file: %s\n", *filename);
|
|
|
|
buffer = String_empty();
|
|
|
|
}
|
2019-10-30 10:47:36 +03:00
|
|
|
fclose(f);
|
2018-01-24 17:53:18 +03:00
|
|
|
} else {
|
|
|
|
printf("Failed to open file: %s\n", *filename);
|
|
|
|
buffer = String_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-10-30 10:47:36 +03:00
|
|
|
char IO_fgetc(FILE *f) { return (char)fgetc(f); }
|
2018-03-23 15:28:04 +03:00
|
|
|
|
2019-10-30 10:47:36 +03:00
|
|
|
void IO_fclose(FILE *f) { fclose(f); }
|
2018-03-23 15:28:04 +03:00
|
|
|
|
2019-06-14 15:38:19 +03:00
|
|
|
FILE *IO_fopen(const String *filename, const String *mode) {
|
2018-03-18 19:51:23 +03:00
|
|
|
return fopen(*filename, *mode);
|
|
|
|
}
|