summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 34d0ff9)
raw | patch | inline | side by side (parent: 34d0ff9)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Tue, 6 Apr 2021 05:28:00 +0000 (08:28 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Tue, 6 Apr 2021 05:28:00 +0000 (08:28 +0300) |
src/CMakeLists.txt | patch | blob | history | |
src/common/files.c | [new file with mode: 0644] | patch | blob |
src/common/files.h | [new file with mode: 0644] | patch | blob |
src/common/streams.c | [new file with mode: 0644] | patch | blob |
src/common/streams.h | [new file with mode: 0644] | patch | blob |
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 65b419637e68be42f584ccf462518461fa424bbd..b7929db3beba03695288e21aac7ae8d1693389c6 100644 (file)
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
set(D2D_OPENAL_ROOT ${D2D_GAME_ROOT}/openal)
set(D2D_STUBSOUND_ROOT ${D2D_GAME_ROOT}/stubsnd)
set(D2D_LIBCP866_ROOT ${D2D_GAME_ROOT}/libs/cp866)
+set(D2D_COMMON_ROOT ${D2D_GAME_ROOT}/common)
aux_source_directory(${D2D_GAME_ROOT} D2D_GAME_SRC)
aux_source_directory(${D2D_SDL_ROOT} D2D_SDL_SRC)
aux_source_directory(${D2D_OPENAL_ROOT} D2D_OPENAL_SRC)
aux_source_directory(${D2D_STUBSOUND_ROOT} D2D_STUBSOUND_SRC)
aux_source_directory(${D2D_LIBCP866_ROOT} D2D_LIBCP866_SRC)
+aux_source_directory(${D2D_COMMON_ROOT} D2D_COMMON_SRC)
if(WITH_SDL)
if(D2D_FOR_EMSCRIPTEN)
message(STATUS "RENDER: " "${RENDER_DRIVER}")
message(STATUS "SOUND: " "${SOUND_DRIVER}")
-set(D2D_USED_SRC ${D2D_GAME_SRC} ${D2D_SYSTEM_SRC} ${D2D_RENDER_SRC} ${D2D_SOUND_SRC} ${D2D_LIBCP866_SRC})
+set(D2D_USED_SRC ${D2D_GAME_SRC} ${D2D_SYSTEM_SRC} ${D2D_RENDER_SRC} ${D2D_SOUND_SRC} ${D2D_LIBCP866_SRC} ${D2D_COMMON_SRC})
set(D2D_USED_INCLUDE_DIR "${D2D_GAME_ROOT}" "${D2D_SYSTEM_INCLUDE_DIR}" "${D2D_RENDER_INCLUDE_DIR}" "${D2D_SOUND_INCLUDE_DIR}" "${D2D_LIBCP866_ROOT}")
set(D2D_USED_LIBRARY "${D2D_SYSTEM_LIBRARY}" "${D2D_RENDER_LIBRARY}" "${D2D_SOUND_LIBRARY}")
#message(STATUS "USED SRC: ${D2D_USED_SRC}")
diff --git a/src/common/files.c b/src/common/files.c
--- /dev/null
+++ b/src/common/files.c
@@ -0,0 +1,95 @@
+#include "files.h"
+
+#include <stdio.h>
+#include <stdint.h>
+#include <assert.h>
+
+static void FILE_Reader_SetPos (Reader *r, int pos) {
+ FILE_Reader *rd = (FILE_Reader*)r;
+ assert(rd != NULL);
+ assert(rd->fp != NULL);
+ assert(pos >= 0);
+ int res = fseek(rd->fp, pos, SEEK_SET);
+ assert(res == 0); // fail
+}
+
+static void FILE_Reader_Read (Reader *r, void *data, size_t size, size_t n) {
+ FILE_Reader *rd = (FILE_Reader*)r;
+ assert(rd != NULL);
+ assert(rd->fp != NULL);
+ size_t res = fread(data, size, n, rd->fp);
+ assert(res == n); // fail
+}
+
+void FILE_AssignReader (FILE_Reader *r, FILE *fp) {
+ assert(r != NULL);
+ assert(fp != NULL);
+ r->base.setpos = FILE_Reader_SetPos;
+ r->base.read = FILE_Reader_Read;
+ r->fp = fp;
+}
+
+int FILE_OpenReader (FILE_Reader *r, const char *name) {
+ assert(r != NULL);
+ assert(name != NULL);
+ FILE *fp = fopen(name, "rb");
+ if (fp) {
+ FILE_AssignReader(r, fp);
+ }
+ return fp != NULL;
+}
+
+void FILE_CloseReader (FILE_Reader *r) {
+ assert(r != NULL);
+ if (r->fp != NULL) {
+ fclose(r->fp);
+ }
+ r->base.setpos = NULL;
+ r->base.read = NULL;
+ r->fp = NULL;
+}
+
+static void FILE_Writer_SetPos (Writer *w, int pos) {
+ FILE_Writer *wr = (FILE_Writer*)w;
+ assert(wr != NULL);
+ assert(wr->fp != NULL);
+ assert(pos >= 0);
+ int res = fseek(wr->fp, pos, SEEK_SET);
+ assert(res == 0); // fail
+}
+
+static void FILE_Writer_Write (Writer *w, const void *data, size_t size, size_t n) {
+ FILE_Writer *wr = (FILE_Writer*)w;
+ assert(wr != NULL);
+ assert(wr->fp != NULL);
+ size_t res = fwrite(data, size, n, wr->fp);
+ assert(res == n); // fail
+}
+
+void FILE_AssignWriter (FILE_Writer *w, FILE *fp) {
+ assert(w != NULL);
+ assert(fp != NULL);
+ w->base.setpos = FILE_Writer_SetPos;
+ w->base.write = FILE_Writer_Write;
+ w->fp = fp;
+}
+
+int FILE_OpenWriter (FILE_Writer *w, const char *name) {
+ assert(w != NULL);
+ assert(name != NULL);
+ FILE *fp = fopen(name, "wb");
+ if (fp) {
+ FILE_AssignWriter(w, fp);
+ }
+ return fp != NULL;
+}
+
+void FILE_CloseWriter (FILE_Writer *w) {
+ assert(w != NULL);
+ if (w->fp != NULL) {
+ fclose(w->fp);
+ }
+ w->base.setpos = NULL;
+ w->base.write = NULL;
+ w->fp = NULL;
+}
diff --git a/src/common/files.h b/src/common/files.h
--- /dev/null
+++ b/src/common/files.h
@@ -0,0 +1,25 @@
+#ifndef COMMON_FILES_H_INCLUDED
+#define COMMON_FILES_H_INCLUDED
+
+#include <stdio.h>
+#include "streams.h"
+
+typedef struct FILE_Reader {
+ Reader base;
+ FILE *fp;
+} FILE_Reader;
+
+typedef struct FILE_Writer {
+ Writer base;
+ FILE *fp;
+} FILE_Writer;
+
+void FILE_AssignReader (FILE_Reader *r, FILE *fp);
+int FILE_OpenReader (FILE_Reader *r, const char *name);
+void FILE_CloseReader (FILE_Reader *r);
+
+void FILE_AssignWriter (FILE_Writer *w, FILE *fp);
+int FILE_OpenWriter (FILE_Writer *w, const char *name);
+void FILE_CloseWriter (FILE_Writer *w);
+
+#endif /* COMMON_FILES_H_INCLUDED */
\ No newline at end of file
diff --git a/src/common/streams.c b/src/common/streams.c
--- /dev/null
+++ b/src/common/streams.c
@@ -0,0 +1,46 @@
+#include "streams.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "misc.h" // endianness conversion
+
+void stream_read (void *data, size_t size, size_t n, Reader *r) {
+ r->read(r, data, size, n);
+}
+
+int8_t stream_read8 (Reader *r) {
+ int8_t x;
+ r->read(r, &x, 1, 1);
+ return x;
+}
+
+int16_t stream_read16 (Reader *r) {
+ int16_t x;
+ r->read(r, &x, 2, 1);
+ return short2host(x);
+}
+
+int32_t stream_read32 (Reader *r) {
+ int32_t x;
+ r->read(r, &x, 4, 1);
+ return int2host(x);
+}
+
+void stream_write (const void *data, size_t size, size_t n, Writer *w) {
+ w->write(w, data, size, n);
+}
+
+void stream_write8 (int8_t x, Writer *w) {
+ w->write(w, &x, 1, 1);
+}
+
+void stream_write16 (int16_t x, Writer *w) {
+ int16_t y = short2host(x);
+ w->write(w, &y, 2, 1);
+}
+
+void stream_write32 (int32_t x, Writer *w) {
+ int32_t y = int2host(x);
+ w->write(w, &y, 4, 1);
+}
diff --git a/src/common/streams.h b/src/common/streams.h
--- /dev/null
+++ b/src/common/streams.h
@@ -0,0 +1,30 @@
+#ifndef COMMON_STREAMS_H_INCLUDED
+#define COMMON_STREAMS_H_INCLUDED
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct Reader Reader;
+typedef struct Writer Writer;
+
+struct Reader {
+ void (*setpos)(Reader *r, int pos);
+ void (*read)(Reader *r, void *data, size_t size, size_t n);
+};
+
+struct Writer {
+ void (*setpos)(Writer *w, int pos);
+ void (*write)(Writer *w, const void *data, size_t size, size_t n);
+};
+
+void stream_read (void *data, size_t size, size_t n, Reader *f);
+int8_t stream_read8 (Reader *f);
+int16_t stream_read16 (Reader *f);
+int32_t stream_read32 (Reader *f);
+
+void stream_write (const void *data, size_t size, size_t n, Writer *w);
+void stream_write8 (int8_t x, Writer *w);
+void stream_write16 (int16_t x, Writer *w);
+void stream_write32 (int32_t x, Writer *w);
+
+#endif /* COMMON_STREAMS_H_INCLUDED */
\ No newline at end of file