mrfioc2  2.3.0
Macros | Functions
linkoptions.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <dbDefs.h>
#include <epicsTypes.h>
#include <epicsString.h>
#include <macLib.h>
#include "linkoptions.h"
Include dependency graph for linkoptions.c:

Go to the source code of this file.

Macros

#define epicsExportSharedSymbols
 
#define HUGE_VALF   HUGE_VAL
 
#define HUGE_VALL   (-(HUGE_VAL))
 

Functions

int epicsShareAPI linkOptionsStore (const linkOptionDef *opts, void *user, const char *str, int options)
 Parse a string a store the result. More...
 
epicsShareFunc const char *epicsShareAPI linkOptionsEnumString (const linkOptionEnumType *emap, int i, const char *def)
 Return the string associated with Enum 'i'. More...
 

Macro Definition Documentation

◆ epicsExportSharedSymbols

#define epicsExportSharedSymbols

Definition at line 23 of file linkoptions.c.

◆ HUGE_VALF

#define HUGE_VALF   HUGE_VAL

Definition at line 27 of file linkoptions.c.

◆ HUGE_VALL

#define HUGE_VALL   (-(HUGE_VAL))

Definition at line 30 of file linkoptions.c.

Function Documentation

◆ linkOptionsEnumString()

epicsShareFunc const char* epicsShareAPI linkOptionsEnumString ( const linkOptionEnumType Enums,
int  i,
const char *  def 
)

Return the string associated with Enum 'i'.

Parameters
EnumsA null-terminated array of string/integer pairs
iAn Enum index
defString to be returned in 'i' isn't a valid Enum index.
Returns
A constant string

Definition at line 226 of file linkoptions.c.

227 {
228  for(; emap && emap->name; emap++) {
229  if ( i == emap->value ) {
230  return emap->name;
231  }
232  }
233  return def;
234 }

◆ linkOptionsStore()

int epicsShareAPI linkOptionsStore ( const linkOptionDef opts,
void *  user,
const char *  str,
int  options 
)

Parse a string a store the result.

Takes the string 'str', parses it according to 'opts', then sorts the result in 'user'.

Parameters
optsA null-terminated array of options.
userPointer to a structure whos member offsets are given in 'opts'
strThe string to parse
optionsSome modifiers for the parsing process or 0. The only option is LINKOPTIONDEBUG.
Returns
0 Ok
-1 Fail ('user' may be partially modified)

Definition at line 135 of file linkoptions.c.

136 {
137  MAC_HANDLE handle; /* only .debug is used */
138 
139  int status=0;
140  size_t i;
141  epicsUInt32 *found;
142  const linkOptionDef *cur;
143  char **pairs=NULL, **arg;
144 
145  for(i=0, cur=opts; cur && cur->name; i++, cur++) {}
146 
147  /* Bit array to find missing required keys */
148  found=calloc( (i/32)+1, sizeof(epicsUInt32) );
149  if (!found) {
150  fprintf(stderr,"store_options: calloc failed\n");
151  status=-1;
152  goto errbitarray;
153  }
154 
155  memset((void*)&handle, 0, sizeof(handle));
156 
157  if (options&LINKOPTIONDEBUG)
158  handle.debug=0xff;
159 
160  /* Parses 'str' and stores the result in the array pairs.
161  * The length of pairs is twice the number of pairs found.
162  * Each even element is a key, each odd element is a value.
163  *
164  * Pair N is (pairs[2*N], pairs[2*N+1])
165  *
166  * After the last pair is a (NULL, NULL)
167  */
168  if (macParseDefns(&handle, str, &pairs)<0) {
169  status=-1;
170  goto errparse;
171  }
172 
173  for (arg=pairs; arg && arg[0]; arg+=2) {
174  int match=0;
175 
176  if (options&LINKOPTIONDEBUG)
177  printf("key %s\n",arg[0]);
178 
179  for (i=0, cur=opts; !match && cur && cur->name; i++, cur++) {
180 
181  if (options&LINKOPTIONDEBUG)
182  fprintf(stderr,"For option: %s\n",cur->name);
183 
184  if( strcmp(arg[0], cur->name)!=0 )
185  continue;
186  match=1;
187 
188  if (found[i/32]&(1<<(i%32)) && !cur->overwrite) {
189  fprintf(stderr,"Option %s was already given\n",cur->name);
190  status=-1;
191  goto errsemantix;
192  }
193 
194  found[i/32] |= 1<<(i%32);
195 
196  status=store_value(cur, user, arg[1], options);
197  if (status)
198  goto errsemantix;
199  }
200 
201  if(!match) {
202  printf("Warning: ignoring unknown INP/OUT option %s=\n", arg[0]);
203  }
204  }
205 
206  for (i=0, cur=opts; cur && cur->name; i++, cur++) {
207 
208  if ( !(found[i/32]&(1<<(i%32))) && cur->required ) {
209  fprintf(stderr,"Missing required option %s\n",cur->name);
210  status=-1;
211  goto errsemantix;
212  }
213  }
214 
215 errsemantix:
216  free((void*)pairs);
217 errparse:
218  free(found);
219 errbitarray:
220  return status;
221 }
#define LINKOPTIONDEBUG
Definition: linkoptions.h:114