ScummVM API documentation
px_linkeddatafile.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * Additional copyright for this file:
8  * Copyright (C) 1999-2000 Revolution Software Ltd.
9  * This code is based on source code created by Revolution Software,
10  * used with permission.
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef ICB_PX_LINKED_DATA_FILE_H
28 #define ICB_PX_LINKED_DATA_FILE_H
29 
30 #include "engines/icb/common/px_rcutypes.h"
31 #include "engines/icb/common/px_common.h"
32 #include "engines/icb/common/px_clu_api.h"
33 #include "engines/icb/debug.h"
34 
35 #include "common/endian.h"
36 
37 namespace ICB {
38 
39 // This value is returned as an error condition.
40 #define PX_LINKED_DATA_FILE_ERROR (0xffffffff)
41 
42 #define ORDER_PRESERVED_FLAG 1
43 #define NO_NAMES_FLAG 2
44 #define SUPPRESS_OUTPUT 4 // Suppress printing of output
45 
46 typedef struct {
47  int32 name_offset; // offset to Null terminated name of the item
48  int32 data_offset; // Offset to the item
49  int32 data_size; // Size of the items data
50  uint32 name_hash_value; // hash value of name item...
52 
53 typedef struct {
54  px_standard_header header;
55 
56  uint32 number_of_items;
57  uint32 flags;
58 
59  LinkedDataFileEntry list[1]; // 1 used to prevent 0 sized array warnings
60  // This structure is a variable size and so should never
61  // be a parameter to sizeof anyway
63 
64 class LinkedDataObject { // Should be CObjectFile
65 
66 public:
67  static inline uint32 Fetch_number_of_items(LinkedDataFile *file) { // how many
68  return (FROM_LE_32(file->number_of_items));
69  }
70 
71  static inline uint32 GetHeaderVersion(LinkedDataFile *file) { return (FROM_LE_32(file->header.version)); }
72 
73  static inline int32 OrderPreserved(LinkedDataFile *file) { return (FROM_LE_32(file->flags) & (ORDER_PRESERVED_FLAG)); }
74 
75  static inline int32 NameSearchable(LinkedDataFile *file) { return (!OrderPreserved(file)); }
76 
77  static uint32 Fetch_item_number_by_hash(LinkedDataFile *file, const uint32 hash);
78  static uint32 Fetch_item_number_by_name(LinkedDataFile *file, const char *name); // reference a number by its ascii name
79 
80  static void *Fetch_item_by_number(LinkedDataFile *file, uint32 number); // reference a resource by number
81 
82  static void *Fetch_item_by_name(LinkedDataFile *file, const char *name); // reference a resource by its ascii name
83 
84  static void *Fetch_items_name_by_number(LinkedDataFile *file, uint32 number); // return pointer to name of item number n
85 
86  static void *Try_fetch_item_by_name(LinkedDataFile *file, const char *); // reference a resource by name
87  static void *Try_fetch_item_by_hash(LinkedDataFile *file, uint32); // reference a resource by hash
88 
89  static void *Try_fetch_item_by_script_name(LinkedDataFile *file, const char *name);
90 };
91 
92 // get DATA given NUMBER
93 inline void *LinkedDataObject::Fetch_item_by_number(LinkedDataFile *file, uint32 number) {
94  // illegal reference number
95  assert(number < FROM_LE_32(file->number_of_items));
96  // return address of resource
97  return (((uint8 *)&file->header) + FROM_LE_32(file->list[number].data_offset));
98 }
99 
100 // get NAME given NUMBER
101 inline void *LinkedDataObject::Fetch_items_name_by_number(LinkedDataFile *file, uint32 number) {
102  // illegal reference number
103  assert(number < FROM_LE_32(file->number_of_items));
104  // return name
105  return (((uint8 *)&file->header) + FROM_LE_32(file->list[number].name_offset));
106 }
107 
108 // this is the one that does the search...
109 // get NUMBER given NAME (does search)
110 inline uint32 LinkedDataObject::Fetch_item_number_by_name(LinkedDataFile *file, const char *name) {
111  uint32 hash;
112 
113  if (!NameSearchable(file)) {
114  Fatal_error("This file is not searchable by name and was created as such (name %s)", name);
115  }
116 
117  hash = EngineHashString(name);
118 
119  return Fetch_item_number_by_hash(file, hash);
120 }
121 
122 // get ITEM given NAME (uses Try_fetch_item_by_name, fatal error if can't find)
123 inline void *LinkedDataObject::Fetch_item_by_name(LinkedDataFile *file, const char *name) {
124  void *search;
125 
126  search = Try_fetch_item_by_name(file, name);
127 
128  if (search == 0) {
129  Fatal_error("pxLinked_data_file::Fetch_item_by_name Object %s not found", name);
130  // Note, for not the engine then the error is not caught which is a bad thing
131  // but we need a generic Fatal_error type thing that converters & the engine can call
132  // i.e. the converters need a Fatal_error function
133  return (NULL);
134  } else
135  return search;
136 }
137 
138 // get DATA given NAME (uses get NUMBER given NAME and returns 0 if not found or uses get DATA given NUMBER to return DATA)
139 inline void *LinkedDataObject::Try_fetch_item_by_name(LinkedDataFile *file, const char *name) {
140  // as Fetch_item_with_name but will return 0 if entry could not be found as opposed to an assert
141  uint32 search = 0;
142 
143  search = Fetch_item_number_by_name(file, name);
144 
145  if (search == PX_LINKED_DATA_FILE_ERROR)
146  return 0; // not found (legal)
147  else
148  return Fetch_item_by_number(file, search);
149 }
150 
151 // get DATA given HASH (uses get NUMBER given HASH and returns 0 if not found or uses get DATA given NUMBER to return DATA)
152 inline void *LinkedDataObject::Try_fetch_item_by_hash(LinkedDataFile *file, uint32 hash) {
153  // as Fetch_item_with_name but will return 0 if entry could not be found as opposed to an assert
154  uint32 search = 0;
155 
156  search = Fetch_item_number_by_hash(file, hash);
157 
158  if (search == PX_LINKED_DATA_FILE_ERROR)
159  return 0; // not found (legal)
160  else
161  return Fetch_item_by_number(file, search);
162 }
163 
164 inline void *LinkedDataObject::Try_fetch_item_by_script_name(LinkedDataFile *file, const char *name) {
165  uint32 search = 0;
166 
167  do {
168  if (strstr((const char *)((uint8 *)&file->header) + FROM_LE_32(file->list[search].name_offset), (const char *)name))
169  return (((uint8 *)&file->header) + FROM_LE_32(file->list[search].data_offset));
170 
171  search++;
172  } while (search < FROM_LE_32(file->number_of_items));
173 
174  // not found at all
175  return (0);
176 }
177 
178 } // End of namespace ICB
179 
180 #endif
Definition: px_linkeddatafile.h:64
Definition: actor.h:32
Definition: px_linkeddatafile.h:53
Definition: px_common.h:100
Definition: px_linkeddatafile.h:46