# newgrf_text.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_text.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00012 #include "stdafx.h"
    00013 #include "newgrf.h"
    00014 #include "strings_func.h"
    00015 #include "newgrf_storage.h"
    00016 #include "string_func.h"
    00017 #include "date_type.h"
    00018
    00019 #include "table/strings.h"
    00020 #include "table/control_codes.h"
    00021
    00022 #define GRFTAB  28
    00023 #define TABSIZE 11
    00024
    00030 enum grf_base_languages {
    00031   GRFLB_AMERICAN    = 0x01,
    00032   GRFLB_ENGLISH     = 0x02,
    00033   GRFLB_GERMAN      = 0x04,
    00034   GRFLB_FRENCH      = 0x08,
    00035   GRFLB_SPANISH     = 0x10,
    00036   GRFLB_GENERIC     = 0x80,
    00037 };
    00038
    00039 enum grf_extended_languages {
    00040   GRFLX_AMERICAN    = 0x00,
    00041   GRFLX_ENGLISH     = 0x01,
    00042   GRFLX_GERMAN      = 0x02,
    00043   GRFLX_FRENCH      = 0x03,
    00044   GRFLX_SPANISH     = 0x04,
    00045   GRFLX_ESPERANTO   = 0x05,
    00046   GRFLX_RUSSIAN     = 0x07,
    00047   GRFLX_CZECH       = 0x15,
    00048   GRFLX_SLOVAK      = 0x16,
    00049   GRFLX_BULGARIAN   = 0x18,
    00050   GRFLX_AFRIKAANS   = 0x1B,
    00051   GRFLX_GREEK       = 0x1E,
    00052   GRFLX_DUTCH       = 0x1F,
    00053   GRFLX_CATALAN     = 0x22,
    00054   GRFLX_HUNGARIAN   = 0x24,
    00055   GRFLX_ITALIAN     = 0x27,
    00056   GRFLX_ROMANIAN    = 0x28,
    00057   GRFLX_ICELANDIC   = 0x29,
    00058   GRFLX_LATVIAN     = 0x2A,
    00059   GRFLX_LITHUANIAN  = 0x2B,
    00060   GRFLX_SLOVENIAN   = 0x2C,
    00061   GRFLX_DANISH      = 0x2D,
    00062   GRFLX_SWEDISH     = 0x2E,
    00063   GRFLX_NORWEGIAN   = 0x2F,
    00064   GRFLX_POLISH      = 0x30,
    00065   GRFLX_GALICIAN    = 0x31,
    00066   GRFLX_FRISIAN     = 0x32,
    00067   GRFLX_UKRAINIAN   = 0x33,
    00068   GRFLX_ESTONIAN    = 0x34,
    00069   GRFLX_FINNISH     = 0x35,
    00070   GRFLX_PORTUGUESE  = 0x36,
    00071   GRFLX_BRAZILIAN   = 0x37,
    00072   GRFLX_CROATIAN    = 0x38,
    00073   GRFLX_JAPANESE    = 0x39,
    00074   GRFLX_KOREAN      = 0x3A,
    00075   GRFLX_TURKISH     = 0x3E,
    00076   GRFLX_UNSPECIFIED = 0x7F,
    00077 };
    00078
    00079
    00080 struct iso_grf {
    00081   char code[6];
    00082   byte grfLangID;
    00083 };
    00084
    00093 const iso_grf iso_codes[] = {
    00094   {"en_US", GRFLX_AMERICAN},
    00095   {"en_GB", GRFLX_ENGLISH},
    00096   {"de_DE", GRFLX_GERMAN},
    00097   {"fr_FR", GRFLX_FRENCH},
    00098   {"es_ES", GRFLX_SPANISH},
    00099   {"af_ZA", GRFLX_AFRIKAANS},
    00100   {"hr_HR", GRFLX_CROATIAN},
    00101   {"cs_CZ", GRFLX_CZECH},
    00102   {"ca_ES", GRFLX_CATALAN},
    00103   {"da_DA", GRFLX_DANISH},
    00104   {"nl_NL", GRFLX_DUTCH},
    00105   {"et_ET", GRFLX_ESTONIAN},
    00106   {"fi_FI", GRFLX_FINNISH},
    00107   {"fy_NL", GRFLX_FRISIAN},
    00108   {"gl_ES", GRFLX_GALICIAN},
    00109   {"el_GR", GRFLX_GREEK},
    00110   {"hu_HU", GRFLX_HUNGARIAN},
    00111   {"is_IS", GRFLX_ICELANDIC},
    00112   {"it_IT", GRFLX_ITALIAN},
    00113   {"lv_LV", GRFLX_LATVIAN},
    00114   {"lt_LT", GRFLX_LITHUANIAN},
    00115   {"nb_NO", GRFLX_NORWEGIAN},
    00116   {"pl_PL", GRFLX_POLISH},
    00117   {"pt_PT", GRFLX_PORTUGUESE},
    00118   {"pt_BR", GRFLX_BRAZILIAN},
    00119   {"ro_RO", GRFLX_ROMANIAN},
    00120   {"ru_RU", GRFLX_RUSSIAN},
    00121   {"sk_SK", GRFLX_SLOVAK},
    00122   {"sl_SL", GRFLX_SLOVENIAN},
    00123   {"sv_SE", GRFLX_SWEDISH},
    00124   {"tr_TR", GRFLX_TURKISH},
    00125   {"uk_UA", GRFLX_UKRAINIAN},
    00126   {"eo_EO", GRFLX_ESPERANTO},
    00127   {"bg_BG", GRFLX_BULGARIAN},
    00128   {"ja_JP", GRFLX_JAPANESE},
    00129   {"ko_KR", GRFLX_KOREAN},
    00130   {"gen",   GRFLB_GENERIC}
    00131 };
    00132
    00133
    00139 struct GRFText {
    00140   public:
    00141     static GRFText* New(byte langid, const char* text)
    00142     {
    00143       return new(strlen(text) + 1) GRFText(langid, text);
    00144     }
    00145
    00146   private:
    00147     GRFText(byte langid_, const char* text_) : next(NULL), langid(langid_)
    00148     {
    00149       strcpy(text, text_);
    00150     }
    00151
    00152     void *operator new(size_t size, size_t extra)
    00153     {
    00154       return ::operator new(size + extra);
    00155     }
    00156
    00157 public:
    00158     /* dummy operator delete to silence VC8:
    00159      * 'void *GRFText::operator new(size_t,size_t)' : no matching operator delete found;
    00160      *     memory will not be freed if initialization throws an exception */
    00161     void operator delete(void *p, size_t extra)
    00162     {
    00163       return ::operator delete(p);
    00164     }
    00165
    00166   public:
    00167     GRFText *next;
    00168     byte langid;
    00169     char text[VARARRAY_SIZE];
    00170 };
    00171
    00172
    00178 struct GRFTextEntry {
    00179   uint32 grfid;
    00180   uint16 stringid;
    00181   StringID def_string;
    00182   GRFText *textholder;
    00183 };
    00184
    00185
    00186 static uint _num_grf_texts = 0;
    00187 static GRFTextEntry _grf_text[(1 << TABSIZE) * 3];
    00188 static byte _currentLangID = GRFLX_ENGLISH;
    00189
    00190
    00191 char *TranslateTTDPatchCodes(uint32 grfid, const char *str)
    00192 {
    00193   char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
    00194   char *d = tmp;
    00195   bool unicode = false;
    00196   WChar c;
    00197   size_t len = Utf8Decode(&c, str);
    00198
    00199   if (c == 0x00DE) {
    00200     /* The thorn ('þ') indicates a unicode string to TTDPatch */
    00201     unicode = true;
    00202     str += len;
    00203   }
    00204
    00205   for (;;) {
    00206     if (unicode && Utf8EncodedCharLen(*str) != 0) {
    00207       c = Utf8Consume(&str);
    00208       /* 'Magic' range of control codes. */
    00209       if (GB(c, 8, 8) == 0xE0) c = GB(c, 0, 8);
    00210     } else {
    00211       c = (byte)*str++;
    00212     }
    00213     if (c == 0) break;
    00214
    00215     switch (c) {
    00216       case 0x01:
    00217         d += Utf8Encode(d, SCC_SETX);
    00218         *d++ = *str++;
    00219         break;
    00220       case 0x0A: break;
    00221       case 0x0D: *d++ = 0x0A; break;
    00222       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
    00223       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
    00224       case 0x1F:
    00225         d += Utf8Encode(d, SCC_SETXY);
    00226         *d++ = *str++;
    00227         *d++ = *str++;
    00228         break;
    00229       case 0x7B:
    00230       case 0x7C:
    00231       case 0x7D:
    00232       case 0x7E:
    00233       case 0x7F:
    00234       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
    00235       case 0x81: {
    00236         StringID string;
    00237         string  = ((uint8)*str++);
    00238         string |= ((uint8)*str++) << 8;
    00239         d += Utf8Encode(d, SCC_STRING_ID);
    00240         d += Utf8Encode(d, MapGRFStringID(grfid, string));
    00241         break;
    00242       }
    00243       case 0x82:
    00244       case 0x83:
    00245       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_SPEED + c - 0x82); break;
    00246       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
    00247       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
    00248       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_LITRES);  break;
    00249       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
    00250       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
    00251       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
    00252       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
    00253       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
    00254       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
    00255       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
    00256       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
    00257       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
    00258       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
    00259       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
    00260       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
    00261       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
    00262       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
    00263       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
    00264       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
    00265       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
    00266       case 0x9A:
    00267         switch (*str++) {
    00268           case 0: /* FALL THROUGH */
    00269           case 1:
    00270             d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY);
    00271             break;
    00272           case 3: {
    00273             uint16 tmp  = ((uint8)*str++);
    00274             tmp        |= ((uint8)*str++) << 8;
    00275             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
    00276             d += Utf8Encode(d, tmp);
    00277           } break;
    00278           case 4:
    00279             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
    00280             d += Utf8Encode(d, *str++);
    00281             break;
    00282           case 6:
    00283             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);
    00284             break;
    00285           case 7:
    00286             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);
    00287             break;
    00288           case 8:
    00289             d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);
    00290             break;
    00291
    00292           default:
    00293             grfmsg(1, "missing handler for extended format code");
    00294             break;
    00295         }
    00296         break;
    00297
    00298       case 0x9E: d += Utf8Encode(d, 0x20AC); break; // Euro
    00299       case 0x9F: d += Utf8Encode(d, 0x0178); break; // Y with diaeresis
    00300       case 0xA0: d += Utf8Encode(d, SCC_UPARROW); break;
    00301       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW); break;
    00302       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK); break;
    00303       case 0xAD: d += Utf8Encode(d, SCC_CROSS); break;
    00304       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW); break;
    00305       case 0xB4: d += Utf8Encode(d, SCC_TRAIN); break;
    00306       case 0xB5: d += Utf8Encode(d, SCC_LORRY); break;
    00307       case 0xB6: d += Utf8Encode(d, SCC_BUS); break;
    00308       case 0xB7: d += Utf8Encode(d, SCC_PLANE); break;
    00309       case 0xB8: d += Utf8Encode(d, SCC_SHIP); break;
    00310       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
    00311       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW); break;
    00312       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
    00313       default:
    00314         /* Validate any unhandled character */
    00315         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
    00316         d += Utf8Encode(d, c);
    00317         break;
    00318     }
    00319   }
    00320
    00321   *d = '\0';
    00322   tmp = ReallocT(tmp, strlen(tmp) + 1);
    00323   return tmp;
    00324 }
    00325
    00326
    00330 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
    00331 {
    00332   char *translatedtext;
    00333   uint id;
    00334
    00335   /* When working with the old language scheme (grf_version is less than 7) and
    00336    * English or American is among the set bits, simply add it as English in
    00337    * the new scheme, i.e. as langid = 1.
    00338    * If English is set, it is pretty safe to assume the translations are not
    00339    * actually translated.
    00340    */
    00341   if (!new_scheme) {
    00342     if (HASBITS(langid_to_add, GRFLB_AMERICAN | GRFLB_ENGLISH)) {
    00343       langid_to_add = GRFLX_ENGLISH;
    00344     } else {
    00345       StringID ret = STR_EMPTY;
    00346       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, text_to_add, def_string);
    00347       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, text_to_add, def_string);
    00348       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string);
    00349       return ret;
    00350     }
    00351   }
    00352
    00353   for (id = 0; id < _num_grf_texts; id++) {
    00354     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
    00355       break;
    00356     }
    00357   }
    00358
    00359   /* Too many strings allocated, return empty */
    00360   if (id == lengthof(_grf_text)) return STR_EMPTY;
    00361
    00362   translatedtext = TranslateTTDPatchCodes(grfid, text_to_add);
    00363
    00364   GRFText *newtext = GRFText::New(langid_to_add, translatedtext);
    00365
    00366   free(translatedtext);
    00367
    00368   /* If we didn't find our stringid and grfid in the list, allocate a new id */
    00369   if (id == _num_grf_texts) _num_grf_texts++;
    00370
    00371   if (_grf_text[id].textholder == NULL) {
    00372     _grf_text[id].grfid      = grfid;
    00373     _grf_text[id].stringid   = stringid;
    00374     _grf_text[id].def_string = def_string;
    00375     _grf_text[id].textholder = newtext;
    00376   } else {
    00377     GRFText **ptext, *text;
    00378     bool replaced = false;
    00379
    00380     /* Loop through all languages and see if we can replace a string */
    00381     for (ptext = &_grf_text[id].textholder; (text = *ptext) != NULL; ptext = &text->next) {
    00382       if (text->langid != langid_to_add) continue;
    00383       newtext->next = text->next;
    00384       *ptext = newtext;
    00385       delete text;
    00386       replaced = true;
    00387       break;
    00388     }
    00389
    00390     /* If a string wasn't replaced, then we must append the new string */
    00391     if (!replaced) *ptext = newtext;
    00392   }
    00393
    00394   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
    00395
    00396   return (GRFTAB << TABSIZE) + id;
    00397 }
    00398
    00399 /* Used to remember the grfid that the last retrieved string came from */
    00400 static uint32 _last_grfid = 0;
    00401
    00405 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
    00406 {
    00407   uint id;
    00408
    00409   /* grfid is zero when we're being called via an include */
    00410   if (grfid == 0) grfid = _last_grfid;
    00411
    00412   for (id = 0; id < _num_grf_texts; id++) {
    00413     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
    00414       return (GRFTAB << TABSIZE) + id;
    00415     }
    00416   }
    00417
    00418   return STR_UNDEFINED;
    00419 }
    00420
    00421
    00422 const char *GetGRFStringPtr(uint16 stringid)
    00423 {
    00424   const GRFText *default_text = NULL;
    00425   const GRFText *search_text;
    00426
    00427   assert(_grf_text[stringid].grfid != 0);
    00428
    00429   /* Remember this grfid in case the string has included text */
    00430   _last_grfid = _grf_text[stringid].grfid;
    00431
    00432   /*Search the list of lang-strings of this stringid for current lang */
    00433   for (search_text = _grf_text[stringid].textholder; search_text != NULL; search_text = search_text->next) {
    00434     if (search_text->langid == _currentLangID) {
    00435       return search_text->text;
    00436     }
    00437
    00438     /* If the current string is English or American, set it as the
    00439      * fallback language if the specific language isn't available. */
    00440     if (search_text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (search_text->langid == GRFLX_ENGLISH || search_text->langid == GRFLX_AMERICAN))) {
    00441       default_text = search_text;
    00442     }
    00443   }
    00444
    00445   /* If there is a fallback string, return that */
    00446   if (default_text != NULL) return default_text->text;
    00447
    00448   /* Use the default string ID if the fallback string isn't available */
    00449   return GetStringPtr(_grf_text[stringid].def_string);
    00450 }
    00451
    00462 void SetCurrentGrfLangID(const char *iso_name)
    00463 {
    00464   /* Use English by default, if we can't match up the iso_code. */
    00465   byte ret = GRFLX_ENGLISH;
    00466   byte i;
    00467
    00468   for (i=0; i < lengthof(iso_codes); i++) {
    00469     if (strncmp(iso_codes[i].code, iso_name, strlen(iso_codes[i].code)) == 0) {
    00470       /* We found a match, so let's use it. */
    00471       ret = iso_codes[i].grfLangID;
    00472       break;
    00473     }
    00474   }
    00475   _currentLangID = ret;
    00476 }
    00477
    00478 bool CheckGrfLangID(byte lang_id, byte grf_version)
    00479 {
    00480   if (grf_version < 7) {
    00481     switch (_currentLangID) {
    00482       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
    00483       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
    00484       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
    00485       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
    00486     }
    00487   }
    00488
    00489   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
    00490 }
    00491
    00496 void CleanUpStrings()
    00497 {
    00498   uint id;
    00499
    00500   for (id = 0; id < _num_grf_texts; id++) {
    00501     GRFText *grftext = _grf_text[id].textholder;
    00502     while (grftext != NULL) {
    00503       GRFText *grftext2 = grftext->next;
    00504       delete grftext;
    00505       grftext = grftext2;
    00506     }
    00507     _grf_text[id].grfid      = 0;
    00508     _grf_text[id].stringid   = 0;
    00509     _grf_text[id].textholder = NULL;
    00510   }
    00511
    00512   _num_grf_texts = 0;
    00513 }
    00514
    00515 struct TextRefStack {
    00516   byte stack[0x30];
    00517   byte position;
    00518   bool used;
    00519
    00520   TextRefStack() : used(false) {}
    00521
    00522   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
    00523   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
    00524
    00525   uint16 PopUnsignedWord()
    00526   {
    00527     uint16 val = this->PopUnsignedByte();
    00528     return val | (this->PopUnsignedByte() << 8);
    00529   }
    00530   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
    00531
    00532   uint32 PopUnsignedDWord()
    00533   {
    00534     uint32 val = this->PopUnsignedWord();
    00535     return val | (this->PopUnsignedWord() << 16);
    00536   }
    00537   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
    00538
    00539   uint64 PopUnsignedQWord()
    00540   {
    00541     uint64 val = this->PopUnsignedDWord();
    00542     return val | (((uint64)this->PopUnsignedDWord()) << 32);
    00543   }
    00544   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
    00545
    00547   void RotateTop4Words()
    00548   {
    00549     byte tmp[2];
    00550     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
    00551     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
    00552     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
    00553   }
    00554
    00555   void PushWord(uint16 word)
    00556   {
    00557     if (this->position >= 2) {
    00558       this->position -= 2;
    00559     } else {
    00560       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
    00561         this->stack[i] = this->stack[i - 2];
    00562       }
    00563     }
    00564     this->stack[this->position]     = GB(word, 0, 8);
    00565     this->stack[this->position + 1] = GB(word, 8, 8);
    00566   }
    00567
    00568   void ResetStack()  { this->position = 0; this->used = true; }
    00569   void RewindStack() { this->position = 0; }
    00570 };
    00571
    00572 static TextRefStack _newgrf_normal_textrefstack;
    00573 static TextRefStack _newgrf_error_textrefstack;
    00574
    00576 static TextRefStack *_newgrf_textrefstack = &_newgrf_normal_textrefstack;
    00577
    00582 void PrepareTextRefStackUsage(byte numEntries)
    00583 {
    00584   extern TemporaryStorageArray<uint32, 0x110> _temp_store;
    00585
    00586   _newgrf_textrefstack->ResetStack();
    00587
    00588   byte *p = _newgrf_textrefstack->stack;
    00589   for (uint i = 0; i < numEntries; i++) {
    00590     for (uint j = 0; j < 32; j += 8) {
    00591       *p = GB(_temp_store.Get(0x100 + i), j, 8);
    00592       p++;
    00593     }
    00594   }
    00595 }
    00596
    00598 void StopTextRefStackUsage() { _newgrf_textrefstack->used = false; }
    00599
    00600 void SwitchToNormalRefStack()
    00601 {
    00602   _newgrf_textrefstack = &_newgrf_normal_textrefstack;
    00603 }
    00604
    00605 void SwitchToErrorRefStack()
    00606 {
    00607   _newgrf_textrefstack = &_newgrf_error_textrefstack;
    00608 }
    00609
    00610 void RewindTextRefStack()
    00611 {
    00612   _newgrf_textrefstack->RewindStack();
    00613 }
    00614
    00621 uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv)
    00622 {
    00623   if (_newgrf_textrefstack->used) {
    00624     switch (scc) {
    00625       default: NOT_REACHED();
    00626       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack->PopSignedByte();    break;
    00627       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack->PopSignedWord();    break;
    00628       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack->PopUnsignedQWord(); break;
    00629
    00630       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
    00631       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack->PopSignedDWord();   break;
    00632
    00633       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack->PopUnsignedByte();  break;
    00634       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack->PopUnsignedDWord(); break;
    00635
    00636       case SCC_NEWGRF_PRINT_HEX_WORD:
    00637       case SCC_NEWGRF_PRINT_WORD_SPEED:
    00638       case SCC_NEWGRF_PRINT_WORD_LITRES:
    00639       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack->PopUnsignedWord();  break;
    00640
    00641       case SCC_NEWGRF_PRINT_DATE:
    00642       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack->PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
    00643
    00644       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack->PopUnsignedWord(); break;
    00645
    00646       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack->RotateTop4Words(); break;
    00647       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack->PushWord(Utf8Consume(str)); break;
    00648       case SCC_NEWGRF_UNPRINT:              *buff -= Utf8Consume(str); break;
    00649
    00650       case SCC_NEWGRF_PRINT_STRING_ID:
    00651         *argv = _newgrf_textrefstack->PopUnsignedWord();
    00652         if (*argv == STR_NULL) *argv = STR_EMPTY;
    00653         break;
    00654     }
    00655   }
    00656
    00657   switch (scc) {
    00658     default: NOT_REACHED();
    00659     case SCC_NEWGRF_PRINT_DWORD:
    00660     case SCC_NEWGRF_PRINT_SIGNED_WORD:
    00661     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
    00662     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
    00663       return SCC_COMMA;
    00664
    00665     case SCC_NEWGRF_PRINT_HEX_BYTE:
    00666     case SCC_NEWGRF_PRINT_HEX_WORD:
    00667     case SCC_NEWGRF_PRINT_HEX_DWORD:
    00668       return SCC_HEX;
    00669
    00670     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
    00671     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
    00672       return SCC_CURRENCY;
    00673
    00674     case SCC_NEWGRF_PRINT_STRING_ID:
    00675       return SCC_STRING1;
    00676
    00677     case SCC_NEWGRF_PRINT_DATE:
    00678       return SCC_DATE_LONG;
    00679
    00680     case SCC_NEWGRF_PRINT_MONTH_YEAR:
    00681       return SCC_DATE_TINY;
    00682
    00683     case SCC_NEWGRF_PRINT_WORD_SPEED:
    00684       return SCC_VELOCITY;
    00685
    00686     case SCC_NEWGRF_PRINT_WORD_LITRES:
    00687       return SCC_VOLUME;
    00688
    00689     case SCC_NEWGRF_DISCARD_WORD:
    00690     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
    00691     case SCC_NEWGRF_PUSH_WORD:
    00692     case SCC_NEWGRF_UNPRINT:
    00693       return 0;
    00694   }
    00695 }