From e19eee4a2533fec2c75326bdcabbc3ab8dd5fce8 Mon Sep 17 00:00:00 2001 From: erg Date: Tue, 26 May 2009 21:17:45 +0000 Subject: [PATCH] Cleaned up and fixed gvpr code. --- cmd/smyrna/gui/menucallbacks.c | 88 ++++++++++------------- cmd/smyrna/gvprpipe.c | 125 +++++++++++++++++++++++++-------- 2 files changed, 134 insertions(+), 79 deletions(-) diff --git a/cmd/smyrna/gui/menucallbacks.c b/cmd/smyrna/gui/menucallbacks.c index 5043ecfe4..7748a691b 100755 --- a/cmd/smyrna/gui/menucallbacks.c +++ b/cmd/smyrna/gui/menucallbacks.c @@ -19,6 +19,7 @@ #include "topview.h" #include "tvnodes.h" #include "selection.h" +#include "gvprpipe.h" #include "topviewsettings.h" #include "gltemplate.h" #include @@ -399,7 +400,6 @@ void mTestgvpr(GtkWidget * widget, gpointer user_data) { // apply_gvpr(view->g[view->activeGraph],"c:/graphviz-ms/bin/makered.g"); int charcnt; - char bf[512]; char* bf2; GtkTextBuffer * gtkbuf; GtkTextIter* startit; @@ -412,63 +412,52 @@ void mTestgvpr(GtkWidget * widget, gpointer user_data) gtk_text_buffer_get_end_iter (gtkbuf,endit); bf2=gtk_text_buffer_get_text(gtkbuf,startit,endit,0); - if(save_gvpr_program(bf2,&bf)) - apply_gvpr(view->g[view->activeGraph],bf); - + run_gvpr (view->g[view->activeGraph], bf2); } - - - /* - opens a file open dialog and load a gvpr program to gvpr script text box - if the current script is modified, user should be informed about it + opens a file open dialog and load a gvpr program to gvpr script text box + if the current script is modified, user should be informed about it */ -void on_gvprbuttonload_clicked(GtkWidget * widget, gpointer user_data) +void +on_gvprbuttonload_clicked(GtkWidget * widget, gpointer user_data) { FILE *input_file=NULL; - char* str; - agxbuf xbuf; - GtkTextBuffer * gtkbuf; /*GTK buffer from glade GUI*/ + char* str; + agxbuf xbuf; + GtkTextBuffer * gtkbuf; /*GTK buffer from glade GUI*/ + + char buf[BUFSIZ]; + unsigned char xbuffer[BUFSIZ]; + + agxbinit (&xbuf, SMALLBUF, xbuffer); - char c[2]; - agxbinit (&xbuf, SMALLBUF, NULL); /*file name should be returned in xbuf*/ - if(openfiledlg(0,NULL,&xbuf)) - { - input_file = fopen(agxbuse (&xbuf), "r"); - if (input_file) - { - - while(1) - { - c[0] = fgetc(input_file); - c[1]= NULL;/*agxbuffer doesnt accept single char, probably strlen is used , FIX ME!!*/ - if(c[0] !=EOF) - agxbput (&xbuf,c); - else - break; /* ...break when EOF is reached */ - } - gtkbuf=gtk_text_view_get_buffer((GtkTextView*) glade_xml_get_widget(xml,"gvprtextinput")); - str=agxbuse (&xbuf); - printf ("%ds",strlen(str)); - if(g_utf8_validate(str,-1,NULL)) - { - gtk_text_buffer_set_text (gtkbuf,agxbuse (&xbuf),-1); - } - else - { - show_gui_warning ("File format is not UTF8!"); - } - fclose (input_file); - } - else - { - show_gui_warning ("file couldnt be opened\n"); - - } + if (openfiledlg(0,NULL,&xbuf)) { + input_file = fopen(agxbuse (&xbuf), "r"); + if (input_file) { + while (fgets(buf, BUFSIZ, input_file)) { + agxbput (&xbuf, buf); + } + gtkbuf = gtk_text_view_get_buffer((GtkTextView*) glade_xml_get_widget(xml,"gvprtextinput")); + str=agxbuse (&xbuf); +#if 0 + printf ("%ds",(size_t)strlen(str)); +#endif + if(g_utf8_validate(str,-1,NULL)) { + gtk_text_buffer_set_text (gtkbuf, str, -1); + } + else { + show_gui_warning ("File format is not UTF8!"); + } + fclose (input_file); + } + else { + show_gui_warning ("file couldn't be opened\n"); } + } + agxbfree (&xbuf); } /* @@ -478,11 +467,8 @@ void on_gvprbuttonload_clicked(GtkWidget * widget, gpointer user_data) void on_gvprbuttonsave_clicked(GtkWidget * widget, gpointer user_data) { FILE *output_file=NULL; - char* str; agxbuf xbuf; - GtkTextBuffer * gtkbuf; /*GTK buffer from glade GUI*/ - char c[2]; agxbinit (&xbuf, SMALLBUF, NULL); /*file name should be returned in xbuf*/ if(savefiledlg(0,NULL,&xbuf)) diff --git a/cmd/smyrna/gvprpipe.c b/cmd/smyrna/gvprpipe.c index 8223b6f6f..fc06675d2 100644 --- a/cmd/smyrna/gvprpipe.c +++ b/cmd/smyrna/gvprpipe.c @@ -18,12 +18,15 @@ #include #ifdef WIN32 -#define BUFSIZE 4096 #include #include #include +#else +#include +#include #endif +#include #include #include @@ -170,8 +173,10 @@ CreateChildProcess(TCHAR* szCmdline,HANDLE g_hChildStd_IN_Rd,HANDLE g_hChildStd_ } else { -/* GetExitCodeProcess( piProcInfo.hProcess,&lpExitCode); +#if 0 + GetExitCodeProcess( piProcInfo.hProcess,&lpExitCode); if (lpExitCode==259) /*still alive*/ +#endif } } @@ -206,14 +211,14 @@ static void createpipes() #endif -Agraph_t* -exec_gvpr(char* filename,Agraph_t* srcGraph) +static Agraph_t* +exec_gvpr(Agraph_t* srcGraph, char* filename) { Agraph_t* G; - DWORD lpExitCode; unsigned char bf[SMALLBUF]; agxbuf xbuf; #ifdef WIN32 + DWORD lpExitCode; Agiodisc_t* xio; Agiodisc_t a; init_security_attr(&saAttr); @@ -240,43 +245,107 @@ exec_gvpr(char* filename,Agraph_t* srcGraph) if (lpExitCode!=259) /*still alive?*/ G=NULL; else - G = ReadFromPipe() ; + G = ReadFromPipe() ; + CloseHandle(piProcInfo.hProcess); + CloseHandle(piProcInfo.hThread); #else pp = popen (agxbuse (&xbuf), "r+"); agwrite(srcGraph, pp); G = agread(pp, NULL) ; pclose (pp); + unlink (filename); #endif - CloseHandle(piProcInfo.hProcess); - CloseHandle(piProcInfo.hThread); return G; } -/* - saves a gvpr file as a temporary file ,returns file name's full path -*/ -int save_gvpr_program(char* prg,char* bf) +/* mkTemp: + */ +#ifdef WIN32 + +static char* +mkTempFile () { - FILE *input_file=NULL; - char buf[512]; - char buf2[512]; - GetTempPath(512,buf); - if(!GetTempFileName( buf,"gvpr",NULL,buf2)) - return 0; - input_file = fopen(buf2, "w"); - strcpy(bf,buf2); - if (input_file) - { - fprintf(input_file,"%s",prg); - fclose (input_file); - return 1; - } - return 0; + char buf[512]; + char buf2[512]; + + GetTempPath(512,buf); + if (!GetTempFileName(buf,"gvpr",NULL,buf2)) + return NULL; + return strdup (buf2); +} + +#else +#define TMP_TEMPLATE "/tmp/_gvprXXXXXX" + +static char* +mkTempFile () +{ + char* name = strdup (TMP_TEMPLATE); + + int rv = mkstemp (name); + if (rv < 0) { + free (name); + return NULL; + } + else { + close (rv); + return name; + } } +#endif +/* + * saves a gvpr file as a temporary file, + * returns file name's full path or NULL on failure. + * Buffer is malloced, so must be freed. + */ +static char* +save_gvpr_program (char* prg) +{ + FILE *fp; + char* tmpname; + + tmpname = mkTempFile (); + fp = fopen(tmpname, "w"); + if (fp) { + fputs (prg, fp); + fclose (fp); + return tmpname; + } + else { +#ifndef WIN32 + unlink (tmpname); +#endif + free (tmpname); + return NULL; + } +} +static int +apply_gvpr(Agraph_t* g,char* prog) +{ + Agraph_t* tempg = exec_gvpr (g, prog); + if (tempg) { + add_graph_to_viewport(tempg); + return 0; + } + else { + fprintf (stderr, "gvpr failed!\n"); + return 1; + } +} - +int run_gvpr (Agraph_t* srcGraph, char* script) +{ + char* tmpf; + int rv = 1; + + if ((tmpf = save_gvpr_program(script))) { + rv = apply_gvpr (srcGraph, tmpf); + free (tmpf); + } + return rv; +} -- 2.40.0