lootsack.h

#ifndef _LOOTSACK_H_
#define _LOOTSACK_H_

#include <time.h>

#define TREASURE_ITEM_MAX 1048576

enum treasure_type {
	/* Increment file version if these values change */
	COIN = 0,
	CROWN,
	DAGGER,
	GEM,
	NECKLACE,
	RING,
	SWORD,

	NUM_TREASURE_TYPES
};

struct treasure {
	/* Increment file version is this struct changes */
	/* sword, crown, necklace, etc */
	enum treasure_type t;
	/* Value expressed in gold pieces (GP) */
	uint16_t v;
	/* Weight expressed in coin-weight */
	uint16_t w;
};

#define SCALED_W_V_RATIO(t) ((t->v<<8)/t->w)

#define TREASURE_FILE_VERSION 1
struct treasure_file_header {
	struct timespec created_on;
	uint32_t       num_entries;
	uint8_t        version;
};

extern struct treasure_type_limits {
	uint16_t v_min, v_max;
	uint16_t w_min, w_max;
} limits[NUM_TREASURE_TYPES];

extern const char *type_names[NUM_TREASURE_TYPES];
extern const char *type_code_names[NUM_TREASURE_TYPES];
extern uint16_t type_frequency[NUM_TREASURE_TYPES];

void print_treasure_item(struct treasure *t);
void print_treasure(struct treasure *t, size_t n);
void print_stats(struct treasure *t, size_t n);

/* Returns number of items written or -1 on error */
int write_treasure_bin(int fd, struct treasure *t, size_t n);
/* Returns number of items written or -1 on error */
int write_treasure_csv(int fd, struct treasure *t, size_t n);
/* Returns number of items read or -1 on error */
int read_treasure_bin(int fd, struct treasure_file_header* h,
    struct treasure **out);
/* Returns number of items read or -1 on error 
 *
 * When reading a csv file, only the `num_entries` attribute of the header
 * will contain meaningful data.
 * */
int read_treasure_csv(int fd, struct treasure_file_header* h,
    struct treasure **out);

#endif