Advertisement
Guest User

qwerty

a guest
Feb 25th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include "gplot.h"
  2.  
  3. int check_x_display(){
  4. char *disp;
  5. disp = getenv("DISPLAY");
  6. if (disp == NULL){
  7. fprintf(stderr, "nu exista x server");
  8. return 1;
  9. }
  10. else return 0;
  11. }
  12.  
  13. gplot_ctrl *gplot_init(){
  14. if(check_x_display()) return NULL;
  15. gplot_ctrl *handler;
  16. handler = (gplot_ctrl*) malloc (sizeof(gplot_ctrl));
  17. gplot_setstyle(handler, "points");
  18. handler -> gpcmd = popen("gnuplot", "w");
  19. if (handler -> gpcmd != NULL) return handler;
  20. else {
  21. free(handler);
  22. fprintf(stderr, "nu s-a conectat");
  23. return NULL;
  24. }
  25. }
  26.  
  27. void gplot_close(gplot_ctrl *handle){
  28. if (pclose(handler -> gpcmd) == -1){
  29. fprintf(stderr,"conexiunea s-a intrerupt");
  30. return;
  31. }
  32. else{
  33. free(handler);
  34. handler = NULL;
  35. return;
  36. }
  37. }
  38.  
  39. void gplot_setstyle(gplot_ctrl *handle, char *plot_style){
  40. if(strcmp(plot_style, "lines") &&
  41. strcmp(plot_style, "points") &&
  42. strcmp(plot_style, "linespoints") &&
  43. strcmp(plot_style, "inpulses") &&
  44. strcmp(plot_style, "dots") &&
  45. strcmp(plot_style, "steps") &&
  46. strcmp(plot_style, "errorbars") &&
  47. strcmp(plot_style, "boxes") &&
  48. strcmp(plot_style, "boxerrorbars")){
  49. fprintf(stderr, "stil de reprezentare grafic necunoscut; setare stil points");
  50. strcpy(handler -> pstyle, "points");
  51. }
  52. else{
  53. strcpy(handler -> pstyle, plot_style);
  54. }
  55.  
  56. }
  57.  
  58. void gplot_set_xlabel(gplot_ctrl *handle, char *label){
  59. char cmd[CP_CMD_SIZE];
  60. cmd = sprintf(cmd,"set xlabel \"%s\"", label);
  61. gplot_cmd(handler, cmd);
  62. }
  63.  
  64. void gplot_set_ylabel(gplot_ctrl *handle, char *label){
  65. char cmd[CP_CMD_SIZE];
  66. cmd = sprintf(cmd,"set ylabel \"%s\"", label);
  67. gplot_cmd(handler, cmd);
  68. }
  69.  
  70. void gplot_cmd(gplot_ctrl *handle, char cmd*,...){
  71. va_list ap;
  72. char local_cmd[GP_CMD_SIZE];
  73. va_start(ap, cmd);
  74. vsprintf(local_cmd, cmd, ap);
  75. va_end(ap);
  76. strcat(local_cmd, "\n");
  77. fputs(local_cmd, handle -> gpcmd);
  78. fflush(handler->gpcmd);
  79. return;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement