Skip to content

Latest commit

 

History

History
133 lines (109 loc) · 2.65 KB

common.c

File metadata and controls

133 lines (109 loc) · 2.65 KB
 
Oct 21, 1999
Oct 21, 1999
1
/*
Oct 18, 2017
Oct 18, 2017
2
Oct 21, 1999
Oct 21, 1999
3
4
5
6
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
Dec 31, 2011
Dec 31, 2011
7
it under the terms of the Perl Artistic License, available in COPYING.
Oct 18, 2017
Oct 18, 2017
8
9
10
11
12
13
14
common.c
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
Oct 21, 1999
Oct 21, 1999
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Oct 18, 2017
Oct 18, 2017
20
#include "SDL.h"
Oct 21, 1999
Oct 21, 1999
21
Oct 18, 2017
Oct 18, 2017
22
23
#include "options.h"
#include "common.h"
Oct 21, 1999
Oct 21, 1999
24
Dec 17, 2019
Dec 17, 2019
25
26
27
28
29
30
31
32
33
34
#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
Oct 18, 2017
Oct 18, 2017
35
36
/* The paths in this list will be tried whenever we're reading a file */
static PathList *pathlist = NULL; /* This is a linked list */
Oct 21, 1999
Oct 21, 1999
37
Oct 18, 2017
Oct 18, 2017
38
39
/* This is meant to find and open files for reading */
SDL_RWops *open_file(const char *name)
Oct 21, 1999
Oct 21, 1999
40
{
Oct 18, 2017
Oct 18, 2017
41
SDL_RWops *rw;
Oct 21, 1999
Oct 21, 1999
42
43
44
if (!name || !(*name))
{
Oct 18, 2017
Oct 18, 2017
45
SNDDBG(("Attempted to open nameless file.\n"));
Oct 21, 1999
Oct 21, 1999
46
47
48
49
50
return 0;
}
/* First try the given name */
Oct 18, 2017
Oct 18, 2017
51
52
53
SNDDBG(("Trying to open %s\n", name));
if ((rw = SDL_RWFromFile(name, "rb")))
return rw;
May 1, 2006
May 1, 2006
54
Dec 17, 2019
Dec 17, 2019
55
if (!is_abspath(name))
Oct 18, 2017
Oct 18, 2017
56
57
58
{
char current_filename[1024];
PathList *plp = pathlist;
Oct 18, 2017
Oct 18, 2017
59
size_t l;
Oct 18, 2017
Oct 18, 2017
60
Oct 21, 1999
Oct 21, 1999
61
62
while (plp) /* Try along the path then */
{
Oct 18, 2017
Oct 18, 2017
63
64
*current_filename = 0;
l = strlen(plp->path);
Oct 21, 1999
Oct 21, 1999
65
66
67
if(l)
{
strcpy(current_filename, plp->path);
Dec 17, 2019
Dec 17, 2019
68
if(!is_dirsep(current_filename[l - 1]))
Oct 18, 2017
Oct 18, 2017
69
{
Dec 17, 2019
Dec 17, 2019
70
current_filename[l] = CHAR_DIRSEP;
Oct 18, 2017
Oct 18, 2017
71
72
current_filename[l + 1] = '\0';
}
Oct 21, 1999
Oct 21, 1999
73
74
}
strcat(current_filename, name);
Oct 18, 2017
Oct 18, 2017
75
76
77
78
SNDDBG(("Trying to open %s\n", current_filename));
if ((rw = SDL_RWFromFile(current_filename, "rb")))
return rw;
plp = plp->next;
Oct 21, 1999
Oct 21, 1999
79
}
Oct 18, 2017
Oct 18, 2017
80
}
Oct 21, 1999
Oct 21, 1999
81
82
/* Nothing could be opened. */
Oct 18, 2017
Oct 18, 2017
83
SNDDBG(("Could not open %s\n", name));
Oct 21, 1999
Oct 21, 1999
84
85
86
87
88
89
90
return 0;
}
/* This'll allocate memory or die. */
void *safe_malloc(size_t count)
{
void *p;
Oct 18, 2017
Oct 18, 2017
91
92
p = malloc(count);
Oct 21, 2017
Oct 21, 2017
93
if (p == NULL) {
Oct 18, 2017
Oct 18, 2017
94
SNDDBG(("Sorry. Couldn't malloc %d bytes.\n", count));
Oct 21, 2017
Oct 21, 2017
95
}
Oct 18, 2017
Oct 18, 2017
96
97
return p;
Oct 21, 1999
Oct 21, 1999
98
99
100
}
/* This adds a directory to the path list */
Dec 17, 2019
Dec 17, 2019
101
void add_to_pathlist(const char *s, size_t l)
Oct 21, 1999
Oct 21, 1999
102
{
Oct 18, 2017
Oct 18, 2017
103
104
105
106
107
PathList *plp = safe_malloc(sizeof(PathList));
if (plp == NULL)
return;
Dec 17, 2019
Dec 17, 2019
108
plp->path = safe_malloc(l + 1);
Oct 18, 2017
Oct 18, 2017
109
110
if (plp->path == NULL)
{
Dec 17, 2019
Dec 17, 2019
111
free (plp);
Oct 18, 2017
Oct 18, 2017
112
113
return;
}
Dec 17, 2019
Dec 17, 2019
114
115
strncpy(plp->path, s, l);
plp->path[l] = 0;
Oct 18, 2017
Oct 18, 2017
116
117
plp->next = pathlist;
pathlist = plp;
Oct 21, 1999
Oct 21, 1999
118
}
Jul 4, 2005
Jul 4, 2005
119
120
121
void free_pathlist(void)
{
Oct 18, 2017
Oct 18, 2017
122
123
PathList *plp = pathlist;
PathList *next;
Jul 4, 2005
Jul 4, 2005
124
Oct 18, 2017
Oct 18, 2017
125
126
127
128
129
130
while (plp)
{
next = plp->next;
free(plp->path);
free(plp);
plp = next;
Jul 4, 2005
Jul 4, 2005
131
}
Oct 18, 2017
Oct 18, 2017
132
pathlist = NULL;
Jul 4, 2005
Jul 4, 2005
133
}