summaryrefslogtreecommitdiff
path: root/statefile.c
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-07-20 14:01:01 +0200
committerKevin Chabowski <kevin@kch42.de>2013-07-20 14:01:01 +0200
commit8e6850107adb5ca37ed0e9a4500282b2e8743294 (patch)
treeccd4ef7d03e79a5c9f32359f643275cdd27eb67c /statefile.c
parent2992a3f013aaa833f2c72ef1e368a9752b2bb889 (diff)
downloadnebula2-8e6850107adb5ca37ed0e9a4500282b2e8743294.tar.gz
nebula2-8e6850107adb5ca37ed0e9a4500282b2e8743294.tar.bz2
nebula2-8e6850107adb5ca37ed0e9a4500282b2e8743294.zip
Rough structure done. Parallel working should work.
Diffstat (limited to 'statefile.c')
-rw-r--r--statefile.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/statefile.c b/statefile.c
new file mode 100644
index 0000000..08fb372
--- /dev/null
+++ b/statefile.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "config.h"
+
+int
+state_load(config_t* conf, uint32_t* map, uint32_t* jobs_done) {
+ FILE* fh = NULL;
+ size_t mapsize;
+ int errsv;
+
+ mapsize = conf->width * conf->height * conf->iters_n;
+
+ if(!(fh = fopen(conf->statefile, "rb"))) {
+ if(errno == ENOENT) {
+ memset(map, 0, mapsize);
+ *jobs_done = 0;
+ return 1;
+ }
+
+ return 0;
+ }
+
+ if(fread(jobs_done, sizeof(uint32_t), 1, fh) != 1) {
+ errsv = errno;
+ fclose(fh);
+ errno = errsv;
+ return 0;
+ }
+
+ if(fread(map, sizeof(uint32_t), mapsize, fh) != mapsize) {
+ errsv = errno;
+ fclose(fh);
+ errno = errsv;
+ return 0;
+ }
+
+ fclose(fh);
+ return 1;
+}
+
+int
+state_save(config_t* conf, uint32_t* map, uint32_t jobs_done) {
+ FILE* fh = NULL;
+ size_t mapsize;
+ int errsv;
+
+ mapsize = conf->width * conf->height * conf->iters_n;
+
+ if(!(fh = fopen(conf->statefile, "wb"))) {
+ return 0;
+ }
+
+ if(fwrite(&jobs_done, sizeof(uint32_t), 1, fh) != 1) {
+ errsv = errno;
+ fclose(fh);
+ errno = errsv;
+ return 0;
+ }
+
+ if(fwrite(map, sizeof(uint32_t), mapsize, fh) != mapsize) {
+ errsv = errno;
+ fclose(fh);
+ errno = errsv;
+ return 0;
+ }
+
+ fclose(fh);
+ return 1;
+}