更新 6.0s1.h

This commit is contained in:
rabix 2024-06-11 00:32:57 +08:00
parent 80463fd688
commit 6bf4d00b6b
1 changed files with 17 additions and 16 deletions

33
6.0s1.h
View File

@ -6,6 +6,7 @@
struct s1_location {
const char *file;
int line;
const char *func;
};
typedef int32_t s1_int;
@ -14,7 +15,7 @@ typedef double s1_double;
typedef char s1_char;
#define S1_LOC() (struct s1_location){__FILE__, __LINE__}
#define S1_LOC() (struct s1_location){__FILE__, __LINE__, __func__}
# ifndef S1_6_0_H_IMPL
@ -24,13 +25,13 @@ typedef char s1_char;
// 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);
fprintf(stderr, "%s:%d [in %s] %s\n", loc.file, loc.line, loc.func, reason);
exit(3);
}
}
void s1_trace(const char *text, struct s1_location loc) {
fprintf(stderr, "Trace: %s:%d: %s\n", loc.file, loc.line, text);
fprintf(stderr, "Trace: %s:%d: [in %s] %s\n", loc.file, loc.line, loc.func, text);
}
@ -39,23 +40,23 @@ void s1_trace(const char *text, struct s1_location loc) {
// 2. simple IO
void write_int(s1_int value, struct s1_location loc) {
int rc = printf(PRId32, value);
s1_assert(rc != EOF, "Error: write_int", loc);
int rc = printf("%" PRId32, value);
s1_assert(rc != EOF, "write_int: IOError", loc);
}
void write_long(s1_long value, struct s1_location loc) {
int rc = printf(PRId64, value);
s1_assert(rc != EOF, "Error: write_long", loc);
int rc = printf("%" PRId64, value);
s1_assert(rc != EOF, "write_long: IOError", loc);
}
void write_double(s1_double value, struct s1_location loc) {
int rc = printf("%lf", value);
s1_assert(rc != EOF, "Error: write_long", loc);
s1_assert(rc != EOF, "write_double: IOError", loc);
}
void write_char(s1_char value, struct s1_location loc) {
int rc = printf("%c", value);
s1_assert(rc != EOF, "Error: write_char", loc);
s1_assert(rc != EOF, "write_char: IOError", loc);
}
# define write_int(value) write_int(value, S1_LOC())
@ -65,29 +66,29 @@ void write_char(s1_char value, struct s1_location loc) {
s1_int read_int(struct s1_location loc) {
s1_int value = 0;
int rc = scanf(SCNd32, &value);
s1_assert(rc == 1, "Error: read_int", loc);
int rc = scanf("%" SCNd32, &value);
s1_assert(rc == 1, "read_int: IOError", loc);
return value;
}
s1_long read_long(struct s1_location loc) {
s1_long value = 0;
int rc = scanf(SCNd64, &value);
s1_assert(rc == 1, "Error: read_long", loc);
int rc = scanf("%" SCNd64, &value);
s1_assert(rc == 1, "read_long: IOError", 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);
s1_assert(rc == 1, "read_double: IOError", 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);
s1_assert(rc == 1, "read_char: IOError", loc);
return value;
}
@ -98,7 +99,7 @@ void read_pnl(struct s1_location loc) {
if (feof(stdin)) {
break;
}
s1_assert(rc == 1, "Error: read_pnl", loc);
s1_assert(rc == 1, "read_pnl: IOError", loc);
if (value == '\n') {
break;
}