| 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 | /* get the resource name as a file path */ |
| 82 | char fname[OSFNMAX]; |
| 83 | os_cvt_url_dir(fname, sizeof(fname), respath); |
| 84 | |
| 85 | /* build a full path from the root directory and the resource path */ |
| 86 | os_build_full_path(filepath, sizeof(filepath), root_dir_, fname); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | /* get the resource name as a file path */ |
| 91 | os_cvt_url_dir(filepath, sizeof(filepath), respath); |
| 92 | } |
| 93 | |
| 94 | /* try opening the file */ |
| 95 | fp = osfoprb(filepath, OSFTBIN); |
| 96 | |
| 97 | /* if we didn't find it, try looking in the default library */ |
| 98 | if (fp == 0 && deflib != 0) |
| 99 | { |
| 100 | char fname[OSFNMAX]; |
| 101 | |
| 102 | /* convert from URL notation to local path conventions */ |
| 103 | os_cvt_url_dir(fname, sizeof(fname), deflib); |
| 104 | |
| 105 | /* build the full path, starting in the root resource directory */ |
| 106 | os_build_full_path(filepath, sizeof(filepath), root_dir_, fname); |
| 107 | |
| 108 | /* add the default resource library extension */ |
| 109 | os_defext(filepath, "t3r"); |
| 110 | |
| 111 | /* try opening the file */ |
| 112 | fp = open_lib_res(filepath, respath); |
| 113 | } |
| 114 | |
| 115 | /* if we still didn't find it, try looking in the executable */ |
| 116 | if (fp == 0 && exerestype != 0) |
| 117 | fp = open_exe_res(respath, exerestype); |
| 118 | |
| 119 | /* return the result */ |
| 120 | return fp; |
| 121 | } |