FireBirdLib - Topfield TMS PVR TAP Programming Library
LangLoadStrings.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <string.h>
3#include "FBLib_ini.h"
4#include "libFireBird.h"
5
6dword LangNrStrings = 0;
7dword *LangStringPtr = NULL;
8char *LangStrings = NULL;
9
10INILOCATION LangLoadStrings(char *LangFile, dword NrStrings, int FallbackLang, char *AppName)
11{
12 TRACEENTER();
13
14 dword i;
15 int OSDLan;
16 char Key[10], s[512], *c;
17 dword TotalLength;
18 char *OldBuffer, *OldCommentBuffer;
19 dword OldBufferSize;
20 INILOCATION LangLocation;
21
23
24 OSDLan = TAP_GetSystemVar(SYSVAR_OsdLan);
25 if((NrStrings > 999999) || !iso639_1(OSDLan) || (FallbackLang > LAN_Bulgarian))
26 {
27 TRACEEXIT();
29 }
30
31 OldBuffer = INIBuffer;
32 OldCommentBuffer = INICommentBuffer;
33 OldBufferSize = BufferSize;
35 if((LangLocation = INIOpenFile(LangFile, AppName)) < INILOCATION_AtCurrentDir)
36 {
37 if(LangLocation == INILOCATION_NewFile) INICloseFile();
39
40 TRACEEXIT();
42 }
43
44 //First loop: count the length of all strings
45 LangStringPtr = TAP_MemAlloc(NrStrings * sizeof(dword));
46 memset(LangStringPtr, 0, NrStrings * sizeof(dword));
47 TotalLength = 0;
48 for(i = 0; i < NrStrings; i++)
49 {
50 LangStringPtr[i] = TotalLength;
51 TAP_SPrint(Key, "%s_%3.3lu", iso639_1(OSDLan), i);
52 INIGetString(Key, s, "", sizeof(s));
53
54 //If the string is not available in the current language, use the fallback language
55 if(!s[0] && (FallbackLang >= 0))
56 {
57 TAP_SPrint(Key, "%s_%3.3lu", iso639_1(FallbackLang), i);
58 INIGetString(Key, s, "", sizeof(s));
59 }
60
61 //Convert control characters
62 c = s;
63 while((c = strchr(c, '\\')))
64 {
65 if(c[1] == 'r') *c ='\r';
66 else if(c[1] == 'n') *c ='\n';
67 else continue;
68
69 memmove(c + 1, c + 2, strchr(c, '\0') - c - 1);
70 }
71 TotalLength += (strlen(s) + 1);
72 }
73
74 //Second loop: Read the strings
75 LangStrings = TAP_MemAlloc(TotalLength);
76 memset(LangStrings, 0, TotalLength);
77 for(i = 0; i < NrStrings; i++)
78 {
79 TAP_SPrint(Key, "%s_%3.3lu", iso639_1(OSDLan), i);
80 INIGetString(Key, s, "", sizeof(s));
81
82 //If the string is not available in the current language, use the fallback language
83 if(!s[0] && (FallbackLang >= 0))
84 {
85 TAP_SPrint(Key, "%s_%3.3lu", iso639_1(FallbackLang), i);
86 INIGetString(Key, s, "", sizeof(s));
87 }
88
89 //Convert control characters
90 c = s;
91 while((c = strchr(c, '\\')))
92 {
93 if(c[1] == 'r') *c ='\r';
94 else if(c[1] == 'n') *c ='\n';
95 else continue;
96
97 memmove(c + 1, c + 2, strchr(c, '\0') - c - 1);
98 }
99
100 strcpy(&LangStrings[LangStringPtr[i]], s);
101 }
102
103 INICloseFile();
104
105 INIBuffer = OldBuffer;
106 INICommentBuffer = OldCommentBuffer;
107 BufferSize = OldBufferSize;
108
109 LangNrStrings = NrStrings;
110
112
113 TRACEEXIT();
114 return LangLocation;
115}
dword BufferSize
Definition: INIOpenFile.c:7
char * INIBuffer
Definition: INIOpenFile.c:6
char * INICommentBuffer
Definition: FBLib_ini.h:9
void INICloseFile(void)
Definition: INICloseFile.c:5
bool INIGetString(char *Key, char *Value, char *DefaultValue, dword MaxLength)
Definition: INIGetString.c:5
INILOCATION INIOpenFile(char *FileName, char *AppName)
Definition: INIOpenFile.c:9
INILOCATION LangLoadStrings(char *LangFile, dword NrStrings, int FallbackLang, char *AppName)
char * LangStrings
dword LangNrStrings
dword * LangStringPtr
void LangUnloadStrings(void)
bool HDD_TAP_PushDir(void)
#define TRACEEXIT()
Definition: libFireBird.h:1244
char * iso639_1(int OSDLan)
Definition: iso639_1.c:3
#define LAN_Bulgarian
Definition: libFireBird.h:2687
INILOCATION
Definition: libFireBird.h:2000
@ INILOCATION_AtCurrentDir
Definition: libFireBird.h:2003
@ INILOCATION_NotFound
Definition: libFireBird.h:2001
@ INILOCATION_NewFile
Definition: libFireBird.h:2002
#define TRACEENTER()
Definition: libFireBird.h:1243
bool HDD_TAP_PopDir(void)
Definition: HDD_TAP_PopDir.c:3