home · contact · privacy
Added command to focus map on player.
[plomrogue] / src / readwrite.c
1 #include "readwrite.h"
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <limits.h>
5
6 static const uint16_t uchar_s = UCHAR_MAX + 1;
7
8 extern uint16_t read_uint16_bigendian(FILE * file) {
9 // Read uint16 from file in big-endian order.
10   unsigned char a = fgetc(file);
11   unsigned char b = fgetc(file);
12   return (a * uchar_s) + b; }
13
14 extern uint32_t read_uint32_bigendian(FILE * file) {
15 // Read uint32 from file in big-endian order.
16   unsigned char a = fgetc(file);
17   unsigned char b = fgetc(file);
18   unsigned char c = fgetc(file);
19   unsigned char d = fgetc(file);
20   return (a * uchar_s * uchar_s * uchar_s) + (b * uchar_s * uchar_s) + (c * uchar_s) + d; }
21
22 extern void write_uint16_bigendian(uint16_t x, FILE * file) {
23 // Write uint16 to file in beg-endian order.
24   unsigned char a = x / uchar_s;
25   unsigned char b = x % uchar_s;
26   fputc(a, file);
27   fputc(b, file); }
28
29 extern void write_uint32_bigendian(uint32_t x, FILE * file) {
30 // Write uint32 to file in beg-endian order.
31   unsigned char a = x / (uchar_s * uchar_s * uchar_s);
32   unsigned char b = (x - (a * uchar_s * uchar_s * uchar_s)) / (uchar_s * uchar_s);
33   unsigned char c = (x - ((a * uchar_s * uchar_s * uchar_s) + (b * uchar_s * uchar_s))) / uchar_s;
34   unsigned char d = x % uchar_s;
35   fputc(a, file);
36   fputc(b, file);
37   fputc(c, file);
38   fputc(d, file); }