1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
|
/* Copyright (C) 2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdio.h>
#include <glib.h>
#include "myx_public_interface.h"
#include "myx_shared_util_functions.h"
static MYX_TABLE_EXPORTER_INFO *make_exporter_info(MYX_TABLE_EXPORTER *exporter)
{
MYX_TABLE_EXPORTER_INFO *info= g_new0(MYX_TABLE_EXPORTER_INFO, 1);
unsigned int i;
info->te= exporter;
for (i= 0; info->te->options[i].name; i++);
info->option_values= g_new0(char*, i);
for (i= 0; info->te->options[i].name; i++)
info->option_values[i]= g_strdup(info->te->options[i].value);
return info;
}
static void add_tabledef(MYX_TABLE_EXPORTER_INFO *info,
unsigned int columns_num, MYX_TE_COLUMN *columns)
{
unsigned int i;
info->table_levels++;
info->columns_num= g_realloc(info->columns_num, sizeof(unsigned int)*info->table_levels);
info->columns= g_realloc(info->columns, sizeof(MYX_TE_COLUMN)*info->table_levels);
info->columns_num[info->table_levels-1]= columns_num;
info->columns[info->table_levels-1]= g_new(MYX_TE_COLUMN, columns_num);
for (i= 0; i < columns_num; i++)
{
info->columns[info->table_levels-1][i]= columns[i];
info->columns[info->table_levels-1][i].name= g_strdup(columns[i].name);
}
}
static void free_exporter_info(MYX_TABLE_EXPORTER_INFO *info)
{
if (info->file_stream)
fclose(info->file_stream);
g_free(info);
}
// ------------------------------------------------------------
// CSV exporter
// ------------------------------------------------------------
//typedef struct {
//} MYX_CSV_EXPORTER_INFO;
static MYX_TABLE_EXPORTER_INFO *csv_exporter_init();
static void csv_exporter_free(MYX_TABLE_EXPORTER_INFO *info);
static int csv_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename);
static void csv_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text);
static void csv_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns);
static void csv_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self);
static void csv_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text);
static void csv_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self);
static void csv_exporter_end(MYX_TABLE_EXPORTER_INFO *self);
static MYX_TE_OPTION csv_exporter_options[]=
{
{"Separator", ",", "Separator for column values.", MYX_TEOP_STRING},
{NULL, NULL, NULL, 0}
};
MYX_TABLE_EXPORTER CSV_TABLE_EXPORTER=
{
"CSV",
"Generates a CSV (Comma Separated Values) File",
"Comma Separated Values File",
"csv",
csv_exporter_init,
csv_exporter_free,
csv_exporter_setup,
csv_exporter_begin,
csv_exporter_table_setup,
csv_exporter_table_header,
csv_exporter_columns,
csv_exporter_table_footer,
csv_exporter_end,
csv_exporter_options
};
static MYX_TABLE_EXPORTER_INFO *csv_exporter_init()
{
MYX_TABLE_EXPORTER_INFO *info;
info= make_exporter_info(&CSV_TABLE_EXPORTER);
// info->priv= g_new0(MYX_CSV_EXPORTER_INFO, 1);
return info;
}
static void csv_exporter_free(MYX_TABLE_EXPORTER_INFO *self)
{
// g_free(self->priv);
free_exporter_info(self);
}
static int csv_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename)
{
self->file_stream= myx_fopen(filename, "w+");
if (!self->file_stream)
return -1;
return 0;
}
static void csv_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text)
{
}
static void csv_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns)
{
add_tabledef(self, columns_num, columns);
}
static void csv_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self)
{
unsigned int i;
self->current_level++;
if (self->current_level > 1)
{
g_warning("Multi-level export not supported for csv");
return;
}
//fprintf(self->file_stream, "# ");
for (i= 0; i < self->columns_num[0]; i++)
{
char *s;
if (self->columns[0][i].name)
s= str_g_replace(g_strdup(self->columns[0][i].name), "\"", "\"\"");
else
s= NULL;
if (i > 0)
fprintf(self->file_stream, "%s\"%s\"", self->option_values[0], s?s:"");
else
fprintf(self->file_stream, "\"%s\"", s?s:"");
g_free(s);
}
fprintf(self->file_stream, "\n");
}
static void csv_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text)
{
unsigned int i;
for (i= 0; i < self->columns_num[0]; i++)
{
if(self->columns[0][i].ctype == MYX_TE_COL_STRING || self->columns[0][i].ctype == MYX_TE_COL_DATE)
{
char *s;
if (text[i])
s= str_g_replace(g_strdup(text[i]), "\"", "\"\"");
else
s= NULL;
if (i > 0)
fprintf(self->file_stream, "%s\"%s\"", self->option_values[0], s?s:"");
else
fprintf(self->file_stream, "\"%s\"", s?s:"");
g_free(s);
}
else
{
const char *s;
if (text[i])
s= text[i];
else
s= "";
if (i > 0)
fprintf(self->file_stream, "%s%s", self->option_values[0], s);
else
fprintf(self->file_stream, "%s", s);
}
}
fprintf(self->file_stream, "\n");
}
static void csv_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self)
{
self->current_level--;
}
static void csv_exporter_end(MYX_TABLE_EXPORTER_INFO *self)
{
}
// ------------------------------------------------------------
// HTML exporter
// ------------------------------------------------------------
typedef struct {
int row_num;
} MYX_HTML_EXPORTER_INFO;
static MYX_TABLE_EXPORTER_INFO *html_exporter_init();
static void html_exporter_free(MYX_TABLE_EXPORTER_INFO *info);
static int html_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename);
static void html_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text);
static void html_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns);
static void html_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self);
static void html_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text);
static void html_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self);
static void html_exporter_end(MYX_TABLE_EXPORTER_INFO *self);
static MYX_TE_OPTION html_exporter_options[]=
{
{"Even Row Color", "", "Color for even rows.", MYX_TEOP_COLOR},
{"Odd Row Color", "", "Color for odd rows.", MYX_TEOP_COLOR},
{"HTML Table Attributes", "border=1 cellspacing=1 cellpadding=0", "Attributes that will be used in the generated HTML table.", MYX_TEOP_STRING},
{NULL, NULL, NULL, 0}
};
MYX_TABLE_EXPORTER HTML_TABLE_EXPORTER=
{
"HTML",
"Generates a HTML File",
"HTML File",
"html",
html_exporter_init,
html_exporter_free,
html_exporter_setup,
html_exporter_begin,
html_exporter_table_setup,
html_exporter_table_header,
html_exporter_columns,
html_exporter_table_footer,
html_exporter_end,
html_exporter_options
};
static MYX_TABLE_EXPORTER_INFO *html_exporter_init()
{
MYX_TABLE_EXPORTER_INFO *info;
info= make_exporter_info(&HTML_TABLE_EXPORTER);
info->priv= g_new0(MYX_HTML_EXPORTER_INFO, 1);
return info;
}
static void html_exporter_free(MYX_TABLE_EXPORTER_INFO *info)
{
g_free(info->priv);
free_exporter_info(info);
}
static int html_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename)
{
self->file_stream= myx_fopen(filename, "w+");
if (!self->file_stream)
return -1;
return 0;
}
static void html_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text)
{
fprintf(self->file_stream, "<html>\n");
fprintf(self->file_stream, "<head>\n");
fprintf(self->file_stream, "<title>%s</title>\n", text);
fprintf(self->file_stream, "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n");
fprintf(self->file_stream, "</head>\n");
fprintf(self->file_stream, "<body><h1>%s</h1>\n", text);
}
static void html_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns)
{
add_tabledef(self, columns_num, columns);
}
static void html_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self)
{
unsigned int i;
const char *table_extra= "";
self->current_level++;
if (self->current_level > 1)
{
fprintf(self->file_stream, "<tr><td colspan=%i>\n",
self->columns_num[self->current_level-2]);
table_extra= "width=100% ";
}
fprintf(self->file_stream, "<table %s%s><tr>\n", table_extra, self->option_values[2]);
for (i= 0; i < self->columns_num[self->current_level-1]; i++)
{
fprintf(self->file_stream, "<th>%s</th>", self->columns[self->current_level-1][i].name);
}
fprintf(self->file_stream, "</tr>\n");
((MYX_HTML_EXPORTER_INFO*)self->priv)->row_num= 0;
}
static void html_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text)
{
unsigned int i;
const char *color;
if (((MYX_HTML_EXPORTER_INFO*)self->priv)->row_num%2)
color= self->option_values[1];
else
color= self->option_values[0];
if (color && *color)
fprintf(self->file_stream, "<tr bgcolor=\"%s\">\n", color);
else
fprintf(self->file_stream, "<tr>\n");
for (i= 0; i < self->columns_num[self->current_level-1]; i++)
{
char *tmp;
if (text[i])
tmp= escape_html_entities(text[i]);
else
tmp= "";
fprintf(self->file_stream, "<td>%s</td>", tmp);
if (text[i])
g_free(tmp);
}
fprintf(self->file_stream, "</tr>\n");
}
static void html_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, "</table>\n");
if (self->current_level > 1)
fprintf(self->file_stream, "</td></tr>\n");
self->current_level--;
}
static void html_exporter_end(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, "</body></html>\n");
}
// ------------------------------------------------------------
// XML exporter
// ------------------------------------------------------------
typedef struct {
char *dtd_filename;
int dtd_written;
} MYX_XML_EXPORTER_INFO;
static MYX_TABLE_EXPORTER_INFO *xml_exporter_init();
static void xml_exporter_free(MYX_TABLE_EXPORTER_INFO *info);
static int xml_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename);
static void xml_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text);
static void xml_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns);
static void xml_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self);
static void xml_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text);
static void xml_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self);
static void xml_exporter_end(MYX_TABLE_EXPORTER_INFO *self);
static MYX_TE_OPTION xml_exporter_options[]=
{
{"Generate DTD", "1", "Write the DTD for the generated XML file.", MYX_TEOP_BOOL},
{"Record Tag", "row", "Name of tag for record delimiter.", MYX_TEOP_STRING},
{"Root Attributes", "", "Attribute name/value pairs for the root tag.", MYX_TEOP_STRINGLIST},
{NULL, NULL, NULL, 0}
};
MYX_TABLE_EXPORTER XML_TABLE_EXPORTER=
{
"XML",
"Generates a XML File",
"XML File",
"xml",
xml_exporter_init,
xml_exporter_free,
xml_exporter_setup,
xml_exporter_begin,
xml_exporter_table_setup,
xml_exporter_table_header,
xml_exporter_columns,
xml_exporter_table_footer,
xml_exporter_end,
xml_exporter_options
};
static MYX_TABLE_EXPORTER_INFO *xml_exporter_init()
{
MYX_TABLE_EXPORTER_INFO *info;
info= make_exporter_info(&XML_TABLE_EXPORTER);
info->priv= g_new0(MYX_XML_EXPORTER_INFO, 1);
return info;
}
static void xml_exporter_free(MYX_TABLE_EXPORTER_INFO *self)
{
MYX_XML_EXPORTER_INFO* xi= (MYX_XML_EXPORTER_INFO*)self->priv;
if(xi->dtd_filename)
g_free(xi->dtd_filename);
g_free(self->priv);
free_exporter_info(self);
}
static int xml_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename)
{
MYX_XML_EXPORTER_INFO* xi= (MYX_XML_EXPORTER_INFO*)self->priv;
char *ptr;
self->file_stream= myx_fopen(filename, "w+");
if(!self->file_stream)
{
return -1;
}
ptr= strrchr(filename,'.');
if (ptr)
xi->dtd_filename= g_strdup_printf("%.*s.dtd", ptr-filename, filename);
else
xi->dtd_filename= g_strdup_printf("%s.dtd", filename);
return 0;
}
static void xml_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text)
{
const char *fn;
fn= strrchr(((MYX_XML_EXPORTER_INFO*)self->priv)->dtd_filename, MYX_PATH_SEPARATOR);
if (!fn)
fn= ((MYX_XML_EXPORTER_INFO*)self->priv)->dtd_filename;
else
fn++;
fprintf(self->file_stream, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(self->file_stream, "<!DOCTYPE ROOT SYSTEM \"%s\">\n", fn);
fprintf(self->file_stream, "<ROOT>\n");
}
static void xml_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns)
{
add_tabledef(self, columns_num, columns);
}
static void xml_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self)
{
MYX_XML_EXPORTER_INFO* xi= (MYX_XML_EXPORTER_INFO*)self->priv;
self->current_level++;
// output dtd
if (!xi->dtd_written && atoi(self->option_values[0]))
{
FILE *file= myx_fopen(xi->dtd_filename, "w+");
xi->dtd_written= 1;
// this is a "simple DTD" solution of #11830
fprintf(file, "<!ELEMENT field (#PCDATA) >\n<!ATTLIST field name CDATA #REQUIRED >\n<!ELEMENT row (field)+ >\n<!ELEMENT ROOT (row)+ >");
/*
fprintf(file, "<!ELEMENT ROOT (%s)+>\n", self->option_values[1]);
fprintf(file, "\t<!ELEMENT %s (", self->option_values[1]);
for (i= 0; i < self->columns_num[0]; i++)
{
if (i > 0)
fprintf(file, ",%s", self->columns[0][i].name);
else
fprintf(file, "%s", self->columns[0][i].name);
}
fprintf(file, ")>\n");
for (i= 0; i < self->columns_num[0]; i++)
{
char *s= escape_xml_entities(self->columns[0][i].name);
fprintf(file, "\t\t<!ELEMENT %s (#PCDATA)>\n", s);
g_free(s);
}
*/
fclose(file);
}
if (self->current_level > 1)
{
fprintf(self->file_stream, "%*s<items>\n", self->current_level*4, "");
}
}
static void xml_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text)
{
unsigned int i;
MYX_TE_COLUMN *columns= self->columns[self->current_level-1];
fprintf(self->file_stream, "%*s<%s>\n", self->current_level*4, "", self->option_values[1]);
for (i= 0; i < self->columns_num[self->current_level-1]; i++)
{
if(text[i])
{
char *s= escape_xml_entities(text[i]);
fprintf(self->file_stream, "%*s<field name=\"%s\">%s</field>\n", self->current_level*4+2, "",
columns[i].name, s?s:"");
g_free(s);
}
else
fprintf(self->file_stream, "%*s<%s/>\n", self->current_level*4+2, "",
columns[i].name);
}
if (self->table_levels > 1 && self->current_level == 1)
;
else
fprintf(self->file_stream, "%*s</%s>\n", self->current_level*4, "",
self->option_values[1]);
}
static void xml_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self)
{
if (self->current_level > 1)
{
fprintf(self->file_stream, "%*s</items>\n", self->current_level*4, "");
}
if (self->table_levels > 1 && self->current_level > 1)
fprintf(self->file_stream, "%*s</%s>\n", (self->current_level-1)*4, "",
self->option_values[1]);
self->current_level--;
}
static void xml_exporter_end(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, "</ROOT>\n");
}
// ------------------------------------------------------------
// Excel exporter
// ------------------------------------------------------------
typedef struct {
int dummy;
} MYX_EXCEL_EXPORTER_INFO;
static MYX_TABLE_EXPORTER_INFO *excel_exporter_init();
static void excel_exporter_free(MYX_TABLE_EXPORTER_INFO *info);
static int excel_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename);
static void excel_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text);
static void excel_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns);
static void excel_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self);
static void excel_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text);
static void excel_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self);
static void excel_exporter_end(MYX_TABLE_EXPORTER_INFO *self);
static MYX_TE_OPTION excel_exporter_options[]=
{
{NULL, NULL, NULL, 0}
};
MYX_TABLE_EXPORTER EXCEL_TABLE_EXPORTER=
{
"Excel",
"Generates an Excel XML File (Excel XP and higher)",
"Excel (XP or higher) XML File",
"xls",
excel_exporter_init,
excel_exporter_free,
excel_exporter_setup,
excel_exporter_begin,
excel_exporter_table_setup,
excel_exporter_table_header,
excel_exporter_columns,
excel_exporter_table_footer,
excel_exporter_end,
excel_exporter_options
};
static MYX_TABLE_EXPORTER_INFO *excel_exporter_init()
{
MYX_TABLE_EXPORTER_INFO *info;
info= make_exporter_info(&EXCEL_TABLE_EXPORTER);
info->priv= g_new0(MYX_EXCEL_EXPORTER_INFO, 1);
return info;
}
static void excel_exporter_free(MYX_TABLE_EXPORTER_INFO *self)
{
g_free(self->priv);
free_exporter_info(self);
}
static int excel_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename)
{
self->file_stream= myx_fopen(filename, "w+");
if (!self->file_stream)
return -1;
return 0;
}
static void excel_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text)
{
fprintf(self->file_stream, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(self->file_stream, "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n"
" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n"
" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n"
" xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n");
}
static void excel_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns)
{
add_tabledef(self, columns_num, columns);
fprintf(self->file_stream, " <Worksheet ss:Name=\"Table1\">\n");
}
static void excel_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self)
{
unsigned int i;
fprintf(self->file_stream, " <Table>\n");
//Set width for text columns
for(i= 0; i<self->columns_num[0]; i++)
if(self->columns[0][i].ctype == MYX_TE_COL_STRING || self->columns[0][i].ctype == MYX_TE_COL_DATE)
{
fprintf(self->file_stream," <Column ss:Index=\"%d\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>", i+1);
}
fprintf(self->file_stream, " <Row>\n");
//Add column headers
for(i= 0; i<self->columns_num[0]; i++)
{
char *s= escape_xml_entities(self->columns[0][i].name);
fprintf(self->file_stream," <Cell><Data ss:Type=\"String\">%s</Data></Cell>\n",
s);
g_free(s);
}
fprintf(self->file_stream, " </Row>\n");
}
static void excel_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text)
{
unsigned int i;
static char *excel_types[]= {
"String",//MYX_TE_COL_STRING
"Number",//MYX_TE_COL_INTEGER
"Number",//MYX_TE_COL_FLOAT
"Number"//MYX_TE_COL_BOOL,
"String",//MYX_TE_COL_DATE
};
fprintf(self->file_stream, " <Row>\n");
for (i= 0; i < self->columns_num[0]; i++)
{
if(text[i])
{
char *s= escape_xml_entities(text[i]);
if (s == NULL)
s = "";
fprintf(self->file_stream, " <Cell><Data ss:Type=\"%s\">%s</Data></Cell>\n",
excel_types[self->columns[0][i].ctype], s);
g_free(s);
}
else
fprintf(self->file_stream, " <Cell><Data ss:Type=\"String\"/></Cell>\n");
}
fprintf(self->file_stream, " </Row>\n");
}
static void excel_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, " </Table>\n");
}
static void excel_exporter_end(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, " </Worksheet>\n");
fprintf(self->file_stream, "</Workbook>\n");
}
// ------------------------------------------------------------
// MacOS PropertyList exporter
// ------------------------------------------------------------
static MYX_TABLE_EXPORTER_INFO *plist_exporter_init();
static void plist_exporter_free(MYX_TABLE_EXPORTER_INFO *info);
static int plist_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename);
static void plist_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text);
static void plist_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns);
static void plist_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self);
static void plist_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text);
static void plist_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self);
static void plist_exporter_end(MYX_TABLE_EXPORTER_INFO *self);
static MYX_TE_OPTION plist_exporter_options[]=
{
{"Old style proplist format", "0", "Use the old, NEXTSTEP style property list file format.", MYX_TEOP_BOOL},
{NULL, NULL, NULL, 0}
};
MYX_TABLE_EXPORTER PLIST_TABLE_EXPORTER=
{
"PLIST",
"Generates a Property List File",
"PropertyList File",
"plist",
plist_exporter_init,
plist_exporter_free,
plist_exporter_setup,
plist_exporter_begin,
plist_exporter_table_setup,
plist_exporter_table_header,
plist_exporter_columns,
plist_exporter_table_footer,
plist_exporter_end,
plist_exporter_options
};
static MYX_TABLE_EXPORTER_INFO *plist_exporter_init()
{
MYX_TABLE_EXPORTER_INFO *info;
info= make_exporter_info(&PLIST_TABLE_EXPORTER);
return info;
}
static void plist_exporter_free(MYX_TABLE_EXPORTER_INFO *self)
{
free_exporter_info(self);
}
static int plist_exporter_setup(MYX_TABLE_EXPORTER_INFO *self, const char *filename)
{
self->file_stream= myx_fopen(filename, "w+");
if (!self->file_stream)
return -1;
return 0;
}
static void plist_exporter_begin(MYX_TABLE_EXPORTER_INFO *self, const char *text)
{
fprintf(self->file_stream, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(self->file_stream, "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");
fprintf(self->file_stream, "<plist version=\"1.0\">\n");
}
static void plist_exporter_table_setup(MYX_TABLE_EXPORTER_INFO *self, unsigned int columns_num, MYX_TE_COLUMN *columns)
{
add_tabledef(self, columns_num, columns);
}
static void plist_exporter_table_header(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, "<array>\n");
}
static void plist_exporter_columns(MYX_TABLE_EXPORTER_INFO *self, const char *const*text)
{
unsigned int i;
MYX_TE_COLUMN *columns= self->columns[0];
fprintf(self->file_stream, " <dict>\n");
for (i= 0; i < self->columns_num[0]; i++)
{
if(text[i])
{
char *s= escape_xml_entities(text[i]);
fprintf(self->file_stream, " <key>%s</key>\n", columns[i].name);
switch (columns[i].ctype)
{
case MYX_TE_COL_STRING:
fprintf(self->file_stream, " <string>%s</string>\n", s);
break;
case MYX_TE_COL_INTEGER:
fprintf(self->file_stream, " <integer>%s</integer>\n", s);
break;
case MYX_TE_COL_FLOAT:
fprintf(self->file_stream, " <real>%s</real>\n", s);
break;
case MYX_TE_COL_BOOL:
fprintf(self->file_stream, " <%s/>\n",
((strcasecmp(s,"true")==0 || strcasecmp(s, "1")==0)) ? "true" : "false");
break;
case MYX_TE_COL_DATE:
fprintf(self->file_stream, " <date>%s</date>\n", s);
break;
}
g_free(s);
}
}
fprintf(self->file_stream, " </dict>\n");
}
static void plist_exporter_table_footer(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, "</array>\n");
}
static void plist_exporter_end(MYX_TABLE_EXPORTER_INFO *self)
{
fprintf(self->file_stream, "</plist>\n");
}
// ============================================================
static MYX_TABLE_EXPORTER *exporters[]= {
&CSV_TABLE_EXPORTER,
&HTML_TABLE_EXPORTER,
&XML_TABLE_EXPORTER,
&EXCEL_TABLE_EXPORTER,
&PLIST_TABLE_EXPORTER
};
MYX_TABLE_EXPORTER *myx_get_table_exporter(const char *format)
{
unsigned int i;
for (i= 0; i < sizeof(exporters)/sizeof(MYX_TABLE_EXPORTER*); i++)
if (strcmp(format, exporters[i]->name)==0)
return exporters[i];
return NULL;
}
int myx_set_table_exporter_option(MYX_TABLE_EXPORTER_INFO *info,
const char *name,
const char *value)
{
int i;
for (i= 0; info->te->options[i].name; i++)
{
if (strcmp(name, info->te->options[i].name)==0)
{
g_free(info->option_values[i]);
info->option_values[i]= g_strdup(value);
return 0;
}
}
return -1;
}
MYX_TABLE_EXPORTER_INFO *myx_get_table_exporter_info(const char *format)
{
const MYX_TABLE_EXPORTER *exporter= myx_get_table_exporter(format);
if (!exporter)
return NULL;
return (*exporter->init)();
}
int myx_free_table_exporter_info(MYX_TABLE_EXPORTER_INFO *info)
{
(*info->te->free)(info);
return 0;
}
MYX_STRINGLIST *myx_get_table_export_formats()
{
MYX_STRINGLIST *sl= g_new0(MYX_STRINGLIST, 1);
unsigned int i;
sl->strings_num= sizeof(exporters)/sizeof(MYX_TABLE_EXPORTER*);
sl->strings= g_new0(char*,sl->strings_num);
for (i= 0; i < sl->strings_num; i++)
sl->strings[i]= g_strdup(exporters[i]->name);
return sl;
}
|