6.0s1/6.0s1.h

117 lines
2.7 KiB
C
Raw Normal View History

2024-06-07 06:27:09 +08:00
#ifndef S1_6_0_H
#define S1_6_0_H
2024-06-07 09:37:16 +08:00
#include <inttypes.h>
2024-06-07 06:27:09 +08:00
struct s1_location {
const char *file;
int line;
};
2024-06-07 09:37:16 +08:00
typedef int32_t s1_int;
typedef int64_t s1_long;
2024-06-07 09:33:04 +08:00
typedef double s1_double;
typedef char s1_char;
2024-06-07 06:27:09 +08:00
#define S1_LOC() (struct s1_location){__FILE__, __LINE__}
# ifndef S1_6_0_H_IMPL
# include <stdio.h>
# include <stdlib.h>
// 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);
}
}
2024-06-07 09:31:06 +08:00
void s1_trace(const char *text, struct s1_location loc) {
fprintf(stderr, "Trace: %s:%d: %s\n", loc.file, loc.line, text);
}
#define s1_trace(text) s1_trace(text, S1_LOC())
2024-06-07 06:27:09 +08:00
// 2. simple IO
2024-06-07 09:33:04 +08:00
void write_int(s1_int value, struct s1_location loc) {
2024-06-07 09:37:16 +08:00
int rc = printf(PRId32, value);
2024-06-07 06:27:09 +08:00
s1_assert(rc != EOF, "Error: write_int", loc);
}
2024-06-07 09:33:04 +08:00
void write_long(s1_long value, struct s1_location loc) {
2024-06-07 09:37:16 +08:00
int rc = printf(PRId64, value);
2024-06-07 06:27:09 +08:00
s1_assert(rc != EOF, "Error: write_long", loc);
}
2024-06-07 09:33:04 +08:00
void write_double(s1_double value, struct s1_location loc) {
2024-06-07 09:31:06 +08:00
int rc = printf("%lf", value);
2024-06-07 06:27:09 +08:00
s1_assert(rc != EOF, "Error: write_long", loc);
}
2024-06-07 09:33:04 +08:00
void write_char(s1_char value, struct s1_location loc) {
2024-06-07 06:27:09 +08:00
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())
2024-06-07 09:33:04 +08:00
s1_int read_int(struct s1_location loc) {
s1_int value = 0;
2024-06-07 09:37:16 +08:00
int rc = scanf(SCNd32, &value);
2024-06-07 06:27:09 +08:00
s1_assert(rc == 1, "Error: read_int", loc);
return value;
}
2024-06-07 09:33:04 +08:00
s1_long read_long(struct s1_location loc) {
s1_long value = 0;
2024-06-07 09:37:16 +08:00
int rc = scanf(SCNd64, &value);
2024-06-07 06:27:09 +08:00
s1_assert(rc == 1, "Error: read_long", loc);
return value;
}
2024-06-07 09:33:04 +08:00
s1_double read_double(struct s1_location loc) {
s1_double value = 0.0;
2024-06-07 06:27:09 +08:00
int rc = scanf("%lf", &value);
s1_assert(rc == 1, "Error: read_double", loc);
return value;
}
2024-06-07 09:33:04 +08:00
s1_char read_char(struct s1_location loc) {
s1_char value = 0;
2024-06-07 06:27:09 +08:00
int rc = scanf("%c", &value);
s1_assert(rc == 1, "Error: read_char", loc);
return value;
}
void read_pnl(struct s1_location loc) {
2024-06-07 09:33:04 +08:00
s1_char value = 0;
2024-06-07 06:27:09 +08:00
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