common.cpp (4221B)
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 #include "../../neo/idlib/precompiled.h" 25 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <string.h> 29 30 #include <errno.h> 31 #include "config.h" 32 #include "common.h" 33 #include "output.h" 34 #include "controls.h" 35 36 /* I guess "rb" should be right for any libc */ 37 #define OPEN_MODE "rb" 38 39 char current_filename[1024]; 40 41 #ifdef DEFAULT_PATH 42 /* The paths in this list will be tried whenever we're reading a file */ 43 static PathList defaultpathlist={DEFAULT_PATH,0}; 44 static PathList *pathlist=&defaultpathlist; /* This is a linked list */ 45 #else 46 static PathList *pathlist=0; 47 #endif 48 49 /* Try to open a file for reading. If the filename ends in one of the 50 defined compressor extensions, pipe the file through the decompressor */ 51 static idFile * try_to_open(char *name, int decompress, int noise_mode) 52 { 53 idFile * fp; 54 55 fp = fileSystem->OpenFileRead( name ); 56 57 if (!fp) 58 return 0; 59 60 return fp; 61 } 62 63 /* This is meant to find and open files for reading, possibly piping 64 them through a decompressor. */ 65 idFile * open_file(const char *name, int decompress, int noise_mode) 66 { 67 idFile * fp; 68 PathList *plp=pathlist; 69 int l; 70 71 if (!name || !(*name)) 72 { 73 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Attempted to open nameless file."); 74 return 0; 75 } 76 77 /* First try the given name */ 78 79 strncpy(current_filename, name, 1023); 80 current_filename[1023]='\0'; 81 82 ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename); 83 if ((fp=try_to_open(current_filename, decompress, noise_mode))) 84 return fp; 85 86 if (name[0] != PATH_SEP) 87 while (plp) /* Try along the path then */ 88 { 89 *current_filename=0; 90 l=strlen(plp->path); 91 if(l) 92 { 93 strcpy(current_filename, plp->path); 94 if(current_filename[l-1]!=PATH_SEP) 95 strcat(current_filename, PATH_STRING); 96 } 97 strcat(current_filename, name); 98 ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename); 99 if ((fp=try_to_open(current_filename, decompress, noise_mode))) 100 return fp; 101 102 plp=(PathList*)plp->next; 103 } 104 105 /* Nothing could be opened. */ 106 107 *current_filename=0; 108 109 if (noise_mode>=2) 110 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", name, strerror(errno)); 111 112 return 0; 113 } 114 115 /* This closes files opened with open_file */ 116 void close_file(idFile * fp) 117 { 118 delete fp; 119 } 120 121 /* This is meant for skipping a few bytes in a file or fifo. */ 122 void skip(idFile * fp, size_t len) 123 { 124 size_t c; 125 char tmp[1024]; 126 while (len>0) 127 { 128 c=len; 129 if (c>1024) c=1024; 130 len-=c; 131 if (c!=fp->Read(tmp, c )) 132 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: skip: %s", 133 current_filename, strerror(errno)); 134 } 135 } 136 137 //extern void *Real_Tim_Malloc( size_t ); 138 /* This'll allocate memory or die. */ 139 void *safe_malloc(size_t count) 140 { 141 void *p; 142 if (count > (1<<21)) 143 { 144 ctl->cmsg(CMSG_FATAL, VERB_NORMAL, 145 "Strange, I feel like allocating %d bytes. This must be a bug.", 146 count); 147 } 148 else if ((p=Real_Tim_Malloc(count))) 149 return p; 150 else 151 ctl->cmsg(CMSG_FATAL, VERB_NORMAL, "Sorry. Couldn't malloc %d bytes.", count); 152 153 ctl->close(); 154 //exit(10); 155 return(NULL); 156 } 157 158 /* This adds a directory to the path list */ 159 void add_to_pathlist(char *s) 160 { 161 PathList *plp=(PathList*)safe_malloc(sizeof(PathList)); 162 strcpy((plp->path=(char *)safe_malloc(strlen(s)+1)),s); 163 plp->next=pathlist; 164 pathlist=plp; 165 } 166 167 /* Required memory management functions */ 168 void *Real_Tim_Malloc( int sz ) { 169 return malloc( sz ); 170 } 171 172 void Real_Tim_Free( void *pt ) { 173 free( pt ); 174 } 175 176 void* Real_Malloc( unsigned int sz ) { 177 return malloc( sz ); 178 }