home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BSRC_250.LZH / LANGLOAD.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  9KB  |  242 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software, Co.                       */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          Freely Available<tm> Software.                 */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  (C) Copyright 1987-91, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                    BinkleyTerm Language File Loader                      */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.250.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU  SHOULD  IMMEDIATELY CONTACT BIT BUCKET    */
  27. /*    SOFTWARE CO.  AT ONE OF THE  ADDRESSES  LISTED BELOW.  IN NO EVENT    */
  28. /*    SHOULD YOU  PROCEED TO USE THIS FILE  WITHOUT HAVING  ACCEPTED THE    */
  29. /*    TERMS  OF  THE  BINKLEYTERM  LICENSING  AGREEMENT,  OR  SUCH OTHER    */
  30. /*    AGREEMENT AS YOU ARE ABLE TO REACH WITH BIT BUCKET SOFTWARE, CO.      */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /* You can contact Bit Bucket Software Co. at any one of the following      */
  34. /* addresses:                                                               */
  35. /*                                                                          */
  36. /* Bit Bucket Software Co.        FidoNet  1:104/501, 1:343/491             */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n343.z1.fidonet.org         */
  40. /*                                                                          */
  41. /* Please feel free to contact us at any time to share your comments about  */
  42. /* our software and/or licensing policies.                                  */
  43. /*                                                                          */
  44. /*--------------------------------------------------------------------------*/
  45.  
  46. /* Include this file before any other includes or defines! */
  47.  
  48. #include "includes.h"
  49.  
  50. #define LANGFILE        ( PRDCT_PRFX ".LNG" )
  51.  
  52. /*
  53.  * Read the compiled BinkleyTerm Language file.
  54.  *
  55.  */
  56.  
  57. int load_language (void)
  58. {
  59.     int pointer_size;
  60.     char *memory;
  61.     unsigned int memory_size;
  62.     char *malloc_target;
  63.     char *envptr;
  64.     char LANGpath[128];
  65.     int error;
  66.     int i;
  67.     int read;
  68.     struct stat stbuf;
  69.     FILE           *fpt;                         /* stream pointer           */
  70.     char *Ptr;
  71.  
  72.    envptr = getenv (PRDCT_PRFX);                 /* get path from environment*/
  73.    if ((envptr != NULL)                          /* If there was one, and    */
  74.        && (!dexists (LANGFILE)))                 /* No local language file,  */
  75.       {
  76.       (void) strcpy (LANGpath, envptr);          /* use BINKLEY as our path  */
  77.       (void) add_backslash (LANGpath);
  78.       }
  79.    else
  80.       LANGpath[0] = '\0';
  81.  
  82.    (void) strcat (LANGpath, LANGFILE);
  83.  
  84.    /*
  85.     * Get some info about the file
  86.     */
  87.  
  88.     error = stat (LANGpath, &stbuf);
  89.     if (error != 0)
  90.         {
  91.         (void) fprintf (stderr, "Cannot get information on file %s\n",LANGpath);
  92.         exit (250);
  93.         }
  94.  
  95.    /*
  96.     * Allocate space for the raw character array and for the
  97.     * pointer and fixup arrays
  98.     *
  99.     */
  100.  
  101.     memory_size = (unsigned int) stbuf.st_size;
  102.  
  103.     Ptr = malloc_target = calloc (1, memory_size);
  104.     if (malloc_target == NULL)
  105.         {
  106.         (void) fprintf (stderr, "Unable to allocate string memory\n");
  107.         exit (250);
  108.         }
  109.    /*
  110.     * Open the input file
  111.     *
  112.     */
  113.  
  114.     fpt = share_fopen (LANGpath, "rb", DENY_WRITE); /* Open the file         */
  115.     if (fpt == NULL)                            /* Were we successful?       */
  116.         {
  117.         (void) fprintf (stderr, "Can not open input file %s\n", LANGpath);
  118.         exit (250);
  119.         }
  120.    /*
  121.     * Read the entire file into memory now.
  122.     *
  123.     */
  124.     read = fread (malloc_target, 1, memory_size, fpt);
  125.     if ((unsigned)read != memory_size)
  126.         {
  127.         (void) fprintf (stderr, "Could not read language data from file %s\n",LANGpath);
  128.         (void) fclose (fpt);
  129.         exit (250);
  130.         }
  131.    /*
  132.     * Close the file.
  133.     *
  134.     */
  135.     error = fclose (fpt);
  136.     if (error != 0)
  137.         {
  138.         (void) fprintf (stderr, "Unable to close language file %s\n",LANGpath);
  139.         exit (250);
  140.         }
  141.    /*
  142.     * Do fixups on the string pointer array as follows:
  143.     *
  144.     * 1. Get element count from input
  145.     * 2. Start of the string memory immediately follows the strings
  146.     * 3. Apply arithmetic correction to entire array.
  147.     *
  148.     */
  149.  
  150.     LangHdr = (struct _lang_hdr *)malloc_target;
  151.     msgtxt = (char **) (malloc_target + sizeof (struct _lang_hdr));
  152.  
  153.     pointer_size = LangHdr->ElemCnt;            /* Count of elements w/o NULL*/
  154.     if (pointer_size != X_TOTAL_MSGS)
  155.         {
  156.         (void) fprintf (stderr, "Count of %d from file does not match %d required\n",
  157.                     pointer_size, X_TOTAL_MSGS);
  158.         exit (250);
  159.         }
  160.     memory = (char *) &msgtxt[pointer_size];    /* Text starts after pointers*/
  161.     for (i = 1; i < pointer_size; i++)
  162.         {
  163.         msgtxt[i] = memory + (int) (msgtxt[i] - msgtxt[0]);
  164.         }
  165.     msgtxt[0] = memory;
  166.  
  167.    /*
  168.     * Process the Terminal Mode Keymap.
  169.     *
  170.     * First comes the integer count.
  171.     * Following that is the array of keymaps.
  172.     *
  173.     */
  174.  
  175.     Ptr = (char *)LangHdr
  176.         + sizeof (struct _lang_hdr)
  177.         + (sizeof ( char * ) * (LangHdr->ElemCnt))
  178.         + LangHdr->PoolSize;
  179.  
  180.     TrmnlKeyFncHdr.KeyFncCnt = *(int *)Ptr;
  181.     Ptr += sizeof (int);
  182.     TrmnlKeyFncHdr.KeyFncTbl = (struct _key_fnc *)Ptr;
  183.  
  184.     Ptr += sizeof (struct _key_fnc) * TrmnlKeyFncHdr.KeyFncCnt;
  185.  
  186.    /*
  187.     * Process the Unattended Mode Keymap.
  188.     *
  189.     * First comes the integer count.
  190.     * Following that is the array of keymaps.
  191.     *
  192.     */
  193.  
  194.     UnattendedKeyFncHdr.KeyFncCnt = *(int *)Ptr;
  195.     Ptr += sizeof (int);
  196.  
  197.     UnattendedKeyFncHdr.KeyFncTbl = (struct _key_fnc *)Ptr;
  198.  
  199.     Ptr += sizeof (struct _key_fnc) * UnattendedKeyFncHdr.KeyFncCnt;
  200.  
  201.    /*
  202.     * Process the product code table.
  203.     *
  204.     * This looks a lot like the language stuff above.
  205.     *
  206.     * The first thing we have here is the header (which contains a count).
  207.     * Following that, the pointer array.
  208.     * Finally, the strings themselves.
  209.     *
  210.     * Get all of that sorted out, do fixups on the string array, and
  211.     * everything should be ducky.
  212.     */
  213.  
  214.     PrdctHdr = (struct _lang_hdr *)Ptr;
  215.     PrdctTbl = (char **) (Ptr + sizeof (struct _lang_hdr));
  216.     memory = (char *)PrdctTbl
  217.            + (sizeof (char *) * PrdctHdr->ElemCnt);
  218.     memory += strlen (memory) + 1;
  219.  
  220.     for (i = 1; i < PrdctHdr->ElemCnt; i++)
  221.         {
  222.         PrdctTbl[i] = memory + (int) (PrdctTbl[i] - PrdctTbl[0]);
  223.         }
  224.     PrdctTbl[0] = memory;
  225.  
  226.     Ptr += sizeof (struct _lang_hdr)
  227.         +  (sizeof ( char * ) * (PrdctHdr->ElemCnt))
  228.         +  PrdctHdr->PoolSize;
  229.  
  230.    /*
  231.     * Finally set up the ANSI output mapping table.
  232.     *
  233.     * This one is easy. Just point at it.
  234.     */
  235.  
  236.     AnsiHdr = (struct _lang_hdr *)Ptr;
  237.     AnsiTbl = (char *) (Ptr + sizeof (struct _lang_hdr));
  238.  
  239.     return (1);
  240. }
  241.  
  242.