hgestrings.cpp
上传用户:jnfxsk
上传日期:2022-06-16
资源大小:3675k
文件大小:3k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.7
  3. ** Copyright (C) 2003-2007, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** hgeStringTable helper class implementation
  7. */
  8. #include "....includehgestrings.h"
  9. #include <ctype.h>
  10. const char STRHEADERTAG[]="[HGESTRINGTABLE]";
  11. const char STRFORMATERROR[]="String table %s has incorrect format.";
  12. HGE *hgeStringTable::hge=0;
  13. hgeStringTable::hgeStringTable(const char *filename)
  14. {
  15. int i;
  16. void *data;
  17. DWORD size;
  18. char *desc, *pdesc;
  19. NamedString *str;
  20. char str_name[MAXSTRNAMELENGTH];
  21. char *str_value, *pvalue;
  22. hge=hgeCreate(HGE_VERSION);
  23. strings=0;
  24. // load string table file
  25. data=hge->Resource_Load(filename, &size);
  26. if(!data) return;
  27. desc = new char[size+1];
  28. memcpy(desc,data,size);
  29. desc[size]=0;
  30. hge->Resource_Free(data);
  31. // check header
  32. if(memcmp(desc, STRHEADERTAG, sizeof(STRHEADERTAG)-1))
  33. {
  34. hge->System_Log(STRFORMATERROR, filename);
  35. delete[] desc;
  36. return;
  37. }
  38. pdesc=desc+sizeof(STRHEADERTAG);
  39. str_value=new char[8192];
  40. for(;;)
  41. {
  42. // skip whitespaces
  43. while(isspace(*pdesc)) pdesc++;
  44. if(!*pdesc) break;
  45. // skip comments
  46. if(*pdesc==';')
  47. {
  48. while(*pdesc && *pdesc != 'n') pdesc++;
  49. pdesc++;
  50. continue;
  51. }
  52. // get string name -> str_name
  53. i=0;
  54. while(pdesc[i] && pdesc[i]!='=' && !isspace(pdesc[i]) && i<MAXSTRNAMELENGTH)
  55. {
  56. str_name[i]=pdesc[i];
  57. i++;
  58. }
  59. str_name[i]=0;
  60. pdesc+=i;
  61. // skip string name overflow characters
  62. while(*pdesc && *pdesc!='=' && !isspace(*pdesc)) pdesc++;
  63. if(!*pdesc) break;
  64. // skip whitespaces to '='
  65. while(isspace(*pdesc)) pdesc++;
  66. if(*pdesc!='=') { hge->System_Log(STRFORMATERROR, filename); break; }
  67. pdesc++;
  68. // skip whitespaces to '"'
  69. while(isspace(*pdesc)) pdesc++;
  70. if(*pdesc!='"') { hge->System_Log(STRFORMATERROR, filename); break; }
  71. pdesc++;
  72. // parse string value till the closing '"' -> str_value
  73. // consider: ", n, \, LF, CR, whitespaces at line begin/end
  74. pvalue=str_value;
  75. while(*pdesc && *pdesc!='"')
  76. {
  77. if(*pdesc=='n' || *pdesc=='r')
  78. {
  79. while(isspace(*pdesc)) pdesc++;
  80. pvalue--;
  81. while(pvalue>=str_value && isspace(*pvalue)) pvalue--;
  82. pvalue++; *pvalue=' '; pvalue++;
  83. continue;
  84. }
  85. if(*pdesc=='\')
  86. {
  87. pdesc++;
  88. if(!*pdesc) continue;
  89. if(*pdesc=='n') *pvalue='n';
  90. else *pvalue=*pdesc;
  91. pvalue++;
  92. pdesc++;
  93. continue;
  94. }
  95. *pvalue=*pdesc; pvalue++;
  96. pdesc++;
  97. }
  98. *pvalue=0;
  99. // add the parsed string to the list
  100. str=new NamedString;
  101. strcpy(str->name, str_name);
  102. str->string=new char[strlen(str_value)+1];
  103. strcpy(str->string, str_value);
  104. str->next=strings;
  105. strings=str;
  106. if(!*pdesc) break;
  107. pdesc++;
  108. }
  109. delete[] str_value;
  110. delete[] desc;
  111. }
  112. hgeStringTable::~hgeStringTable()
  113. {
  114. NamedString *str, *strnext;
  115. str=strings;
  116. while(str)
  117. {
  118. strnext=str->next;
  119. delete[] str->string;
  120. delete str;
  121. str=strnext;
  122. }
  123. hge->Release();
  124. }
  125. char *hgeStringTable::GetString(const char *name)
  126. {
  127. NamedString *str=strings;
  128. while(str)
  129. {
  130. if(!strcmp(name, str->name)) return str->string;
  131. str=str->next;
  132. }
  133. return 0;
  134. }