199
|
1 /*
|
|
2
|
|
3 TiMidity -- Experimental MIDI to WAVE converter
|
|
4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
|
|
5
|
|
6 This program is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with this program; if not, write to the Free Software
|
|
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20 common.c
|
|
21
|
|
22 */
|
|
23
|
|
24 #if HAVE_CONFIG_H
|
|
25 # include <config.h>
|
|
26 #endif
|
|
27
|
|
28 #include <stdio.h>
|
|
29 #include <stdlib.h>
|
|
30 #include <string.h>
|
|
31
|
|
32 #include "SDL_sound.h"
|
|
33
|
|
34 #define __SDL_SOUND_INTERNAL__
|
|
35 #include "SDL_sound_internal.h"
|
|
36
|
|
37 #include "options.h"
|
|
38 #include "common.h"
|
|
39
|
|
40 /* The paths in this list will be tried whenever we're reading a file */
|
|
41 static PathList *pathlist = NULL; /* This is a linked list */
|
|
42
|
|
43 /* This is meant to find and open files for reading */
|
|
44 SDL_RWops *open_file(char *name)
|
|
45 {
|
|
46 SDL_RWops *rw;
|
|
47
|
|
48 if (!name || !(*name))
|
|
49 {
|
|
50 SNDDBG(("Attempted to open nameless file.\n"));
|
|
51 return 0;
|
|
52 }
|
|
53
|
|
54 /* First try the given name */
|
|
55
|
|
56 SNDDBG(("Trying to open %s\n", name));
|
|
57 if ((rw = SDL_RWFromFile(name, "rb")))
|
|
58 return rw;
|
|
59
|
|
60 if (name[0] != PATH_SEP)
|
|
61 {
|
|
62 char current_filename[1024];
|
|
63 PathList *plp = pathlist;
|
|
64 int l;
|
|
65
|
|
66 while (plp) /* Try along the path then */
|
|
67 {
|
|
68 *current_filename = 0;
|
|
69 l = strlen(plp->path);
|
|
70 if(l)
|
|
71 {
|
|
72 strcpy(current_filename, plp->path);
|
|
73 if(current_filename[l - 1] != PATH_SEP)
|
|
74 {
|
|
75 current_filename[l] = PATH_SEP;
|
|
76 current_filename[l + 1] = '\0';
|
|
77 }
|
|
78 }
|
|
79 strcat(current_filename, name);
|
|
80 SNDDBG(("Trying to open %s\n", current_filename));
|
|
81 if ((rw = SDL_RWFromFile(current_filename, "rb")))
|
|
82 return rw;
|
|
83 plp = plp->next;
|
|
84 }
|
|
85 }
|
|
86
|
|
87 /* Nothing could be opened. */
|
|
88 SNDDBG(("Could not open %s\n", name));
|
|
89 return 0;
|
|
90 }
|
|
91
|
|
92 /* This'll allocate memory or die. */
|
|
93 void *safe_malloc(size_t count)
|
|
94 {
|
|
95 void *p;
|
|
96
|
|
97 p = malloc(count);
|
|
98 if (p == NULL)
|
|
99 SNDDBG(("Sorry. Couldn't malloc %d bytes.\n", count));
|
|
100
|
|
101 return p;
|
|
102 }
|
|
103
|
|
104 /* This adds a directory to the path list */
|
|
105 void add_to_pathlist(char *s)
|
|
106 {
|
|
107 PathList *plp = safe_malloc(sizeof(PathList));
|
|
108
|
|
109 if (plp == NULL)
|
|
110 return;
|
|
111
|
|
112 plp->path = safe_malloc(strlen(s) + 1);
|
|
113 if (plp->path == NULL)
|
408
|
114 {
|
|
115 free(plp);
|
199
|
116 return;
|
408
|
117 }
|
|
118
|
199
|
119 strcpy(plp->path, s);
|
|
120 plp->next = pathlist;
|
|
121 pathlist = plp;
|
|
122 }
|
|
123
|
|
124 void free_pathlist(void)
|
|
125 {
|
|
126 PathList *plp = pathlist;
|
|
127 PathList *next;
|
|
128
|
|
129 while (plp)
|
|
130 {
|
|
131 next = plp->next;
|
408
|
132 free(plp->path);
|
199
|
133 free(plp);
|
|
134 plp = next;
|
|
135 }
|
457
|
136 pathlist = NULL;
|
199
|
137 }
|