Chapter 9
// make sure we have a filename parameter
if (filename == NULL) croak("Missing required parameter filename.");
// call the appropriate function based on arguments
if (read_file && default_type != NULL) {
RETVAL = gnome_mime_type_or_default_of_file(filename, default_type);
} else if (read_file) {
RETVAL = gnome_mime_type_of_file(filename);
} else if (default_type != NULL) {
RETVAL = gnome_mime_type_or_default(filename, default_type);
} else {
RETVAL = gnome_mime_type(filename);
}
OUTPUT:
RETVAL
The XSUB starts with syntax for a variable argument function that mimics
C's syntax:
const char *
file_type(...)
Note that unlike C's . . . , you don't need to have at least one fixed parameter.
Next, I set up a number of local variables in a PREINIT block. The contents of
PREINIT are placed first in the output XSUB. In some cases this is essential, but in
this case it's merely a convenient place for local declarations:
PREINIT:
char *filename = NULL; // variables for named params
char *default_type = NULL;
IV read_file = 0;
IV x; // loop counter
Next comes the CODE block proper, where the automatic XS variable items is
used to check the number of parameters:
// check that there are an even number of args
if (items % 2) croak("Usage: Gnome::MIME::file_type(k => v, ...)");
and then iterate through the key/value pairs:
222
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved