source: abuse/trunk/src/imlib/include/specs.hpp @ 17

Last change on this file since 17 was 17, checked in by Sam Hocevar, 17 years ago
  • absolute shitloads of 64 bit fixes.
File size: 6.7 KB
Line 
1#ifndef __SPECS_HPP_
2#define __SPECS_HPP_
3#include "linked.hpp"
4#include <stdio.h>
5#include "jmalloc.hpp"
6#include "system.h"
7#include <fcntl.h>
8#include <stdlib.h>
9#include <stdint.h>
10
11extern char *spec_types[];
12
13#define SPEC_INVALID_TYPE    0
14#define SPEC_COLOR_TABLE     1
15#define SPEC_PALETTE         2
16#define SPEC_IMAGE           4
17#define SPEC_FORETILE        5
18#define SPEC_BACKTILE        6
19#define SPEC_CHARACTER       7
20#define SPEC_MORPH_POINTS_8  8
21#define SPEC_MORPH_POINTS_16 9
22#define SPEC_GRUE_OBJS       10
23#define SPEC_EXTERN_SFX      11
24#define SPEC_DMX_MUS         12
25#define SPEC_PATCHED_MORPH   13
26#define SPEC_NORMAL_FILE     14
27#define SPEC_COMPRESS1_FILE  15
28#define SPEC_VECTOR_IMAGE    16
29#define SPEC_LIGHT_LIST      17
30#define SPEC_GRUE_FGMAP      18
31#define SPEC_GRUE_BGMAP      19
32#define SPEC_DATA_ARRAY      20
33#define SPEC_CHARACTER2      21
34#define SPEC_PARTICLE        22
35#define SPEC_EXTERNAL_LCACHE 23
36
37
38#define SPEC_SIGNATURE    "SPEC1.0"
39#define SPEC_SIG_SIZE     8
40
41#define SPEC_FLAG_LINK    1
42
43#define SPEC_SEARCH_INSIDE_OUTSIDE 1
44#define SPEC_SEARCH_OUTSIDE_INSIDE 2
45#define SPEC_SEARCH_INSIDE_ONLY    3
46
47/* file specs
48              8   signature
49              2   number entries
50                  [entries]
51              1      entry type
52              1      entry name length
53              X      entry name (with null terminator)
54              1      flags
55                     if (flags&LINK)
56              1        link filename length
57              X        link filename
58                     else
59              4        data size
60              4        offset
61*/
62
63void set_spec_main_file(char *filename, int search_order=SPEC_SEARCH_OUTSIDE_INSIDE);
64
65void set_filename_prefix(char *prefix);
66char *get_filename_prefix();
67void set_save_filename_prefix(char *prefix);
68char *get_save_filename_prefix();
69#define JFILE_CLONED 1
70
71class bFILE     // base file type which other files should be derived from (jFILE & NFS for now)
72{
73  protected :
74  unsigned char *rbuf,*wbuf;
75  unsigned long rbuf_start,rbuf_end,rbuf_size,
76                wbuf_end,wbuf_size;                // can't seek while writing!
77  int flush_writes();                             // returns 0 on failure, else # of bytes written
78
79  virtual int unbuffered_read(void *buf, size_t count)  = 0;
80  virtual int unbuffered_write(void *buf, size_t count) = 0;
81  virtual int unbuffered_tell()                         = 0;
82  virtual int unbuffered_seek(long offset, int whence)  = 0;   // whence=SEEK_SET, SEEK_CUR,
83                                                               // SEEK_END, ret=0=success
84  virtual int allow_read_buffering();
85  virtual int allow_write_buffering();
86  public :
87  bFILE();
88  virtual int open_failure() = 0;
89  int read(void *buf, size_t count);       // returns number of bytes read, calls unbuffer_read
90  int write(void *buf, size_t count);      // returns number of bytes written
91  int seek(long offset, int whence);       // whence=SEEK_SET, SEEK_CUR, SEEK_END, ret=0=success
92  int tell();
93  virtual int file_size() = 0;
94
95  virtual ~bFILE();
96
97
98  // these read and writes, allways read/write Intel endian-ness
99  uint16_t read_uint16();
100  uint32_t read_uint32();
101  uint8_t read_uint8();
102  double read_double();
103  void write_uint16(uint16_t x);
104  void write_uint32(uint32_t x);
105  void write_uint8(uint8_t x);
106  void write_double(double x);
107  void set_read_buffer_size(long size);
108} ;
109
110class jFILE : public bFILE     // this file type will use virtual opens inside of a spe
111{
112  char *fname;
113  char *tmp_write_name;
114  int access;
115  int fd,flags;
116  long start_offset,file_length;    // offset of file from actual file begining
117
118  long current_offset;  // current offset
119 
120public :
121        int get_fd() const { return fd; }
122
123  void open_internal(char *filename, char *mode, int flags);
124  void open_external(char *filename, char *mode, int flags);
125
126  jFILE(char *filename, char *access_string);      // same as fopen parameters
127  jFILE(FILE *file_pointer);                      // assumes fp is at begining of file
128  virtual int open_failure() { return fd<0; }
129  virtual int unbuffered_read(void *buf, size_t count);       // returns number of bytes read
130  virtual int unbuffered_write(void *buf, size_t count);     // returns number of bytes written
131  virtual int unbuffered_seek(long offset, int whence);      // whence=SEEK_SET, SEEK_CUR,
132                                                             // SEEK_END, ret=0=success
133  virtual int unbuffered_tell();
134  virtual int file_size() { return file_length; }
135  virtual ~jFILE();
136} ;
137
138class spec_entry
139{
140public :
141  char *name;
142  unsigned long size,offset;
143  unsigned char type;
144 
145  spec_entry(unsigned char spec_type,
146             char *object_name,
147             char *link_filename,
148             unsigned long data_size,
149             unsigned long data_offset)
150  { type=spec_type;
151    name=strcpy((char *)jmalloc(strlen(object_name)+1,"spec_entry::name"),object_name);
152    size=data_size; offset=data_offset;
153  } 
154  void print();
155  ~spec_entry() { if (name) jfree(name); }
156} ;
157
158
159class spec_directory
160{
161public :
162  void startup(bFILE *fp);
163
164  int total;
165  spec_entry **entries;
166  void *data;
167  long size;
168//  spec_directory(char *filename);  ;; not allowed anymore, user must construct file first!
169  spec_directory(FILE *fp);
170  spec_directory(bFILE *fp);
171  spec_directory();
172  spec_entry *find(char *name);
173  spec_entry *find(char *name, int type);
174  spec_entry *find(int type);
175  long find_number(char*name);
176  long find_number(int type);
177  void remove(spec_entry *e);
178  void add_by_hand(spec_entry *e);
179  void calc_offsets();
180  long data_start_offset();  // returns the firsyt offset past directory items
181  long data_end_offset();    // this should be the end of the file
182  long type_total(int type);
183  jFILE *write(char *filename);
184  int    write(bFILE *fp);
185  void print();
186  void delete_entries();   // if the directory was created by hand instead of by file
187  ~spec_directory();
188} ;
189
190/*jFILE *add_directory_entry(char *filename,
191                         unsigned short data_type,
192                         char *data_name,
193                         unsigned long data_size,
194                         char *link_filename=NULL);*/
195
196uint16_t read_uint16(FILE *fp);
197uint32_t read_uint32(FILE *fp);
198uint32_t read_other_uint32(FILE *fp);
199uint16_t read_other_uint16(FILE *fp);
200uint8_t read_uint8(FILE *fp);
201
202void write_uint16(FILE *fp, uint16_t x);
203void write_uint32(FILE *fp, uint32_t x);
204void write_other_uint16(FILE *fp, uint16_t x);
205void write_other_uint32(FILE *fp, uint32_t x);
206void write_uint8(FILE *fp, uint8_t x);
207
208void set_spec_main_file(char *filename, int Search_order);
209void set_file_opener(bFILE *(*open_fun)(char *, char *));
210void set_no_space_handler(void (*handle_fun)());
211bFILE *open_file(char *filename, char *mode);
212#endif
213
214
215
216
217
218
219
Note: See TracBrowser for help on using the repository browser.