Previous: , Up: Use cases   [Contents][Index]


6.7 Library API

One other possibility is to link with the MEDIATEX’s library, using its API to write your own parser, and the mediatex parsers write your own serialiser.

The /usr/share/mediatex/examples/seda-0.2.tgz project provide such examples. It import meta-data from an XML file and export them into a SQL script.

Bellow show how the main functions linked with mediatex are:

#include "mediatex.h"
#include "client/commonHtml.h"
#include "client/catalogHtml.h"

#include <locale.h>

/*==================================================================
 * Function   : usage
 * Description: Print the usage.
 * Synopsis   : static void usage(char* programName)
 * Input      : programName = the name of the program
 * Output     : N/A
 ==================================================================*/
static void 
usage(char* programName)
{
  mdtxUsage(programName);
  fprintf(stderr, "\n\t\t-i label");
  mdtxOptions();
  fprintf(stderr, "  ---\n"
	  "  -i, --label\t\tcollection's label\n");
  return;
}

/*==================================================================
 * Function   : main 
 * Description: Example using libmediatex
 * Synopsis   : ./text2sql
 * Input      : stdin
 * Output     : stdout
 ==================================================================*/
int 
main(int argc, char** argv)
{
  Collection* coll = 0;
  char* label = 0;
  // ---
  int rc = 0;
  int cOption = EOF;
  char* programName = *argv;
  char* options = MDTX_SHORT_OPTIONS"i:";
  struct option longOptions[] = {
    {"label", required_argument, 0, 'i'},
    MDTX_LONG_OPTIONS,             
    {0, 0, 0, 0}                 
  };

  setlocale (LC_ALL, "");
  // so as printf do not write comma in float
  setlocale(LC_NUMERIC, "C"); 

  // import mdtx environment
  getEnv(&env);

  // parse the command line
  while((cOption = getopt_long(argc, argv, options, longOptions, NULL)) 
        != EOF) {
    switch(cOption) {
      
    case 'i':
      label = optarg;
      break;

      GET_MDTX_OPTIONS; // generic options
    }
    if (rc) goto optError;
  }

  // export mdtx environment
  if (!setEnv(programName, &env)) goto optError;
 
  /*******************************************************************/
  logEmit(LOG_INFO, "** txt2sql: %s **", label);

  if (!(coll = mdtxGetCollection(label))) goto error;
  if (!loadCollection(coll, SERV|CTLG|EXTR)) goto error;
  // DO YOUR EXPORT HERE
  if (!releaseCollection(coll, SERV|CTLG|EXTR)) goto error;
  /*******************************************************************/

  rc = TRUE;
 error:
  freeConfiguration();
  ENDINGS;
  rc=!rc;
 optError:
  exit(rc);
}