msg_conf.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "msg_conf.hpp"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "malloc.hpp"
  8. #include "showmsg.hpp"
  9. #include "strlib.hpp"
  10. /*
  11. * Return the message string of the specified number by [Yor]
  12. * (read in table msg_table, with specified lenght table in size)
  13. */
  14. const char* _msg_txt(int msg_number,int size, char ** msg_table)
  15. {
  16. if (msg_number >= 0 && msg_number < size &&
  17. msg_table[msg_number] != NULL && msg_table[msg_number][0] != '\0')
  18. return msg_table[msg_number];
  19. return "??";
  20. }
  21. /*
  22. * Read txt file and store them into msg_table
  23. */
  24. int _msg_config_read(const char* cfgName,int size, char ** msg_table)
  25. {
  26. uint16 msg_number, msg_count = 0, line_num = 0;
  27. char line[1024], w1[8], w2[512];
  28. FILE *fp;
  29. static int called = 1;
  30. if ((fp = fopen(cfgName, "r")) == NULL) {
  31. ShowError("Messages file not found: %s\n", cfgName);
  32. return -1;
  33. }
  34. if ((--called) == 0)
  35. memset(msg_table, 0, sizeof (msg_table[0]) * size);
  36. while (fgets(line, sizeof (line), fp)) {
  37. line_num++;
  38. if (line[0] == '/' && line[1] == '/')
  39. continue;
  40. if (sscanf(line, "%7[^:]: %511[^\r\n]", w1, w2) != 2)
  41. continue;
  42. if (strcmpi(w1, "import") == 0)
  43. _msg_config_read(w2,size,msg_table);
  44. else {
  45. msg_number = atoi(w1);
  46. if (msg_number >= 0 && msg_number < size) {
  47. if (msg_table[msg_number] != NULL)
  48. aFree(msg_table[msg_number]);
  49. size_t len = strnlen(w2,sizeof(w2)) + 1;
  50. msg_table[msg_number] = (char *) aMalloc(len * sizeof (char));
  51. safestrncpy(msg_table[msg_number], w2, len);
  52. msg_count++;
  53. }
  54. else
  55. ShowWarning("Invalid message ID '%s' at line %d from '%s' file.\n",w1,line_num,cfgName);
  56. }
  57. }
  58. fclose(fp);
  59. ShowInfo("Done reading " CL_WHITE "'%d'" CL_RESET " messages in " CL_WHITE "'%s'" CL_RESET ".\n",msg_count,cfgName);
  60. return 0;
  61. }
  62. /*
  63. * Destroy msg_table (freeup mem)
  64. */
  65. void _do_final_msg(int size, char ** msg_table){
  66. int i;
  67. for (i = 0; i < size; i++)
  68. aFree(msg_table[i]);
  69. }
  70. /*
  71. * lookup a langtype string into his associate langtype number
  72. * return -1 if not found
  73. */
  74. int msg_langstr2langtype(char * langtype){
  75. int lang=-1;
  76. if (!strncmpi(langtype, "eng",2)) lang = 0;
  77. else if (!strncmpi(langtype, "rus",2)) lang = 1;
  78. else if (!strncmpi(langtype, "spn",2)) lang = 2;
  79. else if (!strncmpi(langtype, "grm",2)) lang = 3;
  80. else if (!strncmpi(langtype, "chn",2)) lang = 4;
  81. else if (!strncmpi(langtype, "mal",2)) lang = 5;
  82. else if (!strncmpi(langtype, "idn",2)) lang = 6;
  83. else if (!strncmpi(langtype, "frn",2)) lang = 7;
  84. else if (!strncmpi(langtype, "por",2)) lang = 8;
  85. else if (!strncmpi(langtype, "tha",2)) lang = 9;
  86. return lang;
  87. }
  88. /*
  89. * lookup a langtype into his associate lang string
  90. * return ?? if not found
  91. */
  92. const char* msg_langtype2langstr(int langtype){
  93. switch(langtype){
  94. case 0: return "English (ENG)";
  95. case 1: return "Russkiy (RUS)"; //transliteration
  96. case 2: return "Espanol (SPN)";
  97. case 3: return "Deutsch (GRM)";
  98. case 4: return "Hanyu (CHN)"; //transliteration
  99. case 5: return "Bahasa Malaysia (MAL)";
  100. case 6: return "Bahasa Indonesia (IDN)";
  101. case 7: return "Francais (FRN)";
  102. case 8: return "Portugues Brasileiro (POR)";
  103. case 9: return "Thai (THA)";
  104. default: return "??";
  105. }
  106. }
  107. /*
  108. * verify that the choosen langtype is enable
  109. * return
  110. * 1 : langage enable
  111. * -1 : false range
  112. * -2 : disable
  113. */
  114. int msg_checklangtype(int lang, bool display){
  115. uint16 test= (1<<(lang-1));
  116. if(!lang) return 1; //default english
  117. else if(lang < 0 || test > LANG_MAX) return -1; //false range
  118. else if (LANG_ENABLE&test) return 1;
  119. else if(display) {
  120. ShowDebug("Unsupported langtype '%d'.\n",lang);
  121. }
  122. return -2;
  123. }