File: resload.cpp

package info (click to toggle)
qtads 2.1.6-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,156 kB
  • ctags: 18,767
  • sloc: cpp: 133,078; ansic: 26,048; xml: 18; makefile: 11
file content (121 lines) | stat: -rw-r--r-- 3,047 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/tads3/resload.cpp,v 1.2 1999/05/17 02:52:29 MJRoberts Exp $";
#endif

/* 
 *   Copyright (c) 1998, 2002 Michael J. Roberts.  All Rights Reserved.
 *   
 *   Please see the accompanying license file, LICENSE.TXT, for information
 *   on using and copying this software.  
 */
/*
Name
  resload.cpp - resource loader
Function
  
Notes
  
Modified
  10/17/98 MJRoberts  - Creation
*/

#include <stddef.h>
#include <string.h>

#include "t3std.h"
#include "resload.h"

/* ------------------------------------------------------------------------ */
/*
 *   create resource loader 
 */
CResLoader::CResLoader()
{
    /* we have no root directory */
    root_dir_ = 0;

    /* no executable path yet */
    exe_filename_ = 0;
}

/*
 *   create a resource loader given a root directory 
 */
CResLoader::CResLoader(const char *root_dir)
{
    /* remember the root directory for file searches */
    root_dir_ = lib_copy_str(root_dir);

    /* no executable path yet */
    exe_filename_ = 0;
}

/*
 *   delete resource loader 
 */
CResLoader::~CResLoader()
{
    /* free our root directory string and executable path string */
    lib_free_str(root_dir_);
    lib_free_str(exe_filename_);
}

/*
 *   Open a resource file given the resource path.
 */
osfildef *CResLoader::open_res_file(const char *respath,
                                    const char *deflib,
                                    const char *exerestype)
{
    char filepath[OSFNMAX];
    osfildef *fp;
    
    /* 
     *   Look for the resource as an external file.  If we have a root
     *   directory, look for the file under that directory; otherwise,
     *   look for it in the current directory.  
     */
    if (root_dir_ != 0)
    {
        /* get the resource name as a file path */
        char fname[OSFNMAX];
        os_cvt_url_dir(fname, sizeof(fname), respath);

        /* build a full path from the root directory and the resource path */
        os_build_full_path(filepath, sizeof(filepath), root_dir_, fname);
    }
    else
    {
        /* get the resource name as a file path */
        os_cvt_url_dir(filepath, sizeof(filepath), respath);
    }

    /* try opening the file */
    fp = osfoprb(filepath, OSFTBIN);

    /* if we didn't find it, try looking in the default library */
    if (fp == 0 && deflib != 0)
    {
        char fname[OSFNMAX];

        /* convert from URL notation to local path conventions */
        os_cvt_url_dir(fname, sizeof(fname), deflib);

        /* build the full path, starting in the root resource directory */
        os_build_full_path(filepath, sizeof(filepath), root_dir_, fname);

        /* add the default resource library extension */
        os_defext(filepath, "t3r");

        /* try opening the file */
        fp = open_lib_res(filepath, respath);
    }

    /* if we still didn't find it, try looking in the executable */
    if (fp == 0 && exerestype != 0)
        fp = open_exe_res(respath, exerestype);

    /* return the result */
    return fp;
}