File: g_param.c

package info (click to toggle)
plotutils 2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 13,156 kB
  • ctags: 7,141
  • sloc: ansic: 68,670; sh: 20,082; cpp: 12,382; yacc: 2,588; makefile: 889; lex: 137
file content (222 lines) | stat: -rw-r--r-- 7,164 bytes parent folder | download | duplicates (7)
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
/* This file is part of the GNU plotutils package.  Copyright (C) 1995,
   1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.

   The GNU plotutils package is free software.  You may redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software foundation; either version 2, or (at your
   option) any later version.

   The GNU plotutils package is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with the GNU plotutils package; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
   Boston, MA 02110-1301, USA. */

/* 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] = (void *)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
_setplparam (R___(PlotterParams *_plotter_params) const char *parameter, void * value)
{
  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 *)_pl_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 
_pl_g_copy_params_to_plotter (R___(Plotter *_plotter) const PlotterParams *plotter_params)
{
  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 *)_pl_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 *)_pl_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 *)_pl_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. */
void *
_get_plot_param (const plPlotterData *data, const char *parameter_name)
{
  int j;

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

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

/* This function is called when a Plotter is deleted, to delete the
   instance-specific copies of Plotter parameters.  */
void 
_pl_g_free_params_in_plotter (S___(Plotter *_plotter))
{
  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. */
void *
_get_default_plot_param (const char *parameter_name)
{
  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 (void *)NULL;		/* name not matched */
}