rcfile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : wwlib *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/rcfile.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 7/09/99 1:37p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "rcfile.h"
  36. #include <stdlib.h>
  37. const char * RESOURCE_FILE_TYPE_NAME = "File";
  38. ResourceFileClass::ResourceFileClass(HMODULE hmodule, char const *filename) :
  39. ResourceName(NULL),
  40. hModule(NULL),
  41. FileBytes(NULL),
  42. FilePtr(NULL),
  43. EndOfFile(NULL)
  44. {
  45. Set_Name(filename);
  46. HRSRC hresource = FindResource(hmodule,ResourceName,RESOURCE_FILE_TYPE_NAME);
  47. if (hresource) {
  48. HGLOBAL hglob = LoadResource(hmodule,hresource);
  49. if (hglob) {
  50. FileBytes = (unsigned char *)LockResource(hglob);
  51. if (FileBytes) {
  52. FilePtr = FileBytes;
  53. EndOfFile = FileBytes + SizeofResource(hmodule,hresource);
  54. }
  55. }
  56. }
  57. }
  58. ResourceFileClass::~ResourceFileClass(void)
  59. {
  60. if (ResourceName)
  61. free(ResourceName);
  62. }
  63. char const * ResourceFileClass::Set_Name(char const *filename)
  64. {
  65. if (ResourceName) {
  66. free(ResourceName);
  67. ResourceName = NULL;
  68. }
  69. if (filename) {
  70. ResourceName = strdup(filename);
  71. }
  72. return ResourceName;
  73. }
  74. int ResourceFileClass::Read(void *buffer, int size)
  75. {
  76. if (!FilePtr) return 0;
  77. if (FilePtr + size > EndOfFile) {
  78. size = EndOfFile - FilePtr;
  79. }
  80. memcpy(buffer,FilePtr,size);
  81. FilePtr += size;
  82. return size;
  83. }
  84. int ResourceFileClass::Seek(int pos, int dir)
  85. {
  86. switch (dir) {
  87. case SEEK_SET:
  88. FilePtr = FileBytes + pos;
  89. break;
  90. case SEEK_CUR:
  91. FilePtr = FilePtr + pos;
  92. break;
  93. case SEEK_END:
  94. FilePtr = EndOfFile + pos;
  95. break;
  96. }
  97. if (FilePtr > EndOfFile) {
  98. FilePtr = EndOfFile;
  99. }
  100. if (FilePtr < FileBytes) {
  101. FilePtr = FileBytes;
  102. }
  103. return FilePtr - FileBytes;
  104. }
  105. int ResourceFileClass::Size(void)
  106. {
  107. return EndOfFile - FileBytes;
  108. }
  109. void ResourceFileClass::Error(int /*error*/, int /*canretry*/, char const * /*filename*/)
  110. {
  111. }