Isle
Loading...
Searching...
No Matches
legostorage.cpp
Go to the documentation of this file.
1#include "legostorage.h"
2
3#include "decomp.h"
4
5#include <memory.h>
6#include <string.h>
7
11
12// FUNCTION: LEGO1 0x10099080
14{
15 m_buffer = (LegoU8*) p_buffer;
16 m_position = 0;
17}
18
19// FUNCTION: LEGO1 0x10099160
20LegoResult LegoMemory::Read(void* p_buffer, LegoU32 p_size)
21{
22 memcpy(p_buffer, m_buffer + m_position, p_size);
23 m_position += p_size;
24 return SUCCESS;
25}
26
27// FUNCTION: LEGO1 0x10099190
28LegoResult LegoMemory::Write(const void* p_buffer, LegoU32 p_size)
29{
30 memcpy(m_buffer + m_position, p_buffer, p_size);
31 m_position += p_size;
32 return SUCCESS;
33}
34
35// FUNCTION: LEGO1 0x100991c0
37{
38 m_file = NULL;
39}
40
41// FUNCTION: LEGO1 0x10099250
43{
44 if (m_file) {
45 fclose(m_file);
46 }
47}
48
49// FUNCTION: LEGO1 0x100992c0
50LegoResult LegoFile::Read(void* p_buffer, LegoU32 p_size)
51{
52 if (!m_file) {
53 return FAILURE;
54 }
55 if (fread(p_buffer, 1, p_size, m_file) != p_size) {
56 return FAILURE;
57 }
58 return SUCCESS;
59}
60
61// FUNCTION: LEGO1 0x10099300
62LegoResult LegoFile::Write(const void* p_buffer, LegoU32 p_size)
63{
64 if (!m_file) {
65 return FAILURE;
66 }
67 if (fwrite(p_buffer, 1, p_size, m_file) != p_size) {
68 return FAILURE;
69 }
70 return SUCCESS;
71}
72
73// FUNCTION: LEGO1 0x10099340
75{
76 if (!m_file) {
77 return FAILURE;
78 }
79 LegoU32 position = ftell(m_file);
80 if (position == -1) {
81 return FAILURE;
82 }
83 p_position = position;
84 return SUCCESS;
85}
86
87// FUNCTION: LEGO1 0x10099370
89{
90 if (!m_file) {
91 return FAILURE;
92 }
93 if (fseek(m_file, p_position, SEEK_SET) != 0) {
94 return FAILURE;
95 }
96 return SUCCESS;
97}
98
99// FUNCTION: LEGO1 0x100993a0
100LegoResult LegoFile::Open(const char* p_name, LegoU32 p_mode)
101{
102 if (m_file) {
103 fclose(m_file);
104 }
105 char mode[4];
106 mode[0] = '\0';
107 if (p_mode & c_read) {
108 m_mode = c_read;
109 strcat(mode, "r");
110 }
111 if (p_mode & c_write) {
112 if (m_mode != c_read) {
113 m_mode = c_write;
114 }
115 strcat(mode, "w");
116 }
117 if ((p_mode & c_text) != 0) {
118 strcat(mode, "t");
119 }
120 else {
121 strcat(mode, "b");
122 }
123
124 if (!(m_file = fopen(p_name, mode))) {
125 return FAILURE;
126 }
127 return SUCCESS;
128}
Implementation of LegoStorage for file-backed storage using stdio FILE*.
Definition: legostorage.h:387
LegoResult Read(void *p_buffer, LegoU32 p_size) override
Reads bytes from file at current position.
Definition: legostorage.cpp:50
FILE * m_file
C runtime file pointer backing storage.
Definition: legostorage.h:445
~LegoFile() override
Destructor.
Definition: legostorage.cpp:42
LegoFile()
Default constructor initializes with NULL file pointer.
Definition: legostorage.cpp:36
LegoResult GetPosition(LegoU32 &p_position) override
Gets the current file pointer position relative to beginning.
Definition: legostorage.cpp:74
LegoResult Open(const char *p_name, LegoU32 p_mode)
Opens a file with given name and mode, closing existing file if needed.
LegoResult Write(const void *p_buffer, LegoU32 p_size) override
Writes bytes to file at current position.
Definition: legostorage.cpp:62
LegoResult SetPosition(LegoU32 p_position) override
Sets the current file pointer position.
Definition: legostorage.cpp:88
Implementation of LegoStorage for memory-backed buffers.
Definition: legostorage.h:317
LegoU8 * m_buffer
Pointer to target memory buffer for reading/writing.
Definition: legostorage.h:373
LegoResult Read(void *p_buffer, LegoU32 p_size) override
Reads bytes from memory buffer at current position.
Definition: legostorage.cpp:20
LegoU32 m_position
Current read/write offset in buffer.
Definition: legostorage.h:378
LegoMemory(void *p_buffer)
Constructor setting internal buffer pointer and resets position.
Definition: legostorage.cpp:13
LegoResult Write(const void *p_buffer, LegoU32 p_size) override
Writes bytes to memory buffer at current position.
Definition: legostorage.cpp:28
Abstract base class providing an interface for file-like storage with binary and text read/write oper...
Definition: legostorage.h:16
LegoU8 m_mode
File open/access mode.
Definition: legostorage.h:308
@ c_read
Open for read operations. [AI].
Definition: legostorage.h:23
@ c_write
Open for write operations. [AI].
Definition: legostorage.h:24
@ c_text
Open in text mode, otherwise binary. [AI].
Definition: legostorage.h:25
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
unsigned long LegoU32
[AI] Unsigned 32-bit integer type for cross-platform compatibility.
Definition: legotypes.h:71
#define FAILURE
[AI] Used to indicate a failed operation in result codes.
Definition: legotypes.h:34
unsigned char LegoU8
[AI] Unsigned 8-bit integer type used throughout LEGO Island.
Definition: legotypes.h:47
LegoS32 LegoResult
[AI] Function result type (return code): typically SUCCESS (0) or FAILURE (-1).
Definition: legotypes.h:101
#define SUCCESS
[AI] Used to indicate a successful operation in result codes.
Definition: legotypes.h:30