apinewc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file is specific to libplot, rather than libplotter. It defines
  16. the new (i.e., thread-safe) C API. The new C API contains wrappers
  17. around the operations that may be applied to any Plotter object, plus
  18. two additional functions (pl_newpl_r, pl_deletepl_r) that are specific
  19. to libplot.
  20. pl_newpl_r/pl_deletepl_r construct and destroy Plotter instances. Their
  21. names resemble the C++ operations `new' and `delete'. When a new
  22. Plotter of any type is constructed, the appropriate `default_init'
  23. structure, which contains function pointers, is copied into it. Then
  24. its `initialize' method is invoked. Before the Plotter is destroyed,
  25. its `terminate' method is invoked similarly.
  26. The C API also includes the functions pl_newplparams/pl_deleteplparams,
  27. which create/destroy PlotterParams instances, and wrappers around the
  28. methods that may be applied to any PlotterParams object. A pointer to a
  29. PlotterParams object is passed to pl_newpl_r(). It specifies the
  30. device-driver parameters of the Plotter that will be created. */
  31. #include "sys-defines.h"
  32. #include "extern.h"
  33. #include "plot.h" /* header file for C API */
  34. /* Known Plotter types, indexed into by a short mnemonic case-insensitive
  35. string: "generic"=generic (i.e. base Plotter class), "bitmap"=bitmap,
  36. "meta"=metafile, "tek"=Tektronix, "regis"=ReGIS, "hpgl"=HP-GL/2,
  37. "pcl"=PCL 5, "fig"=xfig, "cgm"=CGM, "ps"=PS, "ai"="AI", "svg"=SVG,
  38. "gif"=GIF, "pnm"=PNM (i.e. PBM/PGM/PPM), "z"=PNG, "X"=X11,
  39. "Xdrawable"=X11Drawable. */
  40. typedef struct
  41. {
  42. const char *name;
  43. const Plotter *default_init;
  44. }
  45. Plotter_data;
  46. /* Initializations for the function-pointer part of each type of Plotter.
  47. Each of the initializing structures listed here is defined in the
  48. corresponding ?_defplot.c file. */
  49. static const Plotter_data _plotter_data[] =
  50. {
  51. {"generic", &_pl_g_default_plotter},
  52. {"bitmap", &_pl_b_default_plotter},
  53. {"meta", &_pl_m_default_plotter},
  54. {"tek", &_pl_t_default_plotter},
  55. {"regis", &_pl_r_default_plotter},
  56. {"hpgl", &_pl_h_default_plotter},
  57. {"pcl", &_pl_q_default_plotter},
  58. {"fig", &_pl_f_default_plotter},
  59. {"cgm", &_pl_c_default_plotter},
  60. {"ps", &_pl_p_default_plotter},
  61. {"ai", &_pl_a_default_plotter},
  62. {"svg", &_pl_s_default_plotter},
  63. {"gif", &_pl_i_default_plotter},
  64. {"pnm", &_pl_n_default_plotter},
  65. #ifdef INCLUDE_PNG_SUPPORT
  66. {"png", &_pl_z_default_plotter},
  67. #endif
  68. #ifndef X_DISPLAY_MISSING
  69. {"Xdrawable", &_pl_x_default_plotter},
  70. {"X", &_pl_y_default_plotter},
  71. #endif /* not X_DISPLAY_MISSING */
  72. {(const char *)NULL, (const Plotter *)NULL}
  73. };
  74. /* forward references */
  75. static bool _string_to_plotter_data (const char *type, int *position);
  76. static void _api_warning (const char *msg);
  77. /* These are two user-callable functions that are specific to the new
  78. (i.e., thread-safe) C binding: pl_newpl_r, pl_deletepl_r. */
  79. Plotter *
  80. pl_newpl_r (const char *type, FILE *infile, FILE *outfile, FILE *errfile, const PlotterParams *plotter_params)
  81. {
  82. bool found;
  83. int position;
  84. Plotter *_plotter;
  85. /* determine initialization for specified plotter type */
  86. found = _string_to_plotter_data (type, &position);
  87. if (!found)
  88. {
  89. _api_warning ("ignoring request to create plotter of unknown type");
  90. return NULL;
  91. }
  92. /* create Plotter, copy function pointers to it */
  93. _plotter = (Plotter *)_pl_xmalloc (sizeof(Plotter));
  94. memcpy (_plotter, _plotter_data[position].default_init, sizeof(Plotter));
  95. /* create PlotterData structure, install it in Plotter */
  96. _plotter->data = (plPlotterData *)_pl_xmalloc (sizeof(plPlotterData));
  97. /* copy parameters to it */
  98. _plotter->data->infp = infile;
  99. _plotter->data->outfp = outfile;
  100. _plotter->data->errfp = errfile;
  101. _pl_g_copy_params_to_plotter (_plotter, plotter_params);
  102. /* do any additional needed initializiations of the Plotter (e.g.,
  103. initialize data members of the PlotterData structure in a
  104. device-dependent way); also add the Plotter to the _plotters[] array */
  105. _plotter->initialize (_plotter);
  106. return _plotter;
  107. }
  108. /* utility function, used above; keys into table of Plotter types by a
  109. short mnemonic string */
  110. static bool
  111. _string_to_plotter_data (const char *type, int *position)
  112. {
  113. const Plotter_data *p = _plotter_data;
  114. bool found = false;
  115. int i = 0;
  116. /* search table of known plotter type mnemonics */
  117. while (p->name)
  118. {
  119. if (strcasecmp ((char *)type, (char *)p->name) == 0)
  120. {
  121. found = true;
  122. break;
  123. }
  124. p++;
  125. i++;
  126. }
  127. /* return pointer to plotter data through pointer */
  128. if (found)
  129. *position = i;
  130. return found;
  131. }
  132. int
  133. pl_deletepl_r (Plotter *_plotter)
  134. {
  135. if (_plotter == NULL)
  136. {
  137. _api_warning ("ignoring request to delete a null Plotter");
  138. return -1;
  139. }
  140. /* if luser left the Plotter open, close it */
  141. if (_plotter->data->open)
  142. _API_closepl (_plotter);
  143. /* Invoke an internal Plotter method before deletion. At a minimum, this
  144. private `terminate' method, frees instance-specific copies of class
  145. parameters, and also removes the pointer to the Plotter instance from
  146. the _plotters[] array.
  147. Also, it writes any unwritten graphics to the Plotter's output stream.
  148. This is the case for PSPlotters in particular, which write graphics
  149. only when they are deleted. For a PSPlotter, the terminate method
  150. emits the Plotter's pages of graphics to its output stream and then
  151. deallocates associated storage. For an XPlotter, this method kills
  152. the forked-off processes that are maintaining its popped-up windows
  153. (if any), provided that the VANISH_ON_DELETE parameter is set. */
  154. _plotter->terminate (_plotter);
  155. /* tear down the PlotterData structure */
  156. free (_plotter->data);
  157. /* tear down the Plotter itself */
  158. free (_plotter);
  159. return 0;
  160. }
  161. /* function used in this file to print warning messages */
  162. static void
  163. _api_warning (const char *msg)
  164. {
  165. if (pl_libplot_warning_handler != NULL)
  166. (*pl_libplot_warning_handler)(msg);
  167. else
  168. fprintf (stderr, "libplot: %s\n", msg);
  169. }
  170. /* These are two user-callable functions that are specific to the new
  171. (i.e., thread-safe) C binding: pl_newplparams, pl_deleteplparams,
  172. pl_copyplparams. */
  173. PlotterParams *
  174. pl_newplparams (void)
  175. {
  176. int i;
  177. PlotterParams *_plotter_params_p;
  178. /* create PlotterParams, copy function pointers to it */
  179. _plotter_params_p = (PlotterParams *)_pl_xmalloc (sizeof(PlotterParams));
  180. memcpy (_plotter_params_p, &_default_plotter_params, sizeof(PlotterParams));
  181. /* null out all parameters */
  182. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  183. _plotter_params_p->plparams[i] = (void *)NULL;
  184. return _plotter_params_p;
  185. }
  186. int
  187. pl_deleteplparams (PlotterParams *_plotter_params_p)
  188. {
  189. int i;
  190. /* free all copied strings, and the structure itself */
  191. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  192. if (_known_params[i].is_string && _plotter_params_p->plparams[i] != NULL)
  193. free (_plotter_params_p->plparams[i]);
  194. free (_plotter_params_p);
  195. return 0;
  196. }
  197. PlotterParams *
  198. pl_copyplparams (const PlotterParams *_plotter_params_p)
  199. {
  200. int i;
  201. PlotterParams *new_plotter_params_p;
  202. /* create PlotterParams, copy function pointers to it */
  203. new_plotter_params_p = (PlotterParams *)_pl_xmalloc (sizeof(PlotterParams));
  204. memcpy (new_plotter_params_p, &_default_plotter_params, sizeof(PlotterParams));
  205. /* copy all parameters */
  206. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  207. new_plotter_params_p->plparams[i] = _plotter_params_p->plparams[i];
  208. return new_plotter_params_p;
  209. }
  210. /* The following are C wrappers around the public functions in the
  211. PlotterParams class. Together with the preceding functions, they are
  212. part of the new (i.e., thread-safe) C API. */
  213. int
  214. pl_setplparam (PlotterParams *plotter_params, const char *parameter, void * value)
  215. {
  216. return plotter_params->setplparam (plotter_params, parameter, value);
  217. }
  218. /* END OF WRAPPERS AROUND PLOTTERPARAMS METHODS */