home · contact · privacy
Server: Move common config file reading stuff into read_config_file().
[plomrogue] / src / server / io.c
1 /* src/server/io.c */
2
3 #include "io.h"
4 #include <errno.h> /* global errno */
5 #include <limits.h> /* PIPE_BUF */
6 #include <stddef.h> /* size_t, NULL */
7 #include <stdint.h> /* uint8_t, uint32_t */
8 #include <stdio.h> /* defines EOF, FILE, sprintf(), ungetc() */
9 #include <stdlib.h> /* free(), atoi() */
10 #include <string.h> /* strlen(), memcpy() */
11 #include <sys/types.h> /* time_t */
12 #include <time.h> /* time() */
13 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(),
14                                       * reset_err_try_fgets_counter()
15                                       */
16 #include "../common/readwrite.h" /* try_fopen(), try_fclose_unlink_rename(),
17                                   * try_fwrite(), try_fputc(), try_fgetc(),
18                                   * try_fclose()
19                                   */
20 #include "../common/rexit.h" /* exit_trouble() */
21 #include "../common/try_malloc.h" /* try_malloc() */
22 #include "cleanup.h" /* set_cleanup_flag(), enum cleanup_flag */
23 #include "map_objects.h" /* structs MapObj, MapObjDef, get_map_obj_def() */
24 #include "world.h" /* global world  */
25
26
27
28 /* Cut out and return first \0-terminated string from world.queue and
29  * appropriately reduce world.queue_size. Return NULL if queue is empty.
30  * Superfluous \0 bytes after the string are also cut out. Should the queue
31  * start with \0 bytes, those are cut out, but NULL is returned instead of "".
32 */
33 static char * get_message_from_queue();
34
35 /* Read input file for input into world.queue. new queue input. Wait a few
36  * seconds until giving up. Translate '\n' chars in input file into '\0' chars.
37  */
38 static void read_file_into_queue();
39
40 /* Write world state as visible to clients to its file. Write single dot line to
41  * server output file to satisfy client ping mechanisms.
42  */
43 static void update_worldstate_file();
44
45 /* Write "value" to new \n-delimited line of "file". */
46 static void write_value_as_line(uint32_t value, FILE * file);
47
48 /* Write to "file" player's inventory, one item name per line. End in "%\n". */
49 static void write_inventory(struct MapObj * player, FILE * file);
50
51 /* Write to "file" game map, with map objects super-imposed. Write one row per
52  * \n-delimited line. Super-impose animated objects over inanimate objects.
53  */
54 static void write_map(FILE * file);
55
56
57
58 static char * get_message_from_queue()
59 {
60     char * f_name = "get_message_from_queue()";
61     char * message = NULL;
62     if (world.queue_size)
63     {
64         size_t cutout_len = strlen(world.queue);
65         if (0 < cutout_len)
66         {
67             cutout_len++;
68             message = try_malloc(cutout_len, f_name);
69             memcpy(message, world.queue, cutout_len);
70         }
71         for (;
72              cutout_len != world.queue_size && '\0' == world.queue[cutout_len];
73              cutout_len++);
74         world.queue_size = world.queue_size - cutout_len;
75         if (0 == world.queue_size)
76         {
77             free(world.queue);   /* NULL so read_file_into_queue() may free() */
78             world.queue = NULL;  /* this every time, even when it's           */
79         }                        /* un-allocated first. */
80         else
81         {
82             char * new_queue = try_malloc(world.queue_size, f_name);
83             memcpy(new_queue, &(world.queue[cutout_len]), world.queue_size);
84             free(world.queue);
85             world.queue = new_queue;
86         }
87     }
88     return message;
89 }
90
91
92
93 static void read_file_into_queue()
94 {
95     char * f_name = "read_file_into_queue()";
96     uint8_t wait_seconds = 5;
97     time_t now = time(0);
98     int test;
99     while (EOF == (test = try_fgetc(world.file_in, f_name)))
100     {
101         if (time(0) > now + wait_seconds)
102         {
103             return;
104         }
105     }
106     do
107     {
108         char c = (char) test;
109         if ('\n' == c)
110         {
111             c = '\0';
112         }
113         char * new_queue = try_malloc(world.queue_size + 1, f_name);
114         memcpy(new_queue, world.queue, world.queue_size);
115         char * new_pos = new_queue + world.queue_size;
116         * new_pos = c;
117         world.queue_size++;
118         free(world.queue);
119         world.queue = new_queue;
120     }
121     while (EOF != (test = try_fgetc(world.file_in, f_name)));
122 }
123
124
125
126 static void update_worldstate_file()
127 {
128     char * f_name = "update_worldstate_file()";
129     char path_tmp[strlen(world.path_worldstate) + strlen(world.tmp_suffix) + 1];
130     sprintf(path_tmp, "%s%s", world.path_worldstate, world.tmp_suffix);
131     FILE * file = try_fopen(path_tmp, "w", f_name);
132     struct MapObj * player = get_player();
133     write_value_as_line(world.turn, file);
134     write_value_as_line(player->lifepoints, file);
135     write_inventory(player, file);
136     write_value_as_line(player->pos.y, file);
137     write_value_as_line(player->pos.x, file);
138     write_value_as_line(world.map.size.y, file);
139     write_value_as_line(world.map.size.x, file);
140     write_map(file);
141     if (world.log)
142     {
143         try_fwrite(world.log, strlen(world.log), 1, file, f_name);
144     }
145     try_fclose_unlink_rename(file, path_tmp, world.path_worldstate, f_name);
146     set_cleanup_flag(CLEANUP_WORLDSTATE);
147     char * dot = ".\n";;
148     try_fwrite(dot, strlen(dot), 1, world.file_out, f_name);
149     fflush(world.file_out);
150 }
151
152
153
154 static void write_value_as_line(uint32_t value, FILE * file)
155 {
156     char * f_name = "write_value_as_line()";
157     char write_buf[12];     /* Holds 10 digits of uint32_t maximum + \n + \0. */
158     sprintf(write_buf, "%u\n", value);
159     try_fwrite(write_buf, strlen(write_buf), 1, file, f_name);
160 }
161
162
163
164 static void write_inventory(struct MapObj * player, FILE * file)
165 {
166     char * f_name = "write_inventory()";
167     struct MapObj * owned = player->owns;
168     if (NULL == owned)
169     {
170         char * empty = "(none)\n";
171         try_fwrite(empty, strlen(empty), 1, file, f_name);
172     }
173     else
174     {
175         uint8_t q;
176         for (q = 0; NULL != owned; q++)
177         {
178             struct MapObjDef * mod = get_map_object_def(owned->type);
179             try_fwrite(mod->name, strlen(mod->name), 1, file, f_name);
180             try_fputc('\n', file, f_name);
181             owned = owned->next;
182         }
183     }
184     try_fputc('%', file, f_name);
185     try_fputc('\n', file, f_name);
186 }
187
188
189
190 static void write_map(FILE * file)
191 {
192     char * f_name = "write_map()";
193     uint32_t map_size = world.map.size.y * world.map.size.x;
194     char visible_map[map_size];
195     memcpy(visible_map, world.map.cells, map_size);
196     struct MapObj * o;
197     struct MapObjDef * d;
198     char c;
199     uint8_t i;
200     for (i = 0; i < 2; i++)
201     {
202         for (o = world.map_objs; o != 0; o = o->next)
203         {
204             if ((   (0 == i && 0 == o->lifepoints)
205                  || (1 == i && 0 < o->lifepoints)))
206             {
207                 d = get_map_object_def(o->type);
208                 c = d->char_on_map;
209                 visible_map[(o->pos.y * world.map.size.x) + o->pos.x] = c;
210             }
211         }
212     }
213     uint16_t x, y;
214     for (y = 0; y < world.map.size.y; y++)
215     {
216         for (x = 0; x < world.map.size.x; x++)
217         {
218             try_fputc(visible_map[(y * world.map.size.x) + x], file, f_name);
219         }
220         try_fputc('\n', file, f_name);
221     }
222 }
223
224
225
226 extern void read_config_file(char * path, enum cleanup_flag cleanup,
227                              size_t size, struct EntrySkeleton ** entry_start,
228                              void (* read) (char *, uint32_t, char *,
229                                             struct EntrySkeleton *, FILE *))
230 {
231     char * f_name = "init_map_object_defs()";
232     char * context_prefix = "Failed reading config file: ";
233     char context[strlen(context_prefix) + strlen(path) + 1];
234     sprintf(context, "%s%s", context_prefix, path);
235     char * err_uniq = "Declaration of ID already used.";
236     FILE * file = try_fopen(path, "r", f_name);
237     uint32_t linemax = textfile_width(file);
238     char line[linemax + 1];
239     reset_err_try_fgets_counter();
240     struct EntrySkeleton ** entry_ptr_ptr = entry_start;
241     while (1)
242     {
243         int test_for_end = try_fgetc(file, f_name);
244         if (EOF == test_for_end || '\n' == test_for_end)
245         {
246             break;
247         }
248         exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
249         err_try_fgets(line, linemax, file, context, "nfi8");
250         struct EntrySkeleton * entry = try_malloc(size, f_name);
251         entry->id = atoi(line);
252         struct EntrySkeleton * entry_test = * entry_start;
253         for (; NULL != entry_test; entry_test = entry_test->next)
254         {
255             err_line(entry->id == entry_test->id, line, context, err_uniq);
256         }
257         read(line, linemax, context, entry, file);
258         entry->next = NULL;
259         * entry_ptr_ptr = entry;
260         entry_ptr_ptr = &entry->next;
261         err_try_fgets(line, linemax, file, context, "d");
262     }
263     try_fclose(file, f_name);
264     set_cleanup_flag(cleanup);
265 }
266
267
268
269 extern char * io_round()
270 {
271     char * f_name = "io_round()";
272     if (0 < world.queue_size)
273     {
274         return get_message_from_queue();
275     }
276     if (world.turn != world.last_update_turn)
277     {
278         update_worldstate_file();
279         world.last_update_turn = world.turn;
280     }
281     read_file_into_queue();
282     if (world.queue_size && '\0' != world.queue[world.queue_size - 1])
283     {
284         char * new_queue = try_malloc(world.queue_size + 1, f_name);
285         memcpy(new_queue, world.queue, world.queue_size);
286         new_queue[world.queue_size] = '\0';
287         world.queue_size++;
288         free(world.queue);
289         world.queue = new_queue;
290     }
291     return get_message_from_queue();
292 }