Skip to content

Commit 1aeb676

Browse files
author
Dancer Vesperman
committed
Starting to hack a unified file-access API
1 parent 04fcde6 commit 1aeb676

File tree

7 files changed

+365
-0
lines changed

7 files changed

+365
-0
lines changed

files/Flex.cc

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright (C) 2000 Dancer A.L Vesperman
3+
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.
8+
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.
13+
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
20+
#if __GNUG__ >= 2
21+
# pragma implementation
22+
#endif
23+
24+
25+
26+
#include "Flex.h"
27+
28+
#include <cstdio>
29+
#include <iostream>
30+
31+
void Flex::IndexFlexFile(void)
32+
{
33+
Flex &ret=*this;
34+
FILE *fp;
35+
const char *name=ret.filename.c_str();
36+
fp=fopen(name,"rb");
37+
if(!fp)
38+
{
39+
throw 0;
40+
return;
41+
}
42+
fread(ret.title,sizeof(ret.title),1,fp);
43+
fread(&ret.magic1,sizeof(uint32),1,fp);
44+
fread(&ret.count,sizeof(uint32),1,fp);
45+
fread(&ret.magic2,sizeof(uint32),1,fp);
46+
if(magic1!=0xffff1a00UL)
47+
throw 0; // Not a flex file
48+
for(int i=0;i<9;i++)
49+
fread(&ret.padding[i],sizeof(uint32),1,fp);
50+
#if DEBUGFLEX
51+
cout << "Title: " << ret.title << endl;
52+
cout << "Count: " << ret.count << endl;
53+
#endif
54+
55+
// We should already be there.
56+
fseek(fp,128,SEEK_SET);
57+
for(unsigned int i=0;i<ret.count;i++)
58+
{
59+
Flex::Reference f;
60+
fread(&f.offset,sizeof(uint32),1,fp);
61+
fread(&f.size,sizeof(uint32),1,fp);
62+
#if DEBUGFLEX
63+
cout << "Item " << i << ": " << f.size << " bytes @ " << f.offset << endl;
64+
#endif
65+
ret.object_list.push_back(f);
66+
}
67+
fclose(fp);
68+
}
69+
70+
#if 0
71+
char *Flex::read_object(int objnum,uint32 &length)
72+
{
73+
if((unsigned)objnum>=object_list.size())
74+
{
75+
cerr << "objnum too large in read_object()" << endl;
76+
return 0;
77+
}
78+
FILE *fp=fopen(filename.c_str(),"rb");
79+
if(!fp)
80+
{
81+
cerr << "File open failed in read_object" << endl;
82+
return 0;
83+
}
84+
fseek(fp,object_list[objnum].offset,SEEK_SET);
85+
length=object_list[objnum].size;
86+
char *ret=new char[length];
87+
fread(ret,length,1,fp);
88+
fclose(fp);
89+
return ret;
90+
}
91+
#endif

files/Flex.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (C) 2000 Dancer A.L Vesperman
3+
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.
8+
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.
13+
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
#ifndef __Flex_h_
20+
#define __Flex_h_
21+
22+
#if !AUTOCONFIGURED
23+
#include "../autoconfig.h"
24+
#endif
25+
26+
#include <vector>
27+
#include <string>
28+
#include "common_types.h"
29+
#include "U7file.h"
30+
31+
32+
class Flex : public virtual U7file
33+
{
34+
protected:
35+
char title[80];
36+
uint32 magic1;
37+
uint32 count;
38+
uint32 magic2;
39+
uint32 padding[9];
40+
struct Reference
41+
{
42+
uint32 offset;
43+
uint32 size;
44+
Reference() : offset(0),size(0) {};
45+
};
46+
vector<Reference> object_list;
47+
public:
48+
Flex(const char *fname);
49+
Flex(const string &fname);
50+
~Flex();
51+
52+
// char *read_object(int objnum,uint32 &length);
53+
virtual int number_of_objects(const char *) { return object_list.size(); };
54+
virtual int retrieve(const char *,int objnum,char *,size_t *len); // To a memory block
55+
virtual int retrieve(const char *,int objnum,const char *); // To a file
56+
private:
57+
Flex(); // No default constructor
58+
void IndexFlexFile(void);
59+
};
60+
61+
62+
#endif

files/README

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The goal here is to make a unified files API that is inherently
2+
object-centric rather than file-centric. The same (simple) API
3+
is used to fetch any given object by any given exult subsystem
4+
and the subsystem requesting the object does not need to know
5+
what sort of file-format contains the object. The API takes
6+
care of all of that ugly need-to-know business.
7+
8+
For the future, and a game creator, the API can be extended to
9+
pack files as well. I'm also interested in the idea of
10+
configurable redirections, where an object in a collection can
11+
be overriden and replaced by some other file/object and that
12+
redirection be transparent to the core.

files/Table.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright (C) 2000 Dancer A.L Vesperman
3+
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.
8+
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.
13+
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
20+
#if __GNUG__ >= 2
21+
# pragma implementation
22+
#endif
23+
24+
25+
26+
#include "Table.h"
27+
28+
#include <cstdio>
29+
#include <iostream>
30+
31+
Table AccessTableFile(const char *name)
32+
{
33+
Table ret;
34+
FILE *fp;
35+
ret.filename=name;
36+
fp=fopen(name,"rb");
37+
if(!fp)
38+
{
39+
return ret;
40+
}
41+
unsigned int i=0;
42+
while(1)
43+
{
44+
Table::Reference f;
45+
fread(&f.size,sizeof(uint16),1,fp);
46+
if(f.size==65535)
47+
break;
48+
fread(&f.offset,sizeof(uint32),1,fp);
49+
#if 0
50+
cout << "Item " << i << ": " << f.size << " @ " << f.offset << endl;
51+
#endif
52+
ret.object_list.push_back(f);
53+
i++;
54+
}
55+
fclose(fp);
56+
return ret;
57+
}
58+
59+
char *Table::read_object(int objnum,uint32 &length)
60+
{
61+
if((unsigned)objnum>=object_list.size())
62+
{
63+
cerr << "objnum too large in read_object()" << endl;
64+
return 0;
65+
}
66+
FILE *fp=fopen(filename.c_str(),"rb");
67+
if(!fp)
68+
{
69+
cerr << "File open failed in read_object" << endl;
70+
return 0;
71+
}
72+
fseek(fp,object_list[objnum].offset,SEEK_SET);
73+
// length=object_list[objnum].size;
74+
uint16 sz;
75+
fread(&sz,sizeof(sz),1,fp);
76+
length=sz-2;
77+
char *ret=new char[length];
78+
fread(ret,length,1,fp);
79+
fclose(fp);
80+
return ret;
81+
}

files/Table.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright (C) 2000 Dancer A.L Vesperman
3+
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.
8+
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.
13+
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
#ifndef __Table_h_
20+
#define __Table_h_
21+
22+
#if !AUTOCONFIGURED
23+
#include "../autoconfig.h"
24+
#endif
25+
26+
#include <vector>
27+
#include <string>
28+
#include "common.h"
29+
30+
31+
struct Table
32+
{
33+
string filename;
34+
struct Reference
35+
{
36+
uint32 offset;
37+
uint16 size;
38+
Reference() : offset(0),size(0) {};
39+
};
40+
vector<Reference> object_list;
41+
char *read_object(int objnum,uint32 &length);
42+
};
43+
44+
extern Table AccessTableFile(const char *);
45+
46+
#endif

files/U7file.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#include <string>
3+
#include <map>
4+
5+
class U7file
6+
{
7+
protected:
8+
string filename;
9+
public:
10+
U7file() {};
11+
U7file(const char *name) : filename(name) {};
12+
virtual int number_of_objects(const char *)=0;
13+
virtual int retrieve(const char *,int objnum,char *,size_t *len)=0; // To a memory block
14+
virtual int retrieve(const char *,int objnum,const char *)=0; // To a file
15+
virtual ~U7file();
16+
};
17+
18+
class U7FileManager
19+
{
20+
protected:
21+
map<const string,U7file *> file_list;
22+
public:
23+
U7FileManager();
24+
~U7FileManager();
25+
26+
U7file *get_file_object(const string &s);
27+
};
28+
29+
class U7object
30+
{
31+
protected:
32+
string filename;
33+
int objnumber;
34+
public:
35+
virtual int retrieve(char *,size_t len); // Retrieve to a memory block
36+
virtual int retrieve(const char *); // Retrieve to a filename
37+
38+
U7object(const char *file,int objnum);
39+
virtual ~U7object();
40+
};

files/common_types.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright (C) 2000 Dancer A.L Vesperman
3+
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.
8+
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.
13+
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
#ifndef ____common_h_
20+
#define ____common_h_
21+
22+
#if !AUTOCONFIGURED
23+
#include "../autoconfig.h"
24+
#endif
25+
26+
27+
typedef unsigned char uint8;
28+
typedef unsigned long uint32;
29+
typedef unsigned short uint16;
30+
31+
32+
#endif
33+

0 commit comments

Comments
 (0)