~ubuntu-branches/ubuntu/precise/psicode/precise

« back to all changes in this revision

Viewing changes to src/lib/libipv1/ip_data.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck, Michael Banck, Daniel Leidert
  • Date: 2009-02-23 00:12:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223001202-rutldoy3dimfpesc
Tags: 3.4.0-1
* New upstream release.

[ Michael Banck ]
* debian/patches/01_DESTDIR.dpatch: Refreshed.
* debian/patches/02_FHS.dpatch: Removed, applied upstream.
* debian/patches/03_debian_docdir: Likewise.
* debian/patches/04_man.dpatch: Likewise.
* debian/patches/06_466828_fix_gcc_43_ftbfs.dpatch: Likewise.
* debian/patches/07_464867_move_executables: Fixed and refreshed.
* debian/patches/00list: Adjusted.
* debian/control: Improved description.
* debian/patches-held: Removed.
* debian/rules (install/psi3): Do not ship the ruby bindings for now.

[ Daniel Leidert ]
* debian/rules: Fix txtdir via DEB_MAKE_INSTALL_TARGET.
* debian/patches/01_DESTDIR.dpatch: Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
#include <stdarg.h>
4
 
#include <string.h>
5
 
#include <tmpl.h>
6
 
 
7
 
#ifdef DEC
8
 
#ifdef VOID
9
 
#undef VOID
10
 
#define VOID char
11
 
#endif
12
 
#endif
13
 
 
14
 
#include "ip_types.h"
15
 
#include "ip_global.h"
16
 
#include "ip_error.h"
17
 
#include "ip_error.gbl"
18
 
 
19
 
/* The way the AIX xlc compiler handles vararg decs isn't compatible with
20
 
 * the way the tmpl file is formed, so ip_data.global cannot be used.
21
 
 * Everything global should be defined before it is used here. */
22
 
/* NOTE: ip_data.global will work fine (and be correct) for other source
23
 
 * files.  It's just this one that is a problem because xlc thinks my
24
 
 * declarations below are different than the ones in the global file
25
 
 * (and they are-sort of). */
26
 
#ifndef AIX 
27
 
#include "ip_data.gbl"
28
 
#endif
29
 
 
30
 
#include "ip_data.lcl"
31
 
 
32
 
#include "ip_cwk.gbl"
33
 
 
34
 
int ip_count(char *keyword, int *count, int n, ...)
35
 
{
36
 
  va_list args;
37
 
  int i;
38
 
  int *v;
39
 
  int r;
40
 
 
41
 
  if (n==0) {
42
 
    return ip_count_v(keyword,count,n,NULL);
43
 
    }
44
 
  else {
45
 
    v = (int *) malloc(sizeof(int)*n);
46
 
    if (!v) return IPE_MALLOC;
47
 
    va_start(args, n);
48
 
    for (i=0; i<n; i++) {
49
 
      v[i] = va_arg(args,int);
50
 
      }
51
 
    va_end(args);
52
 
    r = ip_count_v(keyword,count,n,v);
53
 
    free(v);
54
 
    return r;
55
 
    }
56
 
  }
57
 
 
58
 
int ip_count_v(char *keyword, int *count, int n, int *v)
59
 
{
60
 
  ip_value_t *val;
61
 
  int errcod;
62
 
 
63
 
  if ((errcod = ip_value_v(keyword,&val,n,v))) return errcod;
64
 
 
65
 
  if (val->type != IP_ARRAY) return IPE_NOT_AN_ARRAY;
66
 
 
67
 
  *count = val->v.array->n;
68
 
  return IPE_OK;
69
 
  }
70
 
 
71
 
int ip_boolean(char *keyword, int *boolean, int n, ...)
72
 
{
73
 
  va_list args;
74
 
  int i;
75
 
  int *v;
76
 
  int r;
77
 
 
78
 
  if (n==0) {
79
 
    return ip_boolean_v(keyword,boolean,n,NULL);
80
 
    }
81
 
  else {
82
 
    v = (int *) malloc(sizeof(int)*n);
83
 
    if (!v) return IPE_MALLOC;
84
 
    va_start(args, n);
85
 
    for (i=0; i<n; i++) {
86
 
      v[i] = va_arg(args,int);
87
 
      }
88
 
    va_end(args);
89
 
    r = ip_boolean_v(keyword,boolean,n,v);
90
 
    free(v);
91
 
    return r;
92
 
    }
93
 
  }
94
 
 
95
 
int ip_boolean_v(char *keyword, int *boolean, int n, int *v)
96
 
{
97
 
  ip_value_t *val;
98
 
  int errcod;
99
 
  char copy[10],*s;
100
 
 
101
 
  if ((errcod = ip_value_v(keyword,&val,n,v))) return errcod;
102
 
 
103
 
  if (val->type != IP_SCALAR) return IPE_NOT_A_SCALAR;
104
 
 
105
 
  strncpy(copy,val->v.scalar,10);
106
 
  copy[9] = '\0';
107
 
 
108
 
  /* Convert the string to uppercase. */
109
 
  for (s=copy; *s!='\0'; s++) {
110
 
    if (*s>='a' && *s <='z') *s = *s + 'A' - 'a';
111
 
    }
112
 
  
113
 
  if (!strcmp(copy,"YES")) *boolean = 1;
114
 
  else if (!strcmp(copy,"NO")) *boolean = 0;
115
 
  else if (!strcmp(copy,"1")) *boolean = 1;
116
 
  else if (!strcmp(copy,"0")) *boolean = 0;
117
 
  else if (!strcmp(copy,"TRUE")) *boolean = 1;
118
 
  else if (!strcmp(copy,"FALSE")) *boolean = 0;
119
 
  else return IPE_TYPE;
120
 
 
121
 
  return IPE_OK;
122
 
  }
123
 
 
124
 
/* n should always be zero in this version of libip. */
125
 
int ip_exist(char *keyword, int n, ...)
126
 
{
127
 
  va_list args;
128
 
  int i;
129
 
  int *v;
130
 
  int r;
131
 
 
132
 
  if (n==0) {
133
 
    return ip_exist_v(keyword,n,NULL);
134
 
    }
135
 
  else {
136
 
    v = (int *) malloc(sizeof(int)*n);
137
 
    if (!v) {
138
 
      ip_warn("ip_exist: problem mallocing %d integers",n);
139
 
      return 0;
140
 
      }
141
 
    va_start(args, n);
142
 
    for (i=0; i<n; i++) {
143
 
      v[i] = va_arg(args,int);
144
 
      }
145
 
    va_end(args);
146
 
    r = ip_exist_v(keyword,n,v);
147
 
    free(v);
148
 
    return r;
149
 
    }
150
 
  }
151
 
 
152
 
/* n should always be zero in this version of libip. */
153
 
int ip_exist_v(char *keyword, int n, int *v)
154
 
{
155
 
  if (ip_cwk_descend_tree(keyword)) return 1;
156
 
 
157
 
  return 0;
158
 
  }
159
 
 
160
 
int ip_data(char *keyword, char *conv, void *value, int n, ...)
161
 
{
162
 
  va_list args;
163
 
  int i;
164
 
  int *v;
165
 
  int r;
166
 
 
167
 
  if (n==0) {
168
 
    return ip_data_v(keyword,conv,value,n,NULL);
169
 
    }
170
 
  else {
171
 
    v = (int *) malloc(sizeof(int)*n);
172
 
    if (!v) return IPE_MALLOC;
173
 
    va_start(args, n);
174
 
    for (i=0; i<n; i++) {
175
 
      v[i] = va_arg(args,int);
176
 
      }
177
 
    va_end(args);
178
 
    r = ip_data_v(keyword,conv,value,n,v);
179
 
    free(v);
180
 
    return r;
181
 
    }
182
 
  }
183
 
 
184
 
int ip_data_v(char *keyword, char *conv, void *value, int n, int *v)
185
 
{
186
 
  ip_value_t *val;
187
 
  int errcod;
188
 
 
189
 
  if ((errcod = ip_value_v(keyword,&val,n,v))) return errcod;
190
 
 
191
 
  if (val->type != IP_SCALAR) return IPE_NOT_A_SCALAR;
192
 
 
193
 
  if (sscanf(val->v.scalar,conv,value) != 1) return IPE_TYPE;
194
 
 
195
 
  return IPE_OK;
196
 
  }
197
 
 
198
 
int ip_string(char *keyword, char **value, int n, ...)
199
 
{
200
 
  va_list args;
201
 
  int i;
202
 
  int *v;
203
 
  int r;
204
 
 
205
 
  if (n==0) {
206
 
    return ip_string_v(keyword,value,n,NULL);
207
 
    }
208
 
  else {
209
 
    v = (int *) malloc(sizeof(int)*n);
210
 
    if (!v) return IPE_MALLOC;
211
 
    va_start(args, n);
212
 
    for (i=0; i<n; i++) {
213
 
      v[i] = va_arg(args,int);
214
 
      }
215
 
    va_end(args);
216
 
    r = ip_string_v(keyword,value,n,v);
217
 
    free(v);
218
 
    return r;
219
 
    }
220
 
  }
221
 
 
222
 
int ip_string_v(char *keyword, char **value, int n, int *v)
223
 
{
224
 
  ip_value_t *val;
225
 
  int errcod;
226
 
 
227
 
  if ((errcod = ip_value_v(keyword,&val,n,v))) return errcod;
228
 
 
229
 
  if (val->type != IP_SCALAR) return IPE_NOT_A_SCALAR;
230
 
 
231
 
  *value = (char *) malloc(sizeof(char)*(strlen(val->v.scalar)+1));
232
 
  if (! *value) return IPE_MALLOC;
233
 
  strcpy(*value,val->v.scalar);
234
 
  return IPE_OK;
235
 
  }
236
 
 
237
 
int ip_value(char *keyword, ip_value_t **value, int n, ...)
238
 
{
239
 
  va_list args;
240
 
  int i;
241
 
  int *v;
242
 
  int r;
243
 
 
244
 
  if (n==0) {
245
 
    return ip_value_v(keyword,value,n,NULL);
246
 
    }
247
 
  else {
248
 
    v = (int *) malloc(sizeof(int)*n);
249
 
    if (!v) return IPE_MALLOC;
250
 
    va_start(args, n);
251
 
    for (i=0; i<n; i++) {
252
 
      v[i] = va_arg(args,int);
253
 
      }
254
 
    va_end(args);
255
 
    r = ip_value_v(keyword,value,n,v);
256
 
    free(v);
257
 
    return r;
258
 
    }
259
 
  }
260
 
 
261
 
int ip_value_v(char *keyword, ip_value_t **value, int n, int *v)
262
 
{
263
 
  int i;
264
 
  ip_value_t *val;
265
 
 
266
 
  /* Use the cwk list to obtain the value associated with the keyword. */
267
 
  val = ip_key_value(keyword);
268
 
  if (!val) return IPE_KEY_NOT_FOUND;
269
 
 
270
 
  /* Descend thru val to find the subarray that were are interested in. */
271
 
  for (i=0; i<n; i++) {
272
 
    if (val->type != IP_ARRAY) return IPE_NOT_AN_ARRAY;
273
 
    if (v[i] < 0) return IPE_OUT_OF_BOUNDS;
274
 
    if (v[i] >= val->v.array->n) return IPE_OUT_OF_BOUNDS;
275
 
    val = val->v.array->values[v[i]];
276
 
    }
277
 
 
278
 
  *value = val;
279
 
  return IPE_OK;
280
 
  }
281
 
 
282
 
 
283
 
/*
284
 
** ip_int_array()
285
 
**
286
 
** Function reads in an integer array using the PSI input parser.
287
 
** It checks for errors at all stages and makes sure that the array
288
 
** has the proper length.
289
 
**
290
 
** C. David Sherrill
291
 
** Center for Computational Quantum Chemistry
292
 
** August 1995
293
 
**
294
 
** Parameters:
295
 
**    keyword = string containing the keyword for the input parser
296
 
**    arr     = array to hold results
297
 
**    len     = length of array
298
 
**
299
 
** Returns: IP Error code
300
 
**
301
 
** Note: keyword should ordinarily be an uppercase string.
302
 
*/
303
 
 
304
 
int ip_int_array(char *keyword, int *arr, int len)
305
 
{
306
 
  int i, errcod, cnt;
307
 
 
308
 
  errcod = ip_count(keyword,&cnt,0);
309
 
  if (errcod != IPE_OK) return(errcod);
310
 
  if (cnt != len) {
311
 
    fprintf(ip_out," (ip_int_array): Trouble parsing %s array.\n", keyword);
312
 
    fprintf(ip_out," Length is %d should be %d\n",cnt,len);
313
 
    return(IPE_OUT_OF_BOUNDS);
314
 
    }
315
 
  for (i=0; i<len; i++) {
316
 
    errcod = ip_data(keyword,"%d",arr+i,1,i);
317
 
    if (errcod != IPE_OK) {
318
 
      fprintf(ip_out," (ip_int_array): Trouble parsing %s array element %d\n",
319
 
        keyword,i+1);
320
 
      ip_warn(ip_error_message(errcod));
321
 
      return(errcod);
322
 
      }
323
 
   }
324
 
 
325
 
  return(IPE_OK);
326
 
}
327
 
 
328
 
 
329
 
/*
330
 
** ip_double_array()
331
 
**
332
 
** Function reads in an array of doubles using the PSI input parser.
333
 
** It checks for errors at all stages and makes sure that the array
334
 
** has the proper length.
335
 
**
336
 
** Based on ip_int_array by C. David Sherrill
337
 
**
338
 
** Parameters:
339
 
**    keyword = string containing the keyword for the input parser
340
 
**    arr     = array to hold results
341
 
**    len     = length of array
342
 
**
343
 
** Returns: IP Error code
344
 
*/
345
 
 
346
 
int ip_double_array(char *keyword, double *arr, int len)
347
 
{
348
 
  int i, errcod, cnt;
349
 
 
350
 
  errcod = ip_count(keyword,&cnt,0);
351
 
  if (errcod != IPE_OK) return(errcod);
352
 
  if (cnt != len) {
353
 
    fprintf(ip_out," (ip_array): Trouble parsing %s array.\n", keyword);
354
 
    fprintf(ip_out," Length is %d should be %d\n",cnt,len);
355
 
    return(IPE_OUT_OF_BOUNDS);
356
 
    }
357
 
  for (i=0; i<len; i++) {
358
 
    errcod = ip_data(keyword,"%lf",arr+i,1,i);
359
 
    if (errcod != IPE_OK) {
360
 
      fprintf(ip_out," (ip_array): Trouble parsing %s array element %d\n",
361
 
        keyword,i+1);
362
 
      ip_warn(ip_error_message(errcod));
363
 
      return(errcod);
364
 
      }
365
 
   }
366
 
 
367
 
  return(IPE_OK);
368
 
}