summaryrefslogtreecommitdiff
path: root/src/JSystem/JUtility/JUTDirectFile.cpp
blob: 8a670475c549d3830348b463216c22c8b4ac22b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Generated By: dol2asm
// Translation Unit: JUTDirectFile
//

#include "JSystem/JUtility/JUTDirectFile.h"
#include <dolphin/os.h>
#include "global.h"
#include <stdint.h>

//
// Forward References:
//

//
// External References:
//

//
// Declarations:
//

/* 802E8730-802E87F8 2E3070 00C8+00 1/1 0/0 0/0 .text            fetch32byte__13JUTDirectFileFv */
int JUTDirectFile::fetch32byte() {
	mToRead = mLength - ALIGN_PREV(mPos, DVD_MIN_TRANSFER_SIZE);

	if (mToRead > JUTDF_BUFSIZE) {
		mToRead = JUTDF_BUFSIZE;
	}
	int interrupts = OSEnableInterrupts();
	int readRes    = DVDReadAsyncPrio(&mFileInfo, mSectorStart, ALIGN_NEXT(mToRead, DVD_MIN_TRANSFER_SIZE),
                               ALIGN_PREV(mPos, DVD_MIN_TRANSFER_SIZE), NULL, 2);
	OSRestoreInterrupts(interrupts);
	if (!readRes) {
		return -1;
	} else {
		interrupts = OSEnableInterrupts();
		while (DVDGetCommandBlockStatus(&mFileInfo.cb)) {
			;
		}
		OSRestoreInterrupts(interrupts);
		return mToRead;
	}
}

/* 802E87F8-802E881C 2E3138 0024+00 0/0 1/1 0/0 .text            __ct__13JUTDirectFileFv */
JUTDirectFile::JUTDirectFile() {
	mLength      = 0;
	mPos         = 0;
	mToRead      = 0;
	mSectorStart = (u8*)ALIGN_NEXT((uintptr_t)mBuffer, DVD_MIN_TRANSFER_SIZE);
	mIsOpen      = false;
}


/* 802E881C-802E8860 2E315C 0044+00 0/0 1/1 0/0 .text            __dt__13JUTDirectFileFv */
JUTDirectFile::~JUTDirectFile() {
    mIsOpen = false;
}

/* 802E8860-802E88FC 2E31A0 009C+00 0/0 1/1 0/0 .text            fopen__13JUTDirectFileFPCc */
bool JUTDirectFile::fopen(const char* filename) {
	if (!filename) {
		return false;
	}

	int interrupts = OSEnableInterrupts();
	int dvdRes     = DVDOpen(const_cast<char*>(filename), &mFileInfo);
	OSRestoreInterrupts(interrupts);

	if (!dvdRes) {
		mIsOpen = false;
		return false;
	}

	int interrupts2 = OSEnableInterrupts();
	mLength         = mFileInfo.length;
	OSRestoreInterrupts(interrupts2);

	mPos    = 0;
	mIsOpen = true;
	return true;
}

/* 802E88FC-802E8958 2E323C 005C+00 0/0 1/1 0/0 .text            fclose__13JUTDirectFileFv */
void JUTDirectFile::fclose() {
	if (mIsOpen) {
		int interrupts = OSEnableInterrupts();
		DVDClose(&mFileInfo);
		OSRestoreInterrupts(interrupts);
		mIsOpen = false;
	}
}

/* 802E8958-802E8B08 2E3298 01B0+00 0/0 1/1 0/0 .text            fgets__13JUTDirectFileFPvi */
int JUTDirectFile::fgets(void* buf, int len) {
	// if file isn't open, return error (-1).
	if (!mIsOpen) {
		return -1;
	}

	// if desired length to get is 0, get... 0 bytes.
	if (len == 0) {
		return 0;
	}

	// if desired length to get is 1, return 1.
	// (final byte gotten is always 0, so len 1 is pointless).
	if (len == 1) {
		return 1;
	}

	// if buffer to read into doesn't exist, return error.
	if (!buf) {
		return -1;
	}

	// if we're already beyond the file length, return error.
	if (mPos >= mLength) {
		return -1;
	}

	int readMax;
	u8* byteBuf   = (u8*)buf;
	int readCount = 0;
	readMax       = len - 1; // desired bytes of data to get (last value is then 0).

	while (mPos < mLength) {
		// if there's nothing left to read, return error.
		if (mToRead == 0 && fetch32byte() < 0) {
			return -1;
		}

		// read in each chunk.
		u32 currPos   = mPos & (JUTDF_BUFSIZE - 1);
		u32 chunkSize = (mToRead - currPos);
		if (readCount + chunkSize > readMax) {
			chunkSize = len - readCount - 1;
		}

		BOOL isAtEnd = FALSE;
		for (int i = 0; i < chunkSize; i++) {
			u8 byte = mSectorStart[currPos];
			currPos++;
			*byteBuf++ = byte;

			// if we hit the end of a line, stop reading.
			if (byte == '\n') {
				isAtEnd   = TRUE;
				chunkSize = i + 1;
				break;
			}
		}

		// if we exceed the buffer size, stop reading.
		if (currPos >= JUTDF_BUFSIZE) {
			mToRead = 0;
		}

		// if we hit the end of a line, set final byte to 0 and stop reading.
		if (isAtEnd == TRUE) {
			*byteBuf = 0;
			mPos += chunkSize;
			readCount += chunkSize;
			break;
		}

		// we should have read the full chunkSize, so update count/pos.
		mPos += chunkSize;
		readCount += chunkSize;

		// if we're at (or beyond) our desired length, set final byte to 0 and stop reading.
		if (readCount >= readMax) {
			*byteBuf = 0;
			break;
		}
	}

	// if got to the end of the data, set final byte to 0.
	if (mPos >= mLength) {
		*byteBuf = 0;
	}

	return readCount;
}