common.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. TiMidity -- Experimental MIDI to WAVE converter
  3. Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. common.c
  16. */
  17. #include "../../neo/idlib/precompiled.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include "config.h"
  23. #include "common.h"
  24. #include "output.h"
  25. #include "controls.h"
  26. /* I guess "rb" should be right for any libc */
  27. #define OPEN_MODE "rb"
  28. char current_filename[1024];
  29. #ifdef DEFAULT_PATH
  30. /* The paths in this list will be tried whenever we're reading a file */
  31. static PathList defaultpathlist={DEFAULT_PATH,0};
  32. static PathList *pathlist=&defaultpathlist; /* This is a linked list */
  33. #else
  34. static PathList *pathlist=0;
  35. #endif
  36. /* Try to open a file for reading. If the filename ends in one of the
  37. defined compressor extensions, pipe the file through the decompressor */
  38. static idFile * try_to_open(char *name, int decompress, int noise_mode)
  39. {
  40. idFile * fp;
  41. fp = fileSystem->OpenFileRead( name );
  42. if (!fp)
  43. return 0;
  44. return fp;
  45. }
  46. /* This is meant to find and open files for reading, possibly piping
  47. them through a decompressor. */
  48. idFile * open_file(const char *name, int decompress, int noise_mode)
  49. {
  50. idFile * fp;
  51. PathList *plp=pathlist;
  52. int l;
  53. if (!name || !(*name))
  54. {
  55. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Attempted to open nameless file.");
  56. return 0;
  57. }
  58. /* First try the given name */
  59. strncpy(current_filename, name, 1023);
  60. current_filename[1023]='\0';
  61. ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
  62. if ((fp=try_to_open(current_filename, decompress, noise_mode)))
  63. return fp;
  64. if (name[0] != PATH_SEP)
  65. while (plp) /* Try along the path then */
  66. {
  67. *current_filename=0;
  68. l=strlen(plp->path);
  69. if(l)
  70. {
  71. strcpy(current_filename, plp->path);
  72. if(current_filename[l-1]!=PATH_SEP)
  73. strcat(current_filename, PATH_STRING);
  74. }
  75. strcat(current_filename, name);
  76. ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
  77. if ((fp=try_to_open(current_filename, decompress, noise_mode)))
  78. return fp;
  79. plp=(PathList*)plp->next;
  80. }
  81. /* Nothing could be opened. */
  82. *current_filename=0;
  83. if (noise_mode>=2)
  84. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", name, strerror(errno));
  85. return 0;
  86. }
  87. /* This closes files opened with open_file */
  88. void close_file(idFile * fp)
  89. {
  90. delete fp;
  91. }
  92. /* This is meant for skipping a few bytes in a file or fifo. */
  93. void skip(idFile * fp, size_t len)
  94. {
  95. size_t c;
  96. char tmp[1024];
  97. while (len>0)
  98. {
  99. c=len;
  100. if (c>1024) c=1024;
  101. len-=c;
  102. if (c!=fp->Read(tmp, c ))
  103. ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: skip: %s",
  104. current_filename, strerror(errno));
  105. }
  106. }
  107. //extern void *Real_Tim_Malloc( size_t );
  108. /* This'll allocate memory or die. */
  109. void *safe_malloc(size_t count)
  110. {
  111. void *p;
  112. if (count > (1<<21))
  113. {
  114. ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
  115. "Strange, I feel like allocating %d bytes. This must be a bug.",
  116. count);
  117. }
  118. else if ((p=Real_Tim_Malloc(count)))
  119. return p;
  120. else
  121. ctl->cmsg(CMSG_FATAL, VERB_NORMAL, "Sorry. Couldn't malloc %d bytes.", count);
  122. ctl->close();
  123. //exit(10);
  124. return(NULL);
  125. }
  126. /* This adds a directory to the path list */
  127. void add_to_pathlist(char *s)
  128. {
  129. PathList *plp=(PathList*)safe_malloc(sizeof(PathList));
  130. strcpy((plp->path=(char *)safe_malloc(strlen(s)+1)),s);
  131. plp->next=pathlist;
  132. pathlist=plp;
  133. }
  134. /* Required memory management functions */
  135. void *Real_Tim_Malloc( int sz ) {
  136. return malloc( sz );
  137. }
  138. void Real_Tim_Free( void *pt ) {
  139. free( pt );
  140. }
  141. void* Real_Malloc( unsigned int sz ) {
  142. return malloc( sz );
  143. }