home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / LANG.C < prev    next >
C/C++ Source or Header  |  2000-07-13  |  6KB  |  197 lines

  1. /******************************************************************************
  2. LANG.C       Language file functions.
  3.  
  4.     Copyright 1993 - 2000 Paul J. Sidorsky
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License, version 2, as
  8.     published by the Free Software Foundation.
  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. This module contains all functions related to the language file.
  20. ******************************************************************************/
  21.  
  22. #include "top.h"
  23.  
  24. /* Memory consumption while the language file is loading is logged. */
  25. extern FILE *debuglog;
  26.  
  27. /* loadlang() - Load the language file.
  28.    Parameters:  None.
  29.    Returns:  TRUE if successful, FALSE on error.
  30.    Notes:  The name of the language file is obtained from TOP.CFG.
  31. */
  32. char loadlang(void)
  33. {
  34. FILE *langfil = NULL; /* File stream for loading the language file. */
  35. XINT lres; /* Result code. */
  36. long linecount = 0, lld; /* Number of lines, counter. */
  37.  
  38. /* Open the language file. */
  39. sprintf(outbuf, "%s%s", cfg.toppath, cfg.langfile);
  40. langfil = fopen(outbuf, "rt");
  41. if (!langfil)
  42.     {
  43.     errorabort(ERR_CANTOPEN, cfg.langfile);
  44.     }
  45.  
  46. /* Count the number of usable lines (items) in the file. */
  47. while(!feof(langfil))
  48.     {
  49.     if (fgets(outbuf, 256, langfil))
  50.         {
  51.         /* Only count non-comment lines. */
  52.         if (outbuf[0] != ';')
  53.             {
  54.             linecount++;
  55.             }
  56.         }
  57.     }
  58.  
  59. /* Allocate the pointer buffer for the language items. */
  60. numlang = linecount;
  61. langptrs = calloc(linecount, sizeof(lang_text_typ XFAR *));
  62. if (!langptrs)
  63.     {
  64.     fclose(langfil);
  65.     return 0;
  66.     }
  67.  
  68. if (debuglog != NULL)
  69.     {
  70.     printf("\n\nLanguage File Free Memory Check:\n\n");
  71.     }
  72.  
  73. /* Allocate space for each language item. */
  74. for (lld = 0, lres = 0; lld < linecount; lld++)
  75.     {
  76.     lres |= ((langptrs[lld] = malloc(sizeof(lang_text_typ))) == NULL);
  77. #if !defined(__OS2__) && !defined(__WIN32__)
  78.     /* Log the free memory if DEBUG mode is on. */
  79.     if (debuglog != NULL)
  80.         {
  81.         printf("/%lu", farcoreleft());
  82.         }
  83. #endif
  84.     }
  85.  
  86. if (lres)
  87.     {
  88.     /* On error, free all pointers. */
  89.     for (lld = 0; lld < linecount; lld++)
  90.         {
  91.         dofree(langptrs[lld]);
  92.         }
  93.     dofree(langptrs);
  94.     fclose(langfil);
  95.     return 0;
  96.     }
  97.  
  98. /* Load each language item. */
  99. rewind(langfil);
  100. for (lld = 0; lld < linecount; lld++)
  101.     {
  102.     // Some kind of extra errcheck here.
  103.     fgets(outbuf, 256, langfil);
  104.     if (outbuf[0] == ';')
  105.         {
  106.         /* Skip comments. */
  107.         lld--;
  108.         continue;
  109.         }
  110.     /* Remove trailing newline.  Not sure why the stripcr() macro wasn't
  111.        used. */
  112.     if (outbuf[strlen(outbuf) - 1] == '\n')
  113.         {
  114.         outbuf[strlen(outbuf) - 1] = 0;
  115.         }
  116.     /* Break the string to get the name and the rest of the text. */
  117.     lres = split_string(outbuf);
  118.     if (lres > 0)
  119.         {
  120.         /* Copy the item name. */
  121.         memset(langptrs[lld], 0, sizeof(lang_text_typ));
  122.         strncpy(langptrs[lld]->idtag, get_word(0), 30);
  123.         /* Get the length of the text, which starts at the second word. */
  124.         langptrs[lld]->length = strlen(&word_str[word_pos[1]]);
  125.         /* Allocate space for the text. */
  126.         langptrs[lld]->string =
  127.             malloc(langptrs[lld]->length + 1);
  128.         if (!langptrs[lld]->string)
  129.             {
  130.             /* Free all memory on error. */
  131.             for (lld = 0; lld < linecount; lld++)
  132.                 {
  133.                 dofree(langptrs[lld]);
  134.                 }
  135.             dofree(langptrs);
  136.             fclose(langfil);
  137.             return 0;
  138.             }
  139.         /* Copy the string text. */
  140.         strcpy(langptrs[lld]->string, &word_str[word_pos[1]]);
  141.         }
  142.     }
  143.  
  144. fclose(langfil);
  145.  
  146. return 1;
  147. }
  148.  
  149. /* getlang() - Gets a language text item from memory.
  150.    Parameters:  langname - ID tag (name) of the item to get.
  151.    Returns:  String containing the text.
  152. */
  153. unsigned char XFAR *getlang(unsigned char *langname)
  154. {
  155. long gld; /* Counter. */
  156.  
  157. /* Iterate through each item. */
  158. for (gld = 0; gld < numlang; gld++)
  159.     {
  160.     /* The ID tags have to match exactly, except for case. */
  161.     if (!stricmp(langname, langptrs[gld]->idtag))
  162.         {
  163.         /* Return pointer to the language text. */
  164.         return langptrs[gld]->string;
  165.         }
  166.     }
  167.  
  168. /* Return a blank string because the item wasn't found. */
  169. return "\0";
  170. }
  171.  
  172. /* getlangchar() - Gets a single character from a language item.
  173.    Parameters:  langname - ID tag (name) of item to use.
  174.                 charnum - Character number (0-based) to get.
  175.    Returns:  Character in the specified position, or 0 if the item was not
  176.              found.
  177. */
  178. unsigned char getlangchar(unsigned char *langname, XINT charnum)
  179. {
  180. long gld; /* Counter. */
  181.  
  182. /* Iterate through each item. */
  183. for (gld = 0; gld < numlang; gld++)
  184.     {
  185.     /* The ID tags have to match exactly, except for case. */
  186.     if (!stricmp(langname, langptrs[gld]->idtag))
  187.         {
  188.         /* Return character in the specified position. */
  189.         return langptrs[gld]->string[charnum];
  190.         }
  191.     }
  192.  
  193. /* Return ASCII #0, no item was found. */
  194. return 0;
  195. }
  196.  
  197.