Skip to content

Commit 5c23a92

Browse files
committed
Implemented SourceFile.writelines()
Reorganized filesystem.cpp
1 parent 2eb41f2 commit 5c23a92

File tree

3 files changed

+98
-52
lines changed

3 files changed

+98
-52
lines changed

src/core/modules/filesystem/filesystem.cpp

Lines changed: 92 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool IsVPKFile(const char* file_path)
4949

5050

5151
//---------------------------------------------------------------------------------
52-
// SourceFile
52+
// SourceFile - constructor/destructor
5353
//---------------------------------------------------------------------------------
5454
SourceFile::SourceFile(FileHandle_t handle)
5555
{
@@ -75,26 +75,23 @@ SourceFile::~SourceFile()
7575
free(m_mode);
7676
}
7777

78-
void SourceFile::Save(const char* file_path)
78+
SourceFile* SourceFile::Open(const char* pFileName, const char* pMode, const char* pathID)
7979
{
80-
CheckClosed();
81-
FileHandle_t handle = filesystem->Open(file_path, "wb");
80+
FileHandle_t handle = filesystem->Open(pFileName, pMode, pathID);
8281
if (handle == FILESYSTEM_INVALID_HANDLE)
83-
BOOST_RAISE_EXCEPTION(PyExc_IOError, "Failed to open file: %s", file_path)
84-
85-
int size = Size();
86-
void* buffer = new char[size+1];
87-
int bytesRead = filesystem->Read(buffer, size, m_handle);
88-
89-
filesystem->Write(buffer, bytesRead, handle);
90-
filesystem->Close(handle);
82+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to open file: %s", pFileName)
9183

92-
delete buffer;
84+
return new SourceFile(handle, pMode);
9385
}
9486

87+
88+
//---------------------------------------------------------------------------------
89+
// SourceFile - reading
90+
//---------------------------------------------------------------------------------
9591
PyObject* SourceFile::Read(int size)
9692
{
9793
CheckClosed();
94+
CheckReadable();
9895
if (size == -1) {
9996
size = filesystem->Size(m_handle);
10097
}
@@ -107,6 +104,7 @@ PyObject* SourceFile::Read(int size)
107104
object SourceFile::Readline(int size)
108105
{
109106
CheckClosed();
107+
CheckReadable();
110108
// TODO
111109
BOOST_RAISE_EXCEPTION(PyExc_NotImplementedError, "Not implemented yet.")
112110
return object();
@@ -115,11 +113,16 @@ object SourceFile::Readline(int size)
115113
list SourceFile::Readlines(int hint)
116114
{
117115
CheckClosed();
116+
CheckReadable();
117+
118+
list result;
119+
118120
// TODO
119-
BOOST_RAISE_EXCEPTION(PyExc_NotImplementedError, "Not implemented yet.")
120-
return list();
121+
122+
return result;
121123
}
122124

125+
// internal
123126
PyObject* SourceFile::ConsumeBuffer(void* buffer, int bytesRead)
124127
{
125128
PyObject* result = NULL;
@@ -134,10 +137,32 @@ PyObject* SourceFile::ConsumeBuffer(void* buffer, int bytesRead)
134137
return result;
135138
}
136139

137-
int SourceFile::Write(PyObject* data)
140+
141+
//---------------------------------------------------------------------------------
142+
// SourceFile - writing
143+
//---------------------------------------------------------------------------------
144+
void SourceFile::Write(PyObject* data)
145+
{
146+
CheckClosed();
147+
CheckWriteable();
148+
149+
WriteData(data);
150+
}
151+
152+
void SourceFile::Writelines(list lines)
138153
{
139154
CheckClosed();
155+
CheckWriteable();
156+
157+
for (int i=0; i < len(lines); ++i) {
158+
object data = lines[i];
159+
WriteData(data.ptr());
160+
}
161+
}
140162

163+
// internal
164+
void SourceFile::WriteData(PyObject* data)
165+
{
141166
char* input = NULL;
142167
int size = 0;
143168

@@ -158,33 +183,38 @@ int SourceFile::Write(PyObject* data)
158183
input = (char *) PyUnicode_DATA(data);
159184
}
160185

161-
return filesystem->Write((void *) input, size, m_handle);
186+
filesystem->Write((void *) input, size, m_handle);
162187
}
163188

164-
void SourceFile::Writelines(list lines)
189+
int SourceFile::Truncate(int size)
165190
{
166191
CheckClosed();
167-
for (int i=0; i < len(lines); ++i) {
168-
Writeline(lines[i]);
169-
}
170-
}
171-
172-
bool SourceFile::IsBinaryMode()
173-
{
174-
return !m_mode || strchr(m_mode, 'b');
175-
}
192+
CheckWriteable();
176193

177-
void SourceFile::Writeline(object line)
178-
{
179194
// TODO
180195
BOOST_RAISE_EXCEPTION(PyExc_NotImplementedError, "Not implemented yet.")
196+
return 0;
181197
}
182198

183-
int SourceFile::Truncate(int size)
199+
200+
//---------------------------------------------------------------------------------
201+
// SourceFile - misc
202+
//---------------------------------------------------------------------------------
203+
void SourceFile::Save(const char* file_path)
184204
{
185-
// TODO
186-
BOOST_RAISE_EXCEPTION(PyExc_NotImplementedError, "Not implemented yet.")
187-
return 0;
205+
CheckClosed();
206+
FileHandle_t handle = filesystem->Open(file_path, "wb");
207+
if (handle == FILESYSTEM_INVALID_HANDLE)
208+
BOOST_RAISE_EXCEPTION(PyExc_IOError, "Failed to open file: %s", file_path)
209+
210+
int size = Size();
211+
void* buffer = new char[size+1];
212+
int bytesRead = filesystem->Read(buffer, size, m_handle);
213+
214+
filesystem->Write(buffer, bytesRead, handle);
215+
filesystem->Close(handle);
216+
217+
delete buffer;
188218
}
189219

190220
void SourceFile::Close()
@@ -248,6 +278,35 @@ bool SourceFile::Closed()
248278
return m_handle == NULL;
249279
}
250280

281+
282+
//---------------------------------------------------------------------------------
283+
// SourceFile - internal
284+
//---------------------------------------------------------------------------------
285+
void SourceFile::CheckReadable()
286+
{
287+
if (!Readable()) {
288+
BOOST_RAISE_EXCEPTION(PyExc_IOError, "not readable")
289+
}
290+
}
291+
292+
void SourceFile::CheckWriteable()
293+
{
294+
if (!Writeable()) {
295+
BOOST_RAISE_EXCEPTION(PyExc_IOError, "not writeable")
296+
}
297+
}
298+
299+
void SourceFile::CheckClosed()
300+
{
301+
if (m_handle == FILESYSTEM_INVALID_HANDLE)
302+
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "File is already closed.")
303+
}
304+
305+
bool SourceFile::IsBinaryMode()
306+
{
307+
return !m_mode || strchr(m_mode, 'b');
308+
}
309+
251310
bool SourceFile::Readable()
252311
{
253312
return m_mode != NULL && (
@@ -265,18 +324,3 @@ bool SourceFile::Writeable()
265324
|| strchr(m_mode, '+')
266325
);
267326
}
268-
269-
void SourceFile::CheckClosed()
270-
{
271-
if (m_handle == FILESYSTEM_INVALID_HANDLE)
272-
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "File is already closed.")
273-
}
274-
275-
SourceFile* SourceFile::Open(const char* pFileName, const char* pMode, const char* pathID)
276-
{
277-
FileHandle_t handle = filesystem->Open(pFileName, pMode, pathID);
278-
if (handle == FILESYSTEM_INVALID_HANDLE)
279-
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to open file: %s", pFileName)
280-
281-
return new SourceFile(handle, pMode);
282-
}

src/core/modules/filesystem/filesystem.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SourceFile
4545

4646
// File-like methods
4747
PyObject* Read(int size=-1);
48-
int Write(PyObject* data);
48+
void Write(PyObject* data);
4949
void Close();
5050
void Seek(int pos, int seekType=FILESYSTEM_SEEK_HEAD);
5151
unsigned int Tell();
@@ -79,8 +79,10 @@ class SourceFile
7979
private:
8080
PyObject* ConsumeBuffer(void* buffer, int bytesRead);
8181
void CheckClosed();
82-
void Writeline(object data);
82+
void WriteData(PyObject* data);
8383
bool IsBinaryMode();
84+
void CheckReadable();
85+
void CheckWriteable();
8486

8587
private:
8688
FileHandle_t m_handle;

src/core/modules/filesystem/filesystem_wrap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ void export_source_file(scope _filesystem)
9898

9999
_SourceFile.def(
100100
"writelines",
101-
&SourceFile::Truncate
101+
&SourceFile::Writelines
102102
);
103103

104104
_SourceFile.def(
105105
"truncate",
106-
&SourceFile::Writelines,
106+
&SourceFile::Truncate,
107107
(arg("size")=-1)
108108
);
109109

0 commit comments

Comments
 (0)