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

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.6
  3. ** Copyright (C) 2003-2006, 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. // get string name -> str_name
  46. i=0;
  47. while(pdesc[i] && pdesc[i]!='=' && !isspace(pdesc[i]) && i<MAXSTRNAMELENGTH)
  48. {
  49. str_name[i]=pdesc[i];
  50. i++;
  51. }
  52. str_name[i]=0;
  53. pdesc+=i;
  54. // skip string name overflow characters
  55. while(*pdesc && *pdesc!='=' && !isspace(*pdesc)) pdesc++;
  56. if(!*pdesc) break;
  57. // skip whitespaces to '='
  58. while(isspace(*pdesc)) pdesc++;
  59. if(*pdesc!='=') { hge->System_Log(STRFORMATERROR, filename); break; }
  60. pdesc++;
  61. // skip whitespaces to '"'
  62. while(isspace(*pdesc)) pdesc++;
  63. if(*pdesc!='"') { hge->System_Log(STRFORMATERROR, filename); break; }
  64. pdesc++;
  65. // parse string value till the closing '"' -> str_value
  66. // consider: ", n, \, LF, CR, whitespaces at line begin/end
  67. pvalue=str_value;
  68. while(*pdesc && *pdesc!='"')
  69. {
  70. if(*pdesc=='n' || *pdesc=='r')
  71. {
  72. while(isspace(*pdesc)) pdesc++;
  73. pvalue--;
  74. while(pvalue>=str_value && isspace(*pvalue)) pvalue--;
  75. pvalue++; *pvalue=' '; pvalue++;
  76. continue;
  77. }
  78. if(*pdesc=='\')
  79. {
  80. pdesc++;
  81. if(!*pdesc) continue;
  82. if(*pdesc=='n') *pvalue='n';
  83. else *pvalue=*pdesc;
  84. pvalue++;
  85. pdesc++;
  86. continue;
  87. }
  88. *pvalue=*pdesc; pvalue++;
  89. pdesc++;
  90. }
  91. *pvalue=0;
  92. // add the parsed string to the list
  93. str=new NamedString;
  94. strcpy(str->name, str_name);
  95. str->string=new char[strlen(str_value)+1];
  96. strcpy(str->string, str_value);
  97. str->next=strings;
  98. strings=str;
  99. if(!*pdesc) break;
  100. pdesc++;
  101. }
  102. delete[] str_value;
  103. delete[] desc;
  104. }
  105. hgeStringTable::~hgeStringTable()
  106. {
  107. NamedString *str, *strnext;
  108. str=strings;
  109. while(str)
  110. {
  111. strnext=str->next;
  112. delete[] str->string;
  113. delete str;
  114. str=strnext;
  115. }
  116. hge->Release();
  117. }
  118. char *hgeStringTable::GetString(const char *name)
  119. {
  120. NamedString *str=strings;
  121. while(str)
  122. {
  123. if(!strcmp(name, str->name)) return str->string;
  124. str=str->next;
  125. }
  126. return 0;
  127. }