-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
common.c
113 lines (96 loc) · 2.52 KB
/
common.c
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
108
109
110
111
112
113
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License, available in COPYING.
common.c
*/
#define __SDL_SOUND_INTERNAL__
#include "SDL_sound_internal.h"
#include "options.h"
#include "common.h"
#if defined(_WIN32)||defined(__OS2__)
#define CHAR_DIRSEP '\\'
#define is_dirsep(c) ((c) == '/' || (c) == '\\')
#define is_abspath(p) ((p)[0] == '/' || (p)[0] == '\\' || ((p)[0] && (p)[1] == ':'))
#else /* unix: */
#define CHAR_DIRSEP '/'
#define is_dirsep(c) ((c) == '/')
#define is_abspath(p) ((p)[0] == '/')
#endif
/* The paths in this list will be tried whenever we're reading a file */
typedef struct _PathList {
char *path;
struct _PathList *next;
} PathList;
static PathList *pathlist = NULL;
/* This is meant to find and open files for reading */
SDL_RWops *timi_openfile(const char *name)
{
SDL_RWops *rw;
if (!name || !(*name)) {
SNDDBG(("Attempted to open nameless file.\n"));
return NULL;
}
/* First try the given name */
SNDDBG(("Trying to open %s\n", name));
if ((rw = SDL_RWFromFile(name, "rb")) != NULL)
return rw;
if (!is_abspath(name))
{
char current_filename[1024];
PathList *plp = pathlist;
char *p;
size_t l;
while (plp) { /* Try along the path then */
*current_filename = 0;
p = current_filename;
l = SDL_strlen(plp->path);
if(l >= sizeof(current_filename) - 3) l = 0;
if(l) {
SDL_memcpy(current_filename, plp->path, l);
p += l;
if(!is_dirsep(p[-1])) {
*p++ = CHAR_DIRSEP;
l++;
}
}
SDL_strlcpy(p, name, sizeof(current_filename) - l);
SNDDBG(("Trying to open %s\n", current_filename));
if ((rw = SDL_RWFromFile(current_filename, "rb")))
return rw;
plp = plp->next;
}
}
/* Nothing could be opened. */
SNDDBG(("Could not open %s\n", name));
return NULL;
}
/* This adds a directory to the path list */
int timi_add_pathlist(const char *s, size_t l)
{
PathList *plp = SDL_malloc(sizeof(PathList));
if (plp == NULL) return -2;
plp->path = SDL_malloc(l + 1);
if (plp->path == NULL) {
SDL_free (plp);
return -2;
}
SDL_memcpy(plp->path, s, l);
plp->path[l] = 0;
plp->next = pathlist;
pathlist = plp;
return 0;
}
void timi_free_pathlist(void)
{
PathList *plp = pathlist;
PathList *next;
while (plp) {
next = plp->next;
SDL_free(plp->path);
SDL_free(plp);
plp = next;
}
pathlist = NULL;
}