]> git.pond.sub.org Git - empserver/blob - include/file.h
Clean up initialization of empfile[]
[empserver] / include / file.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  file.h: Describes Empire tables (`files' for historical reasons)
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2005
32  */
33
34 #ifndef FILE_H
35 #define FILE_H
36
37 #include <time.h>
38
39 struct empfile {
40     /* Members with immutable values */
41     int uid;                    /* Table ID */
42     char *name;                 /* Empire name (e.g., "treaty") */
43     char *file;                 /* if backed by file, file name relative to
44                                    data directory */
45     struct castr *cadef;        /* table column selectors (column meta-data) */
46     int size;                   /* size of a table entry */
47     int flags;                  /* only EFF_IMMUTABLE immutable, see below
48                                    for use of remaining bits */
49
50     /* Members whose values are fixed when the cache is mapped */
51     char *cache;                /* pointer to cache */
52     int csize;                  /* cache size, in entries */
53     /* flags bit EFF_MEM also fixed then */
54
55     /* Members whose values may vary throughout operation */
56     int baseid;                 /* id of first entry in cache */
57     int cids;                   /* # entries in cache */
58     int fids;                   /* # entries in table */
59     int fd;                     /* file descriptor, -1 if not open */
60     /* flags bits EFF_RDONLY, EFF_CUSTOM also vary */
61
62     /* User callbacks */
63     void (*init)(int, void *);  /* called after entry creation, unless null */
64     int (*postread)(int, void *); /* called after read, unless null */
65     int (*prewrite)(int, void *); /* called before write, unless null */
66 };
67
68 /*
69  * Flag bits for struct empfile member flags
70  * Immutable flags are properties of the table and thus cannot change.
71  * The remaining flags record how the table is being used.
72  */
73 /* Immutable flags, fixed at compile-time */
74 /*
75  * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner /
76  * group of such a table's entries can be safely obtained by
77  * dereferencing entry address cast to struct empobj *.
78  */
79 #define EFF_XY          bit(0)
80 #define EFF_OWNER       bit(1)
81 #define EFF_GROUP       bit(2)
82 /* Table is allocated statically */
83 #define EFF_STATIC      bit(3)
84 /* All the immutable flags */
85 #define EFF_IMMUTABLE   (EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
86 /* Flags set when table contents is mapped */
87 /* Table is entirely in memory */
88 #define EFF_MEM         bit(8)
89 /* Table is read-only */
90 #define EFF_RDONLY      bit(9)
91 /* Table is customized (configuration tables only) */
92 #define EFF_CUSTOM      bit(10)
93 /* Transient flags, only occur in argument of ef_open() */
94 /* Create table file, clobbering any existing file */
95 #define EFF_CREATE      bit(11)
96
97 /*
98  * Empire `file types'
99  * These are really table IDs.  Some tables are backed by files, some
100  * are compiled into the server.
101  */
102 enum {
103     /* Error value */
104     EF_BAD = -1,
105     /* Dynamic game data tables */
106     EF_SECTOR,
107     EF_SHIP,
108     EF_PLANE,
109     EF_LAND,
110     EF_NUKE,
111     EF_NEWS,
112     EF_TREATY,
113     EF_TRADE,
114     EF_POWER,
115     EF_NATION,
116     EF_LOAN,
117     EF_MAP,
118     EF_BMAP,
119     EF_COMM,
120     EF_LOST,
121     EF_REALM,
122     EF_GAME,
123     EF_DYNMAX = EF_GAME,
124     /* Static game data (configuration) */
125     /* Order is relevant; see read_builtin_tables() */
126     EF_ITEM,
127     EF_PRODUCT,
128     EF_SECTOR_CHR,
129     EF_SHIP_CHR,
130     EF_PLANE_CHR,
131     EF_LAND_CHR,
132     EF_NUKE_CHR,
133     EF_NEWS_CHR,
134     EF_INFRASTRUCTURE,
135     EF_UPDATES,                 /* not actually static */
136     EF_TABLE,
137     EF_META,                    /* not really configuration */
138     /* Symbol tables */
139     EF_AGREEMENT_STATUS,
140     EF_LAND_CHR_FLAGS,
141     EF_LEVEL,
142     EF_META_FLAGS,
143     EF_META_TYPE,
144     EF_MISSIONS,
145     EF_NATION_FLAGS,
146     EF_NATION_REJECTS,
147     EF_NATION_RELATIONS,
148     EF_NATION_STATUS,
149     EF_NUKE_CHR_FLAGS,
150     EF_PACKING,
151     EF_PAGE_HEADINGS,
152     EF_PLAGUE_STAGES,
153     EF_PLANE_CHR_FLAGS,
154     EF_PLANE_FLAGS,
155     EF_RESOURCES,
156     EF_RETREAT_FLAGS,
157     EF_SECTOR_NAVIGATION,
158     EF_SHIP_CHR_FLAGS,
159     EF_TREATY_FLAGS,
160     /* Views */
161     EF_COUNTRY,
162     /* Number of types: */
163     EF_MAX
164 };
165
166 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
167 #define EF_IS_VIEW(type) (EF_COUNTRY <= (type) && (type) < EF_MAX)
168
169 extern struct castr *ef_cadef(int);
170 extern int ef_read(int, int, void *);
171 extern void *ef_ptr(int, int);
172 extern char *ef_nameof(int);
173 extern time_t ef_mtime(int);
174 extern int ef_open(int, int);
175 extern int ef_check(int);
176 extern int ef_close(int);
177 extern int ef_flush(int);
178 extern int ef_write(int, int, void *);
179 extern int ef_extend(int, int);
180 extern int ef_ensure_space(int, int, int);
181 extern int ef_nelem(int);
182 extern int ef_flags(int);
183 extern int ef_byname(char *);
184 extern int ef_byname_from(char *, int *);
185 extern int ef_verify(void);
186 extern int ef_elt_byname(int, char *);
187
188 extern struct empfile empfile[EF_MAX + 1];
189 extern void empfile_init(void);
190 extern void empfile_fixup(void);
191
192 #endif