|
|
View previous topic :: View next topic |
Author |
Message |
Jhonatan
Joined: 18 Mar 2019 Posts: 6
|
Help on handling const string tables by pointer or similar |
Posted: Fri Apr 12, 2019 11:31 am |
|
|
So the purpose is handle multi-language support with run time selection in a not "full of ifs" way.
I kind of got to a solution that would not be so ugly, which involves storing the translated texts in tables and point access them, but there will be too much RAM involved.
I can't find a solution to make it work with constant tables as I can't point them (or at least I'm missing something).
The code below does not work but I think it shows the objective very clearly.
I apreciate if someone could put a work around to make these pointers work or something similar to address the correct table.
I did not try CONST=READ_ONLY for the same reason of the RAM usage.
Compiled in CCS v5.076 and tested in MPLAB v8.092
Code: | #include <18F26K22.h>
#device ADC = 10
#FUSES NOWDT, PUT
#use delay(internal = 64000000)
#use RS232(UART1, ERRORS, baud = 9600, stream = PORT1)
enum {
STR_HELLO,
STR_USER
};
const char en_us[][*] = {
"HELLO",
"USER"
};
const char pt_br[][*] = {
"OLA",
"USUARIO"
};
int8 *language;
void main() {
printf("1: %s %s\r\n", en_us[STR_HELLO], en_us[STR_USER]); // Works
printf("2: %s %s\r\n", pt_br[STR_HELLO], pt_br[STR_USER]); // Works
// The purpose is to do this or something similar, for run time language selection
language = &en_us;
printf("3: %s %s\r\n", language[STR_HELLO], language[STR_USER]);
while (TRUE);
} |
Output:
Quote: | 1: HELLO USER
2: OLA USUARIO
3:
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Jhonatan
Joined: 18 Mar 2019 Posts: 6
|
|
Posted: Mon Apr 15, 2019 1:24 pm |
|
|
Thanks PCM programmer, it worked. Perhaps I could have searched more.
Code below outputs this, which is correct, when compiled in v5.076.
Quote: | 1: HELLO USER
2: OLA USUARIO
3: HELLO USER
4: OLA USUARIO
|
Code: | #include <18F26K22.h>
#device ADC = 10
#FUSES NOWDT, PUT
#use delay(internal = 64000000)
#use RS232(UART1, ERRORS, baud = 9600, stream = PORT1)
enum {
STR_HELLO,
STR_USER
};
rom char *en_us[][] = {
"HELLO",
"USER"
};
rom char *pt_br[][] = {
"OLA",
"USUARIO"
};
rom char **language[2] = {&en_us, &pt_br};
void main() {
int8 set = 0;
printf("1: %s %s\r\n", en_us[STR_HELLO], en_us[STR_USER]);
printf("2: %s %s\r\n", pt_br[STR_HELLO], pt_br[STR_USER]);
set = 0;
printf("3: %s %s\r\n", language[set][STR_HELLO], language[set][STR_USER]);
set = 1;
printf("4: %s %s\r\n", language[set][STR_HELLO], language[set][STR_USER]);
while (TRUE);
} |
I also tested in v5.025 and got unexpected output:
Quote: |
1: HELLO USER
2: OLA USUARIO
3: 9o 9o
4: 9o
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 15, 2019 1:30 pm |
|
|
Jhonatan wrote: |
I also tested in v5.025 and got unexpected output:
1: HELLO USER
2: OLA USUARIO
3: 9o 9o
4: 9o
|
CCS did some bug fixes after that version:
5.037 Optimization bugs dealing with some ROM pointers and clear_interrupts(INT_RB) are fixed.
5.033 Some pointers to ROM bugs have been fixed on the 14 bit parts. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|