gattrib
|
00001 /* gEDA - GPL Electronic Design Automation 00002 * gattrib -- gEDA component and net attribute manipulation using spreadsheet. 00003 * Copyright (C) 2003-2010 Stuart D. Brorson. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00027 #include <config.h> 00028 00029 #include <stdio.h> 00030 #ifdef HAVE_STRING_H 00031 #include <string.h> 00032 #endif 00033 #include <math.h> 00034 00035 /*------------------------------------------------------------------ 00036 * Gattrib specific includes 00037 *------------------------------------------------------------------*/ 00038 #include <libgeda/libgeda.h> /* geda library fcns */ 00039 #include "../include/struct.h" /* typdef and struct declarations */ 00040 #include "../include/prototype.h" /* function prototypes */ 00041 #include "../include/globals.h" 00042 00043 #ifdef HAVE_LIBDMALLOC 00044 #include <dmalloc.h> 00045 #endif 00046 00047 00048 /* =================== Public Functions ====================== */ 00049 /* ------------------------------------------------------------- */ 00050 /* \brief Export components to CSV 00051 * 00052 * This function is invoked when the user selects file -> 00053 * export from the pull-down menu. It writes out a CSV file 00054 * of the design for external processing. 00055 * 00056 * \param filename The name of the file to export to 00057 */ 00058 void f_export_components(gchar *filename) 00059 { 00060 gint cur_page; 00061 gint num_rows; 00062 gint num_cols; 00063 gint i,j; 00064 00065 gchar *text; 00066 FILE *fp; 00067 00068 /* ----- Check that we have a component ----- */ 00069 cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook)); 00070 if (cur_page != 0) { 00071 /* We only export the component table */ 00072 /* XXXXX Maybe throw up error message in window instead? */ 00073 x_dialog_unimplemented_feature(); 00074 return; 00075 00076 } 00077 00078 /* ----- First try to open file for writing ----- */ 00079 00080 #ifdef DEBUG 00081 printf("In f_export_components, trying to open %s.\n", filename); 00082 #endif 00083 fp = fopen(filename, "wb"); 00084 if (fp == NULL) { 00085 s_log_message("o_save: Could not open [%s]\n", filename); 00086 /* XXXXX Throw up error message in window */ 00087 return; 00088 } 00089 00090 00091 /* ----- Now write out data ----- */ 00092 num_rows = sheet_head->comp_count; 00093 num_cols = sheet_head->comp_attrib_count; 00094 00095 /* First export top row -- attribute names */ 00096 /* Print out "refdes" since that's always the first column */ 00097 fprintf(fp, "refdes, "); 00098 /* Print out optional attrib names */ 00099 for (j = 0; j < num_cols-1; j++) { 00100 text = g_strdup( s_string_list_get_data_at_index( 00101 sheet_head->master_comp_attrib_list_head, j) ); 00102 fprintf(fp, "%s, ", text); 00103 g_free(text); 00104 } 00105 /* Print out last attrib name with no comma and with \n. */ 00106 text = g_strdup( s_string_list_get_data_at_index( 00107 sheet_head->master_comp_attrib_list_head, j) ); 00108 fprintf(fp, "%s\n", text); 00109 g_free(text); 00110 00111 00112 /* Now export the contents of the sheet */ 00113 for (i = 0; i < num_rows; i++) { 00114 00115 /* First output the component refdes */ 00116 text = g_strdup( s_string_list_get_data_at_index( 00117 sheet_head->master_comp_list_head, i) ); 00118 #ifdef DEBUG 00119 printf("In f_export_components, getting refes, i = %d.\n", i); 00120 printf("In f_export_components, output component refdes %s.\n", text); 00121 #endif 00122 fprintf(fp, "%s, ",text); 00123 g_free(text); 00124 00125 /* Now export the attrib values for first n-1 cols */ 00126 for (j = 0; j < num_cols-1; j++) { 00127 if ( (sheet_head->component_table)[i][j].attrib_value ) { /* found a string */ 00128 /* make a copy of the text, escaping any special chars, like " */ 00129 text = (gchar *) g_strescape( (sheet_head->component_table)[i][j].attrib_value, "" ); 00130 #ifdef DEBUG 00131 printf("In f_export_components, output attribute %s.\n", text); 00132 #endif 00133 /* if there's a comma anywhere in the field, wrap the field in " */ 00134 gboolean havecomma = ( g_strstr_len(text, -1, ",") != NULL ); 00135 if(havecomma) fprintf(fp, "\""); 00136 fprintf(fp, "%s", text); 00137 if(havecomma) fprintf(fp, "\""); 00138 fprintf(fp, ", "); 00139 00140 g_free(text); 00141 } else { /* no attrib string */ 00142 #ifdef DEBUG 00143 printf("In f_export_components, output blank attrib space\n"); 00144 #endif 00145 fprintf(fp, ", "); 00146 } 00147 } /* end of for over cols */ 00148 /* Now export attrib value for last col (with no "," and with "\n" */ 00149 if ( (sheet_head->component_table)[i][j].attrib_value ) { /* found a string */ 00150 /* make a copy of the text, escaping any special chars, like " */ 00151 text = (gchar *) g_strescape( (sheet_head->component_table)[i][j].attrib_value, "" ); 00152 #ifdef DEBUG 00153 printf("In f_export_components, output final attribute %s.\n", text); 00154 #endif 00155 /* if there's a comma anywhere in the field, wrap the field in " */ 00156 gboolean havecomma = ( g_strstr_len(text, -1, ",") != NULL ); 00157 if(havecomma) fprintf(fp, "\""); 00158 fprintf(fp, "%s", text); 00159 if(havecomma) fprintf(fp, "\""); 00160 fprintf(fp, "\n"); 00161 00162 g_free(text); 00163 } else { /* no attrib string */ 00164 #ifdef DEBUG 00165 printf("In f_export_components, output blank at end of line.\n"); 00166 #endif 00167 fprintf(fp, "\n"); 00168 } 00169 #ifdef DEBUG 00170 printf("In f_export_components, Go to next row.\n"); 00171 #endif 00172 } /* close of for over rows */ 00173 00174 fclose(fp); 00175 00176 return; 00177 }