src/gba/context/directories.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "directories.h"
7
8#include "util/vfs.h"
9
10void GBADirectorySetInit(struct GBADirectorySet* dirs) {
11 dirs->base = 0;
12 dirs->archive = 0;
13 dirs->save = 0;
14 dirs->patch = 0;
15 dirs->state = 0;
16 dirs->screenshot = 0;
17}
18
19void GBADirectorySetDeinit(struct GBADirectorySet* dirs) {
20 GBADirectorySetDetachBase(dirs);
21
22 if (dirs->archive) {
23 dirs->archive->close(dirs->archive);
24 dirs->archive = 0;
25 }
26
27 if (dirs->save) {
28 dirs->save->close(dirs->save);
29 dirs->save = 0;
30 }
31
32 if (dirs->patch) {
33 dirs->patch->close(dirs->patch);
34 dirs->patch = 0;
35 }
36
37 if (dirs->state) {
38 dirs->state->close(dirs->state);
39 dirs->state = 0;
40 }
41
42 if (dirs->screenshot) {
43 dirs->screenshot->close(dirs->screenshot);
44 dirs->screenshot = 0;
45 }
46}
47
48void GBADirectorySetAttachBase(struct GBADirectorySet* dirs, struct VDir* base) {
49 dirs->base = base;
50 if (!dirs->save) {
51 dirs->save = dirs->base;
52 }
53 if (!dirs->patch) {
54 dirs->patch = dirs->base;
55 }
56 if (!dirs->state) {
57 dirs->state = dirs->base;
58 }
59 if (!dirs->screenshot) {
60 dirs->screenshot = dirs->base;
61 }
62}
63
64void GBADirectorySetDetachBase(struct GBADirectorySet* dirs) {
65 if (dirs->save == dirs->base) {
66 dirs->save = 0;
67 }
68 if (dirs->patch == dirs->base) {
69 dirs->patch = 0;
70 }
71 if (dirs->state == dirs->base) {
72 dirs->state = 0;
73 }
74 if (dirs->screenshot == dirs->base) {
75 dirs->screenshot = 0;
76 }
77
78 if (dirs->base) {
79 dirs->base->close(dirs->base);
80 dirs->base = 0;
81 }
82}
83
84struct VFile* GBADirectorySetOpenPath(struct GBADirectorySet* dirs, const char* path, bool (*filter)(struct VFile*)) {
85 dirs->archive = VDirOpenArchive(path);
86 struct VFile* file;
87 if (dirs->archive) {
88 file = VDirFindFirst(dirs->archive, filter);
89 if (!file) {
90 dirs->archive->close(dirs->archive);
91 dirs->archive = 0;
92 }
93 } else {
94 file = VFileOpen(path, O_RDONLY);
95 if (file && !filter(file)) {
96 file->close(file);
97 file = 0;
98 }
99 }
100 return file;
101}