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 
4    The GNU plotutils package is free software.  You may redistribute it
5    and/or modify it under the terms of the GNU General Public License as
6    published by the Free Software foundation; either version 2, or (at your
7    option) any later version.
8 
9    The GNU plotutils package is distributed in the hope that it will be
10    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License along
15    with the GNU plotutils package; see the file COPYING.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
17    Boston, MA 02110-1301, USA. */
18 
19 /* This file defines the PlotterParams class, which is a helper class.
20    A PlotterParams object is used for specifying device driver parameters
21    when a Plotter is instantiated.
22 
23    (In libplot, a PlotterParams struct is created with pl_newplparams() and
24    deleted with pl_deleteplparams().  These are defined in apinewc.c; there
25    is also a copy constructor, pl_copyplparams().)
26 
27    This file also includes the functions that are used for copying the
28    parameters into the Plotter at instantiation time, and for accessing
29    them later.  These are Plotter class members. */
30 
31 #include "sys-defines.h"
32 #include "extern.h"
33 
34 #ifndef LIBPLOTTER
35 /* In libplot, this is the initialization for the function-pointer part of
36    a PlotterParams struct. */
37 const PlotterParams _default_plotter_params =
38 {
39   /* methods */
40   _setplparam
41 };
42 #endif /* not LIBPLOTTER */
43 
44 #ifdef LIBPLOTTER
PlotterParams()45 PlotterParams::PlotterParams ()
46 {
47   int i;
48 
49   for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
50     plparams[i] = (void *)NULL;
51 }
52 
~PlotterParams()53 PlotterParams::~PlotterParams ()
54 {
55   int i;
56 
57   for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
58     if (_known_params[i].is_string && plparams[i] != NULL)
59       free (plparams[i]);
60 }
61 
PlotterParams(const PlotterParams & oldPlotterParams)62 PlotterParams::PlotterParams (const PlotterParams& oldPlotterParams)
63 {
64   int i;
65 
66   for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
67     plparams[i] = oldPlotterParams.plparams[i];
68 }
69 
70 PlotterParams& PlotterParams::operator= (const PlotterParams& oldPlotterParams)
71 {
72   int i;
73 
74   for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
75     plparams[i] = oldPlotterParams.plparams[i];
76   return (*this);
77 }
78 #endif
79 
80 /* The parameter-setting method.  This is a PlotterParams method in
81    libplotter (i.e. it is #defined to be PlotterParams::setplparam).  In
82    libplot, a pointer to a PlotterParams struct must be passed to it as its
83    first argument. */
84 int
_setplparam(R___ (PlotterParams * _plotter_params)const char * parameter,void * value)85 _setplparam (R___(PlotterParams *_plotter_params) const char *parameter, void * value)
86 {
87   int j;
88 
89   for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
90     {
91       if (strcmp (_known_params[j].parameter, parameter) == 0)
92 	{
93 	  if (_known_params[j].is_string)
94 	    /* parameter value is a string, so treat specially: copy the
95 	       string, byte by byte */
96 	    {
97 	      if (_plotter_params->plparams[j])
98 		free (_plotter_params->plparams[j]);
99 	      if (value != NULL)
100 		{
101 		  _plotter_params->plparams[j] =
102 		    (char *)_pl_xmalloc (strlen ((char *)value) + 1);
103 		  strcpy ((char *)_plotter_params->plparams[j], (char *)value);
104 		}
105 	      else
106 		_plotter_params->plparams[j] = NULL;
107 	    }
108 	  else
109 	    /* parameter value is a (void *), so just copy the
110                user-specified pointer */
111 	    _plotter_params->plparams[j] = value;
112 
113 	  /* matched, so return happily */
114 	  return 0;
115 	}
116     }
117 
118   /* silently ignore requests to set unknown parameters */
119   return 0;
120 }
121 
122 /**********************************************************************/
123 
124 /* This function is called when a Plotter is instantiated.  It copies
125    parameters from a PlotterParams object into the Plotter.  We adopt the
126    following convention: if the PlotterParams object does not include a
127    value for a parameter, a default value (retrieved in the global
128    _known_params[] array) is used.  Unless, that is, an environment
129    variable of the same name has been set, in which case its value is used
130    instead.
131 
132    We support both parameters whose values are strings (which must be
133    copied byte-by-byte) and those whose values are void pointers (which may
134    simply be copied. */
135 void
_pl_g_copy_params_to_plotter(R___ (Plotter * _plotter)const PlotterParams * plotter_params)136 _pl_g_copy_params_to_plotter (R___(Plotter *_plotter) const PlotterParams *plotter_params)
137 {
138   int j;
139   char *envs;
140 
141   for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
142     {
143       if (!_known_params[j].is_string)
144 	/* not a string, just copy the void pointer into the plotter */
145 	_plotter->data->params[j] = plotter_params->plparams[j];
146 
147       else
148 	/* parameter value is a string, so use malloc and strcpy */
149 	{
150 	  if (plotter_params->plparams[j])
151 	    /* have user-specified value */
152 	    {
153 	      _plotter->data->params[j] =
154 		(char *)_pl_xmalloc (strlen ((char *)plotter_params->plparams[j]) + 1);
155 	      strcpy ((char *)_plotter->data->params[j],
156 		      (char *)plotter_params->plparams[j]);
157 	    }
158 	  else if ((envs = getenv (_known_params[j].parameter)) != NULL)
159 	    /* have value of environment variable */
160 	    {
161 	      _plotter->data->params[j] =
162 		(char *)_pl_xmalloc (strlen (envs) + 1);
163 	      strcpy ((char *)_plotter->data->params[j], envs);
164 	    }
165 	  else if (_known_params[j].default_value)
166 	    /* have default libplot value */
167 	    {
168 	      _plotter->data->params[j] =
169 		(char *)_pl_xmalloc (strlen ((char *)_known_params[j].default_value) + 1);
170 	      strcpy ((char *)_plotter->data->params[j],
171 		      (char *)_known_params[j].default_value);
172 	    }
173 	  else			/* punt */
174 	    _plotter->data->params[j] = NULL;
175 	}
176     }
177 }
178 
179 /* This retrieves the value of any specified Plotter parameter,
180    as stored in a Plotter instance. */
181 void *
_get_plot_param(const plPlotterData * data,const char * parameter_name)182 _get_plot_param (const plPlotterData *data, const char *parameter_name)
183 {
184   int j;
185 
186   for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
187     if (strcmp (_known_params[j].parameter, parameter_name) == 0)
188       return data->params[j];
189 
190   return (void *)NULL;		/* name not matched */
191 }
192 
193 /* This function is called when a Plotter is deleted, to delete the
194    instance-specific copies of Plotter parameters.  */
195 void
_pl_g_free_params_in_plotter(S___ (Plotter * _plotter))196 _pl_g_free_params_in_plotter (S___(Plotter *_plotter))
197 {
198   int j;
199 
200   /* deallocate stored values of class variables */
201   for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
202     if (_known_params[j].is_string && _plotter->data->params[j] != NULL)
203       /* stored parameter is a previously malloc'd string, so free it */
204       free (_plotter->data->params[j]);
205 
206 }
207 
208 /* This retrieves the default value of any specified Plotter parameter.
209    Default values for each parameter are stored in the _known_params[]
210    array, which is read-only global data.  So unlike the preceding
211    functions, this is not a Plotter method. */
212 void *
_get_default_plot_param(const char * parameter_name)213 _get_default_plot_param (const char *parameter_name)
214 {
215   int j;
216 
217   for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
218     if (strcmp (_known_params[j].parameter, parameter_name) == 0)
219       return _known_params[j].default_value;
220 
221   return (void *)NULL;		/* name not matched */
222 }
223