TrinityCore
cascfile.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "cascfile.h"
19#include <CascLib.h>
20#include <cstdio>
21
22CASCFile::CASCFile(std::shared_ptr<CASC::Storage const> casc, const char* filename, bool warnNoExist /*= true*/) :
23 eof(false),
24 buffer(nullptr),
25 pointer(0),
26 size(0)
27{
28 std::unique_ptr<CASC::File> file(casc->OpenFile(filename, CASC_LOCALE_ALL_WOW, false));
29 if (!file)
30 {
31 if (warnNoExist || GetCascError() != ERROR_FILE_NOT_FOUND)
32 fprintf(stderr, "Can't open %s: %s\n", filename, CASC::HumanReadableCASCError(GetCascError()));
33 eof = true;
34 return;
35 }
36
37 init(file.get(), filename);
38}
39
40CASCFile::CASCFile(std::shared_ptr<CASC::Storage const> casc, uint32 fileDataId, std::string const& description, bool warnNoExist /*= true*/) :
41 eof(false),
42 buffer(nullptr),
43 pointer(0),
44 size(0)
45{
46 std::unique_ptr<CASC::File> file(casc->OpenFile(fileDataId, CASC_LOCALE_ALL_WOW, false));
47 if (!file)
48 {
49 if (warnNoExist || GetCascError() != ERROR_FILE_NOT_FOUND)
50 fprintf(stderr, "Can't open %s: %s\n", description.c_str(), CASC::HumanReadableCASCError(GetCascError()));
51 eof = true;
52 return;
53 }
54
55 init(file.get(), description.c_str());
56}
57
58void CASCFile::init(CASC::File* file, const char* description)
59{
60 int64 fileSize = file->GetSize();
61 if (fileSize == -1)
62 {
63 fprintf(stderr, "Can't open %s, failed to get size: %s!\n", description, CASC::HumanReadableCASCError(GetCascError()));
64 eof = true;
65 return;
66 }
67
68 size = fileSize;
69
70 uint32 read = 0;
71 buffer = new char[size];
72 if (!file->ReadFile(buffer, size, &read) || size != read)
73 {
74 fprintf(stderr, "Can't read %s, size=%u read=%u: %s\n", description, uint32(size), uint32(read), CASC::HumanReadableCASCError(GetCascError()));
75 eof = true;
76 return;
77 }
78}
79
80size_t CASCFile::read(void* dest, size_t bytes)
81{
82 if (eof)
83 return 0;
84
85 size_t rpos = pointer + bytes;
86 if (rpos > size)
87 {
88 bytes = size - pointer;
89 eof = true;
90 }
91
92 memcpy(dest, &(buffer[pointer]), bytes);
93
94 pointer = rpos;
95
96 return bytes;
97}
98
99void CASCFile::seek(int offset)
100{
101 pointer = offset;
102 eof = (pointer >= size);
103}
104
106{
107 pointer += offset;
108 eof = (pointer >= size);
109}
110
112{
113 delete[] buffer;
114 buffer = nullptr;
115 eof = true;
116}
int64_t int64
Definition: Define.h:137
uint32_t uint32
Definition: Define.h:142
char * buffer
Definition: cascfile.h:35
bool eof
Definition: cascfile.h:34
void seekRelative(int offset)
Definition: cascfile.cpp:105
CASCFile(const CASCFile &f)=delete
void seek(int offset)
Definition: cascfile.cpp:99
size_t size
Definition: cascfile.h:36
size_t read(void *dest, size_t bytes)
Definition: cascfile.cpp:80
size_t pointer
Definition: cascfile.h:36
void init(CASC::File *file, const char *description)
Definition: cascfile.cpp:58
void close()
Definition: cascfile.cpp:111
int64 GetSize() const
bool ReadFile(void *buffer, uint32 bytes, uint32 *bytesRead)
char const * HumanReadableCASCError(uint32 error)
Definition: CascHandles.cpp:30
constexpr std::size_t size()
Definition: UpdateField.h:796