diff --git a/6.0s1.h b/6.0s1.h new file mode 100644 index 0000000..fe1ff54 --- /dev/null +++ b/6.0s1.h @@ -0,0 +1,109 @@ +#ifndef S1_6_0_H +#define S1_6_0_H + +struct s1_location { + const char *file; + int line; +}; + +typedef int S1_INT; +typedef long long S1_LONG; +typedef double S1_DOUBLE; +typedef char S1_CHAR; + + +#define S1_LOC() (struct s1_location){__FILE__, __LINE__} + +# ifndef S1_6_0_H_IMPL + +# include +# include + +// 1. assert +void s1_assert(int value, const char *reason, struct s1_location loc) { + if (!value) { + fprintf(stderr, "%s:%d %s\n", loc.file, loc.line, reason); + exit(3); + } +} + + +// 2. simple IO + +void write_int(S1_INT value, struct s1_location loc) { + int rc = printf("%d", value); + s1_assert(rc != EOF, "Error: write_int", loc); +} + +void write_long(S1_LONG value, struct s1_location loc) { + int rc = printf("%lld", value); + s1_assert(rc != EOF, "Error: write_long", loc); +} + +void write_double(S1_DOUBLE value, struct s1_location loc) { + int rc = printf("%f", value); + s1_assert(rc != EOF, "Error: write_long", loc); +} + +void write_char(S1_CHAR value, struct s1_location loc) { + int rc = printf("%c", value); + s1_assert(rc != EOF, "Error: write_char", loc); +} + +#define write_int(value) write_int(value, S1_LOC()) +#define write_long(value) write_long(value, S1_LOC()) +#define write_double(value) write_double(value, S1_LOC()) +#define write_char(value) write_char(value, S1_LOC()) + +S1_INT read_int(struct s1_location loc) { + S1_INT value = 0; + int rc = scanf("%d", &value); + s1_assert(rc == 1, "Error: read_int", loc); + return value; +} + +S1_LONG read_long(struct s1_location loc) { + S1_LONG value = 0; + int rc = scanf("%lld", &value); + s1_assert(rc == 1, "Error: read_long", loc); + return value; +} + +S1_DOUBLE read_double(struct s1_location loc) { + S1_DOUBLE value = 0.0; + int rc = scanf("%lf", &value); + s1_assert(rc == 1, "Error: read_double", loc); + return value; +} + +S1_CHAR read_char(struct s1_location loc) { + S1_CHAR value = 0; + int rc = scanf("%c", &value); + s1_assert(rc == 1, "Error: read_char", loc); + return value; +} + +void read_pnl(struct s1_location loc) { + S1_CHAR value = 0; + while (1) { + int rc = scanf("%c", &value); + if (feof(stdin)) { + break; + } + s1_assert(rc == 1, "Error: read_pnl", loc); + if (value == '\n') { + break; + } + } +} + + +#define read_int() read_int(S1_LOC()) +#define read_long() read_long(S1_LOC()) +#define read_double() read_double(S1_LOC()) +#define read_char() read_char(S1_LOC()) +#define read_pnl() read_pnl(S1_LOC()) +# endif + + +#endif \ No newline at end of file