home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / GET_LANG.C < prev    next >
C/C++ Source or Header  |  1998-05-17  |  5KB  |  171 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 <mem.h>
  21. #include <string.h>
  22.  
  23. #include "language.h"
  24.  
  25. int get_language (char *name_of_file)
  26. {
  27.    int len, count_from_file, file_version, escape_on, internal_count, total_size, error;
  28.    char *p, *q, c, *storage, **load_pointers, linebuf[512];
  29.    FILE *fpt;
  30.  
  31.    internal_count = 0;                     /* zero out internal counter */
  32.    count_from_file = 0;                    /* zero out internal counter */
  33.    total_size = 0;                         /* Initialize storage size   */
  34.    error = 0;                              /* Initialize error value    */
  35.  
  36.    load_pointers = pointers;               /* Start at the beginning    */
  37.    storage = memory;                       /* A very good place to start*/
  38.  
  39.    if ((fpt = fopen (name_of_file, "r")) == NULL) {
  40.       fprintf (stderr, "Can not open input file %s\n", name_of_file);
  41.       return (-1);
  42.    }
  43.  
  44.    while (fgets (linebuf, 500, fpt) != NULL) {
  45.       escape_on = 0;
  46.       p = q = linebuf;
  47.  
  48. /*
  49.       if (count_from_file) {
  50.          while (*p != 0 && *p != '"')
  51.             p++;
  52.          if (*p == '"')
  53.             strcpy (linebuf, ++p);
  54.          else
  55.             strcpy (linebuf, p);
  56.          p = q = linebuf;
  57.       }
  58. */
  59.  
  60.       while ((c = *p++) != 0) {
  61.          switch (c) {
  62. /*
  63.             case '"':
  64.                c = '\n';
  65.                *q = *p = '\0';
  66.                break;
  67. */
  68.  
  69.             case ';':
  70.                if (escape_on) {
  71.                   *q++ = ';';
  72.                   --escape_on;
  73.                   break;
  74.                }
  75.  
  76.             case '\n':
  77.                *q = *p = '\0';
  78.                break;
  79.  
  80.             case '\\':
  81.                if (escape_on) {
  82.                   *q++ = '\\';
  83.                   --escape_on;
  84.                }
  85.                else
  86.                   ++escape_on;
  87.                break;
  88.  
  89.             case 'n':
  90.                if (escape_on) {
  91.                   *q++ = '\n';
  92.                   --escape_on;
  93.                }
  94.                else
  95.                   *q++ = c;
  96.                break;
  97.  
  98.             case 'r':
  99.                if (escape_on) {
  100.                   *q++ = '\r';
  101.                   --escape_on;
  102.                }
  103.                else
  104.                   *q++ = c;
  105.                break;
  106.  
  107.             case '_':
  108.                if (escape_on) {
  109.                   *q++ = '_';
  110.                   --escape_on;
  111.                }
  112.                else
  113.                   *q++ = ' ';
  114.                break;
  115.  
  116.             default:
  117.                *q++ = c;
  118.                escape_on = 0;
  119.                break;
  120.          }
  121.       }
  122.  
  123.       if (!(len = (int)(q - linebuf)))
  124.          continue;
  125.  
  126.       if (!count_from_file) {
  127.          sscanf (linebuf,"%d %d",&count_from_file, &file_version);
  128.          if (count_from_file <= pointer_size)
  129.             continue;
  130.  
  131.          fprintf (stderr, "Messages in file = %d, Pointer array size = %d\n", count_from_file, pointer_size);
  132.          error = -2;
  133.          break;
  134.       }
  135.  
  136.       ++len;
  137.       if (((total_size += len) < memory_size) &&  (internal_count < pointer_size)) {
  138.          memcpy (storage, linebuf, len);     /* Copy it now (with term)   */
  139.          *load_pointers++ = storage;         /* Point to start of string  */
  140.          storage += len;                     /* Move pointer into memory  */
  141.       }
  142.  
  143.       ++internal_count;                       /* bump count */
  144.    }
  145.  
  146.    fclose (fpt);
  147.  
  148.    if (internal_count > pointer_size) {
  149.       fprintf (stderr, "%d messages read exceeds pointer array size of %d\n", internal_count, pointer_size);
  150.       error = -3;
  151.    }
  152.  
  153.    if (total_size > memory_size) {
  154.       fprintf (stderr, "Required memory of %d bytes exceeds %d bytes available\n", total_size, memory_size);
  155.       error = -4;
  156.    }
  157.  
  158.    if (count_from_file != internal_count) {
  159.       fprintf (stderr, "Count of %d lines does not match %d lines actually read\n", count_from_file, internal_count);
  160.       error = -5;
  161.    }
  162.  
  163.    if (!error) {
  164.       pointer_size = internal_count;          /* Store final usage counts  */
  165.       memory_size = total_size;
  166.       *load_pointers = NULL;                  /* Terminate pointer table   */
  167.    }
  168.  
  169.    return (error);
  170. }
  171.