File: utils.h

package info (click to toggle)
quickplot 0.10.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,464 kB
  • sloc: ansic: 16,640; sh: 11,163; makefile: 395
file content (328 lines) | stat: -rw-r--r-- 6,397 bytes parent folder | download | duplicates (4)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
  Quickplot - an interactive 2D plotter

  Copyright (C) 1998-2011  Lance Arsenault


  This file is part of Quickplot.

  Quickplot is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  Quickplot 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 Quickplot.  If not, see <http://www.gnu.org/licenses/>.

*/

static inline
long get_plot_num(const char *s, char **endptr)
{
  while(*s && *s < '0' && *s > '9')
    ++s;
  if(!(*s))
    return INT_MAX; /* got nothing more */

  long val;
  errno = 0;
  val = strtol(s, endptr, 10);

  if(*endptr == s)
    return INT_MAX;

  if((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
      || (errno != 0 && val == 0))
    return INT_MAX; /* got error */

  return val;
}

/* Returns 1 on success 0 on failure
 *
 * x and y must be freed
 *
 * parsing  --plot  '0 1 0 2 0 3'
 * or --plot-file '0 1 0 2 0 3'
 *
 * str="0 1 0 2 0 3" */
static inline
int get_plot_option(const char *str, ssize_t **x, ssize_t **y,
    size_t *num_plots, const char *opt, ssize_t min_chan, ssize_t max_chan)
{
  char *endptr;
  char *s;
  long nx, ny;
  int i = 1;

  ASSERT(x);
  ASSERT(y);
  ASSERT(num_plots);
  
  if(!qp_sllist_last(app->sources))
  {
    if(!opt) return 0;

    QP_ERROR("No files loaded yet, bad option %s='%s'\n",
         opt, str);
    exit(1);
  }

  s = (char *) str;

  nx = get_plot_num(s, &endptr);
  if(nx < min_chan || nx > max_chan) goto fail;
  s = endptr;
  ny = get_plot_num(s, &endptr);
  if(ny < min_chan || ny > max_chan) goto fail;
  s = endptr;

  if(x && y)
  {
    *x = qp_malloc(sizeof(ssize_t)*(strlen(s)+1)/2);
    *y = qp_malloc(sizeof(ssize_t)*(strlen(s)+1)/2);
    (*x)[0] = nx;
    (*y)[0] = ny;
  }

  while(*s)
  {
    nx = get_plot_num(s, &endptr);
    if(nx < min_chan || nx > max_chan) goto fail;
    (*x)[i] = nx;
    s = endptr;
    ny = get_plot_num(s, &endptr);
    if(ny < min_chan || ny > max_chan) goto fail;
    s = endptr;
    (*y)[i++] = ny;
  }

  *num_plots = i;

#ifdef QP_DEBUG
  if(opt)
  {
    DEBUG("got %s='", opt);
    if(*num_plots > 0)
      APPEND("%zu %zu", (*x)[0], (*y)[0]);
    for(i=1; i<(*num_plots); ++i)
      APPEND(" %zu %zu", (*x)[i], (*y)[i]);
    APPEND("\n");
  }
#endif

  return 1;

fail:

  if(opt)
  {
    ERROR("got bad option %s='%s'\n", opt, str);
    QP_ERROR("got bad option %s='%s'\n", opt, str);
    exit(1);
  }
  
  if(x && *x)
    free(*x);
  if(y && *y)
    free(*y);

  return 0;
}

/* returns 0 on success */
static inline
int parse_linear_channel(int pass, const char *arg,
    int argc, char **argv, int *i, double *start, double *step)
{
  char *s;
  int err = 0, j = 0;
  double val[2];

  if(start)
    *start = 0;
  if(step)
    *step = 1;

  if(arg == NULL || arg[0] == '\0')
    goto ret;

  s = (char *) arg;

  while(1)
  {
    char *endptr = NULL;
    struct lconv *lc;

    errno = 0;
    val[j] = strtod(s, &endptr);
    if(errno || s == endptr)
    {
      if(argv[*i - 1] != arg)
      {
        /* The argument is part of the option like:
         * '--linear-channel=0 1' or -l'0 3'.
         * arg is not part of another option so
         * this is an error */
        err = 1;
        break;
      }
      else
      {
        /* The optional arg is not ours */
        *i -= 1;
        break;
      }
    }

    if(j == 1)
    {
      if(start)
        *start = val[0];
      if(step)
        *step = val[1];
      break;
    }

    lc = localeconv();

    s = endptr;
    /* go to the next number */
    while(*s && (*s < '0' || *s > '9')
        && *s != '+' && *s != '-' && *s != 'e'
        && *s != 'E' && strcmp(s, lc->decimal_point))
      ++s;
  
    if(!(*s))
    {
      if(start)
        *start = val[0];
      break;
    }

    ++j;
  }
  

  if(err && pass == 0)
    return 1;

  /* we should not get an error on the 2nd pass */
  ASSERT(!err);

  if(err)
    return 1;

ret:

#ifdef QP_DEBUG
  if(start && step)
    DEBUG("got option --linear-channel with start=%.22g step=%.22g\n",
        *start, *step);
#endif

  return 0;
}

/* returns 0 on success and sets maximize
 * if maximize=0 is is a regular window size
 * if maximize=1 is is maximized ??
 * if maximize=2 it is fullscreen
 *
 * returns 1 on error and sets err */
static inline
int GetGeometry(char *arg, int *X, int *Y, int *W, int *H, int *maximize)
{
  int n[4], count = 0, x_count = -1;
  int w, h;
  char *endptr, *s;
  endptr = s = arg;

  while(1)
  {
    long val;

    val = strtol(s, &endptr, 10);
    if(s == endptr || !endptr ||
      val == LONG_MAX || val == LONG_MIN)
      return 1;
    if(*s == '-' && *(s+1) == '0')
      /* mark a "-0" position as in --geometry=600x700-0+0 */
      n[count++] = INT_MIN;
    else
      n[count++] = val;

    if(!(*endptr))
      break;

    s = endptr;
    while(*s && (*s < '0' || *s > '9') && *s != '-' && *s != '+')
    {
      if(*s == 'x' || *s == 'X')
        /* this is where the 'x' is */
        x_count = count;
      ++s;
    }
    if(!(*s))
      break;
  }
  
  if(
      !(count == 2 && (x_count == 1 || x_count == -1))
       &&
      !(count == 4 && (x_count == 1 || x_count == 3))
    )
    return 1;

  if(x_count == 1)
    {
      w = n[0];
      h = n[1];
    }
  else if(x_count == 3)
    {
      w = n[2];
      h = n[3];
    }

  if(w < 1 || h < 1)
    return 1;

  if(x_count == -1 || x_count == 3)
    {
      *X = n[0];
      *Y = n[1];
    }
  else if(count == 4)
    {
      *X = n[2];
      *Y = n[3];
    }

  if(app->root_window_width < 1)
    qp_get_root_window_size();

  if(w > app->root_window_width)
    w = app->root_window_width;
  if(h > app->root_window_height)
    h = app->root_window_height;

  if(w == app->root_window_width && h == app->root_window_height)
    /* We do not want to set app->op_geometry if it is
     * full screen so that they can still toggle out of
     * full screen if they choose to. */
    *maximize = 2; /* Fullscreen */
  else
  {
    *maximize = 0;
    *W = w;
    *H = h;
  }
  return 0;
}