xref: /haiku/src/kits/translation/FuncTranslator.cpp (revision 24159a0c7d6d6dcba9f2a0c1a7c08d2c8167f21b)
1 /*****************************************************************************/
2 //               File: FuncTranslator.cpp
3 //              Class: BFuncTranslator
4 //             Author: Michael Wilber, Translation Kit Team
5 // Originally Created: 2002-06-11
6 //
7 // Description: This file contains the BTranslator based object for
8 //              function based translators, aka, the translators
9 //              that don't use the make_nth_translator() mechanism.
10 //
11 //              This class is used by the OpenBeOS BTranslatorRoster
12 //              so that function based translators, make_nth_translator()
13 //              translators and private BTranslator objects could be
14 //              accessed in the same way.
15 //
16 //
17 // Copyright (c) 2002 OpenBeOS Project
18 //
19 // Permission is hereby granted, free of charge, to any person obtaining a
20 // copy of this software and associated documentation files (the "Software"),
21 // to deal in the Software without restriction, including without limitation
22 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 // and/or sell copies of the Software, and to permit persons to whom the
24 // Software is furnished to do so, subject to the following conditions:
25 //
26 // The above copyright notice and this permission notice shall be included
27 // in all copies or substantial portions of the Software.
28 //
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35 // DEALINGS IN THE SOFTWARE.
36 /*****************************************************************************/
37 
38 #include <FuncTranslator.h>
39 
40 // ---------------------------------------------------------------
41 // Constructor
42 //
43 // Initializes class data.
44 //
45 // Preconditions:
46 //
47 // Parameters: kpData, data and function pointers that do all of
48 //                     the useful work for this class
49 //
50 // Postconditions: If kpData is freed before the BFuncTranslator,
51 //                 the BFuncTranslator will fail to work and
52 //                 could the computer to crash. kpData may point
53 //                 to a translator add-on image, this image
54 //                 should not be unloaded before the
55 //                 BFuncTranslator is freed.
56 //
57 //
58 // Returns:
59 // ---------------------------------------------------------------
60 BFuncTranslator::BFuncTranslator(const translator_data *kpData) : BTranslator()
61 {
62 	fpData = new translator_data;
63 	if (fpData) {
64 		fpData->translatorName = kpData->translatorName;
65 		fpData->translatorInfo = kpData->translatorInfo;
66 		fpData->translatorVersion = kpData->translatorVersion;
67 		fpData->inputFormats = kpData->inputFormats;
68 		fpData->outputFormats = kpData->outputFormats;
69 
70 		fpData->Identify = kpData->Identify;
71 		fpData->Translate = kpData->Translate;
72 		fpData->MakeConfig = kpData->MakeConfig;
73 		fpData->GetConfigMessage = kpData->GetConfigMessage;
74 	}
75 }
76 
77 // ---------------------------------------------------------------
78 // Destructor
79 //
80 // Frees memory used by this object. Note that none of the members
81 // of the struct have delete used on them, this is because most
82 // are const pointers and the one that isn't is a regular int32.
83 //
84 // Preconditions:
85 //
86 // Parameters:
87 //
88 // Postconditions:
89 //
90 // Returns:
91 // ---------------------------------------------------------------
92 BFuncTranslator::~BFuncTranslator()
93 {
94 	delete fpData;
95 	fpData = NULL;
96 }
97 
98 // ---------------------------------------------------------------
99 // TranslatorName
100 //
101 // Returns a short name for the translator that this object stores
102 //
103 // Preconditions:
104 //
105 // Parameters:
106 //
107 // Postconditions:
108 //
109 // Returns: NULL if fpData was not allocated successfully,
110 //			or the short name of the translator otherwise
111 // ---------------------------------------------------------------
112 const char *BFuncTranslator::TranslatorName() const
113 {
114 	if (fpData)
115 		return fpData->translatorName;
116 	else
117 		return NULL;
118 }
119 
120 // ---------------------------------------------------------------
121 // TranslatorInfo
122 //
123 // Returns a verbose name/description for the translator that this
124 // object stores
125 //
126 // Preconditions:
127 //
128 // Parameters:
129 //
130 // Postconditions:
131 //
132 // Returns: NULL if fpData was not allocated successfully,
133 //			or the verbose name of the translator otherwise
134 // ---------------------------------------------------------------
135 const char *BFuncTranslator::TranslatorInfo() const
136 {
137 	if (fpData)
138 		return fpData->translatorInfo;
139 	else
140 		return NULL;
141 }
142 
143 // ---------------------------------------------------------------
144 // TranslatorVersion
145 //
146 // Returns the version number for this translator
147 //
148 // Preconditions:
149 //
150 // Parameters:
151 //
152 // Postconditions:
153 //
154 // Returns:	B_ERROR if fpData was not allocated successfully,
155 //			or the integer representation of the translator
156 //			version otherwise
157 // ---------------------------------------------------------------
158 int32 BFuncTranslator::TranslatorVersion() const
159 {
160 	if (fpData)
161 		return fpData->translatorVersion;
162 	else
163 		return B_ERROR;
164 }
165 
166 // ---------------------------------------------------------------
167 // InputFormats
168 //
169 // Returns the list of supported input formats and the count
170 // of supported input formats
171 //
172 // Preconditions:
173 //
174 // Parameters: out_count, the number of input formats is stored
175 //                        here after the function completes
176 //
177 // Postconditions:
178 //
179 // Returns:	NULL if out_count is NULL,
180 //			NULL if fpData is not allocated,
181 //			or the list of supported input formats if all is well
182 // ---------------------------------------------------------------
183 const translation_format *BFuncTranslator::InputFormats(int32 *out_count) const
184 {
185 	if (!out_count)
186 		return NULL;
187 	if (fpData && fpData->inputFormats != NULL) {
188 		int32 i;
189 		for (i = 0; fpData->inputFormats[i].type; i++);
190 
191 		*out_count = i;
192 		return fpData->inputFormats;
193 	} else {
194 		*out_count = 0;
195 		return NULL;
196 	}
197 }
198 
199 // ---------------------------------------------------------------
200 // OutputFormats
201 //
202 // Returns the list of supported output formats
203 //
204 // Preconditions:
205 //
206 // Parameters: out_count, the number of output formats is stored
207 //                        here after the function completes
208 //
209 // Postconditions:
210 //
211 // Returns:	NULL if out_count is NULL,
212 //			NULL if fpData is not allocated,
213 //			or the list of supported output formats if all is well
214 // ---------------------------------------------------------------
215 const translation_format *BFuncTranslator::OutputFormats(int32 *out_count) const
216 {
217 	if (!out_count)
218 		return NULL;
219 	if (fpData && fpData->outputFormats != NULL) {
220 		int32 i;
221 		for (i = 0; fpData->outputFormats[i].type; i++);
222 
223 		*out_count = i;
224 		return fpData->outputFormats;
225 	} else {
226 		*out_count = 0;
227 		return NULL;
228 	}
229 }
230 
231 // ---------------------------------------------------------------
232 // Identify
233 //
234 // If the translator understands how to convert the data contained
235 // in inSource to media type outType, it fills outInfo with
236 // details about the input format and return B_OK. If it doesn't
237 // know how to translate the data, it returns B_NO_TRANSLATOR.
238 //
239 // The actual work for this function is done by the translator
240 // add-on at the other end of the Identify function pointer.
241 // So, there's no telling the actual behavior of this function
242 // it depends on the translator add-on.
243 //
244 // Preconditions:
245 //
246 // Parameters: inSource, the data that wants to be converted
247 //             inFormat, (can be null) hint about the data in
248 //                       inSource
249 //             ioExtension, (can be null) contains additional
250 //                          information for the translator
251 //                          add-on
252 //             outInfo, information about the capabilities
253 //                      of this translator
254 //             outType, the output type
255 //
256 //
257 // Postconditions:
258 //
259 // Returns: B_OK if this translator can handle the data,
260 //			B_ERROR, if fpData is unallocated or something else
261 //			         went wrong
262 //          B_NO_TRANSLATOR if it can't
263 // ---------------------------------------------------------------
264 status_t BFuncTranslator::Identify(BPositionIO *inSource,
265 	const translation_format *inFormat, BMessage *ioExtension,
266 	translator_info *outInfo, uint32 outType)
267 {
268 	if (fpData && fpData->Identify)
269 		return fpData->Identify(inSource, inFormat, ioExtension, outInfo,
270 			outType);
271 	else
272 		return B_ERROR;
273 }
274 
275 // ---------------------------------------------------------------
276 // Translate
277 //
278 // The translator translates data from inSource to format outType,
279 // writing the output to outDestination.
280 //
281 // The actual work for this function is done by the translator
282 // add-on at the other end of the Translate function pointer.
283 // So, there's no telling the actual behavior of this function
284 // it depends on the translator add-on.
285 //
286 // Preconditions:
287 //
288 // Parameters: inSource, data to be translated
289 //             inInfo, hint about the data in inSource
290 //             ioExtension, contains configuration information
291 //             outType, the format to translate the data to
292 //             outDestination, where the source is translated to
293 //
294 // Postconditions:
295 //
296 // Returns: B_OK, if it converted the data
297 //          B_NO_TRANSLATOR, if it couldn't
298 //			B_ERROR, if fpData is unallocated or something else
299 //			         went wrong
300 //          some other value, if it feels like it
301 // ---------------------------------------------------------------
302 status_t BFuncTranslator::Translate(BPositionIO *inSource,
303 	const translator_info *inInfo, BMessage *ioExtension, uint32 outType,
304 	BPositionIO *outDestination)
305 {
306 	if (fpData && fpData->Translate)
307 		return fpData->Translate(inSource, inInfo, ioExtension, outType,
308 			outDestination);
309 	else
310 		return B_ERROR;
311 }
312 
313 // ---------------------------------------------------------------
314 // MakeConfigurationView
315 //
316 // This creates a BView object that allows the user to configure
317 // the options for the translator. Not all translators support
318 // this feature, and for those that don't, this function
319 // returns B_NO_TRANSLATOR.
320 //
321 // Preconditions:
322 //
323 // Parameters: ioExtension, the settings for the translator
324 //             outView, the view created by the translator
325 //             outExtent, the bounds of the view
326 //
327 // Postconditions:
328 //
329 // Returns: B_ERROR, if this function is not supported
330 //          anything else, whatever the translator feels like
331 //                         returning
332 // ---------------------------------------------------------------
333 status_t BFuncTranslator::MakeConfigurationView(BMessage *ioExtension,
334 	BView **outView, BRect *outExtent)
335 {
336 	if (fpData && fpData->MakeConfig)
337 		return fpData->MakeConfig(ioExtension, outView, outExtent);
338 	else
339 		return B_ERROR;
340 }
341 
342 // ---------------------------------------------------------------
343 // GetConfigurationMessage
344 //
345 // This function stores the current configuration for the
346 // translator into ioExtension. Not all translators
347 // support this function.
348 //
349 // Preconditions:
350 //
351 // Parameters: ioExtension, where the configuration is stored
352 //
353 // Postconditions:
354 //
355 // Returns: B_ERROR, if function is not supported
356 //          something else, if it is
357 // ---------------------------------------------------------------
358 status_t BFuncTranslator::GetConfigurationMessage(BMessage *ioExtension)
359 {
360 	if (fpData && fpData->GetConfigMessage)
361 		return fpData->GetConfigMessage(ioExtension);
362 	else
363 		return B_ERROR;
364 }
365 
366