-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
109 lines (95 loc) · 3.64 KB
/
io.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "io.h"
void fatal_error(const char *message) {
//prints error message, then closes program.
printf("Error: %s\n", message);
exit(1);
}
void write_byte(FILE *out, char val) {
//writes a single char 'val' into FILE *out
if (fwrite(&val, sizeof(char), 1, out) != 1) {
//calls on fatal error if it cannot write the character.
fatal_error("write_byte cannot execute");
}
}
void write_bytes(FILE *out, const char data[], unsigned n) {
//writes the array of characters 'data' into FILE *out
if (fwrite(data, 1, n, out) != n) {
//calls on fatal error if it writes less than or more than
//n, the number of characters in the array.
fatal_error("write_bytes cannot execute");
}
}
void write_u16(FILE *out, uint16_t value) {
//writes a single uint16_t 'value' into FILE *out/
if (fwrite(&value, sizeof(uint16_t), 1, out) != 1) {
//calls on fatal error if it cannot write the uint16_t.
fatal_error("write_u16 cannot execute");
}
}
void write_u32(FILE *out, uint32_t value) {
//writes a single uint32_t 'value' into FILE *out/
if (fwrite(&value, sizeof(uint32_t), 1, out) != 1) {
//calls on fatal error if it cannot write the uint32_t.
fatal_error("write_u32 cannot execute");
}
}
void write_s16(FILE *out, int16_t value) {
//writes a single int16_t 'value' into FILE *out/
if (fwrite(&value, sizeof(int16_t), 1, out) != 1) {
//calls on fatal error if it cannot write the int16_t.
fatal_error("write_s16 cannot execute");
}
}
void write_s16_buf(FILE *out, const int16_t buf[], unsigned n) {
//writes an array of n characters into FILE *out by repeatedly calling
//on the write_s16 function in a for loop.
for (unsigned i = 0; i < n; i++) {
write_s16(out, buf[i]);
}
}
void read_byte(FILE *in, char *val) {
//reads a single char from FILE *in and stores it to the pointer val.
if (fread(val, sizeof(char), 1, in) != 1) {
//calls on fatal error if it does not read exactly 1 char.
fatal_error("read_byte cannot execute");
}
}
void read_bytes(FILE *in, char data[], unsigned n) {
//reads an array of chars from FILE *in and stores it to the
//array data. We expect to read n characters.
if (fread(data, 1, n, in) != n) {
//calls on fatal error if it does not read exactly n chars.
fatal_error("read_bytes cannot execute");
}
}
void read_u16(FILE *in, uint16_t *val) {
//reads a single uint16_t from FILE *in and stores it to the pointer val.
if (fread(val, sizeof(uint16_t), 1, in) != 1) {
//calls on fatal error if it does not read exactly 1 uint16_t.
fatal_error("read_u16 cannot execute");
}
}
void read_u32(FILE *in, uint32_t *val) {
//reads a single uint32_t from FILE *in and stores it to the pointer val.
if (fread(val, sizeof(uint32_t), 1, in) != 1) {
//calls on fatal error if it does not read exactly 1 uint32_t.
fatal_error("read_u32 cannot execute");
}
}
void read_s16(FILE *in, int16_t *val) {
//reads a single int16_t from FILE *in and stores it to the pointer val.
if (fread(val, sizeof(int16_t), 1, in) != 1) {
//calls on fatal error if it does not read exactly 1 int16_t.
fatal_error("read_s16 cannot execute");
}
}
void read_s16_buf(FILE *in, int16_t buf[], unsigned n) {
//reads an array of n characters from FILE *in and stores them
//in an array buf[], passed into the function.
for (unsigned i = 0; i < n; i++) {
read_s16(in, &buf[i]);
}
}