home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / edu / tux / Tuxtype2-1.5.3-installer.exe / src / gettext.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-13  |  4.5 KB  |  186 lines

  1. /***************************************************************************
  2. gettext.c 
  3. -  description: a crossplatform minimal gettext library
  4. -------------------
  5. begin                : Sunday Feb 23, 2003
  6. copyright            : (C) 2003 by Jesse Andrews
  7. email                : jdandr2@uky.edu
  8. ***************************************************************************/
  9.  
  10. /***************************************************************************
  11. *                                                                         *
  12. *   This program is free software; you can redistribute it and/or modify  *
  13. *   it under the terms of the GNU General Public License as published by  *
  14. *   the Free Software Foundation; either version 2 of the License, or     *
  15. *   (at your option) any later version.                                   *
  16. *                                                                         *
  17. ***************************************************************************/
  18.  
  19. #include "stdio.h"
  20. #include "globals.h"
  21.  
  22. /* we will store our list in a linked list since
  23.  * we don't expect too large of a list (famous last words!)
  24.  */
  25.  
  26. struct node {
  27.     unsigned char *in;          // the english
  28.     unsigned char *out;         // the translation
  29.     struct node *next; // ptr to next in list, NULL if last
  30. };
  31.  
  32. typedef struct node item; 
  33.  
  34. item *HEAD=NULL;
  35.  
  36. /* --- add a word to the linked list --- */
  37.  
  38. void add_word(unsigned char *in, unsigned char *out) {
  39.     item *cur;
  40.     /* -- allocate space for the node in the list -- */
  41.     cur = (item *)malloc(sizeof(item));
  42.  
  43.     /* -- allocate space for strings, and copy over -- */
  44.     cur->in = (unsigned char *)malloc(strlen(in)+2);
  45.     strncpy(cur->in, in, strlen(in)+1);
  46.     cur->out = (unsigned char *)malloc(strlen(out)+2);
  47.     strncpy(cur->out, out, strlen(out)+1);
  48.  
  49.     /* -- add to the front of the list -- */
  50.     cur->next = HEAD;
  51.     HEAD = cur;
  52. }
  53.  
  54. int load_trans(char *file) {
  55.     /* this function will load the passed file (a .po file)
  56.      * if need be, it should erase any previously loaded
  57.      * translations.
  58.      *
  59.      * the filename passed must exist!
  60.      *
  61.      * returns: 0 if ok
  62.      *     -1 if file could not be located
  63.      *     -2 if file has errors in it
  64.      */
  65.  
  66.     item *ptr;
  67.     FILE *f;
  68.     unsigned char str[FNLEN];
  69.     unsigned char in[FNLEN];
  70.     unsigned char out[FNLEN];
  71.  
  72.     LOG( "Clearing previous translation list\n" );
  73.  
  74.     while(HEAD != NULL) {
  75.         ptr = HEAD->next;
  76.         free(HEAD);
  77.         HEAD = ptr;
  78.     }
  79.  
  80.     /* Yes, I know, I should use YACC/LEX
  81.      * but, until you provide an GPL YACC/LEX 
  82.      * implimentation on Mac OS _CLASSIC_, we have
  83.      * to do things so they are portable, which
  84.      * means, we have to parse our files by hand
  85.      * using "state machines"
  86.      */
  87.  
  88.  
  89.     LOG( "Loading translation file\n" );
  90.     f = fopen( file, "r" );
  91.  
  92.     if (f == NULL) return -1;
  93.     
  94.     /* ### ADD ERROR CHECKING ### */
  95.  
  96.     do {
  97.         fscanf(f, "%[^\n]\n", str);
  98.  
  99.         /* get rid of any comments! */
  100.         {
  101.             unsigned char mode='O';
  102.             int i;
  103.             for (i=0; i<strlen(str); i++) {
  104.                 if (mode == 'O') {
  105.                     switch (str[i]) {
  106.                         case '"': mode = 'I'; break;
  107.                         case '#': str[i]='\0'; break;
  108.                     }
  109.                 } else {
  110.                     switch (str[i]) {
  111.                         case '\\': 
  112.                             if (mode != 'S') mode = 'S';
  113.                             else mode = 'I';
  114.                             break;
  115.                         case '"': 
  116.                             if (mode != 'S') mode ='O'; 
  117.                             break;
  118.                         default:
  119.                             mode = 'I'; // get rid of any /
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.  
  125.         /* we force msgid or msgstr to be at the begining of the line! */
  126.  
  127.         if (strncmp(str, "msgid", 5) == 0) {
  128.             int start=0, endloc=0, i;
  129.             for (i=0; i<strlen(str); i++)
  130.                 if (str[i] == '"') {
  131.                     if (!start)
  132.                         start = i;
  133.                     else
  134.                         endloc = i;
  135.                 }
  136.             str[endloc]='\0';
  137.             strcpy(in, str+start+1);
  138.         }
  139.         if (strncmp(str, "msgstr", 6) == 0) {
  140.             int start=0,endloc=0, i;
  141.             for (i=0; i<strlen(str); i++)
  142.                 if (str[i] == '"') {
  143.                     if (!start)
  144.                         start = i;
  145.                     else
  146.                         endloc = i;
  147.                 }
  148.             str[endloc]='\0';
  149.             strcpy(out, str+start+1);
  150.             add_word(in, out);
  151.         }
  152.  
  153.     } while( !feof(f) );
  154.  
  155.     LOG( "Completed loading of translation file\n" );
  156.  
  157.     return 0;
  158. }
  159.  
  160. unsigned char * gettext( unsigned char *in ) {
  161.     /* this function will attempt to translate the string
  162.      * "in" to an "translation of in" if one exists.
  163.      * if it doesn't exist in the translation set, it 
  164.      * returns "in".
  165.      */
  166.  
  167.     /* go through each time until we find what we want...
  168.      * if the number of translated words we use increases, 
  169.      * we should move to a hash table.
  170.      */
  171.  
  172.     item *cur=HEAD;
  173.  
  174.     if (useEnglish)
  175.         return in;
  176.  
  177.     while (cur != NULL) 
  178.         if (strcmp(cur->in, in) == 0)
  179.             return cur->out;
  180.         else
  181.             cur = cur->next;
  182.  
  183.     /* if we didn't find anything return what we were asked */
  184.     return in;
  185. }
  186.