scenefile

diff src/scene.c @ 3:b30f83409769

foo
author John Tsiombikas <nuclear@mutantstargoat.com>
date Sat, 21 Jan 2012 04:14:24 +0200
parents c15992cedec9
children
line diff
     1.1 --- a/src/scene.c	Sun Jan 15 08:32:19 2012 +0200
     1.2 +++ b/src/scene.c	Sat Jan 21 04:14:24 2012 +0200
     1.3 @@ -1,5 +1,7 @@
     1.4 +#include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <string.h>
     1.7 +#include <errno.h>
     1.8  #include "scene.h"
     1.9  #include "dynarr.h"
    1.10  
    1.11 @@ -7,6 +9,19 @@
    1.12  	struct mesh **mesh;
    1.13  };
    1.14  
    1.15 +struct scnfile_io {
    1.16 +	void *uptr;	/* user-data */
    1.17 +
    1.18 +	size_t (*read)(void *buf, size_t bytes, void *uptr);
    1.19 +	size_t (*write)(void *buf, size_t bytes, void *uptr);
    1.20 +	long (*seek)(long offs, int whence, void *uptr);
    1.21 +};
    1.22 +
    1.23 +static size_t def_read(void *buf, size_t bytes, void *uptr);
    1.24 +static size_t def_write(void *buf, size_t bytes, void *uptr);
    1.25 +static long def_seek(long offset, int whence, void *uptr);
    1.26 +
    1.27 +
    1.28  int scnfile_init(struct scenefile *scn)
    1.29  {
    1.30  	if(!(scn->mesh = dynarr_alloc(0, sizeof *scn->mesh))) {
    1.31 @@ -59,6 +74,27 @@
    1.32  
    1.33  int scnfile_load(struct scenefile *scn, const char *fname)
    1.34  {
    1.35 +	FILE *fp;
    1.36 +	int res;
    1.37 +
    1.38 +	if(!(fp = fopen(fname, "rb"))) {
    1.39 +		fprintf(stderr, "scenefile: failed to load: %s: %s\n", fname, strerror(errno));
    1.40 +		return -1;
    1.41 +	}
    1.42 +	res = scnfile_read_file(scn, fp);
    1.43 +	fclose(fp);
    1.44 +	return res;
    1.45 +}
    1.46 +
    1.47 +int scnfile_read_file(struct scenefile *scn, FILE *fp)
    1.48 +{
    1.49 +	struct scnfile_io io = {0, def_read, def_write, def_seek};
    1.50 +	io.uptr = fp;
    1.51 +	return scnfile_read(scn, &io);
    1.52 +}
    1.53 +
    1.54 +int scnfile_read(struct scenefile *scn, struct scnfile_io *io)
    1.55 +{
    1.56  	return -1;	/* TODO */
    1.57  }
    1.58  
    1.59 @@ -86,3 +122,44 @@
    1.60  {
    1.61  	return dynarr_size(scn->mesh);
    1.62  }
    1.63 +
    1.64 +
    1.65 +void scnfile_io_user_data(struct scnfile_io *io, void *uptr)
    1.66 +{
    1.67 +	io->uptr = uptr;
    1.68 +}
    1.69 +
    1.70 +void scnfile_io_read_func(struct scnfile_io *io, size_t (*read)(void*, size_t, void*))
    1.71 +{
    1.72 +	io->read = read;
    1.73 +}
    1.74 +
    1.75 +void scnfile_io_write_func(struct scnfile_io *io, size_t (*write)(void*, size_t, void*))
    1.76 +{
    1.77 +	io->write = write;
    1.78 +}
    1.79 +
    1.80 +void scnfile_io_seek_func(struct scnfile_io *io, long (*seek)(long, int, void*))
    1.81 +{
    1.82 +	io->seek = seek;
    1.83 +}
    1.84 +
    1.85 +
    1.86 +static size_t def_read(void *buf, size_t bytes, void *uptr)
    1.87 +{
    1.88 +	return uptr ? fread(buf, 1, bytes, uptr) : 0;
    1.89 +}
    1.90 +
    1.91 +static size_t def_write(void *buf, size_t bytes, void *uptr)
    1.92 +{
    1.93 +	return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
    1.94 +}
    1.95 +
    1.96 +static long def_seek(long offset, int whence, void *uptr)
    1.97 +{
    1.98 +	if(!uptr || fseek(uptr, offset, whence) == -1) {
    1.99 +		return -1;
   1.100 +	}
   1.101 +	return ftell(uptr);
   1.102 +}
   1.103 +