Debian Codegraph @

src/file.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "file.h"
20
21 #include "opentyr.h"
22 #include "varz.h"
23
24 #include "SDL.h"
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 const char *custom_data_dir = NULL;
32
33 // finds the Tyrian data directory
34 const char *data_dir(void)
35 {
36 const char *const dirs[] =
37 {
38 custom_data_dir,
39 TYRIAN_DIR,
40 "data",
41 ".",
42 };
43
44 static const char *dir = NULL;
45
46 if (dir != NULL)
47 return dir;
48
49 for (uint i = 0; i < COUNTOF(dirs); ++i)
50 {
51 if (dirs[i] == NULL)
52 continue;
53
54 FILE *f = dir_fopen(dirs[i], "tyrian1.lvl", "rb");
55 if (f)
56 {
57 fclose(f);
58
59 dir = dirs[i];
60 break;
61 }
62 }
63
64 if (dir == NULL) // data not found
65 dir = "";
66
67 return dir;
68 }
69
70 // prepend directory and fopen
71 FILE *dir_fopen(const char *dir, const char *file, const char *mode)
72 {
73 char *path = malloc(strlen(dir) + 1 + strlen(file) + 1);
74 sprintf(path, "%s/%s", dir, file);
75
76 FILE *f = fopen(path, mode);
77
78 free(path);
79
80 return f;
81 }
82
83 // warn when dir_fopen fails
84 FILE *dir_fopen_warn(const char *dir, const char *file, const char *mode)
85 {
86 FILE *f = dir_fopen(dir, file, mode);
87
88 if (f == NULL)
89 fprintf(stderr, "warning: failed to open '%s': %s\n", file, strerror(errno));
90
91 return f;
92 }
93
94 // die when dir_fopen fails
95 FILE *dir_fopen_die(const char *dir, const char *file, const char *mode)
96 {
97 FILE *f = dir_fopen(dir, file, mode);
98
99 if (f == NULL)
100 {
101 fprintf(stderr, "error: failed to open '%s': %s\n", file, strerror(errno));
102 fprintf(stderr, "error: One or more of the required Tyrian " TYRIAN_VERSION " data files could not be found.\n"
103 " Please read the README file.\n");
104 JE_tyrianHalt(1);
105 }
106
107 return f;
108 }
109
110 // check if file can be opened for reading
111 bool dir_file_exists(const char *dir, const char *file)
112 {
113 FILE *f = dir_fopen(dir, file, "rb");
114 if (f != NULL)
115 fclose(f);
116 return (f != NULL);
117 }
118
119 // returns end-of-file position
120 long ftell_eof(FILE *f)
121 {
122 long pos = ftell(f);
123
124 fseek(f, 0, SEEK_END);
125 long size = ftell(f);
126
127 fseek(f, pos, SEEK_SET);
128
129 return size;
130 }
131
132 void fread_die(void *buffer, size_t size, size_t count, FILE *stream)
133 {
134 size_t result = fread(buffer, size, count, stream);
135 if (result != count)
136 {
137 fprintf(stderr, "error: An unexpected problem occurred while reading from a file.\n");
138 SDL_Quit();
139 exit(EXIT_FAILURE);
140 }
141 }
142
143 void fwrite_die(const void *buffer, size_t size, size_t count, FILE *stream)
144 {
145 size_t result = fwrite(buffer, size, count, stream);
146 if (result != count)
147 {
148 fprintf(stderr, "error: An unexpected problem occurred while writing to a file.\n");
149 SDL_Quit();
150 exit(EXIT_FAILURE);
151 }
152 }