1 /*
2  *
3  * XASTIR, Amateur Station Tracking and Information Reporting
4  * Copyright (C) 1999,2000  Frank Giannandrea
5  * Copyright (C) 2000-2019 The Xastir Group
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * Look at the README for more information on the program.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25   #include "config.h"
26 #endif  // HAVE_CONFIG_H
27 
28 #include "snprintf.h"
29 
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <sys/types.h>
39 
40 #if TIME_WITH_SYS_TIME
41   #include <sys/time.h>
42   #include <time.h>
43 #else   // TIME_WITH_SYS_TIME
44   #if HAVE_SYS_TIME_H
45     #include <sys/time.h>
46   #else  // HAVE_SYS_TIME_H
47     #include <time.h>
48   #endif // HAVE_SYS_TIME_H
49 #endif  // TIME_WITH_SYS_TIME
50 
51 #include <Xm/XmAll.h>
52 
53 #include "xastir.h"
54 #include "lang.h"
55 
56 // Must be last include file
57 #include "leak_detection.h"
58 
59 
60 
61 char lang_code[MAX_LANG_ENTRIES][MAX_LANG_CODE+1];
62 char *lang_code_ptr[MAX_LANG_ENTRIES];
63 char lang_buffer[MAX_LANG_BUFFER];
64 char lang_hotkey[MAX_LANG_ENTRIES];
65 
66 int lang_code_number;
67 long buffer_len;
68 char invalid_code[50];
69 
70 
71 
72 
73 
langcode(char * code)74 char *langcode(char *code)
75 {
76   int i;
77 
78   // Create an invalid code string to return in case we can't find the proper string
79   if (strlen(code) <= MAX_LANG_CODE)      // Code is ok
80   {
81     xastir_snprintf(invalid_code, sizeof(invalid_code), "IC>%s", code);
82   }
83   else    // Code is too long
84   {
85     xastir_snprintf(invalid_code, sizeof(invalid_code), "IC>TOO LONG:%s",code);
86     fprintf(stderr,"IC>TOO LONG:%s\n",code);
87     return(invalid_code);
88   }
89 
90   if(lang_code_number>0)
91   {
92     for(i=0; i<lang_code_number; i++)
93     {
94       if(strcmp(code,lang_code[i])==0)    // Found a match
95       {
96         if (strlen(lang_code[i]) < MAX_LANG_LINE_SIZE)
97         {
98           return(lang_code_ptr[i]);   // Found it, length ok
99         }
100         else
101         {
102           fprintf(stderr,"String size: %d,  Max size: %d, %s\n",
103                   (int)strlen(lang_code[i]),
104                   MAX_LANG_LINE_SIZE,code);
105           return(invalid_code);       // Found it, but string too long
106         }
107       }
108     }
109     fprintf(stderr,"Language String not found:%s\n",code);
110     return(invalid_code);
111   }
112 
113   fprintf(stderr,"No language strings loaded:%s\n",code);
114   return(invalid_code);   // No strings loaded in language file
115 }
116 
117 
118 
119 
120 
langcode_hotkey(char * code)121 char langcode_hotkey(char *code)
122 {
123   int i;
124 
125   if(lang_code_number>0)
126   {
127     for(i=0; i<lang_code_number; i++)
128     {
129       if(strcmp(code,lang_code[i])==0)
130       {
131         return(lang_hotkey[i]); // Found it
132       }
133     }
134   }
135 
136   fprintf(stderr,"No hotkey for:%s\n",code);
137   return(' ');    // No strings loaded in language file
138 }
139 
140 
141 
142 
143 
load_language_file(char * filename)144 int load_language_file(char *filename)
145 {
146   FILE *f;
147   char line[MAX_LANG_LINE_SIZE+1];
148   char *temp_ptr;
149   int i,lt,lcok;
150   char cin;
151   int ok;
152   int line_num;
153   int data_len;
154 
155   lang_code_number=0;
156   buffer_len=0l;
157   ok=1;
158   line_num=1;
159   i=0;
160   f=fopen(filename,"r");
161   if(f != NULL)
162   {
163     line[0]='\0';
164     while(!feof(f))
165     {
166       if(fread(&cin,1,1,f)==1)
167       {
168         if(cin != (char)10 && cin != (char)13)
169         {
170           if(i<MAX_LANG_LINE_SIZE)
171           {
172             line[i++]=cin;
173             line[i]='\0';
174           }
175           else
176           {
177             ok=0;
178             fprintf(stderr,"Error! Line %d too long in language file\n",line_num);
179           }
180         }
181         else
182         {
183           i=0;
184           if (line[0]!='#' && strlen(line)>0)
185           {
186             /* data line */
187             if(lang_code_number < MAX_LANG_ENTRIES)
188             {
189               if(buffer_len < MAX_LANG_BUFFER)
190               {
191                 if(strchr(line,'|')!=NULL)
192                 {
193                   temp_ptr=strtok(line,"|");            /* get code */
194                   if (temp_ptr!=NULL)
195                   {
196                     if(strlen(temp_ptr)<=MAX_LANG_CODE)
197                     {
198                       lcok=1;
199                       for (lt=0; lt <lang_code_number && lcok; lt++)
200                       {
201                         if(strcmp(lang_code[lt],temp_ptr)==0)
202                         {
203                           lcok=0;
204                           break;
205                         }
206                       }
207                       if(lcok)
208                       {
209                         xastir_snprintf(lang_code[lang_code_number],
210                                         MAX_LANG_CODE+1,
211                                         "%s",
212                                         temp_ptr);
213                         temp_ptr=strtok(NULL,"|");         /* get string */
214                         if (temp_ptr!=NULL)
215                         {
216                           data_len=(int)strlen(temp_ptr);
217                           if ((buffer_len+data_len+1)< MAX_LANG_BUFFER)
218                           {
219                             lang_code_ptr[lang_code_number]=lang_buffer+buffer_len;
220                             xastir_snprintf(lang_buffer+buffer_len,
221                                             MAX_LANG_BUFFER-buffer_len,
222                                             "%s",
223                                             temp_ptr);
224                             lang_buffer[buffer_len+data_len]='\0';
225                             buffer_len+=data_len+1;
226                             temp_ptr=strtok(NULL,"|");      /* get hotkey */
227                             if (temp_ptr!=NULL)
228                             {
229                               lang_hotkey[lang_code_number]=temp_ptr[0];
230                               /*fprintf(stderr,"HOTKEY %c\n",lang_hotkey[lang_code_number]);*/
231                             }
232                           }
233                           else
234                           {
235                             ok=0;
236                             fprintf(stderr,"Language data buffer full error on line %d\n",line_num);
237                           }
238                         }
239                         else
240                         {
241                           ok=0;
242                           fprintf(stderr,"Language string parse error on line %d\n",line_num);
243                         }
244                       }
245                       else
246                       {
247                         ok=0;
248                         fprintf(stderr,"Duplicate code! <%s> on line %d\n",temp_ptr,line_num);
249                       }
250                     }
251                     else
252                     {
253                       ok=0;
254                       fprintf(stderr,"Language code on line %d is too long\n",line_num);
255                     }
256                   }
257                   else
258                   {
259                     ok=0;
260                     fprintf(stderr,"Missing Language code data on line %d\n",line_num);
261                   }
262                 }
263                 else
264                 {
265                   ok=0;
266                   fprintf(stderr,"Language code parse error on line %d\n",line_num);
267                 }
268               }
269               else
270               {
271                 ok=0;
272                 fprintf(stderr,"Language data buffer full error on line %d\n",line_num);
273               }
274             }
275             else
276             {
277               ok=0;
278               fprintf(stderr,"Too many Language codes error on line %d\n",line_num);
279             }
280             if (ok)
281             {
282               if (debug_level & 32)
283                 fprintf(stderr,"Code #%d <%s> data <%s> hotkey <%c>\n",lang_code_number,
284                         lang_code[lang_code_number],lang_code_ptr[lang_code_number],
285                         lang_hotkey[lang_code_number]);
286               lang_code_number++;
287             }
288             line[0]='\0';
289           }
290           line_num++;
291         }
292       }
293     }
294     (void)fclose(f);
295   }
296   else
297   {
298     ok=0;
299     fprintf(stderr,"Could not read Language file: %s!\n",filename);
300   }
301   if (debug_level & 32)
302   {
303     fprintf(stderr,"LANG %d\n",lang_code_number);
304   }
305 
306   return(ok);
307 }
308 
309 
310