Debian Codegraph @

src/wx/storm_report.cxx

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 // David Freese, W1HKJ
4 //
5 // This file is part of flmsg
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig 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 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20
21 #include <stdlib.h>
22 #include <iostream>
23 #include <fstream>
24 #include <string>
25 #include <cstring>
26 #include <ctime>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <stdio.h>
30 #include <errno.h>
31
32 #include <FL/Fl.H>
33 #include <FL/Enumerations.H>
34 #include <FL/Fl_Window.H>
35 #include <FL/Fl_Button.H>
36 #include <FL/Fl_Group.H>
37 #include <FL/Fl_Sys_Menu_Bar.H>
38 #include <FL/x.H>
39 #include <FL/Fl_Help_Dialog.H>
40 #include <FL/Fl_Menu_Item.H>
41 #include <FL/Fl_File_Icon.H>
42
43 #include "config.h"
44 #include "flmsg_config.h"
45
46 #include "flmsg.h"
47 #include "templates.h"
48 #include "debug.h"
49 #include "util.h"
50 #include "gettext.h"
51 #include "flmsg_dialog.h"
52 #include "flinput2.h"
53 #include "combo.h"
54 #include "date.h"
55 #include "calendar.h"
56 #include "icons.h"
57 #include "fileselect.h"
58 #include "wrap.h"
59 #include "status.h"
60 #include "parse_xml.h"
61
62 #ifdef WIN32
63 # include "flmsgrc.h"
64 # include "compat.h"
65 # define dirent fl_dirent_no_thanks
66 #endif
67
68 #include <FL/filename.H>
69 #include "dirent-check.h"
70
71 #include <FL/x.H>
72 #include <FL/Fl_Pixmap.H>
73 #include <FL/Fl_Image.H>
74
75 #include "wx_strings.h"
76
77 std::string buffstorm;
78 std::string def_storm_filename = "";
79 std::string base_storm_filename = "";
80 std::string def_storm_TemplateName = "";
81
82 std::string storm_date;
83 std::string storm_time;
84 std::string storm_zone;
85 std::string storm_state;
86 std::string storm_county;
87 std::string storm_location;
88 bool storm_tornado;
89 std::string storm_tornado_cat;
90 bool storm_hail;
91 std::string storm_hail_size;
92 bool storm_wind;
93 std::string storm_wind_cat;
94 bool storm_flood;
95 std::string storm_flood_cat;
96 bool storm_damage;
97 std::string storm_damage_cat;
98 bool storm_snow;
99 std::string storm_snow_tot;
100 std::string storm_snow_dur;
101 bool storm_f_rain;
102 std::string storm_f_rain_tot;
103 std::string storm_f_rain_dur;
104 bool storm_h_rain;
105 std::string storm_h_rain_tot;
106 std::string storm_h_rain_dur;
107 std::string storm_o_name;
108 std::string storm_o_email;
109 std::string storm_o_tele;
110 std::string storm_o_profile;
111 std::string storm_details;
112
113 // could not use real names ... WIN32 barfs
114 enum STORM_QTYPE { B, D, S, M, T, I, F, C, O, E };
115 // bool, std::string, multi-line std::string, text, int, float, character, cOmbo, empty
116
117 struct STORM_QUAD {
118 STORM_QTYPE qtype; // type of field
119 std::string html_fld;
120 void *ptr;
121 Fl_Widget *widget; };
122
123 STORM_QUAD sQ[] = {
124 { D, ":date:", &storm_date, w_storm_date },
125 { S, ":time:", &storm_time, w_storm_time },
126 { O, ":zone:", &storm_zone, w_storm_zone },
127 { O, ":state:", &storm_state, w_storm_state },
128 { O, ":county:", &storm_county, w_storm_county },
129 { S, ":location:", &storm_location, w_storm_location },
130 { B, ":tornado:", &storm_tornado, w_storm_tornado },
131 { O, ":tornado_cat:", &storm_tornado_cat, w_storm_tornado_cat },
132 { B, ":hail:", &storm_hail, w_storm_hail },
133 { O, ":hail_size:", &storm_hail_size, w_storm_hail_size },
134 { B, ":high_wind:", &storm_wind, w_storm_wind },
135 { O, ":speed:", &storm_wind_cat, w_storm_wind_cat },
136 { B, ":flood:", &storm_flood, w_storm_flood },
137 { O, ":flood_cat:", &storm_flood_cat, w_storm_flood_cat },
138 { B, ":damage:", &storm_damage, w_storm_damage },
139 { O, ":damage_cat:", &storm_damage_cat, w_storm_damage_cat },
140 { B, ":snow:", &storm_snow, w_storm_snow },
141 { O, ":snow_tot:", &storm_snow_tot, w_storm_snow_tot },
142 { O, ":snow_dur:", &storm_snow_dur, w_storm_snow_dur },
143 { B, ":f_rain:", &storm_f_rain, w_storm_f_rain },
144 { O, ":f_rain_tot:", &storm_f_rain_tot, w_storm_f_rain_tot },
145 { O, ":f_rain_dur:", &storm_f_rain_dur, w_storm_f_rain_dur },
146 { B, ":h_rain:", &storm_h_rain, w_storm_h_rain },
147 { O, ":h_rain_tot:", &storm_h_rain_tot, w_storm_h_rain_tot },
148 { O, ":h_rain_dur:", &storm_h_rain_dur, w_storm_h_rain_dur },
149 { S, ":o_name:", &storm_o_name, w_storm_o_name },
150 { S, ":o_email:", &storm_o_email, w_storm_o_email },
151 { S, ":o_tele:", &storm_o_tele, w_storm_o_tele },
152 { O, ":o_profile:", &storm_o_profile, w_storm_o_profile },
153 { T, ":details:", &storm_details, w_storm_details }
154 };
155
156 int num_quads = (int)(sizeof(sQ) / sizeof(STORM_QUAD));
157
158 bool using_storm_template = false;
159
160 //------------------------------------------------------------------------------
161 // required to initialize the control pointers in the QUAD array
162 //------------------------------------------------------------------------------
163 static bool fields_initialized = false;
164
165 static void init_widget(std::string s, Fl_Widget * w)
166 {
167 for (int i = 0; i < num_quads; i++) {
168 if (sQ[i].html_fld == s) {
169 sQ[i].widget = w;
170 break;
171 }
172 }
173 }
174
175 static void init_widgets()
176 {
177 init_widget(":date:", (Fl_Widget *)w_storm_date);
178 init_widget(":time:", (Fl_Widget *)w_storm_time);
179 init_widget(":zone:", (Fl_Widget *)w_storm_zone);
180 init_widget(":state:", (Fl_Widget *)w_storm_state);
181 init_widget(":county:", (Fl_Widget *)w_storm_county);
182 init_widget(":location:", (Fl_Widget *)w_storm_location);
183 init_widget(":flood:", (Fl_Widget *)w_storm_flood);
184 init_widget(":flood_cat:", (Fl_Widget *)w_storm_flood_cat);
185 init_widget(":hail:", (Fl_Widget *)w_storm_hail);
186 init_widget(":hail_size:", (Fl_Widget *)w_storm_hail_size);
187 init_widget(":high_wind:", (Fl_Widget *)w_storm_wind);
188 init_widget(":speed:", (Fl_Widget *)w_storm_wind_cat);
189 init_widget(":tornado:", (Fl_Widget *)w_storm_tornado);
190 init_widget(":tornado_cat:", (Fl_Widget *)w_storm_tornado_cat);
191 init_widget(":damage:", (Fl_Widget *)w_storm_damage);
192 init_widget(":damage_cat:", (Fl_Widget *)w_storm_damage_cat);
193 init_widget(":snow:", (Fl_Widget *)w_storm_snow);
194 init_widget(":snow_tot:", (Fl_Widget *)w_storm_snow_tot);
195 init_widget(":snow_dur:", (Fl_Widget *)w_storm_snow_dur);
196 init_widget(":f_rain:", (Fl_Widget *)w_storm_f_rain);
197 init_widget(":f_rain_tot:", (Fl_Widget *)w_storm_f_rain_tot);
198 init_widget(":f_rain_dur:", (Fl_Widget *)w_storm_f_rain_dur);
199 init_widget(":h_rain:", (Fl_Widget *)w_storm_h_rain);
200 init_widget(":h_rain_tot:", (Fl_Widget *)w_storm_h_rain_tot);
201 init_widget(":h_rain_dur:", (Fl_Widget *)w_storm_h_rain_dur);
202 init_widget(":o_name:", (Fl_Widget *)w_storm_o_name);
203 init_widget(":o_email:", (Fl_Widget *)w_storm_o_email);
204 init_widget(":o_tele:", (Fl_Widget *)w_storm_o_tele);
205 init_widget(":o_profile:", (Fl_Widget *)w_storm_o_profile);
206 init_widget(":details:", (Fl_Widget *)w_storm_details);
207
208 fields_initialized = true;
209 }
210 //------------------------------------------------------------------------------
211
212 // bool, std::string, multi-line std::string, text, int, float, character, cOmbo, empty
213 //enum STORM_QTYPE { B, D, S, M, T, I, F, C, O, E };
214
215 static void clearQUAD()
216 {
217 for (int i = 0; i < num_quads; i++) {
218 switch (sQ[i].qtype) {
219 case B : (*(bool *)(sQ[i].ptr)) = false; break;
220 case S :
221 case D :
222 case M : ((std::string *)(sQ[i].ptr))->clear(); break;
223 case T : ((std::string *)(sQ[i].ptr))->clear(); break;
224 case C : (*(char *)(sQ[i].ptr)) = ' '; break;
225 case I : (*(int *)(sQ[i].ptr)) = 0; break;
226 case F : (*(float *)(sQ[i].ptr)) = 0.0; break;
227 case O : ((std::string *)(sQ[i].ptr))->clear(); break;
228 case E : return;
229 }
230 }
231 }
232
233 void clear_stormfields()
234 {
235 if (!fields_initialized) init_widgets();
236 clearQUAD();
237 }
238
239 static bool checkQUAD()
240 {
241 float f = 0;
242 char c = ' ';
243 for (int i = 0; i < num_quads; i++) {
244 switch (sQ[i].qtype) {
245 case B:
246 if (*((bool *)(sQ[i].ptr)) != ((Fl_Check_Button *)sQ[i].widget)->value()){
247 return true;
248 }
249 break;
250 case S:
251 case M:
252 if (*((std::string *)(sQ[i].ptr)) != ((Fl_Input2 *)sQ[i].widget)->value()){
253 return true;
254 }
255 break;
256 case D:
257 if (*((std::string *)(sQ[i].ptr)) != ((Fl_DateInput *)sQ[i].widget)->value()){
258 return true;
259 }
260 break;
261 case T:
262 if (*((std::string *)(sQ[i].ptr)) != ((FTextEdit *)sQ[i].widget)->buffer()->text()){
263 return true;
264 }
265 break;
266 case C:
267 c = ' ';
268 if (((Fl_Input2 *)sQ[i].widget)->value()[0])
269 c = ((Fl_Input2 *)sQ[i].widget)->value()[0];
270 if (*((char *)(sQ[i].ptr)) != c){
271 return true;
272 }
273 break;
274 case I:
275 i = 0;
276 if (((Fl_Input2 *)sQ[i].widget)->value()[0])
277 sscanf( ((Fl_Input2 *)sQ[i].widget)->value(), "%d", &i);
278 if (*((int *)(sQ[i].ptr)) != i){
279 return true;
280 }
281 break;
282 case F:
283 f = 0;
284 if (((Fl_Input2 *)sQ[i].widget)->value()[0])
285 sscanf( ((Fl_Input2 *)sQ[i].widget)->value(), "%f", &f);
286 if (*((float *)(sQ[i].ptr)) != f) {
287 return true;
288 }
289 break;
290 case O:
291 if (((Fl_ListBox *)sQ[i].widget)->index() == 0) break;
292 if (*((std::string *)(sQ[i].ptr)) != ((Fl_ListBox *)sQ[i].widget)->value()) {
293 return true;
294 }
295 break;
296 case E:
297 default: return false;
298 }
299 }
300 return false;
301 }
302
303 bool check_stormfields()
304 {
305 return checkQUAD();
306 }
307
308 static void updateQUAD()
309 {
310 float f = 0;
311 char c = ' ';
312 for (int i = 0; i < num_quads; i++) {
313 switch (sQ[i].qtype) {
314 case B:
315 *((bool *)(sQ[i].ptr)) = ((Fl_Check_Button *)sQ[i].widget)->value();
316 break;
317 case S:
318 case M:
319 *((std::string *)(sQ[i].ptr)) = ((Fl_Input2 *)sQ[i].widget)->value();
320 break;
321 case D:
322 *((std::string *)(sQ[i].ptr)) = ((Fl_DateInput *)sQ[i].widget)->value();
323 break;
324 case O:
325 { std::string s = ((Fl_ListBox *)sQ[i].widget)->value();
326 if (s.find("--") == 0)
327 ((std::string *)(sQ[i].ptr))->clear();
328 else
329 *((std::string *)(sQ[i].ptr)) = ((Fl_ListBox *)sQ[i].widget)->value();
330 }
331 break;
332 case T:
333 *((std::string *)(sQ[i].ptr)) = ((FTextEdit *)sQ[i].widget)->buffer()->text();
334 break;
335 case C:
336 c = ' ';
337 if (((Fl_Input2 *)sQ[i].widget)->value()[0])
338 c = ((Fl_Input2 *)sQ[i].widget)->value()[0];
339 *((char *)(sQ[i].ptr)) = c;
340 break;
341 case I:
342 i = 0;
343 if (((Fl_Input2 *)sQ[i].widget)->value()[0])
344 sscanf( ((Fl_Input2 *)sQ[i].widget)->value(), "%d", &i);
345 *((int *)(sQ[i].ptr)) = i;
346 break;
347 case F:
348 f = 0;
349 if (((Fl_Input2 *)sQ[i].widget)->value()[0])
350 sscanf( ((Fl_Input2 *)sQ[i].widget)->value(), "%f", &f);
351 *((float *)(sQ[i].ptr)) = f;
352 break;
353 case E:
354 default: return;
355 }
356 }
357 }
358
359 void update_stormfields()
360 {
361 if (!fields_initialized) init_widgets();
362 updateQUAD();
363 }
364
365 static void activate_listbox()
366 {
367 if (w_storm_flood->value())
368 w_storm_flood_cat->activate();
369 else
370 w_storm_flood_cat->deactivate();
371 if (w_storm_hail->value())
372 w_storm_hail_size->activate();
373 else
374 w_storm_hail_size->deactivate();
375 if (w_storm_wind->value())
376 w_storm_wind_cat->activate();
377 else
378 w_storm_wind_cat->deactivate();
379 if (w_storm_tornado->value())
380 w_storm_tornado_cat->activate();
381 else
382 w_storm_tornado_cat->deactivate();
383 if (w_storm_damage->value())
384 w_storm_damage_cat->activate();
385 else
386 w_storm_damage_cat->deactivate();
387 if (w_storm_snow->value()) {
388 w_storm_snow_tot->activate();
389 w_storm_snow_dur->activate();
390 } else {
391 w_storm_snow_tot->deactivate();
392 w_storm_snow_dur->deactivate();
393 }
394 if (w_storm_f_rain->value()) {
395 w_storm_f_rain_tot->activate();
396 w_storm_f_rain_dur->activate();
397 } else {
398 w_storm_f_rain_tot->deactivate();
399 w_storm_f_rain_dur->deactivate();
400 }
401 if (w_storm_h_rain->value()) {
402 w_storm_h_rain_tot->activate();
403 w_storm_h_rain_dur->activate();
404 } else {
405 w_storm_h_rain_tot->deactivate();
406 w_storm_h_rain_dur->deactivate();
407 }
408 }
409
410 static void updateFORM()
411 {
412 char val[20];
413 for (int i = 0; i < num_quads; i++ ) {
414 switch (sQ[i].qtype) {
415 case B:
416 ((Fl_Check_Button *)sQ[i].widget)->value(*((bool *)(sQ[i].ptr)));
417 break;
418 case S:
419 case M:
420 ((Fl_Input2 *)sQ[i].widget)->value(((std::string *)(sQ[i].ptr))->c_str());
421 break;
422 case D:
423 ((Fl_DateInput *)sQ[i].widget)->value(((std::string *)(sQ[i].ptr))->c_str());
424 break;
425 case O:
426 if (((std::string *)(sQ[i].ptr))->empty())
427 ((Fl_ListBox *)sQ[i].widget)->index(0);
428 else
429 ((Fl_ListBox *)sQ[i].widget)->put_value(((std::string *)(sQ[i].ptr))->c_str());
430 break;
431 case T:
432 ((FTextEdit *)sQ[i].widget)->clear();
433 ((FTextEdit *)sQ[i].widget)->add(((std::string *)(sQ[i].ptr))->c_str());
434 break;
435 case C:
436 val[0] = *((char *)(sQ[i].ptr));
437 val[1] = 0;
438 ((Fl_Input2 *)sQ[i].widget)->value(val);
439 break;
440 case I:
441 if (*((int *)(sQ[i].ptr)) == 0)
442 ((Fl_Input2 *)sQ[i].widget)->value("");
443 else {
444 snprintf(val, sizeof(val), "%d", *((int *)(sQ[i].ptr)));
445 ((Fl_Input2 *)sQ[i].widget)->value(val);
446 }
447 break;
448 case F:
449 snprintf(val, sizeof(val), "%f", *((float *)(sQ[i].ptr)));
450 ((Fl_Input2 *)sQ[i].widget)->value(val);
451 break;
452 case E:
453 default: return;
454 }
455 }
456 }
457
458 void update_stormform()
459 {
460 if (!fields_initialized) init_widgets();
461 updateFORM();
462 activate_listbox();
463 }
464
465 void clear_storm_form()
466 {
467 clear_stormfields();
468 update_stormform();
469 w_storm_state->index(0);
470 set_storm_counties(0);
471 w_storm_o_profile->index(0);
472 w_storm_location->index(0);
473 w_storm_zone->index(0);
474 }
475
476 static std::string mbuff;
477
478 static void make_buffQUAD()
479 {
480 std::string one = "1"; std::string zero = "0";
481 std::string sval = " ";
482 char szval[20];
483 for (int i = 0; i < num_quads; i++) {
484 switch (sQ[i].qtype) {
485 case B:
486 if (*((bool *)(sQ[i].ptr)) == true)
487 mbuff.append( lineout( sQ[i].html_fld, *((bool *)(sQ[i].ptr)) ? one : zero));
488 break;
489 case O: case S: case D: case M:
490 if (((std::string *)(sQ[i].ptr))->length())
491 mbuff.append( lineout( sQ[i].html_fld, *((std::string *)(sQ[i].ptr))));
492 break;
493 case T:
494 mbuff.append( lineout( sQ[i].html_fld, *((std::string *)(sQ[i].ptr))));
495 break;
496 case C:
497 if ((*(char *)(sQ[i].ptr)) != 0 && *((char *)(sQ[i].ptr)) != ' ') {
498 sval = " ";
499 sval[0] = *((char *)(sQ[i].ptr));
500 mbuff.append( lineout( sQ[i].html_fld, sval));
501 }
502 break;
503 case I:
504 if (*((int*)(sQ[i].ptr)) > 0) {
505 snprintf(szval, sizeof(szval), "%d", *((int *)(sQ[i].ptr)) );
506 sval = szval;
507 mbuff.append( lineout( sQ[i].html_fld, sval) );
508 }
509 break;
510 case F:
511 if (*((float *)(sQ[i].ptr)) > 0) {
512 snprintf(szval, sizeof(szval), "%f", *((float *)(sQ[i].ptr)));
513 sval = szval;
514 mbuff.append( lineout( sQ[i].html_fld, sval) );
515 }
516 break;
517 case E:
518 default: return;
519 }
520 }
521 }
522
523 void make_buff_storm(bool compress = false)
524 {
525 mbuff.clear();
526 make_buffQUAD();
527 if (compress) compress_maybe(mbuff);
528 buffstorm.append(mbuff);
529 }
530
531 static void readQUAD(std::string data)
532 {
533 float f;
534 for (int i = 0; i < num_quads; i++) {
535 switch (sQ[i].qtype) {
536 case B:
537 *((bool *)(sQ[i].ptr)) = (findstr( data, sQ[i].html_fld ) == "1");
538 break;
539 case D: case O: case S: case M:
540 *((std::string *)(sQ[i].ptr)) = findstr( data, sQ[i].html_fld );
541 break;
542 case T:
543 *((std::string *)(sQ[i].ptr)) = findstr( data, sQ[i].html_fld );
544 break;
545 case C:
546 *((char *)(sQ[i].ptr)) = findstr( data, sQ[i].html_fld )[0];
547 break;
548 case I:
549 i = 0;
550 sscanf( findstr( data, sQ[i].html_fld ).c_str(), "%d", &i);
551 *((int *)(sQ[i].ptr)) = i;
552 break;
553 case F:
554 f = 0;
555 sscanf( findstr( data, sQ[i].html_fld ).c_str(), "%f", &f);
556 *((float *)(sQ[i].ptr)) = f;
557 break;
558 case E:
559 default: return;
560 }
561 }
562 }
563
564 void read_storm_buffer(std::string data)
565 {
566 clear_stormfields();
567 read_header(data);
568
569 readQUAD (data);
570 update_stormform();
571 int idx = w_storm_state->index();
572 set_storm_counties(idx);
573 w_storm_county->value( ((std::string *)(sQ[4].ptr))->c_str() );
574 }
575
576 void cb_storm_new()
577 {
578 if (check_stormfields()) {
579 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) {
580 update_header(CHANGED);
581 cb_storm_save();
582 }
583 }
584 clear_storm_form();
585 clear_header();
586 def_storm_filename = ICS_msg_dir;
587 def_storm_filename.append("new").append(FSTRM_EXT);
588 show_filename(def_storm_filename);
589 using_storm_template = false;
590 }
591
592 void cb_storm_import()
593 {
594 fl_alert2("Not implemented");
595 }
596
597 void cb_storm_export()
598 {
599 fl_alert2("Not implemented");
600 }
601
602 void cb_storm_wrap_import(std::string wrapfilename, std::string inpbuffer)
603 {
604 clear_storm_form();
605 read_storm_buffer(inpbuffer);
606 def_storm_filename = ICS_msg_dir;
607 def_storm_filename.append(wrapfilename);
608 show_filename(def_storm_filename);
609 using_storm_template = false;
610 }
611
612 int eval_storm_fsize()
613 {
614 Ccrc16 chksum;
615 evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn ");
616 evalstr.append(base_storm_filename).append("]");
617 update_stormfields();
618 update_header(FROM);
619 evalstr.append(header("<storm_wx>"));
620 buffstorm.clear();
621 make_buff_storm(true);
622 if (buffstorm.empty()) return 0;
623 compress_maybe( buffstorm );
624 evalstr.append( buffstorm );
625 evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]");
626 return evalstr.length();
627 }
628
629 void cb_storm_wrap_export()
630 {
631 if (check_stormfields()) {
632 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
633 return;
634 update_header(CHANGED);
635 }
636 update_stormfields();
637
638 if (base_storm_filename == std::string("new").append(FSTRM_EXT) ||
639 base_storm_filename == std::string("default").append(FSTRM_EXT) )
640 if (!cb_storm_save_as()) return;
641
642 std::string wrapfilename = WRAP_send_dir;
643 wrapfilename.append(base_storm_filename);
644 wrapfilename.append(".wrap");
645 const char *p = FSEL::saveas(
646 "Save as wrap file",
647 "Wrap file\t*.{wrap,WRAP}",
648 wrapfilename.c_str());
649 if (p) {
650 std::string pext = fl_filename_ext(p);
651 wrapfilename = p;
652
653 update_header(FROM);
654 buffstorm.assign(header("<storm_wx>"));
655 make_buff_storm(true);
656 export_wrapfile(base_storm_filename, wrapfilename, buffstorm, pext != ".wrap");
657
658 buffstorm.assign(header("<storm_wx>"));
659 make_buff_storm(false);
660 write_storm(def_storm_filename);
661 }
662 }
663
664 void cb_storm_wrap_autosend()
665 {
666 if (check_stormfields()) {
667 if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0)
668 return;
669 update_header(CHANGED);
670 }
671 update_stormfields();
672
673 if (base_storm_filename == std::string("new").append(FSTRM_EXT) ||
674 base_storm_filename == std::string("default").append(FSTRM_EXT) )
675 if (!cb_storm_save_as()) return;
676
677 update_header(FROM);
678 buffstorm.assign(header("<storm_wx>"));
679 make_buff_storm(true);
680 xfr_via_socket(base_storm_filename, buffstorm);
681
682 buffstorm.assign(header("<storm_wx>"));
683 make_buff_storm(false);
684 write_storm(def_storm_filename);
685 }
686
687 void cb_storm_load_template()
688 {
689 std::string def_storm_filename = def_storm_TemplateName;
690 const char *p = FSEL::select(
691 "Open template file",
692 std::string("Template file\t*").append(TSTRM_EXT).c_str(),
693 def_storm_filename.c_str());
694 if (p) {
695 clear_storm_form();
696 read_data_file(p);
697 def_storm_TemplateName = p;
698 show_filename(def_storm_TemplateName);
699 using_storm_template = true;
700 }
701 }
702
703 void cb_storm_save_template()
704 {
705 if (!using_storm_template) {
706 cb_storm_save_as_template();
707 return;
708 }
709 std::string def_storm_filename = def_storm_TemplateName;
710 const char *p = FSEL::saveas(
711 "Save template file",
712 std::string("Template file\t*").append(TSTRM_EXT).c_str(),
713 def_storm_filename.c_str());
714 if (p) {
715 update_header(CHANGED);
716 update_stormfields();
717 buffstorm.assign(header("<storm_wx>"));
718 make_buff_storm();
719 write_storm(p);
720 }
721 }
722
723 void cb_storm_save_as_template()
724 {
725 std::string def_storm_filename = def_storm_TemplateName;
726 const char *p = FSEL::saveas(
727 "Save as template file",
728 std::string("Template file\t*").append(TSTRM_EXT).c_str(),
729 def_storm_filename.c_str());
730 if (p) {
731 const char *pext = fl_filename_ext(p);
732 def_storm_TemplateName = p;
733 if (strlen(pext) == 0) def_storm_TemplateName.append(TSTRM_EXT);
734 remove_spaces_from_filename(def_storm_TemplateName);
735
736 clear_header();
737 update_header(CHANGED);
738 update_stormfields();
739 buffstorm.assign(header("<storm_wx>"));
740 make_buff_storm();
741 write_storm(def_storm_TemplateName);
742
743 show_filename(def_storm_TemplateName);
744 using_storm_template = true;
745 }
746 }
747
748 void cb_storm_open()
749 {
750 const char *p = FSEL::select(
751 _("Open data file"),
752 std::string("ICS-storm\t*").append(FSTRM_EXT).c_str(),
753 def_storm_filename.c_str());
754 if (!p) return;
755 if (strlen(p) == 0) return;
756 clear_storm_form();
757 read_data_file(p);
758 using_storm_template = false;
759 def_storm_filename = p;
760 show_filename(def_storm_filename);
761 }
762
763 void write_storm(std::string s)
764 {
765 FILE *filestorm = fopen(s.c_str(), "w");
766 if (!filestorm) return;
767
768 fwrite(buffstorm.c_str(), buffstorm.length(), 1, filestorm);
769 fclose(filestorm);
770 }
771
772
773 bool cb_storm_save_as()
774 {
775 const char *p;
776 std::string newfilename;
777
778 std::string name = named_file();
779 if (!name.empty()) {
780 name.append(FSTRM_EXT);
781 newfilename = ICS_msg_dir;
782 newfilename.append(name);
783 } else
784 newfilename = def_storm_filename;
785
786 p = FSEL::saveas(
787 _("Save data file"),
788 std::string("ICS-storm\t*").append(FSTRM_EXT).c_str(),
789 newfilename.c_str());
790
791 if (!p) return false;
792 if (strlen(p) == 0) return false;
793
794 if (progStatus.sernbr_fname) update_sernbr();
795
796 const char *pext = fl_filename_ext(p);
797 def_storm_filename = p;
798 if (strlen(pext) == 0) def_storm_filename.append(FSTRM_EXT);
799
800 remove_spaces_from_filename(def_storm_filename);
801
802 update_header(NEW);
803 update_stormfields();
804 buffstorm.assign(header("<storm_wx>"));
805 make_buff_storm();
806 write_storm(def_storm_filename);
807
808 using_storm_template = false;
809 show_filename(def_storm_filename);
810 return true;
811 }
812
813 void cb_storm_save()
814 {
815 if (base_storm_filename == std::string("new").append(FSTRM_EXT) ||
816 base_storm_filename == std::string("default").append(FSTRM_EXT) ||
817 using_storm_template == true) {
818 cb_storm_save_as();
819 return;
820 }
821
822 if (check_stormfields()) update_header(CHANGED);
823 update_stormfields();
824 buffstorm.assign(header("<storm_wx>"));
825 make_buff_storm();
826 write_storm(def_storm_filename);
827
828 using_storm_template = false;
829 }
830
831 static std::string X = "x"; std::string SP = "_";
832 static std::string sval = " ";
833 static std::string del = "";
834
835 static void quad_to_html(std::string &target)
836 {
837 char szval[20];
838 for (int i = 0; i < num_quads; i++) {
839 if (sQ[i].qtype == B)
840 replacestr(target, sQ[i].html_fld, *((bool *)(sQ[i].ptr)) ? X : SP);
841 else if (sQ[i].qtype == O ) {
842 std::string s = *((std::string *)(sQ[i].ptr));
843 if (sQ[i].html_fld == ":state:") {
844 if (s.length() > 1) s.erase(2);
845 replacestr(target, sQ[i].html_fld, s);
846 } else if (sQ[i].html_fld == ":zone") {
847 size_t cp = s.find(',');
848 s.erase(cp);
849 replacestr(target, sQ[i].html_fld, s);
850 } else if (s.find("--") == 0) {
851 replacestr(target, sQ[i].html_fld, del);
852 } else {
853 if (s.length())
854 replacestr(target, sQ[i].html_fld, s);
855 else replacestr(target, sQ[i].html_fld, del);
856 }
857 } else if (sQ[i].qtype == S || sQ[i].qtype == M || sQ[i].qtype == D) {
858 if (((std::string *)(sQ[i].ptr))->length())
859 replacestr(target, sQ[i].html_fld, *((std::string *)(sQ[i].ptr)));
860 else replacestr(target, sQ[i].html_fld, del);
861 } else if (sQ[i].qtype == M) {
862 sval = *((std::string *)(sQ[i].ptr));
863 size_t np = std::string::npos;
864 while ( (np = sval.find("\n")) != std::string::npos)
865 sval.replace(np, 1, "<br>");
866 replacestr(target, sQ[i].html_fld, sval);
867 } else if (sQ[i].qtype == T)
868 replacestr(target, sQ[i].html_fld, *((std::string *)(sQ[i].ptr)));
869 else if (sQ[i].qtype == C) {
870 sval = " ";
871 sval[0] = *((char *)(sQ[i].ptr));
872 replacestr(target, sQ[i].html_fld, sval);
873 }
874 else if (sQ[i].qtype == I) {
875 if (*((int *)(sQ[i].ptr)) > 0) {
876 snprintf(szval, sizeof(szval), "%d", *((int *)(sQ[i].ptr)) );
877 sval = szval;
878 } else sval.clear();
879 replacestr(target, sQ[i].html_fld, sval);
880 }
881 else if (sQ[i].qtype == F) {
882 if (*((float *)(sQ[i].ptr)) != 0) {
883 snprintf(szval, sizeof(szval), "%f", *((float *)(sQ[i].ptr)));
884 sval = szval;
885 } else sval.clear();
886 replacestr(target, sQ[i].html_fld, sval);
887 }
888 }
889 }
890
891 void cb_storm_html()
892 {
893 std::string name_name = fl_filename_name(def_storm_filename.c_str());
894 size_t p = name_name.rfind('.');
895 if (p != std::string::npos) name_name.erase(p);
896
897 std::string storm_rptsta = ICS_dir;
898 storm_rptsta.append(name_name);
899 storm_rptsta.append(".html");
900
901 update_stormfields();
902 std::string formstorm = storm_html_template;
903
904 replacestr(formstorm, TITLE, name_name);
905
906 quad_to_html (formstorm);
907
908 FILE *filestorm = fopen(storm_rptsta.c_str(), "w");
909 fprintf(filestorm,"%s", formstorm.c_str());
910 fclose(filestorm);
911
912 open_url(storm_rptsta.c_str());
913 }
914
915 static void quad_to_text( std::string &target)
916 {
917 char szval[20];
918 for (int i = 0; i < num_quads; i++) {
919 if (sQ[i].qtype == B)
920 replacestr(target, sQ[i].html_fld, *((bool *)(sQ[i].ptr)) ? X : del);
921 else if (sQ[i].qtype == O ) {
922 std::string s = *((std::string *)(sQ[i].ptr));
923 if (sQ[i].html_fld == ":state:") {
924 if (s.length() > 1) s.erase(2);
925 replacestr(target, sQ[i].html_fld, s);
926 } else if (sQ[i].html_fld == ":zone") {
927 size_t cp = s.find(',');
928 s.erase(cp);
929 replacestr(target, sQ[i].html_fld, s);
930 } else if (s.find("--") == 0) {
931 replacestr(target, sQ[i].html_fld, del);
932 } else {
933 if (s.length())
934 replacestr(target, sQ[i].html_fld, s);
935 else replacestr(target, sQ[i].html_fld, del);
936 }
937 } else if (sQ[i].qtype == S || sQ[i].qtype == M || sQ[i].qtype == D) {
938 if (((std::string *)(sQ[i].ptr))->length())
939 replacestr(target, sQ[i].html_fld, *((std::string *)(sQ[i].ptr)));
940 else replacestr(target, sQ[i].html_fld, del);
941 } else if (sQ[i].qtype == T) {
942 if (((std::string *)(sQ[i].ptr))->length())
943 replacestr(target, sQ[i].html_fld, *((std::string *)(sQ[i].ptr)));
944 else replacestr(target, sQ[i].html_fld, del);
945 } else if (sQ[i].qtype == C) {
946 sval = " ";
947 sval[0] = *((char *)(sQ[i].ptr));
948 if (sval[0] != 0 && sval[0] != ' ')
949 replacestr(target, sQ[i].html_fld, sval);
950 else replacestr(target, sQ[i].html_fld, del);
951 }
952 else if (sQ[i].qtype == I) {
953 if (*((int *)(sQ[i].ptr)) > 0) {
954 snprintf(szval, sizeof(szval), "%d", *((int *)(sQ[i].ptr)) );
955 sval = szval;
956 replacestr(target, sQ[i].html_fld, sval);
957 } else replacestr(target, sQ[i].html_fld, del);
958 }
959 else if (sQ[i].qtype == F) {
960 if (*((float *)(sQ[i].ptr)) != 0) {
961 snprintf(szval, sizeof(szval), "%f", *((float *)(sQ[i].ptr)));
962 sval = szval;
963 replacestr(target, sQ[i].html_fld, sval);
964 } else replacestr(target, sQ[i].html_fld, del);
965 }
966 }
967 }
968
969 void cb_storm_textout()
970 {
971 std::string storm_rptsta = ICS_dir;
972 storm_rptsta.append("storm.txt");
973
974 update_stormfields();
975 std::string formstorm = storm_text_template;
976
977 quad_to_text (formstorm);
978
979 FILE *filestorm = fopen(storm_rptsta.c_str(), "w");
980 fprintf(filestorm,"%s", formstorm.c_str());
981 fclose(filestorm);
982
983 open_url(storm_rptsta.c_str());
984 }
985