2018-02-27 16:14:14 +03:00
|
|
|
#pragma once
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <carp_string.h>
|
2018-01-24 17:53:18 +03:00
|
|
|
|
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-03-23 18:24:32 +03:00
|
|
|
char IO_EOF = (char) EOF;
|
|
|
|
|
2018-02-27 17:30:22 +03:00
|
|
|
#ifndef _WIN32
|
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;
|
|
|
|
}
|
2018-02-27 17:30:22 +03:00
|
|
|
#endif
|
2018-01-24 17:53:18 +03:00
|
|
|
|
2018-03-18 16:53:03 +03:00
|
|
|
String IO_read_MINUS_file(String *filename) {
|
|
|
|
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 15:28:04 +03:00
|
|
|
|
2018-03-23 18:24:32 +03:00
|
|
|
void IO_fclose(FILE *f) {
|
|
|
|
fclose(f);
|
2018-03-23 15:28:04 +03:00
|
|
|
}
|
|
|
|
|
2018-03-18 19:51:23 +03:00
|
|
|
FILE *IO_fopen(String *filename, String *mode) {
|
|
|
|
return fopen(*filename, *mode);
|
|
|
|
}
|