blob: 715f83f67296875a8c64bafea76d8dc9d8045ea6 [file] [log] [blame]
Daniel Stenberg8d38c962002-09-24 12:39:33 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Daniel Stenberg
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Daniel Stenberg8d38c962002-09-24 12:39:33 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
Thomas Martitz6a9a8192010-12-25 18:43:34 +000022#include <stdio.h>
Thomas Martitz2c2e2612010-08-27 12:38:25 +000023#include "file.h"
Daniel Stenberg8d38c962002-09-24 12:39:33 +000024
25#include "language.h"
26#include "lang.h"
27#include "debug.h"
Jens Arnold0f040292005-01-19 21:43:15 +000028#include "string.h"
Tomer Shalev5da75072009-10-05 21:32:29 +000029#ifdef HAVE_LCD_BITMAP
Tomer Shalevb0a99382009-10-05 21:13:55 +000030#include "viewport.h"
Tomer Shalev5da75072009-10-05 21:32:29 +000031#endif
Daniel Stenberg8d38c962002-09-24 12:39:33 +000032
Nils Wallménius4317ff92008-12-03 12:14:46 +000033/* The following header is generated by the build system and only defines
34 MAX_LANGUAGE_SIZE to be the size of the largest currently available
35 language! */
36#include "max_language_size.h"
37
Tomer Shalev2a91a9a2009-10-04 22:25:52 +000038/* These defines must match the initial bytes in the binary lang file */
39/* See tools/genlang (TODO: Use common include for both) */
40#define LANGUAGE_COOKIE 0x1a
Tom Rossec2737b2009-10-18 00:56:42 +000041#define LANGUAGE_VERSION 0x06
Tomer Shalev6d805652009-10-05 11:43:38 +000042#define LANGUAGE_FLAG_RTL 0x01
Nils Wallménius4317ff92008-12-03 12:14:46 +000043
Tomer Shalev6d805652009-10-05 11:43:38 +000044#define HEADER_SIZE 4
Tom Rossec2737b2009-10-18 00:56:42 +000045#define SUBHEADER_SIZE 6
Nils Wallménius4317ff92008-12-03 12:14:46 +000046
Daniel Stenberg8d38c962002-09-24 12:39:33 +000047static unsigned char language_buffer[MAX_LANGUAGE_SIZE];
Tomer Shalev6d805652009-10-05 11:43:38 +000048static unsigned char lang_options = 0;
49
Tom Ross9fbf3462009-10-17 06:07:50 +000050void lang_init(const unsigned char *builtin, unsigned char **dest, int count)
Tomer Shalev6d805652009-10-05 11:43:38 +000051{
Tom Ross9fbf3462009-10-17 06:07:50 +000052 while(count--) {
53 *dest++ = (unsigned char *)builtin;
54 /* advance pointer to next string */
55 builtin += strlen((char *)builtin) + 1;
56 }
Tomer Shalev6d805652009-10-05 11:43:38 +000057}
58
Tom Rossec2737b2009-10-18 00:56:42 +000059int lang_load(const char *filename, const unsigned char *builtin,
60 unsigned char **dest, unsigned char *buffer,
61 unsigned int user_num, int max_lang_size,
62 unsigned int max_id)
Daniel Stenberg8d38c962002-09-24 12:39:33 +000063{
Tom Rossec2737b2009-10-18 00:56:42 +000064 int lang_size;
Daniel Stenberg571e8a22002-09-24 12:48:23 +000065 int fd = open(filename, O_RDONLY);
Daniel Stenberg2d8aef92002-09-24 13:53:41 +000066 int retcode=0;
Tomer Shalev2a91a9a2009-10-04 22:25:52 +000067 unsigned char lang_header[HEADER_SIZE];
Tom Rossec2737b2009-10-18 00:56:42 +000068 unsigned char sub_header[SUBHEADER_SIZE];
Rafaël Carré94f5f5b2010-09-21 08:25:52 +000069 unsigned int id, foffset;
Tom Rossec2737b2009-10-18 00:56:42 +000070
Nils Wallméniusa0199642008-04-16 19:51:43 +000071 if(fd < 0)
Daniel Stenberg2d8aef92002-09-24 13:53:41 +000072 return 1;
Tom Rossec2737b2009-10-18 00:56:42 +000073 read(fd, lang_header, HEADER_SIZE);
74 if((lang_header[0] == LANGUAGE_COOKIE) &&
75 (lang_header[1] == LANGUAGE_VERSION) &&
76 (lang_header[2] == TARGET_ID)) {
77 /* jump to the proper entry in the table of subheaders */
78 lseek(fd, user_num * SUBHEADER_SIZE, SEEK_CUR);
79 read(fd, sub_header, SUBHEADER_SIZE);
80 /* read in information about the requested lang */
Rafaël Carré94f5f5b2010-09-21 08:25:52 +000081#if 0 /* unused */
82 unsigned int num_strings = (sub_header[0]<<8) | sub_header[1];
83#endif
Tom Rossec2737b2009-10-18 00:56:42 +000084 lang_size = (sub_header[2]<<8) | sub_header[3];
85 foffset = (sub_header[4]<<8) | sub_header[5];
86 if(lang_size <= max_lang_size) {
Tom Ross9fbf3462009-10-17 06:07:50 +000087 /* initialize with builtin */
Teruaki Kawashima4f1d9b12010-07-11 15:10:01 +000088 lang_init(builtin, dest, max_id);
Tom Rossec2737b2009-10-18 00:56:42 +000089 lseek(fd, foffset, SEEK_SET);
90 read(fd, buffer, lang_size);
Daniel Stenberg8d38c962002-09-24 12:39:33 +000091
Tom Rossec2737b2009-10-18 00:56:42 +000092 while(lang_size>3) {
93 id = ((buffer[0]<<8) | buffer[1]); /* get two-byte id */
94 buffer += 2; /* pass the id */
95 if(id < max_id) {
Daniel Stenberg5c834872005-02-22 12:55:21 +000096#if 0
Tom Rossec2737b2009-10-18 00:56:42 +000097 DEBUGF("%2x New: %30s ", id, buffer);
98 DEBUGF("Replaces: %s\n", dest[id]);
Daniel Stenberg4ce3f722002-10-29 12:31:34 +000099#endif
Tom Rossec2737b2009-10-18 00:56:42 +0000100 dest[id] = buffer; /* point to this string */
Daniel Stenberg4ce3f722002-10-29 12:31:34 +0000101 }
Tom Rossec2737b2009-10-18 00:56:42 +0000102 while(*buffer) { /* pass the string */
103 lang_size--;
104 buffer++;
105 }
106 lang_size-=3; /* the id and the terminating zero */
107 buffer++; /* pass the terminating zero-byte */
Daniel Stenberg571e8a22002-09-24 12:48:23 +0000108 }
Daniel Stenberg8d38c962002-09-24 12:39:33 +0000109 }
Daniel Stenberg571e8a22002-09-24 12:48:23 +0000110 else {
Tom Rossec2737b2009-10-18 00:56:42 +0000111 DEBUGF("Language %s too large: %d\n", filename, lang_size);
Daniel Stenberg2d8aef92002-09-24 13:53:41 +0000112 retcode = 2;
Daniel Stenberg571e8a22002-09-24 12:48:23 +0000113 }
Daniel Stenberg8d38c962002-09-24 12:39:33 +0000114 }
115 else {
Tom Rossec2737b2009-10-18 00:56:42 +0000116 DEBUGF("Illegal language file\n");
Daniel Stenberg2d8aef92002-09-24 13:53:41 +0000117 retcode = 3;
Daniel Stenberg8d38c962002-09-24 12:39:33 +0000118 }
Daniel Stenberg571e8a22002-09-24 12:48:23 +0000119 close(fd);
Tomer Shalevd5b076b2009-10-11 20:15:22 +0000120 lang_options = retcode ? 0 : lang_header[3];
Daniel Stenberg2d8aef92002-09-24 13:53:41 +0000121 return retcode;
Daniel Stenberg8d38c962002-09-24 12:39:33 +0000122}
Jonathan Gordon340f3232009-09-26 00:58:32 +0000123
Tom Rossec2737b2009-10-18 00:56:42 +0000124int lang_core_load(const char *filename)
125{
126 return lang_load(filename, core_language_builtin, language_strings,
127 language_buffer, 0, MAX_LANGUAGE_SIZE,
128 LANG_LAST_INDEX_IN_ARRAY);
129}
130
Tom Ross9fbf3462009-10-17 06:07:50 +0000131int lang_english_to_id(const char *english)
Jonathan Gordon340f3232009-09-26 00:58:32 +0000132{
133 int i;
Tom Rossec2737b2009-10-18 00:56:42 +0000134 unsigned char *ptr = (unsigned char *) core_language_builtin;
Jonathan Gordon340f3232009-09-26 00:58:32 +0000135
136 for (i = 0; i < LANG_LAST_INDEX_IN_ARRAY; i++) {
137 if (!strcmp(ptr, english))
138 return i;
139 ptr += strlen((char *)ptr) + 1; /* advance pointer to next string */
140 }
141 return -1;
142}
Tom Ross9fbf3462009-10-17 06:07:50 +0000143
144int lang_is_rtl(void)
145{
146 return (lang_options & LANGUAGE_FLAG_RTL) != 0;
147}