summaryrefslogtreecommitdiff
path: root/kon/konsave.h
blob: 9bfe64955a05167163c9fdf39142560990ab2503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* SPDX-License-Identifier: MIT */
/*
 * konsave.h
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef KONSAVE_H
#define KONSAVE_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/*** definition ***/

/* a save is a list of tagged chunks, so each part of a game writes and reads its own and a menu can read just
the little META chunk of every slot. whole thing is checked with a crc, a damaged or cut short file is refused. numbers
are little endian whatever the machine. it all happens in a buffer you own, the file functions are at the end */

typedef struct kon_saveWriter {
	uint8_t *buf;
	size_t cap, len, chunk;
	int error, open;
} kon_saveWriter_t;

typedef struct kon_saveReader {
	const uint8_t *buf;
	size_t len;
	uint32_t version; /* the number you gave kon_saveBegin, to change old saves into new ones */
} kon_saveReader_t;

typedef struct kon_saveCursor {
	const uint8_t *p, *end;
	int error; /* reading past the end gives 0s and sets this */
} kon_saveCursor_t;

/*** writing ***/

void kon_saveBegin(kon_saveWriter_t *w, void *buf, size_t cap, uint32_t version);
void kon_saveChunk(kon_saveWriter_t *w, const char *tag); /* four characters, ends the chunk before it */
void kon_savePutU8(kon_saveWriter_t *w, unsigned v);
void kon_savePutU16(kon_saveWriter_t *w, unsigned v);
void kon_savePutU32(kon_saveWriter_t *w, uint32_t v);
void kon_savePutI32(kon_saveWriter_t *w, int32_t v);
void kon_savePutF32(kon_saveWriter_t *w, float v);
void kon_savePutBytes(kon_saveWriter_t *w, const void *data, size_t n);
void kon_savePutString(kon_saveWriter_t *w, const char *s); /* up to 65535 bytes */
void kon_savePutSwitches(kon_saveWriter_t *w, const uint8_t *switches, int count);
void kon_savePutVars(kon_saveWriter_t *w, const int32_t *vars, int count);

/* how many bytes the save is, or 0 if it did not fit in the buffer */
size_t kon_saveEnd(kon_saveWriter_t *w);

/*** reading ***/

/* 0 if it is not a save, is damaged or is cut short */
int kon_saveOpen(kon_saveReader_t *r, const void *buf, size_t len);
int kon_saveFind(const kon_saveReader_t *r, const char *tag, kon_saveCursor_t *c);

unsigned kon_saveGetU8(kon_saveCursor_t *c);
unsigned kon_saveGetU16(kon_saveCursor_t *c);
uint32_t kon_saveGetU32(kon_saveCursor_t *c);
int32_t kon_saveGetI32(kon_saveCursor_t *c);
float kon_saveGetF32(kon_saveCursor_t *c);
void kon_saveGetBytes(kon_saveCursor_t *c, void *out, size_t n);
void kon_saveGetString(kon_saveCursor_t *c, char *out, size_t size); /* always ends with a 0, cut if it does not fit */

/* into what you have, if the save had more they are dropped and if fewer the rest are 0. return how many the save had */
int kon_saveGetSwitches(kon_saveCursor_t *c, uint8_t *switches, int count);
int kon_saveGetVars(kon_saveCursor_t *c, int32_t *vars, int count);

/*** files ***/

/* written beside the real name first and then moved over it, so a crash leaves the old save. on Windows the old one
is removed just before the move. 1 if it worked */
int kon_saveWriteFile(const char *path, const void *buf, size_t len);

/* how many bytes were read, or -1 if it could not be opened or does not fit in cap */
long kon_saveReadFile(const char *path, void *buf, size_t cap);


#ifdef __cplusplus
}
#endif
#endif /* KONSAVE_H */