mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 13:37:57 +03:00
* feat!: move C library wrapper funtions to IO.Raw * docs: fixed doc string for (String.allocated?) * docs: removed superfluous parameter names for defns * fix: replaces IO.exit by System.exit * fix: ./test/regression.carp for (IO.read->EOF) * fix: ./test/system.carp * feat: implemented IO.fwrite! * feat!: removed IO.exit * feat!: raw character input now returns Int instead of Char * fix: removed irritating space chars * fix: repaired ./example/carp_demo.carp * fix: reverted System.exit to return an Int for SDL examples * fix: removed System.exit! Co-authored-by: guberatsie <gunnar.bernhardt@siemens.com>
56 lines
1.3 KiB
C
56 lines
1.3 KiB
C
void IO_println(String *s) {
|
|
puts(*s);
|
|
}
|
|
void IO_print(String *s) {
|
|
printf("%s", *s);
|
|
}
|
|
|
|
void IO_errorln(String *s) {
|
|
fprintf(stderr, "%s\n", *s);
|
|
}
|
|
void IO_error(String *s) {
|
|
fprintf(stderr, "%s", *s);
|
|
}
|
|
|
|
String IO_get_MINUS_line() {
|
|
char *b = NULL;
|
|
size_t cap = 64;
|
|
size_t sofar = 0;
|
|
while (1) {
|
|
b = CARP_REALLOC(b, cap);
|
|
b[cap - 1] = -1;
|
|
if (!fgets(b + sofar, cap - sofar, stdin)) break;
|
|
sofar = cap - 1;
|
|
if (b[cap - 1] == -1) break;
|
|
cap *= 2;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
String IO_unsafe_MINUS_read_MINUS_file(const String *filename) {
|
|
String buffer = 0;
|
|
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) {
|
|
size_t sz = fread(buffer, 1, length, f);
|
|
assert(sz == length);
|
|
buffer[sz] = '\0';
|
|
} else {
|
|
fprintf(stderr, "Failed to open buffer from file: %s\n", *filename);
|
|
buffer = String_empty();
|
|
}
|
|
fclose(f);
|
|
} else {
|
|
fprintf(stderr, "Failed to open file: %s\n", *filename);
|
|
buffer = String_empty();
|
|
}
|
|
|
|
return buffer;
|
|
}
|