Debian Codegraph @

garglk/cheapglk/cgblorb.cpp

raw symbols SCIP
1 /******************************************************************************
2 * *
3 * Copyright (C) 2006-2009 by Tor Andersson, Andrew Plotkin. *
4 * *
5 * This file is part of Gargoyle. *
6 * *
7 * Gargoyle is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Gargoyle is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with Gargoyle; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 * *
21 *****************************************************************************/
22
23 /* cgblorb.c: Blorb functions for Glk API.
24 Designed by Andrew Plotkin <[email protected]>
25 http://www.eblong.com/zarf/glk/index.html
26
27 Portions of this file are copyright (c) 1998-2016, Andrew Plotkin
28 It is distributed under the MIT license; see the "LICENSE" file.
29 */
30
31 #include <cstdio>
32 #include <vector>
33
34 #include "glk.h"
35 #include "garglk.h"
36 #include "gi_blorb.h"
37
38 /* We'd like to be able to deal with game files in Blorb files, even
39 if we never load a sound or image. We'd also like to be able to
40 deal with Data chunks. So we're willing to set a map here. */
41
42 static giblorb_map_t *blorbmap = 0; /* NULL */
43
44 #ifdef GARGLK
45 static strid_t blorbfile = nullptr;
46 #endif
47
48 giblorb_err_t giblorb_set_resource_map(strid_t file)
49 {
50 giblorb_err_t err;
51
52 #ifdef GARGLK
53 if (file->type != strtype_File && file->type != strtype_Memory) {
54 return giblorb_err_NotAMap;
55 }
56
57 if (file->type == strtype_Memory && file->unicode) {
58 return giblorb_err_NotAMap;
59 }
60
61 if (blorbmap != nullptr) {
62 giblorb_destroy_map(blorbmap);
63 blorbmap = nullptr;
64 }
65
66 if (blorbfile != nullptr) {
67 glk_stream_close(blorbfile, nullptr);
68 blorbfile = nullptr;
69 }
70 #endif
71
72 err = giblorb_create_map(file, &blorbmap);
73 if (err) {
74 blorbmap = 0; /* NULL */
75 return err;
76 }
77
78 #ifdef GARGLK
79 blorbfile = file;
80 #endif
81
82 return giblorb_err_None;
83 }
84
85 giblorb_map_t *giblorb_get_resource_map()
86 {
87 return blorbmap;
88 }
89
90 #ifdef GARGLK
91 bool giblorb_copy_resource(glui32 usage, glui32 resnum, glui32 &type, std::vector<unsigned char> &buf)
92 {
93 if (blorbmap == nullptr) {
94 return false;
95 }
96
97 giblorb_result_t blorbres;
98 auto err = giblorb_load_resource(blorbmap, giblorb_method_FilePos, &blorbres, usage, resnum);
99 if (err != giblorb_err_None) {
100 return false;
101 }
102
103 auto pos = blorbres.data.startpos;
104 auto len = blorbres.length;
105
106 try {
107 buf.resize(len);
108 } catch (const std::bad_alloc &) {
109 return false;
110 }
111
112 switch (blorbfile->type) {
113 case strtype_File:
114 if (std::fseek(blorbfile->file, pos, SEEK_SET) == -1 ||
115 std::fread(buf.data(), len, 1, blorbfile->file) != 1) {
116 return false;
117 }
118 break;
119 case strtype_Memory:
120 std::copy(blorbfile->buf + pos, blorbfile->buf + pos + len, buf.begin());
121 break;
122 default:
123 return false;
124 }
125
126 type = blorbres.chunktype;
127
128 return true;
129 }
130 #endif