LibRan  0.1
Pseudo-random number distribution generator
LRset.c
Go to the documentation of this file.
1 
34 /*
35  * Copyright 2019 R.K. Owen, Ph.D.
36  * License see lgpl.md (Gnu Lesser General Public License)
37  */
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include <stdarg.h> /* va_arg */
43 #include <string.h> /* memcpy */
44 #include "libran.h"
45 
75 int LR_vset(LR_obj *o, char *x, va_list ap) {
76  LR_val t;
77  int i;
78  float f;
79  char x1;
80  int ret = 0;
81 
82  while (x1 = *x++) {
83  switch (x1) {
84  case 'k':
85  i = va_arg(ap, int);
86  (void) memcpy(&(o->k), &i, sizeof(int));
87  break;
88  case 'n':
89  i = va_arg(ap, int);
90  (void) memcpy(&(o->n), &i, sizeof(int));
91  break;
92  case 'p':
93  f = va_arg(ap, double);
94  (void) memcpy(&(o->p), &f, sizeof(float));
95  break;
96  case 'q':
97  f = va_arg(ap, double);
98  (void) memcpy(&(o->q), &f, sizeof(float));
99  break;
100  default: /* rest are dependent on LR_type */
101  switch (o->d) {
102  case LR_int:
103  t.i = va_arg(ap, int);
104  break;
105  case LR_long:
106  t.l = va_arg(ap, long);
107  break;
108  case LR_float:
109  /* floats are promoted to double in ... */
110  t.f = va_arg(ap, double);
111  break;
112  case LR_double:
113  t.d = va_arg(ap, double);
114  break;
115  default:
116  /* should not get here unless a new type was added */
117  return o->errno = LRerr_BadDataType;
118  }
119  }
120  switch (x1) {
121  case 'd':
122  (void) memcpy(&(o->d), &t, sizeof(LR_val));
123  break;
124  case 'a':
125  (void) memcpy(&(o->a), &t, sizeof(LR_val));
126  break;
127  case 'b':
128  (void) memcpy(&(o->b), &t, sizeof(LR_val));
129  break;
130  case 'm':
131  (void) memcpy(&(o->m), &t, sizeof(LR_val));
132  break;
133  case 's':
134  (void) memcpy(&(o->s), &t, sizeof(LR_val));
135  break;
136  case 'x':
137  (void) memcpy(&(o->x), &t, sizeof(LR_val));
138  break;
139  case 'k':
140  case 'n':
141  case 'p':
142  case 'q':
143  break;
144  default:
145  /* it is an error to include non-object attributes */
146  /* but continue on and process remaining */
147  ret++;
148  }
149  }
150  return -ret;
151 }
152 
165 int LR_set(LR_obj *o, char x, ...) {
166  va_list ap;
167  int ret = 0;
168  char xx[2] = "0";
169 
170  va_start(ap, x);
171 
172  xx[0] = x;
173  ret = LR_vset(o, xx, ap);
174 
175  va_end(ap);
176 
177  return ret;
178 }
179 
194 int LR_set_all(LR_obj *o, char *x, ...) {
195  va_list ap;
196  int ret = 0, val;
197 
198  va_start(ap, x);
199 
200  val = LR_vset(o, x, ap);
201  if (val < 0)
202  ret += val;
203  va_end(ap);
204 
205  return ret;
206 }
207 
208 #ifdef __cplusplus
209 }
210 #endif
int LR_set_all(LR_obj *o, char *x,...)
LR_set_all(LR_obj *o, char *x, ...) - set all given LR object parameters.
Definition: LRset.c:194
LR_val b
Definition: libran.h:139
LR_data_type d
Definition: libran.h:137
spans the set of allowed value types
Definition: libran.h:83
float p
Definition: libran.h:145
LR_val x
Definition: libran.h:142
int LR_set(LR_obj *o, char x,...)
LR_set(LR_obj *o, char x, ...) - set a single LR object parameter.
Definition: LRset.c:165
int i
Definition: libran.h:85
#define LRerr_BadDataType
Definition: libran.h:34
int LR_vset(LR_obj *o, char *x, va_list ap)
LR_vset(LR_obj *o, char *x, va_list ap) - read and parse options.
Definition: LRset.c:75
long l
Definition: libran.h:84
float q
Definition: libran.h:146
LR_val s
Definition: libran.h:141
int errno
Definition: libran.h:169
LR_val a
Definition: libran.h:138
double d
Definition: libran.h:87
float f
Definition: libran.h:86
int k
Definition: libran.h:143
The LibRan common header file.
int n
Definition: libran.h:144
the fundamental LibRan random variate distribution object
Definition: libran.h:134
LR_val m
Definition: libran.h:140
Definition: libran.h:99