1
0

ResourceReader.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "ResourceReader.h"
  2. ResourceReader::ResourceReader() : data(NULL), g(NULL), ptr(0)
  3. {}
  4. int ResourceReader::open(const wchar_t *filename, int mode)
  5. {
  6. if (_wcsnicmp(filename, L"res://", 6)) return 0;
  7. filename += 6;
  8. wchar_t blah[MAX_PATH];
  9. lstrcpynW(blah, filename, MAX_PATH);
  10. wchar_t *p = blah;
  11. while (p && *p && *p != ',') p++;
  12. if (p && *p != ',') return 0;
  13. if (p) *p++ = 0;
  14. HINSTANCE hInst = (HINSTANCE)_wtoi64(blah);
  15. int id = _wtoi(p);
  16. HRSRC r = FindResource(hInst, MAKEINTRESOURCE(id), RT_RCDATA);
  17. if (r == NULL) return 0;
  18. g = LoadResource(hInst, r);
  19. if (g == NULL) return 0;
  20. data = (char*)LockResource(g);
  21. if (data == NULL)
  22. {
  23. FreeResource(g); // see win32 doc
  24. g = NULL;
  25. return 0;
  26. }
  27. size = SizeofResource(hInst, r);
  28. ptr = 0;
  29. return 1;
  30. }
  31. size_t ResourceReader::read(__int8 *buffer, size_t length)
  32. {
  33. size_t s = min(size - ptr, length);
  34. if (s)
  35. memcpy(buffer, data + ptr, s);
  36. ptr += s;
  37. return s;
  38. }
  39. size_t ResourceReader::write(const __int8 *buffer, size_t length)
  40. {
  41. return 0;
  42. }
  43. void ResourceReader::close()
  44. {
  45. if (g)
  46. {
  47. UnlockResource(g);
  48. FreeResource(g); // see win32 doc
  49. g = NULL;
  50. data = NULL;
  51. }
  52. }
  53. unsigned __int64 ResourceReader::getPos()
  54. {
  55. return ptr;
  56. }
  57. unsigned __int64 ResourceReader::getLength()
  58. {
  59. return size;
  60. }
  61. int ResourceReader::canSeek()
  62. {
  63. return 1;
  64. }
  65. int ResourceReader::seek(unsigned __int64 position)
  66. {
  67. ptr = (size_t)min(position, size);
  68. return 1;
  69. }
  70. int ResourceReader::exists(const wchar_t *filename)
  71. {
  72. return 1;
  73. } // always exists if open succeeded
  74. #define CBCLASS ResourceReader
  75. START_DISPATCH;
  76. // CB(ISMINE, isMine);
  77. CB(OPEN, open);
  78. CB(READ, read);
  79. CB(WRITE, write);
  80. VCB(CLOSE, close);
  81. // VCB(ABORT, abort);
  82. CB(GETLENGTH, getLength);
  83. CB(GETPOS, getPos);
  84. CB(CANSEEK, canSeek);
  85. // CB(SEEK, seek);
  86. // CB(HASHEADERS,hasHeaders);
  87. // CB(GETHEADER,getHeader);
  88. CB(EXISTS,exists);
  89. // CB(REMOVE,remove);
  90. // CB(REMOVEUNDOABLE,removeUndoable);
  91. // CB(MOVE,move);
  92. // CB(BYTESAVAILABLE,bytesAvailable);
  93. // VCB(SETMETADATACALLBACK,setMetaDataCallback);
  94. // CB(CANPREFETCH,canPrefetch);
  95. // CB(CANSETEOF, canSetEOF);
  96. // CB(SETEOF, setEOF);
  97. END_DISPATCH;
  98. #undef CBCLASS