#include "io.h" #include #include #include static bool read_line_raw(FILE* stream, char** msgptr, size_t* n) { if (msgptr == NULL || n == NULL) return 1; if (*msgptr == NULL) { *n = 128; *msgptr = malloc(*n); if (*msgptr == NULL) return 1; } while (1) { size_t pos = 0; int32_t c; while ((c = fgetc(stream)) != EOF) { /* If the string dosent fit, double the size */ if (pos + 1 >= *n) { size_t new_size = *n * 2; char* new_ptr = realloc(*msgptr, new_size); if (new_ptr == NULL) return 1; *msgptr = new_ptr; *n = new_size; } if (c == '\n' || c == '#') { /* Skip the info after comments */ if (c == '#') { /* Read and discard the rest of the line until a newline or EOF */ while ((c = fgetc(stream)) != EOF && c != '\n'); } break; /* Stop reading for this line. */ } (*msgptr)[pos++] = (char)c; /* Add the corresponding characters */ } if (pos == 0 && c == EOF) return 1; (*msgptr)[pos] = '\0'; /* Add null byte */ return 0; } } char* prompt_line(const char* prompt) { if (isatty(fileno(stdin))) { char* line = readline(prompt); if (line && *line) add_history(line); return line; } else { printf("%s", prompt); fflush(stdout); char* buf = NULL; size_t size = 0; if (read_line_raw(stdin, &buf, &size)) { free(buf); return NULL; } return buf; } } bool read_line(FILE* stream, char** msgptr, size_t* n) { if (msgptr == NULL || n == NULL) return 1; if (*msgptr == NULL) { *n = 128; *msgptr = malloc(*n); if (*msgptr == NULL) return 1; } while (1) { size_t pos = 0; int32_t c; while ((c = fgetc(stream)) != EOF) { /* If the string dosent fit, double the size */ if (pos + 1 >= *n) { size_t new_size = *n * 2; char* new_ptr = realloc(*msgptr, new_size); if (new_ptr == NULL) return 1; *msgptr = new_ptr; *n = new_size; } if (c == '\n' || c == '#') { /* Skip the info after comments */ if (c == '#') { /* Read and discard the rest of the line until a newline or EOF */ while ((c = fgetc(stream)) != EOF && c != '\n'); } break; /* Stop reading for this line. */ } (*msgptr)[pos++] = (char)c; /* Add the corresponding characters */ } if (pos == 0) { if (c == EOF) return 1; else continue; } (*msgptr)[pos] = '\0'; /* Add null byte */ return 0; } } int read_int128_triplet(const char* prompt, int128_t* a, int128_t* b, int128_t* c) { char sa[256], sb[256], sc[256]; if (printf("%s ", prompt) < 0) return -1; if (scanf("%255s %255s %255s", sa, sb, sc) != 3) return -2; *a = str_to_int128(sa); *b = str_to_int128(sb); *c = str_to_int128(sc); return 0; } int128_t str_to_int128(const char* s) { int sign = (*s == '-') ? (s++, -1) : 1; int128_t v = 0; while (*s >= '0' && *s <= '9') v = v * 10 + (*s++ - '0'); return sign * v; } void fprint_int128(FILE* output, int128_t n) { if (n == 0) { fprintf(output, "0"); return; } char buf[40]; int i = 0; int neg = n < 0; if (neg) n = -n; while (n > 0) { buf[i++] = '0' + n % 10; n /= 10; } if (neg) fputc('-', output); while (i--) fputc(buf[i], output); } void print_int128(int128_t n) { if (n == 0) { printf("0"); return; } char buf[40]; int i = 0; int neg = n < 0; if (neg) n = -n; while (n > 0) { buf[i++] = '0' + n % 10; n /= 10; } if (neg) putchar('-'); while (i--) putchar(buf[i]); }