QWPack.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include "QWPack.h"
  16. #include <QStringList>
  17. #define MAX_FILES_IN_PACK 2048
  18. //on disk
  19. typedef struct
  20. {
  21. char name[56];
  22. int filePos, fileLen;
  23. } dpackFile_t;
  24. typedef struct
  25. {
  26. char id[4];
  27. int dirofs;
  28. int dirlen;
  29. } dpackHeader_t;
  30. QWPack::QWPack()
  31. {
  32. }
  33. bool QWPack::load(const QString& filename)
  34. {
  35. QFile packFile(filename);
  36. if(!packFile.open(QIODevice::ReadOnly))
  37. return false;
  38. myFilename = filename;
  39. dpackHeader_t header;
  40. packFile.read((char*)&header, sizeof(dpackHeader_t));
  41. if(
  42. header.id[0] != 'P' ||
  43. header.id[1] != 'A' ||
  44. header.id[2] != 'C' ||
  45. header.id[3] != 'K'
  46. )
  47. {
  48. packFile.close();
  49. return false;
  50. }
  51. int fileCount;
  52. fileCount = header.dirlen / sizeof(dpackFile_t);
  53. if(fileCount > MAX_FILES_IN_PACK)
  54. {
  55. packFile.close();
  56. return false;
  57. }
  58. packFile.seek(header.dirofs);
  59. dpackFile_t *packData = new dpackFile_t[fileCount];
  60. packFile.read((char*)packData, header.dirlen);
  61. for(int i = 0; i < fileCount; ++i)
  62. {
  63. PackedFile file;
  64. file.name = packData[i].name;
  65. file.filePos = packData[i].filePos;
  66. file.fileLen = packData[i].fileLen;
  67. myFiles.push_back(file);
  68. }
  69. delete [] packData;
  70. packFile.close();
  71. return true;
  72. }
  73. bool QWPack::exists(const QString &filename) const
  74. {
  75. QList<PackedFile>::const_iterator itr = myFiles.constBegin();
  76. while(itr != myFiles.constEnd())
  77. {
  78. const PackedFile* p = &(*itr);
  79. if(p->name == filename)
  80. return true;
  81. itr++;
  82. }
  83. return false;
  84. }
  85. QStringList QWPack::files() const
  86. {
  87. QStringList list;
  88. QList<PackedFile>::const_iterator itr = myFiles.constBegin();
  89. while(itr != myFiles.constEnd())
  90. {
  91. const PackedFile* p = &(*itr);
  92. list.push_back(p->name);
  93. itr++;
  94. }
  95. return list;
  96. }
  97. bool QWPack::read(const QString &filename, char **fileData, quint64 *len) const
  98. {
  99. QList<PackedFile>::const_iterator itr = myFiles.constBegin();
  100. while(itr != myFiles.constEnd())
  101. {
  102. const PackedFile* p = &(*itr);
  103. if(p->name == filename)
  104. {
  105. QFile packFile(myFilename);
  106. if(!packFile.open(QIODevice::ReadOnly))
  107. return false;
  108. packFile.seek(p->filePos);
  109. *fileData = new char[p->fileLen];
  110. packFile.read(*fileData, p->fileLen);
  111. *len = p->fileLen;
  112. return true;
  113. }
  114. itr++;
  115. }
  116. return false;
  117. }
  118. const QString& QWPack::fileName() const
  119. {
  120. return myFilename;
  121. }