/* SciGraphica - Scientific graphics and data manipulation * Copyright (C) 2001 Adrian E. Feiguin * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include "sg_worksheet_file_xml.h" #include "sg.h" static void write_header_xml (SGworksheetfile *file); static void write_footer_xml (SGworksheetfile *file); static void write_column_title_xml (SGworksheetfile *file, gint col); static void write_cell_xml (SGworksheetfile *file, gint row, gint col); gboolean sg_worksheet_file_export_xml (SGworksheet *worksheet, gchar *filename, GtkSheetRange *range, FILE *opened) { SGworksheetfile *file; GtkSheet *sheet; sheet = GTK_SHEET(worksheet->sheet); file = sg_worksheet_file_new(worksheet, filename); if(!opened){ file->file = sg_fopen(filename, "w"); if (!file->file){ g_warning("ERROR: Cannot write to file: %s", filename); sg_worksheet_file_destroy(file); return FALSE; } fprintf(file->file,"\n"); } else { file->file = opened; } file->write_header = write_header_xml; file->write_footer = write_footer_xml; file->write_col_title = write_column_title_xml; file->write_cell = write_cell_xml; file->titlecolor = g_strdup("\"BBBBBB\""); file->cellcolor = g_strdup("\"FFFFFF\""); sg_worksheet_file_export(file, range); if(!opened){ sg_fclose(file->file); } sg_worksheet_file_destroy(file); return TRUE; } static void write_header_xml(SGworksheetfile *file) { SGworksheet *worksheet = file->worksheet; if(worksheet->mode == SG_MODE_MATRIX) fprintf(file->file,"\n"); else fprintf(file->file,"\n"); fprintf(file->file, " \n"); fprintf(file->file, " \n"); fprintf(file->file, " application\n"); fprintf(file->file, " scigraphica\n"); fprintf(file->file, " \n"); fprintf(file->file, " \n"); fprintf(file->file, " author\n"); fprintf(file->file, " %s\n", g_get_real_name()); fprintf(file->file, " \n"); fprintf(file->file, " \n"); fprintf(file->file, " \n", worksheet->width, worksheet->height); if(worksheet->mode == SG_MODE_MATRIX){ gchar *xml_text = NULL; if(file->worksheet->matrix.exp) xml_text = xml_get_string(file->worksheet->matrix.exp); else xml_text = g_strdup(""); fprintf(file->file, " \n", worksheet->xmin, worksheet->xmax, worksheet->ymin, worksheet->ymax); fprintf(file->file, " \n", xml_text, file->worksheet->matrix.format, file->worksheet->matrix.internal, file->worksheet->matrix.precision); g_free(xml_text); } fprintf(file->file, " %s\n", worksheet->name); fprintf(file->file, " %d\n", GTK_SHEET(worksheet->sheet)->maxcol); fprintf(file->file, " %d\n", GTK_SHEET(worksheet->sheet)->maxrow); fprintf(file->file, " %d\n", worksheet->begin); fprintf(file->file, " %d\n", worksheet->end); } static void write_footer_xml(SGworksheetfile *file) { if(file->worksheet->mode == SG_MODE_MATRIX) fprintf(file->file,"\n"); else fprintf(file->file,"\n"); } static void write_column_title_xml(SGworksheetfile *file, gint col) { GtkSheetColumn *column; gchar *xml_text = NULL; column = >K_SHEET(file->worksheet->sheet)->column[col]; fprintf(file->file, " \n", col, column->width, column->name, file->worksheet->column[col].type, file->worksheet->column[col].format, file->worksheet->column[col].internal, file->worksheet->column[col].precision); if(file->worksheet->column[col].exp){ xml_text = xml_get_string(file->worksheet->column[col].exp); fprintf(file->file, " %s\n", xml_text); g_free(xml_text); } fprintf(file->file, " \n"); } static void write_cell_xml(SGworksheetfile *file, gint row, gint col) { gchar *text, *formula; gchar *xml_text = NULL; formula = sg_worksheet_cell_get_formula(file->worksheet, row, col); text = sg_worksheet_cell_get_text(file->worksheet, row, col); if((!text || strlen(text) == 0) && (!formula || strlen(formula) == 0)) return; fprintf(file->file, " \n", row, col); if (text && strlen(text) >0) { xml_text = xml_get_string(text); fprintf(file->file, " %s\n", xml_text); } if (formula && strlen(formula) >0) { xml_text = xml_get_string(formula); fprintf(file->file, " %s\n", xml_text); } fprintf(file->file, " \n"); g_free(xml_text); }