ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/freezethaw.h
Revision: 1.15
Committed: Sat Nov 17 23:40:01 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.14: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# User Rev Content
1 root 1.1 /*
2     * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.13 *
4 root 1.15 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.14 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.13 *
7 root 1.1 * Deliantra is free software: you can redistribute it and/or modify it under
8     * the terms of the Affero GNU General Public License as published by the
9     * Free Software Foundation, either version 3 of the License, or (at your
10     * option) any later version.
11 root 1.13 *
12 root 1.1 * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16 root 1.13 *
17 root 1.1 * You should have received a copy of the Affero GNU General Public License
18     * and the GNU General Public License along with this program. If not, see
19     * <http://www.gnu.org/licenses/>.
20 root 1.13 *
21 root 1.1 * The authors can be reached via e-mail to <support@deliantra.net>
22     */
23    
24     #ifndef FREEZETHAW_H__
25     #define FREEZETHAW_H__
26    
27     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
28    
29     // a little dirty hack, maybe unify with something else at a later time
30     // it is used to communicate as much info about the keyword string
31     // to the consuemr as possible, for maximum performance.
32     struct keyword_string
33     {
34     const char *s;
35     const int l;
36    
37     keyword_string (const char *s, int l)
38     : s(s), l(l)
39     { }
40    
41     keyword_string (keyword kw)
42     : s(keyword_str [kw]), l(keyword_len [kw])
43     {
44     }
45    
46     keyword_string (shstr_tmp sh)
47     : s(&sh), l(sh.length ())
48     {
49     }
50     };
51    
52     #define CS(keyword) keyword_string (# keyword, sizeof (# keyword) - 1)
53     #define KW(keyword) CS(keyword)
54    
55     INTERFACE_CLASS(object_freezer)
56     struct object_freezer : dynbuf_text
57     {
58     AV *av;
59    
60     object_freezer ();
61     ~object_freezer ();
62    
63     // serialise perl part
64     void put_ (attachable *ext);
65     void put (attachable *ext)
66     {
67     if (expect_false (ext->self))
68     put_ (ext);
69     }
70    
71     // null value (== no space after keyword)
72     void put (const keyword_string k)
73     {
74     char *p = force (k.l + 1);
75     memcpy (p, k.s, k.l); p += k.l; *p++ = '\n';
76     alloc (p);
77     }
78    
79     void put (const keyword_string k, const keyword_string v)
80     {
81     char *p = force (k.l + 1 + v.l + 1);
82     memcpy (p, k.s, k.l); p += k.l; *p++ = ' ';
83     memcpy (p, v.s, v.l); p += v.l; *p++ = '\n';
84     alloc (p);
85     }
86    
87     void put (const keyword_string k, const_utf8_string v)
88     {
89     if (expect_true (v))
90     put (k, keyword_string (v, strlen (v)));
91     else
92     put (k);
93     }
94    
95     void put (const keyword_string k, shstr_tmp v)
96     {
97     put (k, keyword_string (v));
98     }
99    
100     void put (const keyword_string k, double v)
101     {
102     char *p = force (MAX_KEYWORD_LEN + 2 + 32);
103     memcpy (p, k.s, k.l); p += k.l; *p++ = ' ';
104     p += sprintf (p, "%.7g", v); *p++ = '\n';
105     alloc (p);
106     }
107    
108     void put_(const keyword_string k, sint64 v)
109     {
110     force (MAX_KEYWORD_LEN + 2 + sint64_digits);
111     fadd (k.s, k.l);
112     fadd (' ');
113     add (v);
114     fadd ('\n');
115     }
116    
117     void put_(const keyword_string k, sint32 v)
118     {
119     force (MAX_KEYWORD_LEN + 2 + sint32_digits);
120     fadd (k.s, k.l);
121     fadd (' ');
122     add (v);
123     fadd ('\n');
124     }
125    
126     void put (const keyword_string k, float v) { put (k, (double)v); }
127     void put (const keyword_string k, signed char v) { put_(k, (sint32)v); }
128     void put (const keyword_string k, unsigned char v) { put_(k, (sint32)v); }
129     void put (const keyword_string k, signed short v) { put_(k, (sint32)v); }
130     void put (const keyword_string k, unsigned short v) { put_(k, (sint32)v); }
131     void put (const keyword_string k, signed int v) { put_(k, (sint32)v); }
132     void put (const keyword_string k, unsigned int v) { put_(k, (sint64)v); }
133     void put (const keyword_string k, signed long v) { put_(k, (sint64)v); }
134     void put (const keyword_string k, unsigned long v) { put_(k, (sint64)v); }
135     void put (const keyword_string k, signed long long v) { put_(k, (sint64)v); }
136     void put (const keyword_string k, unsigned long long v) { put_(k, (sint64)v); }
137    
138     void put (const keyword_string kbeg, const keyword_string kend, shstr_tmp v)
139     {
140     force (MAX_KEYWORD_LEN + 1);
141     fadd (kbeg.s, kbeg.l); fadd ('\n');
142    
143     if (expect_true (v))
144     {
145     add (v);
146     add ('\n');
147     }
148    
149     force (MAX_KEYWORD_LEN + 1);
150     fadd (kend.s, kend.l); fadd ('\n');
151     }
152    
153     void put (const keyword_string k, archetype *v);
154     void put (const keyword_string k, treasurelist *v);
155     void put (const keyword_string k, faceinfo *v);
156    
157     template<typename T>
158     void put (const keyword_string k, const refptr<T> &v)
159     {
160     put (k, (T *)v);
161     }
162    
163     MTH bool save (const_octet_string path);
164     utf8_string as_string (); // like strdup
165    
166     operator bool () { return !!av; }
167     };
168    
169 root 1.6 // used as dir argument to object_thawer constructor
170     #define RESOURCE_DIR const_utf8_string (0)
171    
172 root 1.1 INTERFACE_CLASS(object_thawer)
173     struct object_thawer
174     {
175 root 1.4 static bool errors_are_fatal; // true during server startup only
176    
177 root 1.1 char *line; // current beginning of line
178     SV *text; // text part
179     AV *av; // perl part
180     int linenum;
181     keyword kw;
182     char *kw_str; // the keyword parsed, as string
183     char *value; // the value, or 0 if no value
184     const char *value_nn; // the value, or the empty string if no value
185     const char *name;
186    
187     operator bool () const { return !!text; }
188    
189     object_thawer (const_utf8_string path = 0);
190 root 1.5 object_thawer (const_utf8_string dir, const_utf8_string file);
191 root 1.1 object_thawer (const_utf8_string data, AV *perlav);
192     ~object_thawer ();
193    
194     void get (attachable *obj, int oid);
195    
196     // parse next line as keyword-value pair
197     MTH void next ();
198    
199 root 1.2 // parse next line, as a single value
200     // skips initial whitespace and comments
201     // and sets kw to KW_value on success.
202 root 1.3 MTH bool next_line ();
203 root 1.1
204     // skip the current key-value (usually fetch next line, for
205     // multiline-fields, skips till the corresponding end-kw
206     MTH void skip ();
207     MTH void skip_block (); // skips till and over KW_end
208    
209 root 1.10 // returns true IFF a value was specified and it is not the empty string
210     // formerly, this just etsetd for a value, but the editor is too dumb
211     // and creates lots of empty values.
212     bool has_value () const { return *value_nn; }
213 root 1.7 const_utf8_string get_str () const { return value_nn; } // empty string when missing
214 root 1.1 void get_ml (keyword kend, shstr &sh);
215    
216     void get_ornull (shstr &sh) const { sh = value; }
217     void get (shstr &sh) const { sh = value; } // might want to check for non-null here
218    
219     bool get_bool () const { return *value_nn == '1'; }
220     sint32 get_sint32 () const;
221     sint64 get_sint64 () const { return strtoll (value_nn, 0, 10); }
222     double get_double () const { return strtod (value_nn, 0); }
223    
224 root 1.7 void get (float &v) const { v = get_double (); }
225     void get (double &v) const { v = get_double (); }
226 root 1.1
227 root 1.7 void get (bool &i) const { i = get_bool (); }
228     void get (sint8 &i) const { i = get_sint32 (); }
229     void get (uint8 &i) const { i = get_sint32 (); }
230     void get (sint16 &i) const { i = get_sint32 (); }
231     void get (uint16 &i) const { i = get_sint32 (); }
232     void get (sint32 &i) const { i = get_sint32 (); }
233 root 1.1
234 root 1.7 void get (uint32 &i) const { i = get_sint64 (); }
235     void get (sint64 &i) const { i = get_sint64 (); }
236 root 1.1
237 root 1.7 void get (region_ptr &r) const;
238 root 1.8 void get (materialtype_t *&mt) const;
239 root 1.7
240     MTH void parse_warn (const_utf8_string msg) const;
241     MTH bool parse_error (const_utf8_string type = 0, const_utf8_string name = 0, bool skip = true) const;
242 root 1.1
243     struct delayed_ref {
244     attachable *op;
245     object_ptr *ptr;
246     const_utf8_string ref;
247     };
248     std::vector<delayed_ref> delrefs;
249    
250     void delayed_deref (attachable *op, object_ptr &ptr, const_utf8_string ref);
251     MTH void resolve_delayed_derefs (bool deref = true);
252 root 1.5 private:
253 root 1.9 void init (const_utf8_string path);
254     void init_from_file (const_utf8_string path);
255 root 1.1 };
256    
257     #endif
258