⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnuplot_i.c

📁 图像置乱代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-------------------------------------------------------------------------*//**  @file		gnuplot_i.c  @author	N. Devillard  @date	Sep 1998  @version	$Revision: 2.10 $  @brief	C interface to gnuplot.  gnuplot is a freely available, command-driven graphical display tool for  Unix. It compiles and works quite well on a number of Unix flavours as  well as other operating systems. The following module enables sending  display requests to gnuplot through simple C calls.  *//*--------------------------------------------------------------------------*//*	$Id: gnuplot_i.c,v 2.10 2003/01/27 08:58:04 ndevilla Exp $	$Author: ndevilla $	$Date: 2003/01/27 08:58:04 $	$Revision: 2.10 $ *//*---------------------------------------------------------------------------                                Includes ---------------------------------------------------------------------------*/#include "gnuplot_i.h"/*---------------------------------------------------------------------------                                Defines ---------------------------------------------------------------------------*//** Maximal size of a gnuplot command */#define GP_CMD_SIZE     	2048/** Maximal size of a plot title */#define GP_TITLE_SIZE   	80/** Maximal size for an equation */#define GP_EQ_SIZE      	512/** Maximal size of a name in the PATH */#define PATH_MAXNAMESZ       4096/** Define P_tmpdir if not defined (this is normally a POSIX symbol) */#ifndef P_tmpdir#define P_tmpdir "."#endif/*---------------------------------------------------------------------------                            Function codes ---------------------------------------------------------------------------*//*-------------------------------------------------------------------------*//**  @brief	Find out where a command lives in your PATH.  @param	pname Name of the program to look for.  @return	pointer to statically allocated character string.  This is the C equivalent to the 'which' command in Unix. It parses  out your PATH environment variable to find out where a command  lives. The returned character string is statically allocated within  this function, i.e. there is no need to free it. Beware that the  contents of this string will change from one call to the next,  though (as all static variables in a function).  The input character string must be the name of a command without  prefixing path of any kind, i.e. only the command name. The returned  string is the path in which a command matching the same name was  found.  Examples (assuming there is a prog named 'hello' in the cwd):  @verbatim  gnuplot_get_program_path("hello") returns "."  gnuplot_get_program_path("ls") returns "/bin"  gnuplot_get_program_path("csh") returns "/usr/bin"  gnuplot_get_program_path("/bin/ls") returns NULL  @endverbatim   *//*-------------------------------------------------------------------------*/char * gnuplot_get_program_path(char * pname){    int         i, j, lg;    char    *   path;    static char buf[PATH_MAXNAMESZ];    /* Trivial case: try in CWD */    sprintf(buf, "./%s", pname) ;    if (access(buf, X_OK)==0) {        sprintf(buf, ".");        return buf ;    }    /* Try out in all paths given in the PATH variable */    buf[0] = 0;    path = getenv("PATH") ;    if (path!=NULL) {        for (i=0; path[i]; ) {            for (j=i ; (path[j]) && (path[j]!=':') ; j++);            lg = j - i;            strncpy(buf, path + i, lg);            if (lg == 0) buf[lg++] = '.';            buf[lg++] = '/';            strcpy(buf + lg, pname);            if (access(buf, X_OK) == 0) {                /* Found it! */                break ;            }            buf[0] = 0;            i = j;            if (path[i] == ':') i++ ;        }    } else {		fprintf(stderr, "PATH variable not set\n");	}    /* If the buffer is still empty, the command was not found */    if (buf[0] == 0) return NULL ;    /* Otherwise truncate the command name to yield path only */    lg = strlen(buf) - 1 ;    while (buf[lg]!='/') {        buf[lg]=0 ;        lg -- ;    }    buf[lg] = 0;    return buf ;}/*-------------------------------------------------------------------------*//**  @brief	Opens up a gnuplot session, ready to receive commands.  @return	Newly allocated gnuplot control structure.  This opens up a new gnuplot session, ready for input. The struct  controlling a gnuplot session should remain opaque and only be  accessed through the provided functions.  The session must be closed using gnuplot_close(). *//*--------------------------------------------------------------------------*/gnuplot_ctrl * gnuplot_init(void){    gnuplot_ctrl *  handle ;    if (getenv("DISPLAY") == NULL) {        fprintf(stderr, "cannot find DISPLAY variable: is it set?\n") ;    }	if (gnuplot_get_program_path("gnuplot")==NULL) {		fprintf(stderr, "cannot find gnuplot in your PATH");		return NULL ;	}    /*      * Structure initialization:     */    handle = (gnuplot_ctrl*)malloc(sizeof(gnuplot_ctrl)) ;    handle->nplots = 0 ;    gnuplot_setstyle(handle, "points") ;    handle->ntmp = 0 ;    handle->gnucmd = popen("gnuplot", "w") ;    if (handle->gnucmd == NULL) {        fprintf(stderr, "error starting gnuplot\n") ;        free(handle) ;        return NULL ;    }    return handle;}/*-------------------------------------------------------------------------*//**  @brief	Closes a gnuplot session previously opened by gnuplot_init()  @param	handle Gnuplot session control handle.  @return	void  Kills the child PID and deletes all opened temporary files.  It is mandatory to call this function to close the handle, otherwise  temporary files are not cleaned and child process might survive. *//*--------------------------------------------------------------------------*/void gnuplot_close(gnuplot_ctrl * handle){    int     i ;	    if (pclose(handle->gnucmd) == -1) {        fprintf(stderr, "problem closing communication to gnuplot\n") ;        return ;    }    if (handle->ntmp) {        for (i=0 ; i<handle->ntmp ; i++) {            remove(handle->to_delete[i]) ;        }    }    free(handle) ;    return ;}/*-------------------------------------------------------------------------*//**  @brief	Sends a command to an active gnuplot session.  @param	handle Gnuplot session control handle  @param	cmd    Command to send, same as a printf statement.  This sends a string to an active gnuplot session, to be executed.  There is strictly no way to know if the command has been  successfully executed or not.  The command syntax is the same as printf.  Examples:  @code  gnuplot_cmd(g, "plot %d*x", 23.0);  gnuplot_cmd(g, "plot %g * cos(%g * x)", 32.0, -3.0);  @endcode  Since the communication to the gnuplot process is run through  a standard Unix pipe, it is only unidirectional. This means that  it is not possible for this interface to query an error status  back from gnuplot. *//*--------------------------------------------------------------------------*/void gnuplot_cmd(gnuplot_ctrl *  handle, char *  cmd, ...){    va_list ap ;    char    local_cmd[GP_CMD_SIZE];    va_start(ap, cmd);    vsprintf(local_cmd, cmd, ap);    va_end(ap);    strcat(local_cmd, "\n");    fputs(local_cmd, handle->gnucmd) ;    fflush(handle->gnucmd) ;    return ;}/*-------------------------------------------------------------------------*//**  @brief	Change the plotting style of a gnuplot session.  @param	h Gnuplot session control handle  @param	plot_style Plotting-style to use (character string)  @return	void  The provided plotting style is a character string. It must be one of  the following:  - lines  - points  - linespoints  - impulses  - dots  - steps  - errorbars  - boxes  - boxeserrorbars *//*--------------------------------------------------------------------------*/void gnuplot_setstyle(gnuplot_ctrl * h, char * plot_style) {    if (strcmp(plot_style, "lines") &&        strcmp(plot_style, "points") &&        strcmp(plot_style, "linespoints") &&        strcmp(plot_style, "impulses") &&        strcmp(plot_style, "dots") &&        strcmp(plot_style, "steps") &&        strcmp(plot_style, "errorbars") &&        strcmp(plot_style, "boxes") &&        strcmp(plot_style, "boxerrorbars")) {        fprintf(stderr, "warning: unknown requested style: using points\n") ;        strcpy(h->pstyle, "points") ;    } else {        strcpy(h->pstyle, plot_style) ;    }    return ;}/*-------------------------------------------------------------------------*//**  @brief	Sets the x label of a gnuplot session.  @param	h Gnuplot session control handle.  @param	label Character string to use for X label.  @return	void  Sets the x label for a gnuplot session. *//*--------------------------------------------------------------------------*/void gnuplot_set_xlabel(gnuplot_ctrl * h, char * label){    char    cmd[GP_CMD_SIZE] ;    sprintf(cmd, "set xlabel \"%s\"", label) ;    gnuplot_cmd(h, cmd) ;    return ;}/*-------------------------------------------------------------------------*//**  @brief	Sets the y label of a gnuplot session.  @param	h Gnuplot session control handle.  @param	label Character string to use for Y label.  @return	void  Sets the y label for a gnuplot session. *//*--------------------------------------------------------------------------*/void gnuplot_set_ylabel(gnuplot_ctrl * h, char * label){    char    cmd[GP_CMD_SIZE] ;    sprintf(cmd, "set ylabel \"%s\"", label) ;    gnuplot_cmd(h, cmd) ;    return ;}/*-------------------------------------------------------------------------*//**  @brief	Resets a gnuplot session (next plot will erase previous ones).  @param	h Gnuplot session control handle.  @return	void  Resets a gnuplot session, i.e. the next plot will erase all previous  ones. *//*--------------------------------------------------------------------------*/void gnuplot_resetplot(gnuplot_ctrl * h){    int     i ;    if (h->ntmp) {        for (i=0 ; i<h->ntmp ; i++) {            remove(h->to_delete[i]) ;        }    }    h->ntmp = 0 ;    h->nplots = 0 ;    return ;}/*-------------------------------------------------------------------------*//**  @brief	Plots a 2d graph from a list of doubles.  @param	handle	Gnuplot session control handle.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -