Debian Codegraph @

support/babel/treaty_builder.h

raw symbols SCIP
1 /* treaty_builder.h common macros to build a treaty module
2 *
3 * 2006 By L. Ross Raszewski
4 *
5 * This file is public domain, but be aware that any changes to it may
6 * cause it to cease to be compliant with the Treaty of Babel.
7 *
8 * This file depends on treaty.h
9 *
10 * The purpose of this file is to simplify the building of a treaty
11 * module. It automatically generates a generic treaty function.
12 *
13 * Usage:
14 *
15 * #define the following values:
16 * FORMAT The treaty name of the format
17 * HOME_PAGE A string containing the URL of the format home page
18 * FORMAT_EXT A string containing a comma separated list of common
19 * extensions for game files in this format
20 * NO_METADATA If the format does not support metadata
21 * NO_COVER If the format does not support cover art
22 * CUSTOM_EXTENSION If game files should not always use the first listed extension
23 *
24 * (Note: Formats which support metadata and cover art via a container should
25 * define NO_METADATA and NO_COVER as container support is handled separately)
26 *
27 * #include "treaty_builder.h"
28 * Define the following functions:
29 * static int32 get_story_file_IFID(void *, int32, char *, int32);
30 * static int32 claim_story_file(void *, int32);
31 * Define the following functions if NO_METADATA is not defined:
32 * static int32 get_story_file_metadata_extent(void *, int32);
33 * static int32 get_story_file_metadata(void *, int32, char *, int32);
34 * Define the following functions if NO_COVER is not defined
35 * static int32 get_story_file_cover_extent(void *, int32);
36 * static int32 get_story_file_cover_format(void *, int32);
37 * static int32 get_story_file_cover(void *, int32, void *, int32);
38 * Define the following if CUSTOM_EXTENSION is defined
39 * static int32 get_story_file_extension(void *, int32, char *, int32);
40 *
41 * The two-parameter functions take the story file and story file extent
42 * as parameters. The four-parameter ones also take the output
43 * buffer and its extent. They perform the corresponding task to the
44 * similarly-named selector.
45 *
46 * This file also defines the macro ASSERT_OUTPUT_SIZE(x) which
47 * returns INVALID_USAGE_RV if output_extent is less than x.
48 *
49 * #define CONTAINER_FORMAT before inclusion to generate a container
50 * module. A container module should define three additional functions:
51 * static int32 get_story_format(void *, int32, char *, int32);
52 * static int32 get_story_extent(void *, int32);
53 * static int32 get_story_file(void *, int32, void *, int32);
54 *
55 */
56
57 #ifndef TREATY_BUILDER
58 #define TREATY_BUILDER
59
60 #include "treaty.h"
61 #include <string.h>
62
63 #define ASSERT_OUTPUT_SIZE(x) do { if (output_extent < (x)) return INVALID_USAGE_RV; } while (0)
64
65 #ifndef NO_METADATA
66 static int32 get_story_file_metadata_extent(void *, int32);
67 static int32 get_story_file_metadata(void *, int32, char *, int32);
68 #endif
69 #ifndef NO_COVER
70 static int32 get_story_file_cover_extent(void *, int32);
71 static int32 get_story_file_cover_format(void *, int32);
72 static int32 get_story_file_cover(void *, int32, void *, int32);
73 #endif
74 static int32 get_story_file_IFID(void *, int32, char *, int32);
75 static int32 claim_story_file(void *, int32);
76 #ifdef CONTAINER_FORMAT
77 static int32 get_story_file(void *, int32, void *, int32);
78 static int32 get_story_format(void *, int32, char *, int32);
79 static int32 get_story_extent(void *, int32);
80 #endif
81 #ifdef CUSTOM_EXTENSION
82 static int32 get_story_file_extension(void *, int32, char *, int32);
83 #else
84 #include <stdio.h>
85 static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
86 {
87 int i;
88
89 if (!sf || !extent) return INVALID_STORY_FILE_RV;
90
91 for(i=0;FORMAT_EXT[i] && FORMAT_EXT[i]!=',';i++);
92 ASSERT_OUTPUT_SIZE(i+1);
93 memcpy(out,FORMAT_EXT,i);
94 out[i]=0;
95 return strlen(out);
96 }
97
98 #endif
99
100 #define TREATY_FUNCTION(X) DEEP_TREATY_FUNCTION(X)
101 #define DEEP_TREATY_FUNCTION(X) X ## _treaty
102 #define dSTRFRY(X) #X
103 #define STRFRY(X) dSTRFRY(X)
104
105 int32 TREATY_FUNCTION(FORMAT)(int32 selector,
106 void *story_file, int32 extent,
107 void *output, int32 output_extent)
108 {
109 int32 ll, csf;
110 if ((TREATY_SELECTOR_INPUT & selector) &&
111 (csf=claim_story_file(story_file, extent))<NO_REPLY_RV)
112 return INVALID_STORY_FILE_RV;
113
114
115 if ((TREATY_SELECTOR_OUTPUT & selector) &&
116 (output_extent ==0 ||
117 output==NULL))
118 return INVALID_USAGE_RV;
119 switch(selector)
120 {
121 case GET_HOME_PAGE_SEL:
122 ASSERT_OUTPUT_SIZE((signed) strlen(HOME_PAGE)+1);
123 strcpy((char *) output,HOME_PAGE);
124 return NO_REPLY_RV;
125 case GET_FORMAT_NAME_SEL:
126 ASSERT_OUTPUT_SIZE(512);
127 strncpy((char *) output,STRFRY(FORMAT),output_extent-1);
128 return NO_REPLY_RV;
129 case GET_FILE_EXTENSIONS_SEL:
130 ll=(strlen(FORMAT_EXT)+1) < 512 ? (strlen(FORMAT_EXT)+1):512;
131 ASSERT_OUTPUT_SIZE(ll);
132 strncpy((char *) output, FORMAT_EXT, output_extent);
133 return NO_REPLY_RV;
134 case CLAIM_STORY_FILE_SEL:
135 return csf;
136 case GET_STORY_FILE_METADATA_EXTENT_SEL:
137 #ifndef NO_METADATA
138 return get_story_file_metadata_extent(story_file, extent);
139 #endif
140 case GET_STORY_FILE_METADATA_SEL:
141 #ifndef NO_METADATA
142 return get_story_file_metadata(story_file, extent, (char *)output, output_extent);
143 #else
144 return NO_REPLY_RV;
145 #endif
146 case GET_STORY_FILE_COVER_EXTENT_SEL:
147 #ifndef NO_COVER
148 return get_story_file_cover_extent(story_file, extent);
149 #endif
150 case GET_STORY_FILE_COVER_FORMAT_SEL:
151 #ifndef NO_COVER
152 return get_story_file_cover_format(story_file, extent);
153 #endif
154 case GET_STORY_FILE_COVER_SEL:
155 #ifndef NO_COVER
156 return get_story_file_cover(story_file, extent, output, output_extent);
157 #else
158 return NO_REPLY_RV;
159 #endif
160 case GET_STORY_FILE_IFID_SEL:
161 return get_story_file_IFID(story_file, extent, (char *)output, output_extent);
162 case GET_STORY_FILE_EXTENSION_SEL:
163 return get_story_file_extension(story_file, extent, (char *)output, output_extent);
164 #ifdef CONTAINER_FORMAT
165 case CONTAINER_GET_STORY_FORMAT_SEL:
166 return get_story_format(story_file, extent, (char *)output, output_extent);
167 case CONTAINER_GET_STORY_EXTENT_SEL:
168 return get_story_extent(story_file, extent);
169 case CONTAINER_GET_STORY_FILE_SEL:
170 return get_story_file(story_file, extent, output, output_extent);
171 #endif
172
173 }
174 return UNAVAILABLE_RV;
175 }
176
177
178 #else
179 #error "treaty_builder should be used as most once in any source file";
180 #endif