langtool.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/perl
  2. # Processes language_xx.h files into language.cpp and language.h
  3. use strict;
  4. use warnings;
  5. my @langs = ("en","cz","it","es","pl");
  6. sub parselang
  7. {
  8. my ($filename) = @_;
  9. open(my $fh, '<:encoding(UTF-8)', $filename)
  10. or die "Could not open file '$filename' $!";
  11. # Create a new hash reference.
  12. my $out = {};
  13. while (my $line = <$fh>) {
  14. chomp $line;
  15. next if (index($line, 'MSG') == -1);
  16. $line =~ /(?is)\#define\s*(\S*)\s*(.*)/;
  17. my $symbol = $1;
  18. my $v = $2;
  19. next if (! defined $symbol or length($symbol) == 0);
  20. # Trim whitespaces from both sides
  21. $v =~ s/^\s+|\s+$//g;
  22. #$string =~ s/" MACHINE_NAME "/Prusa i3/;
  23. $v =~ s/" FIRMWARE_URL "/https:\/\/github.com\/prusa3d\/Prusa-i3-Plus\//;
  24. $v =~ s/" PROTOCOL_VERSION "/1.0/;
  25. $v =~ s/" STRINGIFY\(EXTRUDERS\) "/1/;
  26. $v =~ s/" MACHINE_UUID "/00000000-0000-0000-0000-000000000000/;
  27. ${$out}{$symbol} = $v;
  28. }
  29. return $out;
  30. }
  31. my %texts;
  32. my $num_languages = 0;
  33. foreach my $lang (@langs) {
  34. my $symbols = parselang("language_$lang.h");
  35. foreach my $key (keys %{$symbols}) {
  36. if (! (exists $texts{$key})) {
  37. $texts{$key} = [];
  38. }
  39. my $strings = $texts{$key};
  40. die "Symbol $key defined first in $lang, undefined in the preceding language files."
  41. if (scalar(@$strings) != $num_languages);
  42. push @$strings, ${$symbols}{$key};
  43. }
  44. $num_languages += 1;
  45. foreach my $key (keys %texts) {
  46. my $strings = $texts{$key};
  47. if (scalar(@$strings) != $num_languages) {
  48. # die "Symbol $key undefined in $lang."
  49. print "Symbol $key undefined in $lang. Using the english variant.\n";
  50. push @$strings, ${$strings}[0];
  51. }
  52. }
  53. }
  54. my $filename = 'language_all.h';
  55. open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
  56. # For the programmatic access to the program memory, read
  57. # http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
  58. print $fh <<END
  59. #ifndef LANGUAGE_ALL_H
  60. #define LANGUAGE_ALL_H
  61. #define LANG_NUM (${num_languages})
  62. extern unsigned char lang_selected;
  63. #define LANG_TABLE_SELECT_EXPLICIT(TABLE, LANG) ((const char*)(pgm_read_ptr(TABLE + (LANG))))
  64. #define LANG_TABLE_SELECT(TABLE) LANG_TABLE_SELECT_EXPLICIT(TABLE, lang_selected)
  65. END
  66. ;
  67. foreach my $key (sort(keys %texts)) {
  68. print $fh "extern const char* const ${key}_LANG_TABLE[LANG_NUM];\n";
  69. print $fh "#define $key LANG_TABLE_SELECT(${key}_LANG_TABLE)\n";
  70. print $fh "#define ${key}_EXPLICIT(LANG) LANG_TABLE_SELECT_EXPLICIT(${key}_LANG_TABLE, LANG)\n"
  71. if ($key eq "MSG_LANGUAGE_NAME" || $key eq "MSG_LANGUAGE_SELECT");
  72. }
  73. print $fh <<END
  74. extern char* CAT2(const char *s1,const char *s2);
  75. extern char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4);
  76. #endif //LANGUAGE_ALL.H
  77. END
  78. ;
  79. close $fh;
  80. print ".h created\n";
  81. $filename = 'language_all.cpp';
  82. open($fh, '>', $filename) or die "Could not open file '$filename' $!";
  83. print $fh <<'END'
  84. #include <avr/pgmspace.h>
  85. #include "configuration_prusa.h"
  86. #include "language_all.h"
  87. #define LCD_WIDTH 20
  88. extern unsigned char lang_selected;
  89. END
  90. ;
  91. foreach my $key (sort(keys %texts)) {
  92. my $strings = $texts{$key};
  93. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  94. my $suffix = uc($langs[$i]);
  95. print $fh "const char ${key}_${suffix}[] PROGMEM = ${$strings}[$i];\n";
  96. }
  97. print $fh "const char * const ${key}_LANG_TABLE[LANG_NUM] PROGMEM = {\n";
  98. for (my $i = 0; $i <= $#{$strings}; $i ++) {
  99. my $suffix = uc($langs[$i]);
  100. print $fh "\t${key}_${suffix}";
  101. print $fh ',' if $i < $#{$strings};
  102. print $fh "\n";
  103. }
  104. print $fh "};\n\n";
  105. }
  106. print $fh <<'END'
  107. char langbuffer[LCD_WIDTH+1];
  108. char* CAT2(const char *s1,const char *s2) {
  109. unsigned char len=0;
  110. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  111. len+=strlen_P(s1);
  112. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  113. return langbuffer;
  114. }
  115. char* CAT4(const char *s1,const char *s2,const char *s3,const char *s4) {
  116. unsigned char len=0;
  117. strncpy_P(langbuffer+len,s1,LCD_WIDTH-len);
  118. len+=strlen_P(s1);
  119. strncpy_P(langbuffer+len,s2,LCD_WIDTH-len);
  120. len+=strlen_P(s2);
  121. strncpy_P(langbuffer+len,s3,LCD_WIDTH-len);
  122. len+=strlen_P(s3);
  123. strncpy_P(langbuffer+len,s4,LCD_WIDTH-len);
  124. return langbuffer;
  125. }
  126. END
  127. ;
  128. print ".cpp created.\nDone!\n";