Added some additional fixed bugs to the NEWS file
[geda-gaf/cesar.git] / gattrib / src / f_export.c
blobc1245f7157856d9039a8f71f869f71ca68577fcb
1 /* gEDA - GPL Electronic Design Automation
2 * gattrib -- gEDA component and net attribute manipulation using spreadsheet.
3 * Copyright (C) 2003-2008 Stuart D. Brorson.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20 /*! \file
21 * \brief Import/export functions
23 * This file holds fcns used for import/export of attribute sheets.
24 * At the moment, this is only component sheets.
27 #include <config.h>
29 #include <stdio.h>
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33 #include <math.h>
35 /*------------------------------------------------------------------
36 * Gattrib specific includes
37 *------------------------------------------------------------------*/
38 #include <libgeda/libgeda.h> /* geda library fcns */
39 #include "../include/struct.h" /* typdef and struct declarations */
40 #include "../include/prototype.h" /* function prototypes */
41 #include "../include/globals.h"
43 #ifdef HAVE_LIBDMALLOC
44 #include <dmalloc.h>
45 #endif
48 /* =================== Public Functions ====================== */
49 /* ------------------------------------------------------------- */
50 /* \brief Export components to CSV
52 * This function is invoked when the user selects file ->
53 * export from the pull-down menu. It writes out a CSV file
54 * of the design for external processing.
56 * \param filename The name of the file to export to
58 void f_export_components(gchar *filename)
60 gint cur_page;
61 gint num_rows;
62 gint num_cols;
63 gint i,j;
65 gchar *text;
66 FILE *fp;
68 /* ----- Check that we have a component ----- */
69 cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
70 if (cur_page != 0) {
71 /* We only export the component table */
72 /* XXXXX Maybe throw up error message in window instead? */
73 x_dialog_unimplemented_feature();
74 return;
78 /* ----- First try to open file for writing ----- */
80 #ifdef DEBUG
81 printf("In f_export_components, trying to open %s.\n", filename);
82 #endif
83 fp = fopen(filename, "wb");
84 if (fp == NULL) {
85 s_log_message("o_save: Could not open [%s]\n", filename);
86 /* XXXXX Throw up error message in window */
87 return;
91 /* ----- Now write out data ----- */
92 num_rows = sheet_head->comp_count;
93 num_cols = sheet_head->comp_attrib_count;
95 /* First export top row -- attribute names */
96 /* Print out "refdes" since that's always the first column */
97 fprintf(fp, "refdes, ");
98 /* Print out optional attrib names */
99 for (j = 0; j < num_cols-1; j++) {
100 text = g_strdup( s_string_list_get_data_at_index(
101 sheet_head->master_comp_attrib_list_head, j) );
102 fprintf(fp, "%s, ", text);
103 g_free(text);
105 /* Print out last attrib name with no comma and with \n. */
106 text = g_strdup( s_string_list_get_data_at_index(
107 sheet_head->master_comp_attrib_list_head, j) );
108 fprintf(fp, "%s\n", text);
109 g_free(text);
112 /* Now export the contents of the sheet */
113 for (i = 0; i < num_rows; i++) {
115 /* First output the component refdes */
116 text = g_strdup( s_string_list_get_data_at_index(
117 sheet_head->master_comp_list_head, i) );
118 #ifdef DEBUG
119 printf("In f_export_components, getting refes, i = %d.\n", i);
120 printf("In f_export_components, output component refdes %s.\n", text);
121 #endif
122 fprintf(fp, "%s, ",text);
123 g_free(text);
125 /* Now export the attrib values for first n-1 cols */
126 for (j = 0; j < num_cols-1; j++) {
127 if ( (sheet_head->component_table)[i][j].attrib_value ) { /* found a string */
128 text = (gchar *) g_strdup( (sheet_head->component_table)[i][j].attrib_value );
129 #ifdef DEBUG
130 printf("In f_export_components, output attribute %s.\n", text);
131 #endif
132 fprintf(fp, "%s, ", text);
133 g_free(text);
134 } else { /* no attrib string */
135 #ifdef DEBUG
136 printf("In f_export_components, output blank attrib space\n");
137 #endif
138 fprintf(fp, ", ");
140 } /* end of for over cols */
141 /* Now export attrib value for last col (with no "," and with "\n" */
142 if ( (sheet_head->component_table)[i][j].attrib_value ) { /* found a string */
143 text = (gchar *) g_strdup( (sheet_head->component_table)[i][j].attrib_value );
144 #ifdef DEBUG
145 printf("In f_export_components, output final attribute %s.\n", text);
146 #endif
147 fprintf(fp, "%s\n", text);
148 g_free(text);
149 } else { /* no attrib string */
150 #ifdef DEBUG
151 printf("In f_export_components, output blank at end of line.\n");
152 #endif
153 fprintf(fp, "\n");
155 #ifdef DEBUG
156 printf("In f_export_components, Go to next row.\n");
157 #endif
158 } /* close of for over rows */
160 fclose(fp);
162 return;