Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

workspace.c

Go to the documentation of this file.
00001 
00005 /* $Id: workspace_8c-source.html,v 1.5 2004/05/09 09:31:57 darryll Exp $
00006  * 
00007  * GtkSQL -- an interactive graphical query tool for PostgreSQL and MySQL.
00008  * Copyright (C) 2002-2003 Darryl Luff
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  *
00024  * Workspace handling. Hold implementation pending separation of db/interface 
00025  * code.
00026  *
00027  * Workspace format:
00028  * GTKSQL WORKSPACE
00029  * QUERY "select ..."
00030  * QUERY "insert into ..."
00031  * ...
00032  */
00033 
00034 #include "../config.h"
00035 
00036 #include <ctype.h>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #include "buf.h"
00042 #include "guiapi.h"
00043 #include "queries.h"
00044 #include "luaif.h"
00045 #include "workspace.h"
00046 
00048 #define CR_CHAR '\\'
00049 
00050 /* Function prototypes. */
00051 void map_chars(char *sql, char c1, char c2);
00052 
00054 void map_chars(char *txt, char c1, char c2)
00055 {
00056     char *c = txt;
00057     if ((!txt) || (strlen(txt) == 0))
00058         return;
00059 
00060     while (*c) {
00061         if (*c == c1)
00062             *c = c2;
00063         c++;
00064     }
00065 }
00066 
00070 void ws_load_file(char *fn)
00071 {
00072     if (fn == NULL)
00073         return;
00074 #ifdef USE_LUA
00075     luaif_runfile(fn);
00076 #else
00077     printf("WARNING: Workspace loading disabled. No scripting support.\n");
00078 #endif
00079 }
00080 
00084 int ws_save_file(char *fn)
00085 {
00086     FILE *ws;
00087     char *qryTxt;
00088     char *newTxt;
00089     int qryNum;
00090     char *fname;
00091     char *url;
00092     ABuf *qry = buf_new(100);
00093 
00094     if ((fn != NULL) && (strlen(fn) > 0))
00095         fname = fn;
00096     else
00097         fname = cfg_get_default_wsname();
00098 
00099     ws = fopen(fname, "w");
00100     if (!ws) {
00101         perror("opening workspace file for writing");
00102         return -1;
00103     }
00104 
00105     if (gui_connection_ok()) {  /* Save the connection and queries  */
00106         /* Save the connection URL  */
00107         url = gui_get_connection_url();
00108         if ((url != NULL) && (strlen(url) > 0)) {
00109             qry = buf_strcpy(qry, "connect(\"");
00110             qry = buf_strcat(qry, url);
00111             qry = buf_strcat(qry, "\")\n");
00112             fprintf(ws, qry->b_dat);
00113 
00114             /* Now, run through the open queries */
00115             qryNum = 0;
00116             while ((qryTxt = query_get_text_no(qryNum))) {
00117                 newTxt = strdup(qryTxt);
00118                 if (newTxt) {
00120                     map_chars(newTxt, '\n', ' ');
00121                     qry = buf_strcpy(qry, "addQuery(\"");
00122                     qry = buf_strcat(qry, newTxt);
00123                     qry = buf_strcat(qry, "\")\n");
00124                     fprintf(ws, qry->b_dat);
00125                     g_free(newTxt);
00126                 }
00127                 newTxt = NULL;
00128                 qryNum++;
00129             }
00130         }
00131     }
00132 
00133     /* Cleanup. */
00134     qry = buf_free(qry);
00135     fclose(ws);
00136     return 0;
00137 }

Generated on Sun May 9 19:19:17 2004 for GtkSQL by doxygen1.2.18