summaryrefslogtreecommitdiff
path: root/src/stream.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2023-12-15 23:02:45 -0500
committerFreya Murphy <freya@freyacat.org>2023-12-15 23:02:45 -0500
commit42d1c82a0bfa615b832f5ecc2652edc290bf6e9c (patch)
tree09223f3ca9b54fc3cd5a7cb45180542cc3a8a1ba /src/stream.c
parentfix printing arrays (diff)
downloadnbtvis-42d1c82a0bfa615b832f5ecc2652edc290bf6e9c.tar.gz
nbtvis-42d1c82a0bfa615b832f5ecc2652edc290bf6e9c.tar.bz2
nbtvis-42d1c82a0bfa615b832f5ecc2652edc290bf6e9c.zip
add json support and other things
Diffstat (limited to '')
-rw-r--r--src/stream.c95
1 files changed, 70 insertions, 25 deletions
diff --git a/src/stream.c b/src/stream.c
index 622fafa..6780d02 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -7,19 +7,37 @@
#include <stdlib.h>
#include <string.h>
-stream_t stream_open(char *path, char* mode) {
+static uint64_t longswap(uint64_t ll) {
+ if (htons(20) == 20)
+ return ll;
+
+ union { uint64_t ll; uint8_t c[8]; } out = {0};
+ union { uint64_t ll; uint8_t c[8]; } in = {ll};
+
+ for (int i = 0; i < 8; i++)
+ out.c[7-i] = in.c[i];
+
+ return out.ll;
+}
+
+
+stream_t stream_open(const char *path, const char* mode) {
stream_t stream;
if (strcmp("-", path) == 0) {
- stream.__file = stdin;
+ if (*mode == 'r')
+ stream.__file = stdin;
+ else
+ stream.__file = stdout;
stream.__alloc = false;
+ return stream;
}
stream.__file = fopen(path, mode);
stream.__alloc = true;
if (stream.__file == NULL) {
- perror_and_die("cannot read '%s'", path);
+ perror_and_die("cannot open '%s'", path);
};
return stream;
@@ -30,7 +48,7 @@ void stream_close(stream_t *stream) {
fclose(stream->__file);
}
-bool stream_read(stream_t *stream, void *res, size_t amount) {
+bool stream_read(const stream_t *stream, void *res, size_t amount) {
size_t read;
read = fread(res, 1, amount, stream->__file);
@@ -38,62 +56,89 @@ bool stream_read(stream_t *stream, void *res, size_t amount) {
if (feof(stream->__file) || errno == 0)
return false;
else
- perror_and_die("cannot read open stream");
+ perror_and_die("cannot read from stream");
}
return true;
}
-bool stream_read_i8(stream_t *stream, int8_t *res) {
+bool stream_read_i8(const stream_t *stream, int8_t *res) {
if (stream_read(stream, res, 1) == false)
return false;
return true;
}
-bool stream_read_i16(stream_t *stream, int16_t *res) {
+bool stream_read_i16(const stream_t *stream, int16_t *res) {
if (stream_read(stream, res, 2) == false)
return false;
*res = ntohs(*res);
return true;
}
-bool stream_read_i32(stream_t *stream, int32_t *res) {
+bool stream_read_i32(const stream_t *stream, int32_t *res) {
if (stream_read(stream, res, 4) == false)
return false;
*res = ntohl(*res);
return true;
}
-static uint64_t ntohll(uint64_t ll) {
- if (htons(20) == 20)
- return ll;
+bool stream_read_i64(const stream_t *stream, int64_t *res) {
+ if (stream_read(stream, res, 8) == false)
+ return false;
+ *res = longswap(*res);
+ return true;
+}
- union { uint64_t ll; uint8_t c[8]; } out = {0};
- union { uint64_t ll; uint8_t c[8]; } in = {ll};
+bool stream_read_u16(const stream_t *stream, uint16_t *res) {
+ if (stream_read(stream, res, 2) == false)
+ return false;
+ *res = ntohs(*res);
+ return true;
+}
- for (int i = 0; i < 8; i++)
- out.c[7-i] = in.c[i];
+bool stream_write(const stream_t *stream, const void *buf, size_t amount) {
+ size_t wrote;
+ wrote = fwrite(buf, 1, amount, stream->__file);
- return out.ll;
+ if (wrote == 0)
+ perror_and_die("cannot write to stream");
+
+ if (wrote < amount)
+ return false;
+
+ return true;
}
-bool stream_read_i64(stream_t *stream, int64_t *res) {
- if (stream_read(stream, res, 8) == false)
+bool stream_write_i8(const stream_t *stream, int8_t b) {
+ if (stream_write(stream, &b, 1) == false)
return false;
- *res = ntohll(*res);
return true;
}
-bool stream_read_u16(stream_t *stream, uint16_t *res) {
- if (stream_read(stream, res, 2) == false)
+bool stream_write_i16(const stream_t *stream, int16_t s) {
+ s = htons(s);
+ if (stream_write(stream, &s, 2) == false)
return false;
- *res = ntohs(*res);
return true;
}
-bool stream_read_u32(stream_t *stream, uint32_t *res) {
- if (stream_read(stream, res, 4) == false)
+bool stream_write_i32(const stream_t *stream, int32_t i) {
+ i = htonl(i);
+ if (stream_write(stream, &i, 4) == false)
+ return false;
+ return true;
+}
+
+bool stream_write_i64(const stream_t *stream, int64_t l) {
+ l = longswap(l);
+ if (stream_write(stream, &l, 8) == false)
+ return false;
+ return true;
+}
+
+bool stream_write_u16(const stream_t *stream, uint16_t s) {
+ s = htons(s);
+ if (stream_write(stream, &s, 2) == false)
return false;
- *res = ntohl(*res);
return true;
}