| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// < (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 <cstring> |
| 25 |
#include <ctime> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
#include <FL/Fl.H> |
| 32 |
#include <FL/Enumerations.H> |
| 33 |
#include <FL/Fl_Window.H> |
| 34 |
#include <FL/Fl_Button.H> |
| 35 |
#include <FL/Fl_Group.H> |
| 36 |
#include <FL/Fl_Sys_Menu_Bar.H> |
| 37 |
#include <FL/x.H> |
| 38 |
#include <FL/Fl_Help_Dialog.H> |
| 39 |
#include <FL/Fl_Menu_Item.H> |
| 40 |
#include <FL/Fl_File_Icon.H> |
| 41 |
|
| 42 |
#include "config.h" |
| 43 |
#include "flmsg_config.h" |
| 44 |
|
| 45 |
#include "flmsg.h" |
| 46 |
#include "templates.h" |
| 47 |
#include "debug.h" |
| 48 |
#include "util.h" |
| 49 |
#include "gettext.h" |
| 50 |
#include "flmsg_dialog.h" |
| 51 |
#include "flinput2.h" |
| 52 |
#include "date.h" |
| 53 |
#include "calendar.h" |
| 54 |
#include "icons.h" |
| 55 |
#include "fileselect.h" |
| 56 |
#include "wrap.h" |
| 57 |
#include "status.h" |
| 58 |
#include "parse_xml.h" |
| 59 |
#include "icons.h" |
| 60 |
|
| 61 |
#ifdef WIN32 |
| 62 |
# include "flmsgrc.h" |
| 63 |
# include "compat.h" |
| 64 |
# define dirent fl_dirent_no_thanks |
| 65 |
#endif |
| 66 |
|
| 67 |
#include <FL/filename.H> |
| 68 |
#include "dirent-check.h" |
| 69 |
|
| 70 |
#include <FL/x.H> |
| 71 |
#include <FL/Fl_Pixmap.H> |
| 72 |
#include <FL/Fl_Image.H> |
| 73 |
|
| 74 |
// csvform fields |
| 75 |
|
| 76 |
std::string csvbuffer; |
| 77 |
std::string def_csv_filename = ""; |
| 78 |
std::string base_csv_filename = ""; |
| 79 |
std::string def_csv_TemplateName = ""; |
| 80 |
|
| 81 |
std::string csv_title = ":TITLE:"; |
| 82 |
std::string csv_msg = ":mg:"; |
| 83 |
std::string csv_field; |
| 84 |
|
| 85 |
bool using_csv_template = false; |
| 86 |
|
| 87 |
void clear_csvfields() |
| 88 |
{ |
| 89 |
csv_field.clear(); |
| 90 |
} |
| 91 |
|
| 92 |
bool check_csvfields() |
| 93 |
{ |
| 94 |
return (csv_field != txt_csv_msg->buffer()->text()); |
| 95 |
} |
| 96 |
|
| 97 |
void update_csvfields() |
| 98 |
{ |
| 99 |
csv_field = txt_csv_msg->buffer()->text(); |
| 100 |
} |
| 101 |
|
| 102 |
void clear_csv_form() |
| 103 |
{ |
| 104 |
clear_csvfields(); |
| 105 |
txt_csv_msg->clear(); |
| 106 |
} |
| 107 |
|
| 108 |
void update_csvform() |
| 109 |
{ |
| 110 |
txt_csv_msg->clear(); |
| 111 |
txt_csv_msg->add(csv_field.c_str()); |
| 112 |
} |
| 113 |
|
| 114 |
void read_csvbuffer(std::string data) |
| 115 |
{ |
| 116 |
clear_csvfields(); |
| 117 |
read_header(data); |
| 118 |
csv_field = findstr(data, csv_msg); |
| 119 |
update_csvform(); |
| 120 |
} |
| 121 |
|
| 122 |
void cb_csv_new() |
| 123 |
{ |
| 124 |
if (check_csvfields()) { |
| 125 |
if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 1) { |
| 126 |
update_header(CHANGED); |
| 127 |
cb_csv_save(); |
| 128 |
} |
| 129 |
} |
| 130 |
clear_csv_form(); |
| 131 |
clear_header(); |
| 132 |
def_csv_filename = ICS_msg_dir; |
| 133 |
def_csv_filename.append("new").append(CSVFILE_EXT); |
| 134 |
show_filename(def_csv_filename); |
| 135 |
using_csv_template = false; |
| 136 |
} |
| 137 |
|
| 138 |
void cb_csv_import() |
| 139 |
{ |
| 140 |
fl_alert2("Not implemented"); |
| 141 |
} |
| 142 |
|
| 143 |
void cb_csv_export() |
| 144 |
{ |
| 145 |
fl_alert2("Not implemented"); |
| 146 |
} |
| 147 |
|
| 148 |
void cb_csv_wrap_import(std::string wrapfilename, std::string inpbuffer) |
| 149 |
{ |
| 150 |
clear_csv_form(); |
| 151 |
read_csvbuffer(inpbuffer); |
| 152 |
def_csv_filename = ICS_msg_dir; |
| 153 |
def_csv_filename.append(wrapfilename); |
| 154 |
show_filename(def_csv_filename); |
| 155 |
using_csv_template = false; |
| 156 |
} |
| 157 |
|
| 158 |
int eval_csv_fsize() |
| 159 |
{ |
| 160 |
Ccrc16 chksum; |
| 161 |
evalstr.assign("[WRAP:beg][WRAP:lf][WRAP:fn "); |
| 162 |
evalstr.append(base_csv_filename).append("]"); |
| 163 |
update_csvfields(); |
| 164 |
update_header(FROM); |
| 165 |
evalstr.append(header("<csvform>")); |
| 166 |
std::string outbuf = lineout( csv_msg, csv_field ); |
| 167 |
if (outbuf.empty()) return 0; |
| 168 |
compress_maybe( outbuf ); |
| 169 |
evalstr.append( outbuf ); |
| 170 |
evalstr.append("[WRAP:chksum ").append(chksum.scrc16(evalstr)).append("][WRAP:end]"); |
| 171 |
return evalstr.length(); |
| 172 |
} |
| 173 |
|
| 174 |
void cb_csv_wrap_export() |
| 175 |
{ |
| 176 |
if (check_csvfields()) { |
| 177 |
if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0) |
| 178 |
return; |
| 179 |
update_header(CHANGED); |
| 180 |
} |
| 181 |
update_csvfields(); |
| 182 |
if (csv_field.empty()) return; |
| 183 |
|
| 184 |
if (base_csv_filename == std::string("new").append(CSVFILE_EXT) || |
| 185 |
base_csv_filename == std::string("default").append(CSVFILE_EXT) ) |
| 186 |
if (!cb_csv_save_as()) return; |
| 187 |
|
| 188 |
std::string wrapfilename = WRAP_send_dir; |
| 189 |
wrapfilename.append(base_csv_filename); |
| 190 |
wrapfilename.append(WRAP_EXT); |
| 191 |
const char *p = FSEL::saveas( |
| 192 |
"Save as wrap file", |
| 193 |
"Wrap file\t*.{wrap,WRAP}", |
| 194 |
wrapfilename.c_str()); |
| 195 |
if (p) { |
| 196 |
std::string pext = fl_filename_ext(p); |
| 197 |
wrapfilename = p; |
| 198 |
update_header(FROM); |
| 199 |
csvbuffer.assign(header("<csvform>")); |
| 200 |
std::string outbuf = lineout( csv_msg, csv_field ); |
| 201 |
compress_maybe(outbuf); |
| 202 |
csvbuffer.append(outbuf); |
| 203 |
export_wrapfile(base_csv_filename, wrapfilename, csvbuffer, pext != WRAP_EXT); |
| 204 |
|
| 205 |
csvbuffer.assign(header("<csvform>")).append(lineout( csv_msg, csv_field )); |
| 206 |
write_csv(def_csv_filename); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void cb_csv_wrap_autosend() |
| 211 |
{ |
| 212 |
if (check_csvfields()) { |
| 213 |
if (fl_choice2("Form modified, save?", "No", "Yes", NULL) == 0) |
| 214 |
return; |
| 215 |
update_header(CHANGED); |
| 216 |
} |
| 217 |
update_csvfields(); |
| 218 |
if (csv_field.empty()) return; |
| 219 |
|
| 220 |
if (base_csv_filename == std::string("new").append(CSVFILE_EXT) || |
| 221 |
base_csv_filename == std::string("default").append(CSVFILE_EXT) ) |
| 222 |
if (!cb_csv_save_as()) return; |
| 223 |
|
| 224 |
update_header(FROM); |
| 225 |
csvbuffer.assign(header("<csvform>")); |
| 226 |
std::string outbuf = lineout( csv_msg, csv_field ); |
| 227 |
|
| 228 |
compress_maybe(outbuf); |
| 229 |
csvbuffer.append(outbuf); |
| 230 |
xfr_via_socket(base_csv_filename, csvbuffer); |
| 231 |
|
| 232 |
csvbuffer.assign(header("<csvform>")).append(lineout( csv_msg, csv_field )); |
| 233 |
write_csv(def_csv_filename); |
| 234 |
} |
| 235 |
|
| 236 |
void cb_csv_load_template() |
| 237 |
{ |
| 238 |
std::string def_csv_filename = def_csv_TemplateName; |
| 239 |
const char *p = FSEL::select( |
| 240 |
"Open template file", |
| 241 |
std::string("Template file\t*").append(CSVTEMP_EXT).c_str(), |
| 242 |
def_csv_filename.c_str()); |
| 243 |
if (p) { |
| 244 |
clear_csv_form(); |
| 245 |
read_data_file(p); |
| 246 |
def_csv_TemplateName = p; |
| 247 |
show_filename(def_csv_TemplateName); |
| 248 |
using_csv_template = true; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
void cb_csv_save_template() |
| 253 |
{ |
| 254 |
if (!using_csv_template) { |
| 255 |
cb_csv_save_as_template(); |
| 256 |
return; |
| 257 |
} |
| 258 |
std::string def_csv_filename = def_csv_TemplateName; |
| 259 |
const char *p = FSEL::saveas( |
| 260 |
"Save template file", |
| 261 |
std::string("Template file\t*").append(CSVTEMP_EXT).c_str(), |
| 262 |
def_csv_filename.c_str()); |
| 263 |
if (p) { |
| 264 |
update_header(CHANGED); |
| 265 |
update_csvfields(); |
| 266 |
csvbuffer.assign(header("<csvform>")); |
| 267 |
csvbuffer.append( lineout( csv_msg, csv_field ) ); |
| 268 |
write_csv(p); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void cb_csv_save_as_template() |
| 273 |
{ |
| 274 |
std::string def_csv_filename = def_csv_TemplateName; |
| 275 |
const char *p = FSEL::saveas( |
| 276 |
"Save as template file", |
| 277 |
std::string("Template file\t*").append(CSVTEMP_EXT).c_str(), |
| 278 |
def_csv_filename.c_str()); |
| 279 |
if (p) { |
| 280 |
const char *pext = fl_filename_ext(p); |
| 281 |
def_csv_TemplateName = p; |
| 282 |
if (strlen(pext) == 0) def_csv_TemplateName.append(CSVTEMP_EXT); |
| 283 |
remove_spaces_from_filename(def_csv_TemplateName); |
| 284 |
clear_header(); |
| 285 |
update_header(CHANGED); |
| 286 |
update_csvfields(); |
| 287 |
csvbuffer.assign(header("<csvform>")); |
| 288 |
csvbuffer.append( lineout( csv_msg, csv_field ) ); |
| 289 |
write_csv(def_csv_TemplateName); |
| 290 |
show_filename(def_csv_TemplateName); |
| 291 |
using_csv_template = true; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
void cb_csv_open() |
| 296 |
{ |
| 297 |
const char *p = FSEL::select(_("Open data file"), "csv form\t*.c2s", |
| 298 |
def_csv_filename.c_str()); |
| 299 |
if (!p) return; |
| 300 |
if (strlen(p) == 0) return; |
| 301 |
clear_csv_form(); |
| 302 |
read_data_file(p); |
| 303 |
using_csv_template = false; |
| 304 |
def_csv_filename = p; |
| 305 |
show_filename(def_csv_filename); |
| 306 |
} |
| 307 |
|
| 308 |
void write_csv(std::string s) |
| 309 |
{ |
| 310 |
if (csvbuffer.empty()) |
| 311 |
return; |
| 312 |
FILE *csvfile = fopen(s.c_str(), "w"); |
| 313 |
if (!csvfile) return; |
| 314 |
fwrite(csvbuffer.c_str(), csvbuffer.length(), 1, csvfile); |
| 315 |
fclose(csvfile); |
| 316 |
} |
| 317 |
|
| 318 |
bool cb_csv_save_as() |
| 319 |
{ |
| 320 |
const char *p; |
| 321 |
std::string newfilename; |
| 322 |
|
| 323 |
std::string name = named_file(); |
| 324 |
if (!name.empty()) { |
| 325 |
name.append(".c2s"); |
| 326 |
newfilename = ICS_msg_dir; |
| 327 |
newfilename.append(name); |
| 328 |
} else |
| 329 |
newfilename = def_csv_filename; |
| 330 |
|
| 331 |
p = FSEL::saveas(_("Save data file"), "csv form\t*.c2s", |
| 332 |
newfilename.c_str()); |
| 333 |
|
| 334 |
if (!p) return false; |
| 335 |
if (strlen(p) == 0) return false; |
| 336 |
|
| 337 |
if (progStatus.sernbr_fname) update_sernbr(); |
| 338 |
|
| 339 |
const char *pext = fl_filename_ext(p); |
| 340 |
def_csv_filename = p; |
| 341 |
if (strlen(pext) == 0) def_csv_filename.append(".c2s"); |
| 342 |
|
| 343 |
remove_spaces_from_filename(def_csv_filename); |
| 344 |
update_header(NEW); |
| 345 |
update_csvfields(); |
| 346 |
csvbuffer.assign(header("<csvform>")); |
| 347 |
csvbuffer.append( lineout( csv_msg, csv_field ) ); |
| 348 |
write_csv(def_csv_filename); |
| 349 |
|
| 350 |
using_csv_template = false; |
| 351 |
show_filename(def_csv_filename); |
| 352 |
return true; |
| 353 |
} |
| 354 |
|
| 355 |
void cb_csv_save() |
| 356 |
{ |
| 357 |
if (base_csv_filename == "new.c2s" || |
| 358 |
base_csv_filename == "default.c2s" || |
| 359 |
using_csv_template == true) { |
| 360 |
cb_csv_save_as(); |
| 361 |
return; |
| 362 |
} |
| 363 |
if (check_csvfields()) update_header(CHANGED); |
| 364 |
update_csvfields(); |
| 365 |
csvbuffer.assign(header("<csvform>")); |
| 366 |
csvbuffer.append( lineout( csv_msg, csv_field ) ); |
| 367 |
write_csv(def_csv_filename); |
| 368 |
using_csv_template = false; |
| 369 |
} |
| 370 |
|
| 371 |
void cb_csv_msg_type() |
| 372 |
{ |
| 373 |
if (tabs_msg_type->value() == tab_csv ) { |
| 374 |
show_filename(def_csv_filename); |
| 375 |
} else { |
| 376 |
show_filename(def_rg_filename); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
void cb_csv_import_data() |
| 381 |
{ |
| 382 |
def_csv_filename = CSV_dir; |
| 383 |
def_csv_filename.append("default.csv"); |
| 384 |
|
| 385 |
const char *p = FSEL::select( |
| 386 |
_("Import CSV data"), |
| 387 |
"csv file\t*.csv", |
| 388 |
def_csv_filename.c_str()); |
| 389 |
if (!p) return; |
| 390 |
if (strlen(p) == 0) return; |
| 391 |
|
| 392 |
clear_csv_form(); |
| 393 |
clear_estimate(); |
| 394 |
|
| 395 |
// open the CSV file, read all data |
| 396 |
long filesize = 0; |
| 397 |
FILE *csv_datafile; |
| 398 |
|
| 399 |
csv_datafile = fopen (p, "rb"); |
| 400 |
if (!csv_datafile) { |
| 401 |
fl_alert2(_("Could not open file")); |
| 402 |
return; |
| 403 |
} |
| 404 |
// determine its size for buffer creation |
| 405 |
fseek (csv_datafile, 0, SEEK_END); |
| 406 |
filesize = ftell (csv_datafile); |
| 407 |
// test file integrity |
| 408 |
if (filesize == 0) { |
| 409 |
fl_alert2(_("Empty file")); |
| 410 |
fclose(csv_datafile); |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
char *buff = new char[filesize + 1]; |
| 415 |
memset(buff, 0, filesize + 1); |
| 416 |
// read the entire file into the buffer |
| 417 |
fseek (csv_datafile, 0, SEEK_SET); |
| 418 |
int retval = fread (buff, 1, filesize, csv_datafile); |
| 419 |
fclose (csv_datafile); |
| 420 |
|
| 421 |
if (retval != filesize) { |
| 422 |
fl_alert2(_("Error reading file")); |
| 423 |
delete[] buff; |
| 424 |
return; |
| 425 |
} |
| 426 |
|
| 427 |
csv_field = buff; |
| 428 |
// strip any cr-lf pairs if the file was a DOS text file |
| 429 |
size_t ptr = csv_field.find("\r\n"); |
| 430 |
while (ptr != std::string::npos) { |
| 431 |
csv_field.erase(ptr, 1); |
| 432 |
ptr = csv_field.find("\r\n"); |
| 433 |
} |
| 434 |
update_csvform(); |
| 435 |
|
| 436 |
delete [] buff; |
| 437 |
|
| 438 |
using_csv_template = false; |
| 439 |
|
| 440 |
def_csv_filename = ICS_msg_dir; |
| 441 |
def_csv_filename.append(fl_filename_name(p)); |
| 442 |
size_t pext = def_csv_filename.find(".csv"); |
| 443 |
if (pext == std::string::npos) pext = def_csv_filename.find(".CSV"); |
| 444 |
if (pext != std::string::npos) def_csv_filename.erase(pext); |
| 445 |
def_csv_filename.append(".c2s"); |
| 446 |
show_filename(def_csv_filename); |
| 447 |
estimate(); |
| 448 |
} |
| 449 |
|
| 450 |
void cb_csv_export_data(bool open_file) |
| 451 |
{ |
| 452 |
update_csvfields(); |
| 453 |
if (csv_field.empty()) return; |
| 454 |
|
| 455 |
std::string fname_name = fl_filename_name(def_csv_filename.c_str()); |
| 456 |
size_t p = fname_name.rfind('.'); |
| 457 |
if (p != std::string::npos) fname_name.erase(p); |
| 458 |
|
| 459 |
std::string csv_name = CSV_dir; |
| 460 |
csv_name.append(fname_name).append(".csv"); |
| 461 |
|
| 462 |
const char *pfilename = FSEL::saveas( |
| 463 |
_("Export CSV data"), |
| 464 |
"csv file\t*.csv", |
| 465 |
csv_name.c_str()); |
| 466 |
|
| 467 |
if (!pfilename) return; |
| 468 |
if (strlen(pfilename) == 0) return; |
| 469 |
csv_name = pfilename; |
| 470 |
if (csv_name.find(".csv") == std::string::npos) |
| 471 |
csv_name.append(".csv"); |
| 472 |
|
| 473 |
FILE *csvfile = fopen(csv_name.c_str(), "w"); |
| 474 |
fprintf(csvfile,"%s", csv_field.c_str()); |
| 475 |
fclose(csvfile); |
| 476 |
|
| 477 |
if (open_file) { |
| 478 |
fl_alert2("If you modify the data you must save as CSV and\nimport the modified file"); |
| 479 |
open_url(pfilename); |
| 480 |
} else |
| 481 |
fl_alert2("Data written to %s", csv_name.c_str()); |
| 482 |
|
| 483 |
} |
| 484 |
|
| 485 |
void csv_set_fname(const char *fn) |
| 486 |
{ |
| 487 |
std::string fname = fn; |
| 488 |
size_t pext = fname.find(".csv"); |
| 489 |
if (pext == std::string::npos) pext = fname.find(".CSV"); |
| 490 |
if (pext == std::string::npos) { |
| 491 |
txt_csv_msg->clear(); |
| 492 |
return; |
| 493 |
} |
| 494 |
using_csv_template = false; |
| 495 |
def_csv_filename = ICS_msg_dir; |
| 496 |
def_csv_filename.append(fl_filename_name(fn)); |
| 497 |
def_csv_filename.find(".csv"); |
| 498 |
if (pext == std::string::npos) pext = def_csv_filename.find(".CSV"); |
| 499 |
if (pext != std::string::npos) def_csv_filename.erase(pext); |
| 500 |
def_csv_filename.append(".c2s"); |
| 501 |
show_filename(def_csv_filename); |
| 502 |
} |
| 503 |
|
| 504 |
void custom_csv_html(std::string form, std::string contents) |
| 505 |
{ |
| 506 |
std::string fname_name = fl_filename_name(def_csv_filename.c_str()); |
| 507 |
size_t p = fname_name.rfind('.'); |
| 508 |
if (p != std::string::npos) fname_name.erase(p); |
| 509 |
|
| 510 |
std::string csv_name = FLMSG_temp_dir; |
| 511 |
|
| 512 |
csv_name.append(fname_name).append(".htm"); |
| 513 |
|
| 514 |
std::string line; |
| 515 |
std::string field; |
| 516 |
std::string data; |
| 517 |
while (data[0] == '\n') data.erase(0,1); |
| 518 |
size_t comma = contents.find(","); |
| 519 |
while (contents[comma+1] == ' ') contents.erase(comma+1,1); |
| 520 |
size_t quote = contents.find("\""); |
| 521 |
size_t lfptr = contents.find("\n"); |
| 522 |
size_t tab; |
| 523 |
if (quote == comma + 1) { |
| 524 |
contents.erase(quote,1); |
| 525 |
lfptr = contents.find("\"\n"); |
| 526 |
} |
| 527 |
while (comma != std::string::npos) { |
| 528 |
field = ":"; field.append(contents.substr(0, comma)).append(":"); |
| 529 |
if (lfptr != std::string::npos) { |
| 530 |
data = contents.substr(comma + 1, lfptr - comma - 1); |
| 531 |
tab = data.find("\t"); |
| 532 |
while (tab != std::string::npos) { |
| 533 |
data.replace(tab, 1, " "); |
| 534 |
tab = data.find("\t"); |
| 535 |
} |
| 536 |
contents.erase(0, lfptr + 1); |
| 537 |
while (data[0] == '\n') data.erase(0,1); |
| 538 |
while ((quote = data.find("\"")) != std::string::npos) data.erase(quote,1); |
| 539 |
} else { |
| 540 |
data = contents.substr(comma + 1); |
| 541 |
tab = data.find("\t"); |
| 542 |
while (tab != std::string::npos) { |
| 543 |
data.replace(tab, 1, " "); |
| 544 |
tab = data.find("\t"); |
| 545 |
} |
| 546 |
while ((quote = data.find("\"")) != std::string::npos) data.erase(quote,1); |
| 547 |
contents.clear(); |
| 548 |
} |
| 549 |
lfptr = data.find("\n"); |
| 550 |
while (lfptr != std::string::npos) { |
| 551 |
data.replace(lfptr, 1, "<br>"); |
| 552 |
lfptr = data.find("\n"); |
| 553 |
} |
| 554 |
replacestr(form, field, data); |
| 555 |
comma = contents.find(","); |
| 556 |
while (contents[comma+1] == ' ') contents.erase(comma+1,1); |
| 557 |
quote = contents.find("\""); |
| 558 |
if (quote == comma + 1) { |
| 559 |
contents.erase(quote,1); |
| 560 |
lfptr = contents.find("\"\n"); |
| 561 |
} else |
| 562 |
lfptr = contents.find("\n"); |
| 563 |
} |
| 564 |
|
| 565 |
FILE *csvfile = fopen(csv_name.c_str(), "w"); |
| 566 |
fprintf(csvfile,"%s", form.c_str()); |
| 567 |
fclose(csvfile); |
| 568 |
|
| 569 |
open_url(csv_name.c_str()); |
| 570 |
} |
| 571 |
|
| 572 |
void cb_csv_html() |
| 573 |
{ |
| 574 |
update_csvfields(); |
| 575 |
if (csv_field.find("CUSTOM_FORM") == 0) { |
| 576 |
size_t plf = csv_field.find("\n"); |
| 577 |
if (plf != std::string::npos) { |
| 578 |
std::string fname = CUSTOM_dir; |
| 579 |
fname.append(csv_field.substr(12, plf - 12)); |
| 580 |
fname.append(".htm"); |
| 581 |
FILE *ffile = fopen(fname.c_str(), "r"); |
| 582 |
if (!ffile) { |
| 583 |
fname += 'l'; |
| 584 |
ffile = fopen(fname.c_str(), "r"); |
| 585 |
} |
| 586 |
if (ffile) { |
| 587 |
std::string form; |
| 588 |
char c = fgetc(ffile); |
| 589 |
while (!feof(ffile)) { |
| 590 |
form += c; |
| 591 |
c = fgetc(ffile); |
| 592 |
} |
| 593 |
fclose(ffile); |
| 594 |
custom_csv_html(form, csv_field.substr(plf+1)); |
| 595 |
return; |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
csvbuffer.assign(header("<csvform>")); |
| 601 |
csvbuffer.append( lineout( csv_msg, csv_field ) ); |
| 602 |
write_csv(def_csv_filename); |
| 603 |
|
| 604 |
std::string fname_name = fl_filename_name(def_csv_filename.c_str()); |
| 605 |
size_t p = fname_name.rfind('.'); |
| 606 |
if (p != std::string::npos) fname_name.erase(p); |
| 607 |
std::string csv_name = FLMSG_temp_dir; |
| 608 |
std::string html_text = ""; |
| 609 |
csv_name.append(fname_name); |
| 610 |
csv_name.append(".htm"); |
| 611 |
|
| 612 |
std::string csvform = csv_html_template; |
| 613 |
std::string rows; |
| 614 |
std::string row; |
| 615 |
std::string field; |
| 616 |
size_t p_eol = std::string::npos; |
| 617 |
size_t p_eof = std::string::npos; |
| 618 |
char sepchar = ','; |
| 619 |
|
| 620 |
rows.assign(csv_field); |
| 621 |
int nrows = 0, ncols = 0; |
| 622 |
int cols = 0; |
| 623 |
while (!rows.empty()) { |
| 624 |
p_eol = rows.find('\n'); |
| 625 |
nrows++; |
| 626 |
cols = 0; |
| 627 |
if (p_eol != std::string::npos) |
| 628 |
row.assign(rows.substr(0,p_eol)); |
| 629 |
else |
| 630 |
row.assign(rows); |
| 631 |
while(!row.empty()) { |
| 632 |
cols++; |
| 633 |
p_eof = row.find(sepchar); |
| 634 |
if (p_eof != std::string::npos) |
| 635 |
row.erase(0, p_eof + 1); |
| 636 |
else |
| 637 |
row.clear(); |
| 638 |
} |
| 639 |
if (cols > ncols) ncols = cols; |
| 640 |
if (p_eol != std::string::npos) |
| 641 |
rows.erase(0,p_eol+1); |
| 642 |
else |
| 643 |
rows.clear(); |
| 644 |
} |
| 645 |
|
| 646 |
char header[200]; |
| 647 |
snprintf(header, sizeof(header),"<table BORDER=1 CELLPADDING=4 CELLSPACING=0 COLS=%d>\ |
| 648 |
<tbody>\ |
| 649 |
", ncols); |
| 650 |
html_text.append(header); |
| 651 |
rows.assign(csv_field); |
| 652 |
while (!rows.empty()) { |
| 653 |
cols = 0; |
| 654 |
html_text.append("<tr>\n"); |
| 655 |
p_eol = rows.find('\n'); |
| 656 |
if (p_eol != std::string::npos) |
| 657 |
row.assign(rows.substr(0,p_eol)); |
| 658 |
else |
| 659 |
row.assign(rows); |
| 660 |
while(!row.empty()) { |
| 661 |
cols++; |
| 662 |
if (row[0] == '"') { |
| 663 |
p_eof = row.find('"', 1); |
| 664 |
field.assign(row.substr(1, p_eof - 1)); |
| 665 |
p_eof = row.find(sepchar, p_eof); |
| 666 |
} |
| 667 |
else { |
| 668 |
p_eof = row.find(sepchar); |
| 669 |
field.assign(row.substr(0, p_eof)); |
| 670 |
} |
| 671 |
if (field.empty()) field.assign("<br>"); |
| 672 |
p = 0; |
| 673 |
while((p = field.find('"', p)) != std::string::npos) |
| 674 |
field.replace(p, 1,"""); |
| 675 |
html_text.append("<td VALIGN=top>").append(field).append("</td>\n"); |
| 676 |
if (p_eof != std::string::npos) |
| 677 |
row.erase(0, p_eof + 1); |
| 678 |
else |
| 679 |
row.clear(); |
| 680 |
} |
| 681 |
while (cols < ncols) { |
| 682 |
html_text.append("<td></td>\n"); |
| 683 |
cols++; |
| 684 |
} |
| 685 |
html_text.append("</tr>\n"); |
| 686 |
if (p_eol != std::string::npos) |
| 687 |
rows.erase(0,p_eol+1); |
| 688 |
else |
| 689 |
rows.clear(); |
| 690 |
} |
| 691 |
html_text.append("</tbody>\n"); |
| 692 |
|
| 693 |
replacestr(csvform, csv_title, fname_name); |
| 694 |
replacestr(csvform, csv_msg, html_text); |
| 695 |
|
| 696 |
FILE *csvfile = fopen(csv_name.c_str(), "w"); |
| 697 |
fprintf(csvfile,"%s", csvform.c_str()); |
| 698 |
fclose(csvfile); |
| 699 |
|
| 700 |
open_url(csv_name.c_str()); |
| 701 |
} |
| 702 |
|
| 703 |
void custom_csv_text(std::string form, std::string contents) |
| 704 |
{ |
| 705 |
std::string fname_name = fl_filename_name(def_csv_filename.c_str()); |
| 706 |
size_t p = fname_name.rfind('.'); |
| 707 |
if (p != std::string::npos) fname_name.erase(p); |
| 708 |
|
| 709 |
std::string csv_name = FLMSG_temp_dir; |
| 710 |
|
| 711 |
csv_name.append(fname_name).append(".txt"); |
| 712 |
|
| 713 |
std::string line; |
| 714 |
std::string field; |
| 715 |
std::string data; |
| 716 |
while (data[0] == '\n') data.erase(0,1); |
| 717 |
size_t comma = contents.find(","); |
| 718 |
while (contents[comma+1] == ' ') contents.erase(comma+1,1); |
| 719 |
size_t quote = contents.find("\""); |
| 720 |
size_t lfptr = contents.find("\n"); |
| 721 |
size_t tab; |
| 722 |
if (quote == comma + 1) { |
| 723 |
contents.erase(quote,1); |
| 724 |
lfptr = contents.find("\"\n"); |
| 725 |
} |
| 726 |
while (comma != std::string::npos) { |
| 727 |
field = ":"; field.append(contents.substr(0, comma)).append(":"); |
| 728 |
if (lfptr != std::string::npos) { |
| 729 |
data = contents.substr(comma + 1, lfptr - comma - 1); |
| 730 |
tab = data.find("\t"); |
| 731 |
while (tab != std::string::npos) { |
| 732 |
data.replace(tab, 1, " "); |
| 733 |
tab = data.find("\t"); |
| 734 |
} |
| 735 |
contents.erase(0, lfptr + 1); |
| 736 |
while (data[0] == '\n') data.erase(0,1); |
| 737 |
while ((quote = data.find("\"")) != std::string::npos) data.erase(quote,1); |
| 738 |
} else { |
| 739 |
data = contents.substr(comma + 1); |
| 740 |
tab = data.find("\t"); |
| 741 |
while (tab != std::string::npos) { |
| 742 |
data.replace(tab, 1, " "); |
| 743 |
tab = data.find("\t"); |
| 744 |
} |
| 745 |
while ((quote = data.find("\"")) != std::string::npos) data.erase(quote,1); |
| 746 |
contents.clear(); |
| 747 |
} |
| 748 |
replacestr(form, field, data); |
| 749 |
comma = contents.find(","); |
| 750 |
while (contents[comma+1] == ' ') contents.erase(comma+1,1); |
| 751 |
quote = contents.find("\""); |
| 752 |
if (quote == comma + 1) { |
| 753 |
contents.erase(quote,1); |
| 754 |
lfptr = contents.find("\"\n"); |
| 755 |
} else |
| 756 |
lfptr = contents.find("\n"); |
| 757 |
} |
| 758 |
|
| 759 |
FILE *csvfile = fopen(csv_name.c_str(), "w"); |
| 760 |
fprintf(csvfile,"%s", form.c_str()); |
| 761 |
fclose(csvfile); |
| 762 |
|
| 763 |
open_url(csv_name.c_str()); |
| 764 |
} |
| 765 |
|
| 766 |
void cb_csv_textout() |
| 767 |
{ |
| 768 |
update_csvfields(); |
| 769 |
|
| 770 |
csvbuffer.assign(header("<csvform>")); |
| 771 |
csvbuffer.append( lineout( csv_msg, csv_field ) ); |
| 772 |
write_csv(def_csv_filename); |
| 773 |
|
| 774 |
if (csv_field.find("CUSTOM_FORM") == 0) { |
| 775 |
size_t plf = csv_field.find("\n"); |
| 776 |
if (plf != std::string::npos) { |
| 777 |
std::string fname = CUSTOM_dir; |
| 778 |
fname.append(csv_field.substr(12, plf - 12)); |
| 779 |
fname.append(".txt"); |
| 780 |
FILE *ffile = fopen(fname.c_str(), "r"); |
| 781 |
if (ffile) { |
| 782 |
std::string form; |
| 783 |
char c = fgetc(ffile); |
| 784 |
while (!feof(ffile)) { |
| 785 |
form += c; |
| 786 |
c = fgetc(ffile); |
| 787 |
} |
| 788 |
fclose(ffile); |
| 789 |
custom_csv_text(form, csv_field.substr(plf+1)); |
| 790 |
return; |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
std::string fname_name = fl_filename_name(def_csv_filename.c_str()); |
| 796 |
size_t p = fname_name.rfind('.'); |
| 797 |
if (p != std::string::npos) fname_name.erase(p); |
| 798 |
|
| 799 |
std::string csv_name = FLMSG_temp_dir; |
| 800 |
|
| 801 |
csv_name.append(fname_name).append(".txt"); |
| 802 |
|
| 803 |
std::string csvform = csv_txt_template; |
| 804 |
|
| 805 |
replacestr(csvform, csv_msg, csv_field); |
| 806 |
|
| 807 |
FILE *csvfile = fopen(csv_name.c_str(), "w"); |
| 808 |
fprintf(csvfile,"%s", csvform.c_str()); |
| 809 |
fclose(csvfile); |
| 810 |
|
| 811 |
open_url(csv_name.c_str()); |
| 812 |
} |