]> git.pond.sub.org Git - empserver/blob - src/lib/common/type.c
84ebdc21a44f88d60f07a0c750b7668216a459be
[empserver] / src / lib / common / type.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  type.c: typename to array offset translation
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2004-2011
33  */
34
35 #include <config.h>
36
37 #include "item.h"
38 #include "land.h"
39 #include "match.h"
40 #include "nsc.h"
41 #include "nuke.h"
42 #include "plane.h"
43 #include "product.h"
44 #include "prototypes.h"
45 #include "sect.h"
46 #include "ship.h"
47
48 /*
49  * Return index of sector called @name in dchr[], or M_NOTFOUND.
50  */
51 int
52 sct_typematch(char *name)
53 {
54     int i;
55
56     if (name[0] && !name[1])
57         for (i = 0; dchr[i].d_name; ++i)
58             if (dchr[i].d_mnem == *name)
59                 return i;
60     return M_NOTFOUND;
61 }
62
63 /*
64  * Search table @type for an element matching @name, return its index.
65  * Accepts EF_BAD, but of course never finds anything then.
66  * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are
67  * several.
68  */
69 int
70 ef_elt_byname(int type, char *name)
71 {
72     switch (type) {
73     case EF_BAD:
74         return M_NOTFOUND;
75     case EF_NATION:
76         return cnumb(name);
77     case EF_SECTOR_CHR:
78         return sct_typematch(name);
79     case EF_SHIP_CHR:
80         return stmtch(name, mchr,
81                       offsetof(struct mchrstr, m_name),
82                       sizeof(mchr[0]));
83     case EF_LAND_CHR:
84         return stmtch(name, lchr,
85                       offsetof(struct lchrstr, l_name),
86                       sizeof(lchr[0]));
87     case EF_PLANE_CHR:
88         return stmtch(name, plchr,
89                       offsetof(struct plchrstr, pl_name),
90                       sizeof(plchr[0]));
91     case EF_NUKE_CHR:
92         return stmtch(name, nchr,
93                       offsetof(struct nchrstr, n_name),
94                       sizeof(nchr[0]));
95     case EF_ITEM:
96         return stmtch(name, ichr,
97                       offsetof(struct ichrstr, i_name),
98                       sizeof(ichr[0]));
99     case EF_PRODUCT:
100         return stmtch(name, pchr,
101                       offsetof(struct pchrstr, p_sname),
102                       sizeof(pchr[0]));
103     case EF_TABLE:
104         return stmtch(name, empfile,
105                       offsetof(struct empfile, name),
106                       sizeof(empfile[0]));
107     default:
108         if (ef_cadef(type) == symbol_ca)
109             return stmtch(name, ef_ptr(type, 0),
110                           offsetof(struct symbol, name),
111                           sizeof(struct symbol));
112     }
113     return M_NOTFOUND;
114 }