123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include "../../neo/idlib/precompiled.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include "config.h"
- #include "common.h"
- #include "output.h"
- #include "controls.h"
- #define OPEN_MODE "rb"
- char current_filename[1024];
- #ifdef DEFAULT_PATH
- static PathList defaultpathlist={DEFAULT_PATH,0};
- static PathList *pathlist=&defaultpathlist;
- #else
- static PathList *pathlist=0;
- #endif
- static idFile * try_to_open(char *name, int decompress, int noise_mode)
- {
- idFile * fp;
- fp = fileSystem->OpenFileRead( name );
- if (!fp)
- return 0;
- return fp;
- }
- idFile * open_file(const char *name, int decompress, int noise_mode)
- {
- idFile * fp;
- PathList *plp=pathlist;
- int l;
- if (!name || !(*name))
- {
- ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Attempted to open nameless file.");
- return 0;
- }
-
- strncpy(current_filename, name, 1023);
- current_filename[1023]='\0';
- ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
- if ((fp=try_to_open(current_filename, decompress, noise_mode)))
- return fp;
- if (name[0] != PATH_SEP)
- while (plp)
- {
- *current_filename=0;
- l=strlen(plp->path);
- if(l)
- {
- strcpy(current_filename, plp->path);
- if(current_filename[l-1]!=PATH_SEP)
- strcat(current_filename, PATH_STRING);
- }
- strcat(current_filename, name);
- ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Trying to open %s", current_filename);
- if ((fp=try_to_open(current_filename, decompress, noise_mode)))
- return fp;
- plp=(PathList*)plp->next;
- }
-
- *current_filename=0;
- if (noise_mode>=2)
- ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", name, strerror(errno));
- return 0;
- }
- void close_file(idFile * fp)
- {
- delete fp;
- }
- void skip(idFile * fp, size_t len)
- {
- size_t c;
- char tmp[1024];
- while (len>0)
- {
- c=len;
- if (c>1024) c=1024;
- len-=c;
- if (c!=fp->Read(tmp, c ))
- ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: skip: %s",
- current_filename, strerror(errno));
- }
- }
- void *safe_malloc(size_t count)
- {
- void *p;
- if (count > (1<<21))
- {
- ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
- "Strange, I feel like allocating %d bytes. This must be a bug.",
- count);
- }
- else if ((p=Real_Tim_Malloc(count)))
- return p;
- else
- ctl->cmsg(CMSG_FATAL, VERB_NORMAL, "Sorry. Couldn't malloc %d bytes.", count);
- ctl->close();
-
- return(NULL);
- }
- void add_to_pathlist(char *s)
- {
- PathList *plp=(PathList*)safe_malloc(sizeof(PathList));
- strcpy((plp->path=(char *)safe_malloc(strlen(s)+1)),s);
- plp->next=pathlist;
- pathlist=plp;
- }
- void *Real_Tim_Malloc( int sz ) {
- return malloc( sz );
- }
- void Real_Tim_Free( void *pt ) {
- free( pt );
- }
- void* Real_Malloc( unsigned int sz ) {
- return malloc( sz );
- }
|