home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / LANGLOAD.C < prev    next >
C/C++ Source or Header  |  1998-05-17  |  4KB  |  167 lines

  1.  
  2. // LoraBBS Version 2.41 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <string.h>
  24. #include <alloc.h>
  25.  
  26. #include "lsetup.h"
  27. #include "sched.h"
  28. #include "msgapi.h"
  29. #include "externs.h"
  30. #include "prototyp.h"
  31.  
  32. /*
  33.  * Read the compiled BinkleyTerm Language file.
  34.  *
  35.  */
  36.  
  37. int load_language (which)
  38. int which;
  39. {
  40.    int pointer_size;
  41.    char *memory;
  42.    unsigned int memory_size;
  43.    char *malloc_target;
  44.    char LANGpath[128];
  45.    int error;
  46.    int i;
  47.    int read;
  48.    struct stat stbuf;
  49.    FILE           *fpt;                         /* stream pointer           */
  50.  
  51.    for (i = 0; config->language[i].descr[0]; i++);
  52.    if (which >= i)
  53.       return (0);
  54.  
  55.    strcpy (LANGpath, config->menu_path);
  56.    strcat (LANGpath, config->language[which].basename);
  57.    strcat (LANGpath, ".LNG");
  58.  
  59.    /*
  60.     * Get some info about the file
  61.     */
  62.  
  63.     error = stat (LANGpath, &stbuf);
  64.     if (error != 0)
  65.         {
  66.         status_line ("!Cannot get information on file %s\n",LANGpath);
  67.         exit (250);
  68.         }
  69.  
  70.    /*
  71.     * Allocate space for the raw character array and for the
  72.     * pointer and fixup arrays
  73.     *
  74.     */
  75.  
  76.     memory_size = (unsigned int) stbuf.st_size;
  77.  
  78.     malloc_target = malloc (memory_size);
  79.     if (malloc_target == NULL)
  80.         {
  81.         status_line ("!Unable to allocate string memory\n");
  82.         exit (250);
  83.         }
  84.  
  85.    /*
  86.     * Open the input file
  87.     *
  88.     */
  89.  
  90.     fpt = fopen (LANGpath, "rb");               /* Open the file             */
  91.     if (fpt == NULL)                            /* Were we successful?       */
  92.         {
  93.         fprintf (stderr, "Can not open input file %s\n", LANGpath);
  94.         exit (250);
  95.         }
  96.  
  97.    /*
  98.     * Read the entire file into memory now.
  99.     *
  100.     */
  101.  
  102.     read = fread (malloc_target, 1, memory_size, fpt);
  103.     if (read != memory_size)
  104.         {
  105.         fprintf (stderr, "Could not read language data from file %s\n",LANGpath);
  106.         fclose (fpt);
  107.         exit (250);
  108.         }
  109.  
  110.    /*
  111.     * Close the file.
  112.     *
  113.     */
  114.  
  115.     error = fclose (fpt);
  116.     if (error != 0)
  117.         {
  118.         fprintf (stderr, "Unable to close language file %s\n",LANGpath);
  119.         exit (250);
  120.         }
  121.  
  122.    /*
  123.     * Do fixups on the string pointer array as follows:
  124.     *
  125.     * 1. Find the NULL pointer in the mess here.
  126.     * 2. Start of the string memory is the "following pointer".
  127.     * 3. Apply arithmetic correction to entire array.
  128.     *
  129.     */
  130.  
  131.     bbstxt = (char **) malloc_target;
  132.     for (i = 0; bbstxt[i] != NULL; i++)         /* Find NULL marker in array */
  133.         ;
  134.  
  135.     pointer_size = i - 1;                       /* Count of elements w/o NULL*/
  136.     if (pointer_size != X_TOTAL_MSGS)
  137.         {
  138.         fprintf (stderr, "Count of %d from file does not match %d required\n",
  139.                     pointer_size, X_TOTAL_MSGS);
  140.         exit (250);
  141.         }
  142.  
  143.     memory = (char *) &bbstxt[++i];             /* Text starts after NULL    */
  144.  
  145.     for (i = 1; i <= pointer_size; i++)
  146.         {
  147.         bbstxt[i] = memory + (short) (bbstxt[i] - bbstxt[0]);
  148.         }
  149.     bbstxt[0] = memory;
  150.  
  151.     mesi[0] = bbstxt[B_JAN];
  152.     mesi[1] = bbstxt[B_FEB];
  153.     mesi[2] = bbstxt[B_MAR];
  154.     mesi[3] = bbstxt[B_APR];
  155.     mesi[4] = bbstxt[B_MAY];
  156.     mesi[5] = bbstxt[B_JUN];
  157.     mesi[6] = bbstxt[B_JUL];
  158.     mesi[7] = bbstxt[B_AGO];
  159.     mesi[8] = bbstxt[B_SEP];
  160.     mesi[9] = bbstxt[B_OCT];
  161.     mesi[10] = bbstxt[B_NOV];
  162.     mesi[11] = bbstxt[B_DEC];
  163.  
  164.     bbstxt[0] = memory;
  165.     return (1);
  166. }
  167.