From: Christian Heller Date: Wed, 19 Jun 2013 00:28:11 +0000 (+0200) Subject: Moved low-level read/write-to-file functions into their own library. X-Git-Tag: tce~1217 X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/todos?a=commitdiff_plain;h=cbaece42c0073aef0c915966483c5d138315e126;p=plomrogue Moved low-level read/write-to-file functions into their own library. --- diff --git a/Makefile b/Makefile index 72e9113..176bd68 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,9 @@ roguelike: cc -Wall -g -o windows.o -c windows.c cc -Wall -g -o draw_wins.o -c draw_wins.c cc -Wall -g -o keybindings.o -c keybindings.c + cc -Wall -g -o readwrite.o -c readwrite.c cc -Wall -g -o roguelike.o -c roguelike.c - cc -Wall -g -o roguelike windows.o draw_wins.o keybindings.o roguelike.o -lncurses + cc -Wall -g -o roguelike windows.o draw_wins.o keybindings.o readwrite.o roguelike.o -lncurses clean: rm *.o; rm roguelike diff --git a/readwrite.c b/readwrite.c new file mode 100644 index 0000000..ed5a3a3 --- /dev/null +++ b/readwrite.c @@ -0,0 +1,40 @@ +#include +#include +#include + +uint16_t read_uint16_bigendian(FILE * file) { +// Read uint16 from file in big-endian order. + const uint16_t nchar = UCHAR_MAX + 1; + unsigned char a = fgetc(file); + unsigned char b = fgetc(file); + return (a * nchar) + b; } + +void write_uint16_bigendian(uint16_t x, FILE * file) { +// Write uint16 to file in beg-endian order. + const uint16_t nchar = UCHAR_MAX + 1; + unsigned char a = x / nchar; + unsigned char b = x % nchar; + fputc(a, file); + fputc(b, file); } + +uint32_t read_uint32_bigendian(FILE * file) { +// Read uint32 from file in big-endian order. + const uint16_t nchar = UCHAR_MAX + 1; + unsigned char a = fgetc(file); + unsigned char b = fgetc(file); + unsigned char c = fgetc(file); + unsigned char d = fgetc(file); + return (a * nchar * nchar * nchar) + (b * nchar * nchar) + (c * nchar) + d; } + +void write_uint32_bigendian(uint32_t x, FILE * file) { +// Write uint32 to file in beg-endian order. + const uint16_t nchar = UCHAR_MAX + 1; + unsigned char a = x / (nchar * nchar * nchar); + unsigned char b = (x - (a * nchar * nchar * nchar)) / (nchar * nchar); + unsigned char c = (x - ((a * nchar * nchar * nchar) + (b * nchar * nchar))) / nchar; + unsigned char d = x % nchar; + fputc(a, file); + fputc(b, file); + fputc(c, file); + fputc(d, file); } + diff --git a/readwrite.h b/readwrite.h new file mode 100644 index 0000000..28bc3ab --- /dev/null +++ b/readwrite.h @@ -0,0 +1,5 @@ +uint16_t read_uint16_bigendian(FILE * file); +void write_uint16_bigendian(uint16_t x, FILE * file); +uint32_t read_uint32_bigendian(FILE * file); +void write_uint32_bigendian(uint32_t x, FILE * file); + diff --git a/readwrite.o b/readwrite.o new file mode 100644 index 0000000..b8faf76 Binary files /dev/null and b/readwrite.o differ diff --git a/roguelike.c b/roguelike.c index 2bab85f..f0057e2 100644 --- a/roguelike.c +++ b/roguelike.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -9,6 +8,7 @@ #include "draw_wins.h" #include "roguelike.h" #include "keybindings.h" +#include "readwrite.h" uint16_t rrand(char use_seed, uint32_t new_seed) { // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability. @@ -18,42 +18,6 @@ uint16_t rrand(char use_seed, uint32_t new_seed) { seed = ((seed * 1103515245) + 12345) % 2147483648; // Values as recommended by POSIX.1-2001 (see rand(3)). return (seed / 65536); } // Ignore least significant 16 bits (they are less random). -uint16_t read_uint16_bigendian(FILE * file) { -// Read uint16 from file in big-endian order. - const uint16_t nchar = UCHAR_MAX + 1; - unsigned char a = fgetc(file); - unsigned char b = fgetc(file); - return (a * nchar) + b; } - -void write_uint16_bigendian(uint16_t x, FILE * file) { -// Write uint16 to file in beg-endian order. - const uint16_t nchar = UCHAR_MAX + 1; - unsigned char a = x / nchar; - unsigned char b = x % nchar; - fputc(a, file); - fputc(b, file); } - -uint32_t read_uint32_bigendian(FILE * file) { -// Read uint32 from file in big-endian order. - const uint16_t nchar = UCHAR_MAX + 1; - unsigned char a = fgetc(file); - unsigned char b = fgetc(file); - unsigned char c = fgetc(file); - unsigned char d = fgetc(file); - return (a * nchar * nchar * nchar) + (b * nchar * nchar) + (c * nchar) + d; } - -void write_uint32_bigendian(uint32_t x, FILE * file) { -// Write uint32 to file in beg-endian order. - const uint16_t nchar = UCHAR_MAX + 1; - unsigned char a = x / (nchar * nchar * nchar); - unsigned char b = (x - (a * nchar * nchar * nchar)) / (nchar * nchar); - unsigned char c = (x - ((a * nchar * nchar * nchar) + (b * nchar * nchar))) / nchar; - unsigned char d = x % nchar; - fputc(a, file); - fputc(b, file); - fputc(c, file); - fputc(d, file); } - void save_game(struct World * world) { // Save game data to game file. FILE * file = fopen("savefile", "w"); diff --git a/roguelike.h b/roguelike.h index 3fdbbaf..d82313e 100644 --- a/roguelike.h +++ b/roguelike.h @@ -25,10 +25,6 @@ struct Monster { uint16_t x; }; uint16_t rrand(char, uint32_t); -uint16_t read_uint16_bigendian(FILE * file); -void write_uint16_bigendian(uint16_t x, FILE * file); -uint32_t read_uint32_bigendian(FILE * file); -void write_uint32_bigendian(uint32_t x, FILE * file); void save_game(struct World *); void toggle_window (struct WinMeta *, struct Win *); void scroll_pad (struct WinMeta *, char);