libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
region_open.c
1#include <clod/region.h>
2#include "region_impl.h"
3#include "error.h"
4#include <stdlib.h>
5#include <string.h>
6
7
8struct clod_region *clod_region_open(const char *path, const struct clod_region_opts *opts) {
9 struct clod_region *r = malloc(sizeof(struct clod_region));
10 if (!r) return nullptr;
11
12 memset(r, 0, sizeof(struct clod_region));
13 if (!read_opts(&r->opts, opts)) {
14 free(r);
15 return nullptr;
16 }
17
18 mutex_init(&r->mtx);
19 auto const res = dir_open(&r->d, path, &r->opts);
20 if (res != CLOD_REGION_OK) {
21 mutex_destroy(&r->mtx);
22 free(r);
23 return nullptr;
24 }
25
26 r->cache_len = 0;
27 r->cache = nullptr;
28
29 return r;
30}
32 if (r->inside != 0) {
33 region_error(CLOD_REGION_INVALID_USAGE, "Attempted to close region while still in use.");
34 exit(EXIT_FAILURE);
35 }
36
37 mutex_destroy(&r->mtx);
38 auto const dir_res = dir_close(r->d);
39 auto const fc_res = file_cache_destroy(r);
40 free(r);
41 return dir_res != CLOD_REGION_OK ? dir_res : fc_res;
42}
clod_region_result
Definition region.h:33
struct clod_region * clod_region_open(const char *path, const struct clod_region_opts *opts)
Definition region_open.c:8
enum clod_region_result clod_region_close(struct clod_region *r)
Definition region_open.c:31
@ CLOD_REGION_INVALID_USAGE
Definition region.h:39
@ CLOD_REGION_OK
Definition region.h:35
Sized string helpers.