Change ReadResFile/WriteResFile interface to take size_t args; removes 64KB size limitation on key-value files; bug #1112

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3558 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2011-02-15 12:04:28 +00:00
parent 19e4587288
commit 6ff079b8e8
9 changed files with 50 additions and 66 deletions
+3 -3
View File
@@ -48,14 +48,14 @@ typedef void *(ResourceLoadFileFun) (uio_Stream *fp, DWORD len);
void *LoadResourceFromPath(const char *pathname, ResourceLoadFileFun fn);
uio_Stream *res_OpenResFile (uio_DirHandle *dir, const char *filename, const char *mode);
int ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp);
int WriteResFile (const void *lpBuf, COUNT size, COUNT count, uio_Stream *fp);
size_t ReadResFile (void *lpBuf, size_t size, size_t count, uio_Stream *fp);
size_t WriteResFile (const void *lpBuf, size_t size, size_t count, uio_Stream *fp);
int GetResFileChar (uio_Stream *fp);
int PutResFileChar (char ch, uio_Stream *fp);
int PutResFileNewline (uio_Stream *fp);
long SeekResFile (uio_Stream *fp, long offset, int whence);
long TellResFile (uio_Stream *fp);
long LengthResFile (uio_Stream *fp);
size_t LengthResFile (uio_Stream *fp);
BOOLEAN res_CloseResFile (uio_Stream *fp);
BOOLEAN DeleteResFile (uio_DirHandle *dir, const char *filename);
+5 -5
View File
@@ -60,8 +60,8 @@ DeleteResFile (uio_DirHandle *dir, const char *filename)
return (uio_unlink (dir, filename) == 0);
}
int
ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp)
size_t
ReadResFile (void *lpBuf, size_t size, size_t count, uio_Stream *fp)
{
int retval;
@@ -70,8 +70,8 @@ ReadResFile (void *lpBuf, COUNT size, COUNT count, uio_Stream *fp)
return (retval);
}
int
WriteResFile (const void *lpBuf, COUNT size, COUNT count, uio_Stream *fp)
size_t
WriteResFile (const void *lpBuf, size_t size, size_t count, uio_Stream *fp)
{
int retval;
@@ -131,7 +131,7 @@ TellResFile (uio_Stream *fp)
return (retval);
}
long
size_t
LengthResFile (uio_Stream *fp)
{
struct stat sb;
+1 -1
View File
@@ -43,7 +43,7 @@ void *
LoadResourceFromPath (const char *path, ResourceLoadFileFun *loadFun)
{
uio_Stream *stream;
long dataLen;
unsigned long dataLen;
void *resdata;
stream = res_OpenResFile (contentDir, path, "rb");
+20 -38
View File
@@ -17,56 +17,38 @@
*/
#include "resintrn.h"
#include "libs/declib.h"
#include "libs/memlib.h"
#include "libs/log.h"
void *
GetResourceData (uio_Stream *fp, DWORD length)
{
BYTE *RDPtr;
void *result;
DECODE_REF fh = 0;
DWORD compLen;
if (length == ~(DWORD)0)
length = LengthResFile (fp);
else if ((fh = copen (fp, FILE_STREAM, STREAM_READ)))
cfilelength (fh, &length);
else
length -= sizeof (DWORD);
// Resource data used to be prefixed by its length in package files.
// A valid length prefix indicated compressed data, and
// a length prefix ~0 meant uncompressed.
// Currently, .ct and .xlt files still carry a ~0 length prefix.
if (ReadResFile (&compLen, sizeof (compLen), 1, fp) != 1)
return NULL;
if (compLen != ~(DWORD)0)
{
log_add (log_Warning, "LZ-compressed binary data not supported");
return NULL;
}
length -= sizeof (DWORD);
result = AllocResourceData (length);
RDPtr = result;
if (RDPtr)
if (!result)
return NULL;
if (ReadResFile (result, 1, length, fp) != length)
{
COUNT num_read;
do
{
#define READ_LENGTH 0x00007FFFL
num_read = length >= READ_LENGTH ?
(COUNT)READ_LENGTH : (COUNT)length;
if (fh)
{
if (cread (RDPtr, 1, num_read, fh) != num_read)
break;
}
else
{
if ((int)(ReadResFile (RDPtr, 1, num_read, fp)) != (int)num_read)
break;
}
RDPtr += num_read;
} while (length -= num_read);
if (length > 0)
{
FreeResourceData (result);
result = NULL;
}
FreeResourceData (result);
result = NULL;
}
cclose (fh);
return result;
}
+3 -1
View File
@@ -98,7 +98,7 @@ PropFile_from_string (char *d, PROPERTY_HANDLER handler, const char *prefix)
void
PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix)
{
long flen;
size_t flen;
char *data;
flen = LengthResFile (f);
@@ -108,6 +108,8 @@ PropFile_from_file (uio_Stream *f, PROPERTY_HANDLER handler, const char *prefix)
return;
}
// We may end up with less bytes than we asked for due to the
// DOS->Unix newline conversion
flen = ReadResFile (data, 1, flen, f);
data[flen] = '\0';
+1 -1
View File
@@ -65,7 +65,7 @@ void
_GetConversationData (const char *path, RESOURCE_DATA *resdata)
{
uio_Stream *fp;
long dataLen;
unsigned long dataLen;
void *result;
int n, path_len, num_data_sets;
DWORD opos,
+8 -8
View File
@@ -107,7 +107,7 @@ cread_a8 (DECODE_REF fh, BYTE *ar, COUNT count)
return cread (ar, 1, count, fh) == count;
}
static inline COUNT
static inline size_t
read_8 (void *fp, BYTE *v)
{
BYTE t;
@@ -116,7 +116,7 @@ read_8 (void *fp, BYTE *v)
return ReadResFile (v, 1, 1, fp);
}
static inline COUNT
static inline size_t
read_16 (void *fp, UWORD *v)
{
UWORD t;
@@ -125,7 +125,7 @@ read_16 (void *fp, UWORD *v)
return ReadResFile (v, 2, 1, fp);
}
static inline COUNT
static inline size_t
read_32 (void *fp, DWORD *v)
{
DWORD t;
@@ -134,7 +134,7 @@ read_32 (void *fp, DWORD *v)
return ReadResFile (v, 4, 1, fp);
}
static inline COUNT
static inline size_t
read_32s (void *fp, SDWORD *v)
{
DWORD t;
@@ -147,28 +147,28 @@ read_32s (void *fp, SDWORD *v)
return ret;
}
static inline COUNT
static inline size_t
read_ptr (void *fp)
{
DWORD t;
return read_32 (fp, &t); /* ptrs are useless in saves */
}
static inline COUNT
static inline size_t
read_a8 (void *fp, BYTE *ar, COUNT count)
{
assert (ar != NULL);
return ReadResFile (ar, 1, count, fp) == count;
}
static inline COUNT
static inline size_t
read_str (void *fp, char *str, COUNT count)
{
// no type conversion needed for strings
return read_a8 (fp, (BYTE *)str, count);
}
static inline COUNT
static inline size_t
read_a16 (void *fp, UWORD *ar, COUNT count)
{
assert (ar != NULL);
+8 -9
View File
@@ -74,44 +74,44 @@ cwrite_a8 (DECODE_REF fh, const BYTE *ar, COUNT count)
return cwrite (ar, 1, count, fh) == count;
}
static inline COUNT
static inline size_t
write_8 (void *fp, BYTE v)
{
return WriteResFile (&v, 1, 1, fp);
}
static inline COUNT
static inline size_t
write_16 (void *fp, UWORD v)
{
return WriteResFile (&v, 2, 1, fp);
}
static inline COUNT
static inline size_t
write_32 (void *fp, DWORD v)
{
return WriteResFile (&v, 4, 1, fp);
}
static inline COUNT
static inline size_t
write_ptr (void *fp)
{
return write_32 (fp, 0); /* ptrs are useless in saves */
}
static inline COUNT
static inline size_t
write_a8 (void *fp, const BYTE *ar, COUNT count)
{
return WriteResFile (ar, 1, count, fp) == count;
}
static inline COUNT
static inline size_t
write_str (void *fp, const char *str, COUNT count)
{
// no type conversion needed for strings
return write_a8 (fp, (const BYTE *)str, count);
}
static inline COUNT
static inline size_t
write_a16 (void *fp, const UWORD *ar, COUNT count)
{
for ( ; count > 0; --count, ++ar)
@@ -838,8 +838,7 @@ RetrySave:
success = SaveSummary (SummPtr, out_fp);
// Then write the rest of the data.
if (success && WriteResFile (h, (COUNT)flen, 1,
out_fp) == 0)
if (success && WriteResFile (h, flen, 1, out_fp) != 1)
success = FALSE;
if (res_CloseResFile ((uio_Stream *)out_fp) == 0)