g_param.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 defines the PlotterParams class, which is a helper class.
  16. A PlotterParams object is used for specifying device driver parameters
  17. when a Plotter is instantiated.
  18. (In libplot, a PlotterParams struct is created with pl_newplparams() and
  19. deleted with pl_deleteplparams(). These are defined in apinewc.c; there
  20. is also a copy constructor, pl_copyplparams().)
  21. This file also includes the functions that are used for copying the
  22. parameters into the Plotter at instantiation time, and for accessing
  23. them later. These are Plotter class members. */
  24. #include "sys-defines.h"
  25. #include "extern.h"
  26. #ifndef LIBPLOTTER
  27. /* In libplot, this is the initialization for the function-pointer part of
  28. a PlotterParams struct. */
  29. const PlotterParams _default_plotter_params =
  30. {
  31. /* methods */
  32. _setplparam
  33. };
  34. #endif /* not LIBPLOTTER */
  35. #ifdef LIBPLOTTER
  36. PlotterParams::PlotterParams ()
  37. {
  38. int i;
  39. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  40. plparams[i] = (void *)NULL;
  41. }
  42. PlotterParams::~PlotterParams ()
  43. {
  44. int i;
  45. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  46. if (_known_params[i].is_string && plparams[i] != NULL)
  47. free (plparams[i]);
  48. }
  49. PlotterParams::PlotterParams (const PlotterParams& oldPlotterParams)
  50. {
  51. int i;
  52. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  53. plparams[i] = oldPlotterParams.plparams[i];
  54. }
  55. PlotterParams& PlotterParams::operator= (const PlotterParams& oldPlotterParams)
  56. {
  57. int i;
  58. for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
  59. plparams[i] = oldPlotterParams.plparams[i];
  60. return (*this);
  61. }
  62. #endif
  63. /* The parameter-setting method. This is a PlotterParams method in
  64. libplotter (i.e. it is #defined to be PlotterParams::setplparam). In
  65. libplot, a pointer to a PlotterParams struct must be passed to it as its
  66. first argument. */
  67. int
  68. _setplparam (R___(PlotterParams *_plotter_params) const char *parameter, void * value)
  69. {
  70. int j;
  71. for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
  72. {
  73. if (strcmp (_known_params[j].parameter, parameter) == 0)
  74. {
  75. if (_known_params[j].is_string)
  76. /* parameter value is a string, so treat specially: copy the
  77. string, byte by byte */
  78. {
  79. if (_plotter_params->plparams[j])
  80. free (_plotter_params->plparams[j]);
  81. if (value != NULL)
  82. {
  83. _plotter_params->plparams[j] =
  84. (char *)_pl_xmalloc (strlen ((char *)value) + 1);
  85. strcpy ((char *)_plotter_params->plparams[j], (char *)value);
  86. }
  87. else
  88. _plotter_params->plparams[j] = NULL;
  89. }
  90. else
  91. /* parameter value is a (void *), so just copy the
  92. user-specified pointer */
  93. _plotter_params->plparams[j] = value;
  94. /* matched, so return happily */
  95. return 0;
  96. }
  97. }
  98. /* silently ignore requests to set unknown parameters */
  99. return 0;
  100. }
  101. /**********************************************************************/
  102. /* This function is called when a Plotter is instantiated. It copies
  103. parameters from a PlotterParams object into the Plotter. We adopt the
  104. following convention: if the PlotterParams object does not include a
  105. value for a parameter, a default value (retrieved in the global
  106. _known_params[] array) is used. Unless, that is, an environment
  107. variable of the same name has been set, in which case its value is used
  108. instead.
  109. We support both parameters whose values are strings (which must be
  110. copied byte-by-byte) and those whose values are void pointers (which may
  111. simply be copied. */
  112. void
  113. _pl_g_copy_params_to_plotter (R___(Plotter *_plotter) const PlotterParams *plotter_params)
  114. {
  115. int j;
  116. char *envs;
  117. for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
  118. {
  119. if (!_known_params[j].is_string)
  120. /* not a string, just copy the void pointer into the plotter */
  121. _plotter->data->params[j] = plotter_params->plparams[j];
  122. else
  123. /* parameter value is a string, so use malloc and strcpy */
  124. {
  125. if (plotter_params->plparams[j])
  126. /* have user-specified value */
  127. {
  128. _plotter->data->params[j] =
  129. (char *)_pl_xmalloc (strlen ((char *)plotter_params->plparams[j]) + 1);
  130. strcpy ((char *)_plotter->data->params[j],
  131. (char *)plotter_params->plparams[j]);
  132. }
  133. else if ((envs = getenv (_known_params[j].parameter)) != NULL)
  134. /* have value of environment variable */
  135. {
  136. _plotter->data->params[j] =
  137. (char *)_pl_xmalloc (strlen (envs) + 1);
  138. strcpy ((char *)_plotter->data->params[j], envs);
  139. }
  140. else if (_known_params[j].default_value)
  141. /* have default libplot value */
  142. {
  143. _plotter->data->params[j] =
  144. (char *)_pl_xmalloc (strlen ((char *)_known_params[j].default_value) + 1);
  145. strcpy ((char *)_plotter->data->params[j],
  146. (char *)_known_params[j].default_value);
  147. }
  148. else /* punt */
  149. _plotter->data->params[j] = NULL;
  150. }
  151. }
  152. }
  153. /* This retrieves the value of any specified Plotter parameter,
  154. as stored in a Plotter instance. */
  155. void *
  156. _get_plot_param (const plPlotterData *data, const char *parameter_name)
  157. {
  158. int j;
  159. for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
  160. if (strcmp (_known_params[j].parameter, parameter_name) == 0)
  161. return data->params[j];
  162. return (void *)NULL; /* name not matched */
  163. }
  164. /* This function is called when a Plotter is deleted, to delete the
  165. instance-specific copies of Plotter parameters. */
  166. void
  167. _pl_g_free_params_in_plotter (S___(Plotter *_plotter))
  168. {
  169. int j;
  170. /* deallocate stored values of class variables */
  171. for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
  172. if (_known_params[j].is_string && _plotter->data->params[j] != NULL)
  173. /* stored parameter is a previously malloc'd string, so free it */
  174. free (_plotter->data->params[j]);
  175. }
  176. /* This retrieves the default value of any specified Plotter parameter.
  177. Default values for each parameter are stored in the _known_params[]
  178. array, which is read-only global data. So unlike the preceding
  179. functions, this is not a Plotter method. */
  180. void *
  181. _get_default_plot_param (const char *parameter_name)
  182. {
  183. int j;
  184. for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
  185. if (strcmp (_known_params[j].parameter, parameter_name) == 0)
  186. return _known_params[j].default_value;
  187. return (void *)NULL; /* name not matched */
  188. }