| | 1 | #ifdef RCSID |
| | 2 | static char RCSid[] = |
| | 3 | "$Header: d:/cvsroot/tads/tads3/resload.cpp,v 1.2 1999/05/17 02:52:29 MJRoberts Exp $"; |
| | 4 | #endif |
| | 5 | |
| | 6 | /* |
| | 7 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| | 8 | * |
| | 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| | 10 | * on using and copying this software. |
| | 11 | */ |
| | 12 | /* |
| | 13 | Name |
| | 14 | resload.cpp - resource loader |
| | 15 | Function |
| | 16 | |
| | 17 | Notes |
| | 18 | |
| | 19 | Modified |
| | 20 | 10/17/98 MJRoberts - Creation |
| | 21 | */ |
| | 22 | |
| | 23 | #include <stddef.h> |
| | 24 | #include <string.h> |
| | 25 | |
| | 26 | #include "t3std.h" |
| | 27 | #include "resload.h" |
| | 28 | |
| | 29 | /* ------------------------------------------------------------------------ */ |
| | 30 | /* |
| | 31 | * create resource loader |
| | 32 | */ |
| | 33 | CResLoader::CResLoader() |
| | 34 | { |
| | 35 | /* we have no root directory */ |
| | 36 | root_dir_ = 0; |
| | 37 | |
| | 38 | /* no executable path yet */ |
| | 39 | exe_filename_ = 0; |
| | 40 | } |
| | 41 | |
| | 42 | /* |
| | 43 | * create a resource loader given a root directory |
| | 44 | */ |
| | 45 | CResLoader::CResLoader(const char *root_dir) |
| | 46 | { |
| | 47 | /* remember the root directory for file searches */ |
| | 48 | root_dir_ = lib_copy_str(root_dir); |
| | 49 | |
| | 50 | /* no executable path yet */ |
| | 51 | exe_filename_ = 0; |
| | 52 | } |
| | 53 | |
| | 54 | /* |
| | 55 | * delete resource loader |
| | 56 | */ |
| | 57 | CResLoader::~CResLoader() |
| | 58 | { |
| | 59 | /* free our root directory string and executable path string */ |
| | 60 | lib_free_str(root_dir_); |
| | 61 | lib_free_str(exe_filename_); |
| | 62 | } |
| | 63 | |
| | 64 | /* |
| | 65 | * Open a resource file given the resource path. |
| | 66 | */ |
| | 67 | osfildef *CResLoader::open_res_file(const char *respath, |
| | 68 | const char *deflib, |
| | 69 | const char *exerestype) |
| | 70 | { |
| | 71 | char filepath[OSFNMAX]; |
| | 72 | osfildef *fp; |
| | 73 | |
| | 74 | /* |
| | 75 | * Look for the resource as an external file. If we have a root |
| | 76 | * directory, look for the file under that directory; otherwise, |
| | 77 | * look for it in the current directory. |
| | 78 | */ |
| | 79 | if (root_dir_ != 0) |
| | 80 | { |
| | 81 | char fname[OSFNMAX]; |
| | 82 | |
| | 83 | /* get the resource name as a file path */ |
| | 84 | os_cvt_url_dir(fname, sizeof(fname), respath, FALSE); |
| | 85 | |
| | 86 | /* build a full path from the root directory and the resource path */ |
| | 87 | os_build_full_path(filepath, sizeof(filepath), root_dir_, fname); |
| | 88 | } |
| | 89 | else |
| | 90 | { |
| | 91 | /* get the resource name as a file path */ |
| | 92 | os_cvt_url_dir(filepath, sizeof(filepath), respath, FALSE); |
| | 93 | } |
| | 94 | |
| | 95 | /* try opening the file */ |
| | 96 | fp = osfoprb(filepath, OSFTBIN); |
| | 97 | |
| | 98 | /* if we didn't find it, try looking in the default library */ |
| | 99 | if (fp == 0 && deflib != 0) |
| | 100 | { |
| | 101 | char fname[OSFNMAX]; |
| | 102 | |
| | 103 | /* convert from URL notation to local path conventions */ |
| | 104 | os_cvt_url_dir(fname, sizeof(fname), deflib, FALSE); |
| | 105 | |
| | 106 | /* build the full path, starting in the root resource directory */ |
| | 107 | os_build_full_path(filepath, sizeof(filepath), root_dir_, fname); |
| | 108 | |
| | 109 | /* add the default resource library extension */ |
| | 110 | os_defext(filepath, "t3r"); |
| | 111 | |
| | 112 | /* try opening the file */ |
| | 113 | fp = open_lib_res(filepath, respath); |
| | 114 | } |
| | 115 | |
| | 116 | /* if we still didn't find it, try looking in the executable */ |
| | 117 | if (fp == 0 && exerestype != 0) |
| | 118 | fp = open_exe_res(respath, exerestype); |
| | 119 | |
| | 120 | /* return the result */ |
| | 121 | return fp; |
| | 122 | } |