1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "System/FileStream.h"
25 #include "Interface.h"
27 FileStream::FileStream(void)
35 FileStream::~FileStream(void)
37 if (autoFree
&& str
) {
39 core
->FileStreamPtrCount
--;
46 bool FileStream::Open(const char* fname
, bool aF
)
48 if (str
&& autoFree
) {
50 core
->FileStreamPtrCount
--;
56 str
= _fopen( fname
, "rb" );
61 core
->FileStreamPtrCount
++;
66 //FIXME: this is a very lame way to tell the file length
67 _fseek( str
, 0, SEEK_END
);
69 _fseek( str
, 0, SEEK_SET
);
70 ExtractFileFromPath( filename
, fname
);
71 strncpy( originalfile
, fname
, _MAX_PATH
);
76 bool FileStream::Modify(const char* fname
, bool aF
)
78 if (str
&& autoFree
) {
80 core
->FileStreamPtrCount
--;
85 str
= _fopen( fname
, "r+b" );
90 core
->FileStreamPtrCount
++;
95 //FIXME: this is a very lame way to tell the file length
96 _fseek( str
, 0, SEEK_END
);
98 _fseek( str
, 0, SEEK_SET
);
99 ExtractFileFromPath( filename
, fname
);
100 strncpy( originalfile
, fname
, _MAX_PATH
);
105 bool FileStream::Open(_FILE
* stream
, int spos
, int maxsize
, bool aF
)
107 if (str
&& autoFree
) {
109 core
->FileStreamPtrCount
--;
119 core
->FileStreamPtrCount
++;
127 _fseek( str
, spos
, SEEK_SET
);
132 //Creating file in the cache
133 //Create is ALWAYS autofree
134 bool FileStream::Create(const char* fname
, SClass_ID ClassID
)
136 return Create(core
->CachePath
, fname
, ClassID
);
139 //Creating file outside of the cache
140 bool FileStream::Create(const char *folder
, const char* fname
, SClass_ID ClassID
)
142 if (str
&& autoFree
) {
144 core
->FileStreamPtrCount
--;
149 ExtractFileFromPath( filename
, fname
);
150 strcpy( originalfile
, folder
);
151 strcat( originalfile
, SPathDelimiter
);
152 strcat( originalfile
, filename
);
153 strcat( originalfile
, core
->TypeExt( ClassID
) );
154 str
= _fopen( originalfile
, "wb" );
166 int FileStream::Read(void* dest
, unsigned int length
)
171 //we don't allow partial reads anyway, so it isn't a problem that
172 //i don't adjust length here (partial reads are evil)
173 if (Pos
+length
>size
) {
176 size_t c
= _fread( dest
, 1, length
, str
);
181 ReadDecrypted( dest
, c
);
187 int FileStream::Write(const void* src
, unsigned int length
)
192 // do encryption here if needed
194 size_t c
= _fwrite( src
, 1, length
, str
);
205 int FileStream::Seek(int newpos
, int type
)
207 if (!opened
&& !created
) {
212 _fseek( str
, startpos
+ size
- newpos
, SEEK_SET
);
215 case GEM_CURRENT_POS
:
216 _fseek( str
, newpos
, SEEK_CUR
);
220 case GEM_STREAM_START
:
221 _fseek( str
, startpos
+ newpos
, SEEK_SET
);
229 printf("[Streams]: Invalid seek position: %ld (limit: %ld)\n",Pos
, size
);
235 /** No descriptions */
236 int FileStream::ReadLine(void* buf
, unsigned int maxlen
)
241 unsigned char * p
= ( unsigned char * ) buf
;
251 while (i
< ( maxlen
- 1 )) {
252 int ch
= _fgetc( str
);
256 ch
^= GEM_ENCRYPTION_KEY
[Pos
& 63];
259 if (( ( char ) ch
) == '\n')
261 if (( ( char ) ch
) == '\t')
263 if (( ( char ) ch
) != '\r')
265 //Warning:this feof implementation reads forward one byte
273 unsigned long FileStream::GetStartPos() const