home · contact · privacy
Forced new documentation style on readwrite library.
[plomrogue] / src / readwrite.c
1 /*  readwrite.c */
2
3 #include "readwrite.h"
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <limits.h>
7
8 static const uint16_t uchar_s = UCHAR_MAX + 1;
9
10 extern uint16_t read_uint16_bigendian(FILE * file) {
11   unsigned char a = fgetc(file);
12   unsigned char b = fgetc(file);
13   return (a * uchar_s) + b; }
14
15 extern uint32_t read_uint32_bigendian(FILE * file) {
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   unsigned char a = x / uchar_s;
24   unsigned char b = x % uchar_s;
25   fputc(a, file);
26   fputc(b, file); }
27
28 extern void write_uint32_bigendian(uint32_t x, FILE * file) {
29   unsigned char a = x / (uchar_s * uchar_s * uchar_s);
30   unsigned char b = (x - (a * uchar_s * uchar_s * uchar_s)) / (uchar_s * uchar_s);
31   unsigned char c = (x - ((a * uchar_s * uchar_s * uchar_s) + (b * uchar_s * uchar_s))) / uchar_s;
32   unsigned char d = x % uchar_s;
33   fputc(a, file);
34   fputc(b, file);
35   fputc(c, file);
36   fputc(d, file); }