libdspl-2.0
Digital Signal Processing Algorithm Library
gnuplot.c
1 /*
2 * Copyright (c) 2015-2019 Sergey Bakhurin
3 * Digital Signal Processing Library [http://dsplib.org]
4 *
5 * This file is part of DSPL.
6 *
7 * is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * DSPL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #include <stdio.h>
21 #include <unistd.h>
22 #include "dspl.h"
23 
24 #define GNUPLOT_NO 1
25 #define GNUPLOT_WIN 3
26 #define GNUPLOT_PNG 4
27 
28 
29 
30 #ifdef DOXYGEN_ENGLISH
31 
113 #endif
114 #ifdef DOXYGEN_RUSSIAN
115 
199 #endif
200 int DSPL_API gnuplot_create(int argc, char* argv[],
201  int w, int h, char* fn_png,
202  void** hplot)
203 {
204  FILE* hp;
205  char str[1024] = {0};
206  int state = 0;
207  int err;
208  if(argc>1 && !argv)
209  return ERROR_ARG_PARAM;
210 
211  if(argc < 2)
212  state = GNUPLOT_WIN;
213  else
214  {
215  if(!strcmp(argv[1], "--noplot"))
216  state = GNUPLOT_NO;
217  if(!strcmp(argv[1], "--plotwin"))
218  state = GNUPLOT_WIN;
219  if(!strcmp(argv[1], "--plotpng"))
220  state = GNUPLOT_PNG;
221  }
222  if(!hplot)
223  return ERROR_PTR;
224 
225  switch(state)
226  {
227  case GNUPLOT_NO:
228  hp = NULL;
229 
230  err = RES_OK;
231  break;
232 
233  case GNUPLOT_WIN:
234  hp = popen("gnuplot -p", "w");
235  if(!hp)
236  return ERROR_GNUPLOT_CREATE;
237  memset(str, 0, 1024*sizeof(char));
238  sprintf(str, "set terminal wxt size %d,%d", w,h);
239  gnuplot_cmd(hp, str);
240 
241  err = RES_OK;
242  break;
243 
244  case GNUPLOT_PNG:
245  if(!fn_png)
246  return ERROR_GNUPLOT_FNPNG;
247  hp = popen("gnuplot -p", "w");
248  if(!hp)
249  return ERROR_GNUPLOT_CREATE;
250  memset(str, 0, 1024*sizeof(char));
251  sprintf(str, "set terminal pngcairo size %d,%d\
252  enhanced font 'Verdana,8'", w, h);
253 
254  gnuplot_cmd(hp, str);
255 
256  memset(str, 0, 1024*sizeof(char));
257  sprintf(str, "set output '%s'", fn_png);
258  gnuplot_cmd(hp, str);
259  err = RES_OK;
260  break;
261 
262  default:
263  err = ERROR_GNUPLOT_TERM;
264  hp = NULL;
265  }
266  *hplot = hp;
267  return err;
268 }
269 
270 
271 
272 
273 #ifdef DOXYGEN_ENGLISH
274 
298 #endif
299 #ifdef DOXYGEN_RUSSIAN
300 
324 #endif
325 void DSPL_API gnuplot_close(void* h)
326 {
327  if(h)
328  fclose((FILE*)h);
329 }
330 
331 
332 
333 
334 #ifdef DOXYGEN_ENGLISH
335 
359 #endif
360 #ifdef DOXYGEN_RUSSIAN
361 
389 #endif
390 void DSPL_API gnuplot_cmd(void* h, char* cmd)
391 {
392  if(h)
393  {
394  FILE *pf = (FILE*)h;
395  fprintf(pf, cmd);
396  fflush(pf);
397  fprintf(pf, "\n");
398  fflush(pf);
399  }
400 }
401 
402 
403 
404 
405 
406 #ifdef DOXYGEN_ENGLISH
407 
444 #endif
445 #ifdef DOXYGEN_RUSSIAN
446 
481 #endif
482 int DSPL_API gnuplot_open(void** hplot)
483 {
484  if(!hplot)
485  return ERROR_PTR;
486  *hplot = popen("gnuplot -p", "w");
487 
488  if(!(*hplot))
489  return ERROR_GNUPLOT_CREATE;
490  return RES_OK;
491 }
492 
493 
494 
495 
#define ERROR_GNUPLOT_TERM
Unknown program call parameter specifying the GNUPLOT terminal. This parameter can be one of the fol...
Definition: dspl.h:528
#define ERROR_PTR
Pointer error. This error means that one of the required pointers (memory to be allocated for) is tra...
Definition: dspl.h:545
void DSPL_API gnuplot_close(void *h)
Close GNUPLOT handle.
Definition: gnuplot.c:325
void DSPL_API gnuplot_cmd(void *h, char *cmd)
Function sends cmd command to GNUPLOT corresponds to h handle.
Definition: gnuplot.c:390
#define ERROR_ARG_PARAM
Invalid program call parameter. This error occurs when a program is called with an invalid the number...
Definition: dspl.h:501
#define RES_OK
The function completed correctly. No errors.
Definition: dspl.h:497
#define ERROR_GNUPLOT_CREATE
Unable to connect to the GNUPLOT package. Please check that the package is available.
Definition: dspl.h:526
int DSPL_API gnuplot_open(void **hplot)
Open GNUPLOT program.
Definition: gnuplot.c:482
int DSPL_API gnuplot_create(int argc, char *argv[], int w, int h, char *fn_png, void **hplot)
Create GNUPLOT chart.
Definition: gnuplot.c:200