You remember Shaw's Nightmare right? Anyway I decided to create a new
resource file format but a problem broke out. I think it's the offsets.
When I launch Shaw's Nightmare it gives me this when it loads the strings:
Read only -1 of 2.
I've attached the source files of my latest creation for you to trace the
problem and tell me what I'm doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <dos.h>
#include <sys\stat.h>
#include <sys\types.h>
#include <string.h>
#define MAXITEMS 256
typedef struct
{
short id;
unsigned long size, offset;
} Item_T;
long numitems, indexoffset, indexsize;
Item_T item[MAXITEMS];
static char filespec[MAXITEMS][128], filelist[MAXITEMS][16];
static long fileleng[MAXITEMS];
char header[4], buf[65536];
long dathandle;
void findfiles(char *dafilespec)
{
struct find_t fileinfo;
long i, daspeclen;
char daspec[128];
strcpy(daspec,dafilespec);
for(daspeclen=0;daspec[daspeclen]!=0;daspeclen++);
while ((daspec[daspeclen] != '\\') && (daspeclen > 0)) daspeclen--;
if (daspeclen > 0) daspec[daspeclen++] = '\\';
if (_dos_findfirst(dafilespec,_A_NORMAL,&fileinfo) != 0) return;
do
{
strcpy(&filelist[numitems][0],fileinfo.name);
fileleng[numitems] = fileinfo.size;
filelist[numitems][12] = (char)(fileleng[numitems]&255);
filelist[numitems][13] = (char)((fileleng[numitems]>>8)&255);
filelist[numitems][14] = (char)((fileleng[numitems]>>16)&255);
filelist[numitems][15] = (char)((fileleng[numitems]>>24)&255);
strcpy(&filespec[numitems][0],daspec);
strcpy(&filespec[numitems][daspeclen],fileinfo.name);
item[numitems].size = fileleng[numitems];
numitems++;
if (numitems > MAXITEMS)
{
printf("FATAL ERROR: TOO MANY FILES SELECTED! (MAX is 256)\n");
exit(0);
}
} while (_dos_findnext(&fileinfo) == 0);
}
int main(int argc, char *argv[])
{
long i, j, k, l, fil, fil2;
if (argc < 3)
{
printf("COMPRESS v1.0 - By Micheal Muniko\n");
printf("Syntax: compress [data file] [script file]\n");
exit(0);
}
if ((fil = open(argv[2],O_BINARY|O_RDONLY,S_IREAD)) != -1)
{
l = read(fil,buf,65536);
j = 0;
while ((j < l) && (buf[j] <= 32)) j++;
while (j < l)
{
k = j;
while ((k < l) && (buf[k] > 32)) k++;
buf[k] = 0;
findfiles(&buf[j]);
item[numitems].id++;
j = k+1;
while ((j < l) && (buf[j] <= 32)) j++;
}
close(fil);
}
else {
printf("Cannot open %s\n", argv[2]);
exit(1);
}
if ((dathandle = open(argv[1], O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IWRITE)) == -1)
{
printf("Cannot open %s\n", argv[1]);
exit(1);
}
header[0] = 'D';
header[1] = 'A';
header[2] = 'T';
header[3] = 0x1a;
write(dathandle,&header,4);
write(dathandle,&numitems,2);
indexoffset = tell(dathandle)+8;
for(i=0;i<numitems;i++)
{
for(j=0;j<fileleng[i];j+=65536)
{
k = min(fileleng[i]-j,65536);
indexsize += k;
indexoffset += k;
}
}
write(dathandle,&indexoffset,4);
write(dathandle,&indexsize,4);
for(i=0;i<numitems;i++)
{
printf("Adding item %s...\n",filespec[i]);
if ((fil2 = open(filespec[i],O_BINARY|O_RDONLY,S_IREAD)) == -1)
{
printf("Error: %s not found\n",filespec[i]);
close(fil);
exit(0);
}
for(j=0;j<fileleng[i];j+=65536)
{
k = min(fileleng[i]-j,65536);
read(fil2,buf,k);
if (write(dathandle,buf,k) < k)
{
close(fil2);
close(dathandle);
printf("OUT OF HD SPACE!\n");
exit(0);
}
}
close(fil2);
}
for(i=0;i<numitems;i++)
{
for(j=0;j<fileleng[i];j+=65536)
{
k = min(fileleng[i]-j,65536);
item[i].offset += fileleng[i]+14;
}
}
write(dathandle,&item[0],sizeof(Item_T)*numitems);
close(dathandle);
printf("DAT file created\n");
return(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <dos.h>
#include <sys\stat.h>
#include <sys\types.h>
#include <string.h>
#include "debug.h"
#include "dat.h"
long numitems, indexsize, indexoffset;
char *reloadfilename, header[4];
Item_T item[MAXITEMS];
void loaddat(char *filename)
{
long handle;
if ((handle = open(filename,O_BINARY|O_RDONLY,S_IREAD)) == -1)
{
printf("Cannot load data file %s\n", filename);
exit(1);
}
read(handle,&header,4);
if ((header[0] != 'D') && (header[1] != 'A') && (header[2] != 'T')
&& (header[3] != 0x1a))
{
close(handle);
printf("Data file is corrupt\n");
exit(1);
}
read(handle,&numitems,2);
read(handle,&indexoffset,4);
read(handle,&indexsize,4);
lseek(handle, indexoffset, SEEK_SET);
read(handle,&item[0],sizeof(Item_T)*numitems);
close(handle);
strcpy(reloadfilename,filename);
}
long loaditem(short id)
{
long handle;
char tempbuf[80];
if ((handle = open(reloadfilename,O_BINARY|O_RDONLY,S_IREAD)) == -1)
error("Cannot load data file", 862);
if (id > numitems)
{
sprintf(tempbuf, "%d > numitems", id);
error(tempbuf, 864);
}
lseek(handle, item[id].offset, SEEK_SET);
return(handle);
}
void readitem(long handle, void *buf, long size)
{
char tempbuf[80];
long l;
l = read(handle, buf, size);
if (l < size)
{
sprintf(tempbuf, "Read only %d of %d", l, size);
error(tempbuf, 863);
}
}
void closeitem(long handle)
{
close(handle);
}
#define MAXITEMS 256
typedef struct
{
short id;
unsigned long size, offset;
} Item_T;
extern Item_T item[MAXITEMS];
void loaddat(char *filename);
long loaditem(short id);
void readitem(long handle, void *buf, long size);
void closeitem(long handle);
------------------------------------------------------------------------------
Write once. Port to many.
Get the SDK and tools to simplify cross-platform app development. Create
new or port existing apps to sell to consumers worldwide. Explore the
Intel AppUpSM program developer opportunity. appdeveloper.intel.com/join
http://p.sf.net/sfu/intel-appdev
_______________________________________________
Freedos-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freedos-devel