home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / BTMTSRC3.ZIP / GET_LANG.C < prev    next >
C/C++ Source or Header  |  1990-07-03  |  10KB  |  229 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-90, Bit Bucket Software Co., a Delaware Corporation. */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*             BinkleyTerm Language Compiler Raw Input Module               */
  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.240.    */
  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:132/491, 1:141/491  */
  37. /* P.O. Box 460398                AlterNet 7:491/0                          */
  38. /* Aurora, CO 80046               BBS-Net  86:2030/1                        */
  39. /*                                Internet f491.n132.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 <stdio.h>
  47. #include <memory.h>
  48.  
  49. #include "language.h"
  50.  
  51. /*
  52.  *    get_language - read in BinkleyTerm language source
  53.  *
  54.  * Read lines into table, ignoring blanks and comments
  55.  * Store into a contiguous block of memory with the individual
  56.  *     members being referenced by an array of pointers
  57.  * Store number of lines read into pointer_size
  58.  * Store amount of memory used into memory_size
  59.  *
  60.  */
  61.  
  62. int get_language (name_of_file)
  63. char *name_of_file;
  64. {
  65.     int             len;                        /* length of current string  */
  66.     int             count_from_file;            /* no. of strings in file    */
  67.     int             file_version;               /* version of file           */
  68.     char           *p, *q;                      /* miscellaneous pointers    */
  69.     char            c;                          /* character from *p array   */
  70.     int             escape_on;                  /* was prev char an escape   */
  71.     char           *storage;                    /* where we're storing now   */
  72.     char          **load_pointers;              /* pointer to pointer array  */
  73.     char            linebuf[255];               /* biggest line we'll allow  */
  74.     FILE           *fpt;                        /* stream pointer            */
  75.     int             internal_count;             /* How many strings we got   */
  76.     int             total_size;                 /* How big it all is         */
  77.     int             error;                      /* Internal error value      */
  78.  
  79.     internal_count = 0;                         /* zero out internal counter */
  80.     count_from_file = 0;                        /* zero out internal counter */
  81.     total_size = 0;                             /* Initialize storage size   */
  82.     error = 0;                                  /* Initialize error value    */
  83.  
  84.     load_pointers = pointers;                   /* Start at the beginning    */
  85.     storage = memory;                           /* A very good place to start*/
  86.  
  87.  
  88.     /*
  89.      * Open the file now. Then read in the appropriate table. First line of
  90.      * the file contains the number of lines we want Second line through end:
  91.      * ignore if it starts with a ; and store only up to ;
  92.      *
  93.      */
  94.  
  95.  
  96.     fpt = fopen (name_of_file, "r");            /* Open the file             */
  97.     if (fpt == NULL)                            /* Were we successful?       */
  98.         {
  99.         fprintf (stderr, "Can not open input file %s\n", name_of_file);
  100.         return (-1);                            /* Return failure to caller  */
  101.         }
  102.  
  103.     while (fgets (linebuf, 254, fpt) != NULL)   /* read a line in            */
  104.         {
  105.         escape_on = 0;                          /* zero out escape char flag */
  106.         p = q = linebuf;                        /* set up for scan           */
  107.         while (c = *p++)
  108.             {
  109.             switch (c)
  110.                 {
  111.                 case ';':
  112.                     if (escape_on)
  113.                         {
  114.                         *q++ = ';';
  115.                         --escape_on;
  116.                         break;
  117.                         }
  118.                 /* Otherwise drop into newline code */
  119.  
  120.                 case '\n':
  121.                     *q = *p = '\0';
  122.                     break;
  123.  
  124.                 case '\\':
  125.                     if (escape_on)
  126.                         {
  127.                         *q++ = '\\';
  128.                         --escape_on;
  129.                         }
  130.                     else
  131.                         ++escape_on;
  132.                     break;
  133.  
  134.                 case 'n':
  135.                     if (escape_on)
  136.                         {
  137.                         *q++ = '\n';
  138.                         --escape_on;
  139.                         }
  140.                     else
  141.                         *q++ = c;
  142.                     break;
  143.  
  144.                 case 'r':
  145.                     if (escape_on)
  146.                         {
  147.                         *q++ = '\r';
  148.                         --escape_on;
  149.                         }
  150.                     else
  151.                         *q++ = c;
  152.                     break;
  153.  
  154.                 default:
  155.                     *q++ = c;
  156.                     escape_on = 0;
  157.                     break;
  158.                      }
  159.             }
  160.  
  161.         if (!(len = q - linebuf))               /* Is anything there?        */
  162.             continue;                           /* No -- ignore.             */
  163.  
  164.         if (!count_from_file)
  165.             {
  166.             sscanf (linebuf,"%d %d",&count_from_file, &file_version);
  167.             if (count_from_file <= pointer_size)
  168.                 continue;
  169.  
  170.             fprintf (stderr, 
  171.                 "Messages in file = %d, Pointer array size = %d\n",
  172.                     count_from_file, pointer_size);
  173.             error = -2;
  174.             break;
  175.             }
  176.  
  177.         ++len;                                  /* Allow for the terminator  */
  178.         if (((total_size += len) < memory_size) /* Make sure it will fit     */
  179.         &&  (internal_count < pointer_size))
  180.             {
  181.             memcpy (storage, linebuf, len);     /* Copy it now (with term)   */
  182.             *load_pointers++ = storage;         /* Point to start of string  */
  183.             storage += len;                     /* Move pointer into memory  */                
  184.             }
  185.  
  186.         ++internal_count;                       /* bump count */
  187.         }
  188.     /*
  189.      * Close the file. Make sure the counts match and that memory size was
  190.      * not exceeded. If so, we have a winner! If not, snort and puke. 
  191.      *
  192.      */
  193.  
  194.     fclose (fpt);
  195.  
  196.     if (internal_count > pointer_size)          /* Enough pointers?          */
  197.         {
  198.         fprintf (stderr,
  199.             "%d messages read exceeds pointer array size of %d\n",
  200.                 internal_count, pointer_size);
  201.         error = -3;
  202.         }
  203.  
  204.     if (total_size > memory_size)               /* Did we fit?               */
  205.         {
  206.         fprintf (stderr,
  207.             "Required memory of %d bytes exceeds %d bytes available\n",
  208.                 total_size, memory_size);
  209.         error = -4;
  210.         }
  211.  
  212.     if (count_from_file != internal_count)
  213.         {
  214.         fprintf (stderr, 
  215.             "Count of %d lines does not match %d lines actually read\n",
  216.                 count_from_file, internal_count);
  217.         error = -5;
  218.         }
  219.  
  220.     if (!error)
  221.         {
  222.         pointer_size = internal_count;          /* Store final usage counts  */
  223.         memory_size = total_size;
  224.         *load_pointers = NULL;                  /* Terminate pointer table   */
  225.         }
  226.  
  227.     return (error);
  228. }
  229.