[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

General Programming Concepts: Writing and Debugging Programs


Culture-Specific Data Processing

Culture-specific data handling may be part of a program, and such a program may supply different data for different locales. In addition, a program may use different algorithms to process character data based on the language and culture. For example, recognition of the start and end of a word and the method of hyphenation of a word across two lines varies depending on the locale. Programs that deal with such functionality need access to these tables or algorithms based on the current locale setting at runtime. You can handle such programs in the following ways:

Culture-Specific Tables

If the culture-specific data can be processed by accessing tables based on the current locale setting, then this can be accomplished by using the standard file I/O subroutines (fopen, fread, open, read, and so on). Such tables must be provided in the directory defined in /usr/lpp/Name where Name is the name of the particular application under the appropriate locale name.

Standard path prefix: /usr/lpp/Name (AIX-specific pathname)
Culture-specific directory: Obtain the current locale for the appropriate category that describes the tables. Concatenate it to the above prefix.
Access: Use standard file access subroutines (fopen, fread, and so on) as appropriate.

Culture-Specific Algorithms

The culture-specific algorithms reside in the /usr/lpp/Name/%L directory. Here %L represents the current locale setting for the appropriate category.

Use the load system call to access program-specific algorithms from an object module.

Standard path prefix: /usr/lpp/Name
Culture-specific directory: Obtain the current locale for the appropriate category. Concatenate it to the above prefix.
Method: Concatenate the method name to it.

Example: Load a Culture-Specific Module for Arabic Text for an Application

Header File

The methods.h include file has one structure as follows:

struct Methods {
        int     version;
        char    *(*hyphen)();
        char    *(*wordbegin)();
        char    *(*wordend)();
} ;

The Main Program

Let the program name be textpr.

The main program determines the module to load and invokes it. Note that the textpr.h include file is used to specify the path name of the load object. This way, the path name, which is system-specific, can be changed easily.

#include    <stdio.h>
#include    <errno.h>
#include    "methods.h"
#include    "textpr.h"    /* contains the pathname where
                             the load object can be found */

extern     int   errno;

main()
{
     char libpath[PATH_MAX];   /* stores the full pathname of the
                                  load object */
      char *prefix_path=PREFIX_PATH;        /* from textpr.h */
      char *method=METHOD;                  /* from textpr.h */
      int (*func)();
      char *path;
      /* Methods */
      int   ver;
      char  *p;
      struct Methods *md;
      
  
      setlocale(LC_ALL, "");

      path = setlocale(LC_CTYPE, 0);    /* obtain the locale 
                                         for LC_CTYPE category */
      /* Construct the full pathname for the */
      /* object to be loaded                 */
      strcpy(libpath, prefix_path);
      strcat(libpath, path);
      strcat(libpath, "/");
      strcat(libpath, method);
   
      func = load(conv, 1, libpath);        /* load the object */
      if(func==NULL){
            strerror(errno);
            exit(1);
      }
      /* invoke the loaded module ");
      md =(struct Methods *) func();       /* Obtain the methods
                                              structure */
      ver = md->version;
      /* Invoke the methods as needed */
      p = (md->hyphen)();
      p = (md->wordbegin)();
      p = (md->wordend)();
}

Methods

This module contains culture-specific algorithms. In this example, it provides the Arabic method. The method.c program follows:

#include "methods.h"

char *Arabic_hyphen(char *);
char *Arabic_wordbegin(char *);
char *Arabic_wordend(char *);

static struct Methods ArabicMethods= {
        1,
        Arabic_hyphen,
        Arabic_wordbegin,
        Arabic_wordend
} ;

struct Methods *start_methods()
{
        /* startup methods */
        return ( &ArabicMethods);
}

char *Arabic_hyphen(char *string)
{
        /* Arabic hyphen */
        return( string );
}
char *Arabic_wordbegin(char *string)
{
        /*Arabic word begin */);
        return( string );
}
char *Arabic_wordend(char *string)
{
        /* Arabic word end */;
        return( string);
}

Include File: textpr

The include file contains the path name of the module to be loaded.

#define PREFIX_PATH "/usr/lpp/textpr"   
                    /* This is an AIX-specific pathname */

Related Information

Chapter 16, National Language Support.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]