Update of Czech language - FS#11674 by Marek Salaba
[maemo-rb.git] / apps / language.c
blobf8ec0a30877e71facb4464db445da14cf4d532bb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "file.h"
24 #include "language.h"
25 #include "lang.h"
26 #include "debug.h"
27 #include "string.h"
28 #ifdef HAVE_LCD_BITMAP
29 #include "viewport.h"
30 #endif
32 /* The following header is generated by the build system and only defines
33 MAX_LANGUAGE_SIZE to be the size of the largest currently available
34 language! */
35 #include "max_language_size.h"
37 /* These defines must match the initial bytes in the binary lang file */
38 /* See tools/genlang (TODO: Use common include for both) */
39 #define LANGUAGE_COOKIE 0x1a
40 #define LANGUAGE_VERSION 0x06
41 #define LANGUAGE_FLAG_RTL 0x01
43 #define HEADER_SIZE 4
44 #define SUBHEADER_SIZE 6
46 static unsigned char language_buffer[MAX_LANGUAGE_SIZE];
47 static unsigned char lang_options = 0;
49 void lang_init(const unsigned char *builtin, unsigned char **dest, int count)
51 while(count--) {
52 *dest++ = (unsigned char *)builtin;
53 /* advance pointer to next string */
54 builtin += strlen((char *)builtin) + 1;
58 int lang_load(const char *filename, const unsigned char *builtin,
59 unsigned char **dest, unsigned char *buffer,
60 unsigned int user_num, int max_lang_size,
61 unsigned int max_id)
63 int lang_size;
64 int fd = open(filename, O_RDONLY);
65 int retcode=0;
66 unsigned char lang_header[HEADER_SIZE];
67 unsigned char sub_header[SUBHEADER_SIZE];
68 unsigned int id, foffset;
70 if(fd < 0)
71 return 1;
72 read(fd, lang_header, HEADER_SIZE);
73 if((lang_header[0] == LANGUAGE_COOKIE) &&
74 (lang_header[1] == LANGUAGE_VERSION) &&
75 (lang_header[2] == TARGET_ID)) {
76 /* jump to the proper entry in the table of subheaders */
77 lseek(fd, user_num * SUBHEADER_SIZE, SEEK_CUR);
78 read(fd, sub_header, SUBHEADER_SIZE);
79 /* read in information about the requested lang */
80 #if 0 /* unused */
81 unsigned int num_strings = (sub_header[0]<<8) | sub_header[1];
82 #endif
83 lang_size = (sub_header[2]<<8) | sub_header[3];
84 foffset = (sub_header[4]<<8) | sub_header[5];
85 if(lang_size <= max_lang_size) {
86 /* initialize with builtin */
87 lang_init(builtin, dest, max_id);
88 lseek(fd, foffset, SEEK_SET);
89 read(fd, buffer, lang_size);
91 while(lang_size>3) {
92 id = ((buffer[0]<<8) | buffer[1]); /* get two-byte id */
93 buffer += 2; /* pass the id */
94 if(id < max_id) {
95 #if 0
96 DEBUGF("%2x New: %30s ", id, buffer);
97 DEBUGF("Replaces: %s\n", dest[id]);
98 #endif
99 dest[id] = buffer; /* point to this string */
101 while(*buffer) { /* pass the string */
102 lang_size--;
103 buffer++;
105 lang_size-=3; /* the id and the terminating zero */
106 buffer++; /* pass the terminating zero-byte */
109 else {
110 DEBUGF("Language %s too large: %d\n", filename, lang_size);
111 retcode = 2;
114 else {
115 DEBUGF("Illegal language file\n");
116 retcode = 3;
118 close(fd);
119 lang_options = retcode ? 0 : lang_header[3];
120 return retcode;
123 int lang_core_load(const char *filename)
125 return lang_load(filename, core_language_builtin, language_strings,
126 language_buffer, 0, MAX_LANGUAGE_SIZE,
127 LANG_LAST_INDEX_IN_ARRAY);
130 int lang_english_to_id(const char *english)
132 int i;
133 unsigned char *ptr = (unsigned char *) core_language_builtin;
135 for (i = 0; i < LANG_LAST_INDEX_IN_ARRAY; i++) {
136 if (!strcmp(ptr, english))
137 return i;
138 ptr += strlen((char *)ptr) + 1; /* advance pointer to next string */
140 return -1;
143 int lang_is_rtl(void)
145 return (lang_options & LANGUAGE_FLAG_RTL) != 0;