-
Notifications
You must be signed in to change notification settings - Fork 2
/
ptuclib.h
48 lines (40 loc) · 977 Bytes
/
ptuclib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#define writeString(x) printf("%s",(x))
#define writeInteger(x) printf("%d",(x))
#define writeFloat(x) printf("%f", (x))
#define writeReal(x) printf("%g",(x))
#define BUFSIZE 1024
char *readString() {
char buffer[BUFSIZE];
buffer[0] = '\0';
fgets(buffer, BUFSIZE, stdin);
/* strip newline from the end */
uint32_t blen = strlen(buffer);
if (blen > 0 && buffer[blen - 1] == '\n')
buffer[blen - 1] = '\0';
return strdup(buffer);
}
#undef BUFSIZE
/*
this is much more safe and concise that just
using atoi, atof that the original implementation
used
*/
int readInteger() {
char *s = readString();
uint32_t val = (uint32_t) strtol(s, NULL, 10);
free(s);
return val;
}
double readReal() {
char *s = readString();
double val = strtod(s, NULL);
free(s);
return val;
}