Mercurial > audlegacy-plugins
comparison src/Input/timidity/libtimidity/common.c @ 0:13389e613d67 trunk
[svn] - initial import of audacious-plugins tree (lots to do)
| author | nenolod |
|---|---|
| date | Mon, 18 Sep 2006 01:11:49 -0700 |
| parents | |
| children | 088092a52fea |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:13389e613d67 |
|---|---|
| 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 "libaudacious/vfs.h" | |
| 29 #include <stdlib.h> | |
| 30 #include <string.h> | |
| 31 | |
| 32 /* I guess "rb" should be right for any libc */ | |
| 33 #define OPEN_MODE "rb" | |
| 34 | |
| 35 #include "timidity.h" | |
| 36 #include "timidity_internal.h" | |
| 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 VFSFile *open_file(char *name) | |
| 45 { | |
| 46 VFSFile *fp; | |
| 47 | |
| 48 if (!name || !(*name)) | |
| 49 { | |
| 50 DEBUG_MSG("Attempted to open nameless file.\n"); | |
| 51 return 0; | |
| 52 } | |
| 53 | |
| 54 /* First try the given name */ | |
| 55 | |
| 56 DEBUG_MSG("Trying to open %s\n", name); | |
| 57 if ((fp = vfs_fopen(name, OPEN_MODE))) | |
| 58 return fp; | |
| 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 DEBUG_MSG("Trying to open %s\n", current_filename); | |
| 81 if ((fp = vfs_fopen(current_filename, OPEN_MODE))) | |
| 82 return fp; | |
| 83 plp = plp->next; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 /* Nothing could be opened. */ | |
| 88 DEBUG_MSG("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 | |
| 99 #ifdef DEBUG | |
| 100 if (p == NULL) | |
| 101 DEBUG_MSG("Sorry. Couldn't malloc %d bytes.\n", count); | |
| 102 #endif | |
| 103 | |
| 104 return p; | |
| 105 } | |
| 106 | |
| 107 /* This adds a directory to the path list */ | |
| 108 void add_to_pathlist(char *s) | |
| 109 { | |
| 110 PathList *plp = safe_malloc(sizeof(PathList)); | |
| 111 | |
| 112 if (plp == NULL) | |
| 113 return; | |
| 114 | |
| 115 plp->path = safe_malloc(strlen(s) + 1); | |
| 116 if (plp->path == NULL) | |
| 117 { | |
| 118 free(plp); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 strcpy(plp->path, s); | |
| 123 plp->next = pathlist; | |
| 124 pathlist = plp; | |
| 125 } | |
| 126 | |
| 127 void free_pathlist(void) | |
| 128 { | |
| 129 PathList *plp = pathlist; | |
| 130 PathList *next; | |
| 131 | |
| 132 while (plp) | |
| 133 { | |
| 134 next = plp->next; | |
| 135 free(plp->path); | |
| 136 free(plp); | |
| 137 plp = next; | |
| 138 } | |
| 139 pathlist = NULL; | |
| 140 } |
