[GRASS-SVN] r38954 - in grass/branches/develbranch_6: lib/form vector/v.digit

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Sep 3 05:42:24 EDT 2009


Author: marisn
Date: 2009-09-03 05:42:23 -0400 (Thu, 03 Sep 2009)
New Revision: 38954

Removed:
   grass/branches/develbranch_6/vector/v.digit/generate.c
   grass/branches/develbranch_6/vector/v.digit/open.c
Modified:
   grass/branches/develbranch_6/lib/form/form.c
   grass/branches/develbranch_6/lib/form/generate.c
   grass/branches/develbranch_6/lib/form/open.c
   grass/branches/develbranch_6/vector/v.digit/Makefile
Log:
v.digit now uses lib/forms. Should work if GISBASE or GISDBASE contains spaces. Fix form mess introduced in r38898.

Modified: grass/branches/develbranch_6/lib/form/form.c
===================================================================
--- grass/branches/develbranch_6/lib/form/form.c	2009-09-02 21:22:20 UTC (rev 38953)
+++ grass/branches/develbranch_6/lib/form/form.c	2009-09-03 09:42:23 UTC (rev 38954)
@@ -91,7 +91,7 @@
 
     /* Check if all internal values are set */
     if (Drvname == NULL || Dbname == NULL || Tblname == NULL || Key == NULL) {
-	G_warning("db connection was not set by form\n");
+	G_warning("db connection was not set by form");
 	sprintf(buf, "set submit_msg \"db connection was not set by form.\"");
 	Tcl_Eval(interp, buf);
 	Tcl_Eval(interp, "set submit_result 0");
@@ -102,7 +102,7 @@
     G_debug(2, "Open driver");
     driver = db_start_driver(Drvname);
     if (driver == NULL) {
-	G_warning("Cannot open driver\n");
+	G_warning("Cannot open driver");
 	sprintf(buf, "set submit_msg \"Cannot open driver '%s'\"", Drvname);
 	Tcl_Eval(interp, buf);
 	Tcl_Eval(interp, "set submit_result 0");
@@ -114,11 +114,12 @@
     db_set_handle(&handle, Dbname, NULL);
     G_debug(2, "Open database");
     if (db_open_database(driver, &handle) != DB_OK) {
-	G_warning("Cannot open database\n");
+	G_warning("Cannot open database");
 	db_shutdown_driver(driver);
 	sprintf(buf,
 		"set submit_msg \"Cannot open database '%s' by driver '%s'\"",
 		Dbname, Drvname);
+
 	Tcl_Eval(interp, buf);
 	Tcl_Eval(interp, "set submit_result 0");
 	return TCL_OK;
@@ -127,7 +128,7 @@
 
     db_set_string(&table_name, Tblname);
     if (db_describe_table(driver, &table_name, &table) != DB_OK) {
-	G_warning("Cannot describe table\n");
+	G_warning("Cannot describe table");
 	db_shutdown_driver(driver);
 	db_close_database(driver);
 	sprintf(buf, "set submit_msg \"Cannot describe table '%s'\"",

Modified: grass/branches/develbranch_6/lib/form/generate.c
===================================================================
--- grass/branches/develbranch_6/lib/form/generate.c	2009-09-02 21:22:20 UTC (rev 38953)
+++ grass/branches/develbranch_6/lib/form/generate.c	2009-09-03 09:42:23 UTC (rev 38954)
@@ -4,6 +4,19 @@
 #include <grass/dbmi.h>
 #include <grass/form.h>
 
+/* Escape string for use in TCL */
+char *escape_tcl_string(char *input)
+{
+    char *escaped;
+
+    escaped = G_str_replace(input, "\\", "\\\\");
+    escaped = G_str_replace(escaped, "[", "\\[");
+    escaped = G_str_replace(escaped, "]", "\\]");
+    escaped = G_str_replace(escaped, "$", "\\$");
+
+    return escaped;
+}
+
 /* Generate form in HTML/TXT format.
  *  Pointer to resulting string is stored to 'form'. This string must be freed by application.
  *
@@ -60,8 +73,9 @@
     G_debug(2, "Open driver");
     driver = db_start_driver(drvname);
     if (driver == NULL) {
-	G_warning("Cannot open driver\n");
-	sprintf(buf, "Cannot open driver '%s'<BR>", drvname);
+	G_warning("Cannot open driver");
+	sprintf(buf, "Cannot open driver '%s'<BR>",
+		escape_tcl_string(drvname));
 	*form = G_store(buf);
 	return -1;
     }
@@ -71,10 +85,10 @@
     db_set_handle(&handle, dbname, NULL);
     G_debug(2, "Open database");
     if (db_open_database(driver, &handle) != DB_OK) {
-	G_warning("Cannot open database\n");
+	G_warning("Cannot open database");
 	db_shutdown_driver(driver);
-	sprintf(buf, "Cannot open database '%s' by driver '%s'<BR>", dbname,
-		drvname);
+	sprintf(buf, "Cannot open database '%s' by driver '%s'<BR>",
+		escape_tcl_string(dbname), escape_tcl_string(drvname));
 	*form = G_store(buf);
 	return -1;
     }
@@ -88,12 +102,13 @@
     G_debug(2, "%s", buf);
     db_set_string(&sql, buf);
     if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
-	G_warning("Cannot open select cursor\n");
+	G_warning("Cannot open select cursor");
 	db_close_database(driver);
 	db_shutdown_driver(driver);
 	sprintf(buf,
 		"Cannot open select cursor:<BR>'%s'<BR>on database '%s' by driver '%s'<BR>",
-		db_get_string(&sql), dbname, drvname);
+		escape_tcl_string(db_get_string(&sql)),
+		escape_tcl_string(dbname), escape_tcl_string(drvname));
 	*form = G_store(buf);
 	return -1;
     }
@@ -102,7 +117,7 @@
     table = db_get_cursor_table(&cursor);
 
     if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
-	G_warning("Cannot fetch next record\n");
+	G_warning("Cannot fetch next record");
 	db_close_cursor(&cursor);
 	db_close_database(driver);
 	db_shutdown_driver(driver);
@@ -128,19 +143,23 @@
 		db_append_string(&html, "<FORM>");
 
 		sprintf(buf, "<INPUT type=hidden name=%s value=\"%s\">",
-			F_DRIVER_FNAME, drvname);
+			escape_tcl_string(F_DRIVER_FNAME),
+			escape_tcl_string(drvname));
 		db_append_string(&html, buf);
 		/* Note: because html_library.tcl failes to parse
 		 *  <INPUT name=abc value='dbname=xxx'> and returnes
 		 *  name="xxx" value="dbname=xxx" order of value and name parameters is changed */
 		sprintf(buf, "<INPUT type=hidden value=\"%s\" name=%s>",
-			dbname, F_DATABASE_FNAME);
+			escape_tcl_string(dbname),
+			escape_tcl_string(F_DATABASE_FNAME));
 		db_append_string(&html, buf);
 		sprintf(buf, "<INPUT type=hidden name=%s value=\"%s\">",
-			F_TABLE_FNAME, tblname);
+			escape_tcl_string(F_TABLE_FNAME),
+			escape_tcl_string(tblname));
 		db_append_string(&html, buf);
 		sprintf(buf, "<INPUT type=hidden name=%s value=\"%s\">",
-			F_KEY_FNAME, key);
+			escape_tcl_string(F_KEY_FNAME),
+			escape_tcl_string(key));
 		db_append_string(&html, buf);
 
 	    }
@@ -156,19 +175,22 @@
 		G_debug(2, "%s: %s", colname, db_get_string(&str));
 
 		if (edit_mode == F_VIEW) {
-		    sprintf(buf, "<B>%s : </B> %s <BR>", colname,
-			    db_get_string(&str));
+		    sprintf(buf, "<B>%s : </B> %s <BR>",
+			    escape_tcl_string(G_strdup(colname)),
+			    escape_tcl_string(db_get_string(&str)));
 		    db_append_string(&html, buf);
 		}
 		else {
-		    sprintf(buf, "<B>%s : </B>", colname);
+		    sprintf(buf, "<B>%s : </B>",
+			    escape_tcl_string(G_strdup(colname)));
 		    db_append_string(&html, buf);
 
 		    if (G_strcasecmp(colname, key) == 0) {
 			sprintf(buf,
 				"%s<BR> <INPUT type=hidden name=%s value=\"%s\">",
-				db_get_string(&str), colname,
-				db_get_string(&str));
+				escape_tcl_string(db_get_string(&str)),
+				escape_tcl_string(G_strdup(colname)),
+				escape_tcl_string(db_get_string(&str)));
 		    }
 		    else {
 			switch (ctype) {
@@ -187,7 +209,9 @@
 			}
 			sprintf(buf,
 				"<INPUT type=text size=%s name=%s value=\"%s\"><BR>",
-				buf1, colname, db_get_string(&str));
+				escape_tcl_string(buf1),
+				escape_tcl_string(G_strdup(colname)),
+				escape_tcl_string(db_get_string(&str)));
 		    }
 		    db_append_string(&html, buf);
 		}

Modified: grass/branches/develbranch_6/lib/form/open.c
===================================================================
--- grass/branches/develbranch_6/lib/form/open.c	2009-09-02 21:22:20 UTC (rev 38953)
+++ grass/branches/develbranch_6/lib/form/open.c	2009-09-03 09:42:23 UTC (rev 38954)
@@ -107,9 +107,9 @@
 
 
 
-	sprintf(command, "\"%s/etc/form/form\"", G_gisbase());
-	sprintf(script, "\"%s/etc/form/form.tcl\"", G_gisbase());
-G_message("FC: %s form -f %s", command, script);
+	sprintf(command, "%s/etc/form/form", G_gisbase());
+	sprintf(script, "%s/etc/form/form.tcl", G_gisbase());
+
 	execl(command, "form", "-f", script, NULL);
 
 	G_debug(2, "CHILD END\n");

Modified: grass/branches/develbranch_6/vector/v.digit/Makefile
===================================================================
--- grass/branches/develbranch_6/vector/v.digit/Makefile	2009-09-02 21:22:20 UTC (rev 38953)
+++ grass/branches/develbranch_6/vector/v.digit/Makefile	2009-09-03 09:42:23 UTC (rev 38954)
@@ -2,8 +2,8 @@
 
 PGM = v.digit
 
-LIBES = $(DISPLAYLIB) $(RASTERLIB) $(VECTLIB) $(GISLIB) $(TCLTKLIBPATH) $(TCLTKLIBS)
-DEPENDENCIES= $(DISPLAYDEP) $(RASTERDEP) $(VECTDEP) $(GISDEP)
+LIBES = $(FORMLIB) $(DISPLAYLIB) $(RASTERLIB) $(VECTLIB) $(GISLIB) $(TCLTKLIBPATH) $(TCLTKLIBS)
+DEPENDENCIES= $(FORMDEP) $(DISPLAYDEP) $(RASTERDEP) $(VECTDEP) $(GISDEP) 
 
 EXTRA_CFLAGS = $(XCFLAGS) $(TCLINCDIR) $(TKINCDIR) $(VECT_CFLAGS)
 XTRA_LDFLAGS=$(DFLAGS) 

Deleted: grass/branches/develbranch_6/vector/v.digit/generate.c
===================================================================
--- grass/branches/develbranch_6/vector/v.digit/generate.c	2009-09-02 21:22:20 UTC (rev 38953)
+++ grass/branches/develbranch_6/vector/v.digit/generate.c	2009-09-03 09:42:23 UTC (rev 38954)
@@ -1,287 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include <grass/form.h>
-
-/* I was unable to find standard strrep function. */
-char *strrep(char *from, char *to, char symbol) {
-	int i = 0, j = 0, size;
-
-	size = sizeof(from)*strlen(from)+1;
-	to = (char *)malloc(size);
-	if (!to) {
-		return NULL;
-	}
-	while (from[i]) {
-		if (from[i]==symbol) {
-			size++;
-			to = realloc(to, size);
-			if (!to)
-				return NULL;
-			to[j] = '\\';
-			j++;
-		}
-		to[j] = from[i];
-		i++;
-		j++;
-	}
-	to[j]='\0';
-	
-	return to;
-}
-
-
-/* Generate form in HTML/TXT format.
- *  Pointer to resulting string is stored to 'form'. This string must be freed by application.
- *
- *  returns: -1 error 
- *            0 success
- */
-int
-F_generate(char *drvname, char *dbname, char *tblname, char *key, int keyval,
-	   char *frmname, char *frmmapset,
-	   int edit_mode, int format, char **form)
-{
-    int col, ncols, ctype, sqltype, more;
-    char buf[5000], buf1[100];
-    char *escstr;
-    const char *colname;
-    dbString sql, html, str;
-    dbDriver *driver;
-    dbHandle handle;
-    dbCursor cursor;
-    dbTable *table;
-    dbColumn *column;
-    dbValue *value;
-
-    int i = 0;
-
-    /* see /usr/lib/tcl8.4/encoding/ */
-    static char *encoding_list[] = {
-	"utf-8",
-	"ascii",
-	"iso8859-1",
-	"iso8859-2",
-	"iso8859-15",
-	"iso2022-jp",
-	"koi8-r",
-	"euc-jp",
-	NULL
-    };
-    char *enc_env;
-
-    G__read_env();
-    enc_env = G__getenv("GRASS_DB_ENCODING");
-
-    /* TODO: support 'format' (txt, html), currently html only */
-
-    G_debug(2,
-	    "F_generate(): drvname = '%s', dbname = '%s'\n      tblname = '%s', key = '%s', keyval = %d\n"
-	    "    form = '%s', form_mapset = '%s'\n      edit_mode = %d",
-	    drvname, dbname, tblname, key, keyval, frmname, frmmapset,
-	    edit_mode);
-
-    db_init_string(&sql);
-    db_init_string(&html);	/* here is the result stored */
-    db_init_string(&str);
-
-    G_debug(2, "Open driver");
-    driver = db_start_driver(drvname);
-    if (driver == NULL) {
-	G_warning("Cannot open driver\n");
-	sprintf(buf, "Cannot open driver '%s'<BR>", drvname);
-	*form = G_store(buf);
-	return -1;
-    }
-    G_debug(2, "Driver opened");
-
-    db_init_handle(&handle);
-    db_set_handle(&handle, dbname, NULL);
-    G_debug(2, "Open database");
-    if (db_open_database(driver, &handle) != DB_OK) {
-	G_warning("Cannot open database\n");
-	db_shutdown_driver(driver);
-	sprintf(buf, "Cannot open database '%s' by driver '%s'<BR>", dbname,
-		drvname);
-	*form = G_store(buf);
-	return -1;
-    }
-    G_debug(2, "Database opened");
-
-    /* TODO: test if table exist first, but this should be tested by application befor
-     *        F_generate() is called, because it may be correct (connection defined in DB
-     *        but table does not exist) */
-
-    sprintf(buf, "select * from %s where %s = %d", tblname, key, keyval);
-    G_debug(2, "%s", buf);
-    db_set_string(&sql, buf);
-    if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
-	G_warning("Cannot open select cursor\n");
-	db_close_database(driver);
-	db_shutdown_driver(driver);
-	sprintf(buf,
-		"Cannot open select cursor:<BR>'%s'<BR>on database '%s' by driver '%s'<BR>",
-		db_get_string(&sql), dbname, drvname);
-	*form = G_store(buf);
-	return -1;
-    }
-    G_debug(2, "Select Cursor opened");
-
-    table = db_get_cursor_table(&cursor);
-
-    if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
-	G_warning("Cannot fetch next record\n");
-	db_close_cursor(&cursor);
-	db_close_database(driver);
-	db_shutdown_driver(driver);
-	*form = G_store("Cannot fetch next record");
-	return -1;
-    }
-
-    if (!more) {
-	G_warning("No database record");
-	if (format == F_HTML) {
-	    *form = G_store("No record selected.<BR>");
-	}
-	else {
-	    *form = G_store("No record selected.");
-	}
-    }
-    else {
-	ncols = db_get_table_number_of_columns(table);
-
-	/* Start form */
-	if (format == F_HTML) {
-	    if (edit_mode == F_EDIT) {
-		db_append_string(&html, "<FORM>");
-
-		sprintf(buf, "<INPUT type=hidden name=%s value=\"%s\">",
-			F_DRIVER_FNAME, drvname);
-		db_append_string(&html, buf);
-		/* Note: because html_library.tcl failes to parse
-		 *  <INPUT name=abc value='dbname=xxx'> and returnes
-		 *  name="xxx" value="dbname=xxx" order of value and name parameters is changed */
-		/* Note: TCL will process \$[] chars in dbname (path) and thus we need to escape them. 
-		 *  As {} chars are converted by html_library.tcl to HTML entities, it's no use to escape them.*/
-		escstr = strrep(dbname,escstr,'\\');
-		escstr = strrep(escstr,escstr,'$');
-		escstr = strrep(escstr,escstr,'[');
-		escstr = strrep(escstr,escstr,']');
-		sprintf(buf, "<INPUT type=hidden value=\"%s\" name=%s>",
-			escstr, F_DATABASE_FNAME);
-		db_append_string(&html, buf);
-		sprintf(buf, "<INPUT type=hidden name=%s value=\"%s\">",
-			F_TABLE_FNAME, tblname);
-		db_append_string(&html, buf);
-		sprintf(buf, "<INPUT type=hidden name=%s value=\"%s\">",
-			F_KEY_FNAME, key);
-		db_append_string(&html, buf);
-
-	    }
-
-	    for (col = 0; col < ncols; col++) {
-		column = db_get_table_column(table, col);
-		sqltype = db_get_column_sqltype(column);
-		ctype = db_sqltype_to_Ctype(sqltype);
-		value = db_get_column_value(column);
-		db_convert_value_to_string(value, sqltype, &str);
-		colname = db_get_column_name(column);
-
-		G_debug(2, "%s: %s", colname, db_get_string(&str));
-
-		if (edit_mode == F_VIEW) {
-		    sprintf(buf, "<B>%s : </B> %s <BR>", colname,
-			    db_get_string(&str));
-		    db_append_string(&html, buf);
-		}
-		else {
-		    sprintf(buf, "<B>%s : </B>", colname);
-		    db_append_string(&html, buf);
-
-		    if (G_strcasecmp(colname, key) == 0) {
-			sprintf(buf,
-				"%s<BR> <INPUT type=hidden name=%s value=\"%s\">",
-				db_get_string(&str), colname,
-				db_get_string(&str));
-		    }
-		    else {
-			switch (ctype) {
-			case DB_C_TYPE_INT:
-			    sprintf(buf1, "20");
-			    break;
-			case DB_C_TYPE_DOUBLE:
-			    sprintf(buf1, "30");
-			    break;
-			case DB_C_TYPE_STRING:
-			    sprintf(buf1, "%d", db_get_column_length(column));
-			    break;
-			case DB_C_TYPE_DATETIME:
-			    sprintf(buf1, "20");
-			    break;
-			}
-			sprintf(buf,
-				"<INPUT type=text size=%s name=%s value=\"%s\"><BR>",
-				buf1, colname, db_get_string(&str));
-		    }
-		    db_append_string(&html, buf);
-		}
-	    }
-
-	    if (edit_mode == F_EDIT) {
-		sprintf(buf,
-			"<HR>   Assume data encoding as:<BR><BR><SELECT NAME=%s SIZE=4><HR><BR>",
-			F_ENCODING);
-		db_append_string(&html, buf);
-
-		i = 0;
-		while (encoding_list[i] != NULL) {
-
-		    if (G_strcasecmp(encoding_list[i], enc_env) == 0)
-			sprintf(buf, "<OPTION VALUE=\"%s\" SELECTED>%s",
-				encoding_list[i], encoding_list[i]);
-		    else
-			sprintf(buf, "<OPTION VALUE=\"%s\">%s",
-				encoding_list[i], encoding_list[i]);
-		    ++i;
-		    db_append_string(&html, buf);
-		}
-
-		sprintf(buf, "</SELECT>");
-		db_append_string(&html, buf);
-	    }
-
-	    /* Close form */
-	    if (edit_mode == F_EDIT) {
-		db_append_string(&html, "</FORM>");
-	    }
-	}
-	else {			/* F_TXT */
-	    for (col = 0; col < ncols; col++) {
-		column = db_get_table_column(table, col);
-		sqltype = db_get_column_sqltype(column);
-		ctype = db_sqltype_to_Ctype(sqltype);
-		value = db_get_column_value(column);
-		db_convert_value_to_string(value, sqltype, &str);
-		colname = db_get_column_name(column);
-
-		G_debug(2, "%s: %s", colname, db_get_string(&str));
-
-		sprintf(buf, "%s : %s\n", colname, db_get_string(&str));
-		db_append_string(&html, buf);
-	    }
-	}
-    }
-    G_debug(2, "FORM STRING:\n%s\n", db_get_string(&html));
-    db_close_cursor(&cursor);
-    db_close_database(driver);
-    db_shutdown_driver(driver);
-
-    *form = G_store(db_get_string(&html));
-
-    db_free_string(&sql);
-    db_free_string(&html);
-    db_free_string(&str);
-
-    return 0;
-}

Deleted: grass/branches/develbranch_6/vector/v.digit/open.c
===================================================================
--- grass/branches/develbranch_6/vector/v.digit/open.c	2009-09-02 21:22:20 UTC (rev 38953)
+++ grass/branches/develbranch_6/vector/v.digit/open.c	2009-09-03 09:42:23 UTC (rev 38954)
@@ -1,58 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include <grass/gis.h>
-#include <grass/dbmi.h>
-#include "global.h"
-#include "proto.h"
-
-int first = 1;
-
-/* Open new form
- *
- *  returns: 0 success
- */
-int F_open(char *title, char *html)
-{
-    int len;
-    char *buf;
-
-    if (first) {
-	Tcl_Eval(Toolbox, "init_form");
-	first = 0;
-    }
-
-    G_debug(2, "PARENT HTML:\n%s\n", html);
-
-    len = strlen(title) + strlen(html) + 20;
-    buf = G_malloc(len);
-    sprintf(buf, "open_form {%s} {%s}", title, html);
-    Tcl_Eval(Toolbox, buf);
-    G_free(buf);
-
-    return 0;
-}
-
-/* Clear old forms from window
- *
- */
-void F_clear(void)
-{
-    G_debug(2, "F_clear()");
-
-    if (first)
-	return;
-
-    Tcl_Eval(Toolbox, "clear_form");
-}
-
-void F_close(void)
-{
-    G_debug(2, "F_close()");
-
-    if (first)
-	return;
-
-    Tcl_Eval(Toolbox, "done_form");
-
-    first = 1;
-}



More information about the grass-commit mailing list