File: g_param.c

package info (click to toggle)
plotutils 2.4.1-15
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,072 kB
  • ctags: 6,952
  • sloc: ansic: 76,305; cpp: 12,402; sh: 8,475; yacc: 2,604; makefile: 894; lex: 144
file content (233 lines) | stat: -rw-r--r-- 6,955 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This file defines the PlotterParams class, which is a helper class.
   A PlotterParams object is used for specifying device driver parameters
   when a Plotter is instantiated.

   (In libplot, a PlotterParams struct is created with pl_newplparams() and
   deleted with pl_deleteplparams().  These are defined in apinewc.c; there
   is also a copy constructor, pl_copyplparams().)

   This file also includes the functions that are used for copying the
   parameters into the Plotter at instantiation time, and for accessing
   them later.  These are Plotter class members. */

#include "sys-defines.h"
#include "extern.h"

#ifndef LIBPLOTTER
/* In libplot, this is the initialization for the function-pointer part of
   a PlotterParams struct. */
const PlotterParams _default_plotter_params = 
{
  /* methods */
  _setplparam
};
#endif /* not LIBPLOTTER */

#ifdef LIBPLOTTER
PlotterParams::PlotterParams ()
{
  int i;
  
  for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
    plparams[i] = (voidptr_t)NULL;
}

PlotterParams::~PlotterParams ()
{
  int i;
  
  for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
    if (_known_params[i].is_string && plparams[i] != NULL)
      free (plparams[i]);
}

PlotterParams::PlotterParams (const PlotterParams& oldPlotterParams)
{
  int i;
  
  for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
    plparams[i] = oldPlotterParams.plparams[i];
}

PlotterParams& PlotterParams::operator= (const PlotterParams& oldPlotterParams)
{
  int i;
  
  for (i = 0; i < NUM_PLOTTER_PARAMETERS; i++)
    plparams[i] = oldPlotterParams.plparams[i];
  return (*this);
}
#endif

/* The parameter-setting method.  This is a PlotterParams method in
   libplotter (i.e. it is #defined to be PlotterParams::setplparam).  In
   libplot, a pointer to a PlotterParams struct must be passed to it as its
   first argument. */
int
#ifdef _HAVE_PROTOS
_setplparam (R___(PlotterParams *_plotter_params) const char *parameter, voidptr_t value)
#else
_setplparam (R___(_plotter_params) parameter, value)
     S___(PlotterParams *_plotter_params;)
     const char *parameter;
     voidptr_t value;
#endif
{
  int j;

  for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
    {
      if (strcmp (_known_params[j].parameter, parameter) == 0)
	{
	  if (_known_params[j].is_string)
	    /* parameter value is a string, so treat specially: copy the
	       string, byte by byte */
	    {
	      if (_plotter_params->plparams[j])
		free (_plotter_params->plparams[j]);
	      if (value != NULL)
		{
		  _plotter_params->plparams[j] = 
		    (char *)_plot_xmalloc (strlen ((char *)value) + 1);
		  strcpy ((char *)_plotter_params->plparams[j], (char *)value);
		}
	      else
		_plotter_params->plparams[j] = NULL;
	    }
	  else
	    /* parameter value is a (void *), so just copy the
               user-specified pointer */
	    _plotter_params->plparams[j] = value;
	  
	  /* matched, so return happily */
	  return 0;
	}
    }

  /* silently ignore requests to set unknown parameters */
  return 0;
}

/**********************************************************************/ 

/* This function is called when a Plotter is instantiated.  It copies
   parameters from a PlotterParams object into the Plotter.  We adopt the
   following convention: if the PlotterParams object does not include a
   value for a parameter, a default value (retrieved in the global
   _known_params[] array) is used.  Unless, that is, an environment
   variable of the same name has been set, in which case its value is used
   instead.

   We support both parameters whose values are strings (which must be
   copied byte-by-byte) and those whose values are void pointers (which may
   simply be copied. */
void 
#ifdef _HAVE_PROTOS
_copy_params_to_plotter (R___(Plotter *_plotter) const PlotterParams *plotter_params)
#else
_copy_params_to_plotter (R___(_plotter) plotter_params)
     S___(Plotter *_plotter;) 
     const PlotterParams *plotter_params;
#endif
{
  int j;
  char *envs;

  for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
    {
      if (!_known_params[j].is_string)
	/* not a string, just copy the void pointer into the plotter */
	_plotter->data->params[j] = plotter_params->plparams[j];

      else
	/* parameter value is a string, so use malloc and strcpy */
	{
	  if (plotter_params->plparams[j])
	    /* have user-specified value */
	    {
	      _plotter->data->params[j] = 
		(char *)_plot_xmalloc (strlen ((char *)plotter_params->plparams[j]) + 1);
	      strcpy ((char *)_plotter->data->params[j], 
		      (char *)plotter_params->plparams[j]);
	    }
	  else if ((envs = getenv (_known_params[j].parameter)) != NULL)
	    /* have value of environment variable */
	    {
	      _plotter->data->params[j] = 
		(char *)_plot_xmalloc (strlen (envs) + 1);
	      strcpy ((char *)_plotter->data->params[j], envs);
	    }
	  else if (_known_params[j].default_value)
	    /* have default libplot value */
	    {
	      _plotter->data->params[j] = 
		(char *)_plot_xmalloc (strlen ((char *)_known_params[j].default_value) + 1);
	      strcpy ((char *)_plotter->data->params[j], 
		      (char *)_known_params[j].default_value);
	    }
	  else			/* punt */
	    _plotter->data->params[j] = NULL;
	}
    }
}

/* This retrieves the value of any specified Plotter parameter,
   as stored in a Plotter instance. */
voidptr_t
#ifdef _HAVE_PROTOS
_get_plot_param (const plPlotterData *data, const char *parameter_name)
#else
_get_plot_param (data, parameter_name)
     const plPlotterData *data;
     const char *parameter_name;
#endif
{
  int j;

  for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
    if (strcmp (_known_params[j].parameter, parameter_name) == 0)
      return data->params[j];

  return (voidptr_t)NULL;		/* name not matched */
}

/* This function is called when a Plotter is deleted, to delete the
   instance-specific copies of Plotter parameters.  */
void 
#ifdef _HAVE_PROTOS
_free_params_in_plotter (S___(Plotter *_plotter))
#else
_free_params_in_plotter (S___(_plotter))
     S___(Plotter *_plotter;) 
#endif
{
  int j;

  /* deallocate stored values of class variables */
  for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
    if (_known_params[j].is_string && _plotter->data->params[j] != NULL)
      /* stored parameter is a previously malloc'd string, so free it */
      free (_plotter->data->params[j]);

}

/* This retrieves the default value of any specified Plotter parameter.
   Default values for each parameter are stored in the _known_params[]
   array, which is read-only global data.  So unlike the preceding
   functions, this is not a Plotter method. */
voidptr_t
#ifdef _HAVE_PROTOS
_get_default_plot_param (const char *parameter_name)
#else
_get_default_plot_param (parameter_name)
     const char *parameter_name;
#endif
{
  int j;

  for (j = 0; j < NUM_PLOTTER_PARAMETERS; j++)
    if (strcmp (_known_params[j].parameter, parameter_name) == 0)
      return _known_params[j].default_value;

  return (voidptr_t)NULL;		/* name not matched */
}