"Fossies" - the Fresh Open Source Software Archive

Member "links-1.03/language.c" (18 Sep 2011, 2995 Bytes) of archive /linux/www/links-1.03.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "language.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8_vs_1.03.

    1 #include "links.h"
    2 
    3 struct translation {
    4     int code;
    5     unsigned char *name;
    6 };
    7 
    8 struct translation_desc {
    9     struct translation *t;
   10 };
   11 
   12 unsigned char dummyarray[T__N_TEXTS];
   13 
   14 #include "language.inc"
   15 
   16 unsigned char **translation_array[N_LANGUAGES][N_CODEPAGES];
   17 
   18 int current_language;
   19 int current_lang_charset;
   20 
   21 void init_trans()
   22 {
   23     int i, j;
   24     for (i = 0; i < N_LANGUAGES; i++)
   25         for (j = 0; j < N_CODEPAGES; j++)
   26             translation_array[i][j] = NULL;
   27     current_language = 0;
   28     current_lang_charset = 0;
   29 }
   30 
   31 void shutdown_trans()
   32 {
   33     int i, j, k;
   34     for (i = 0; i < N_LANGUAGES; i++)
   35         for (j = 0; j < N_CODEPAGES; j++) if (translation_array[i][j]) {
   36             for (k = 0; k < T__N_TEXTS; k++) if (translation_array[i][j][k])
   37                 mem_free(translation_array[i][j][k]);
   38             mem_free(translation_array[i][j]);
   39         }
   40 }
   41 
   42 static inline int is_direct_text(unsigned char *text)
   43 {
   44 /* Do not compare to dummyarray directly - thwart some misoptimizations */
   45     unsigned char * volatile dm = dummyarray;
   46     return !(text >= dm && text < dm + T__N_TEXTS);
   47 }
   48 
   49 unsigned char *get_text_translation(unsigned char *text, struct terminal *term)
   50 {
   51     unsigned char **current_tra;
   52     struct conv_table *conv_table;
   53     unsigned char *trn;
   54     int ch = term ? term->spec->charset : 0;
   55     if (is_direct_text(text)) return text;
   56     if ((current_tra = translation_array[current_language][ch])) {
   57         unsigned char *tt;
   58         if ((trn = current_tra[text - dummyarray])) return trn;
   59         tr:
   60         if (!(tt = translations[current_language].t[text - dummyarray].name)) {
   61             trn = stracpy(translation_english[text - dummyarray].name);
   62         } else {
   63             conv_table = get_translation_table(current_lang_charset, ch);
   64             trn = convert_string(conv_table, tt, strlen(tt));
   65         }
   66         current_tra[text - dummyarray] = trn;
   67     } else {
   68         if (current_lang_charset && ch != current_lang_charset) {
   69             current_tra = translation_array[current_language][ch] = mem_alloc(sizeof (unsigned char **) * T__N_TEXTS);
   70             memset(current_tra, 0, sizeof (unsigned char **) * T__N_TEXTS);
   71             goto tr;
   72         }
   73         if (!(trn = translations[current_language].t[text - dummyarray].name)) {
   74             trn = translations[current_language].t[text - dummyarray].name = translation_english[text - dummyarray].name;   /* modifying translation structure */
   75         }
   76     }
   77     return trn;
   78 }
   79 
   80 unsigned char *get_english_translation(unsigned char *text)
   81 {
   82     if (is_direct_text(text)) return text;
   83     return translation_english[text - dummyarray].name;
   84 }
   85 
   86 int n_languages()
   87 {
   88     return N_LANGUAGES;
   89 }
   90 
   91 unsigned char *language_name(int l)
   92 {
   93     return translations[l].t[T__LANGUAGE].name;
   94 }
   95 
   96 void set_language(int l)
   97 {
   98     int i;
   99     unsigned char *cp;
  100     for (i = 0; i < T__N_TEXTS; i++) if (translations[l].t[i].code != i) {
  101         internal("Bad table for language %s. Run script synclang.", translations[l].t[T__LANGUAGE].name);
  102         return;
  103     }
  104     current_language = l;
  105     cp = translations[l].t[T__CHAR_SET].name;
  106     i = get_cp_index(cp);
  107     if (i == -1) {
  108         internal("Unknown charset for language %s.", translations[l].t[T__LANGUAGE].name);
  109         i = 0;
  110     }
  111     current_lang_charset = i;
  112 }