ISIS Logo
UTILITIES
EPICS Utilities
iocstringtest.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <math.h>
10 #include <exception>
11 #include <algorithm>
12 #include <stdexcept>
13 #include <iostream>
14 #include <map>
15 #include <list>
16 #include <string>
17 #include <time.h>
18 #include <sstream>
19 #include <fstream>
20 #include <vector>
21 #include <memory>
22 
23 #include "epicsStdlib.h"
24 #include "epicsString.h"
25 #include "dbDefs.h"
26 #include "epicsMutex.h"
27 #include "dbBase.h"
28 #include "dbStaticLib.h"
29 #include "dbFldTypes.h"
30 #include "dbCommon.h"
31 #include "dbAccessDefs.h"
32 #include <epicsTypes.h>
33 #include <epicsTime.h>
34 #include <epicsThread.h>
35 #include <epicsString.h>
36 #include <epicsTimer.h>
37 #include <epicsMutex.h>
38 #include <iocsh.h>
39 #include "envDefs.h"
40 #include "macLib.h"
41 #include "errlog.h"
42 
43 #include "postfix.h"
44 #include "cvtFast.h"
45 
46 #include <boost/scoped_array.hpp>
47 
48 #include <string.h>
49 #include <registryFunction.h>
50 
51 #include <epicsExport.h>
52 
53 #include "utilities.h"
54 
57 static int stringtestres(const char* lhs, int operation, const char* rhs)
58 {
59  if (lhs == NULL)
60  {
61  errlogPrintf("iocstringtest: ERROR: NULL lhs arg");
62  return 0;
63  }
64  bool verbose = (operation & 0x1);
65  bool reverse = (operation & 0x8 ? true : false);
66  char* lhs_expand = macEnvExpand(lhs);
67  char* rhs_expand = (rhs != NULL ? macEnvExpand(rhs) : NULL);
68  if (lhs_expand == NULL)
69  {
70  errlogPrintf("iocstringtest: ERROR: NULL expanded lhs expression arg");
71  return 0;
72  }
73  if (verbose)
74  {
75  printf("iocstringtest: expanded expression=\"%s\"\n", lhs_expand);
76  }
77  long result = 0;
78  if (operation & 0x2)
79  {
80  if (strlen(lhs_expand) > 0)
81  {
82  result = 1;
83  }
84  }
85  else if (operation & 0x4)
86  {
87  if (rhs_expand != NULL)
88  {
89  result = (strcmp(lhs_expand, rhs_expand) == 0 ? 1 : 0);
90  }
91  else
92  {
93  errlogPrintf("iocstringtest: ERROR: NULL expanded rhs expression arg");
94  }
95  }
96  else // old default
97  {
98  if (strlen(lhs_expand) > 0)
99  {
100  result = 1;
101  }
102  }
103  if (reverse)
104  {
105  result = (result == 0 ? 1 : 0);
106  }
107  free(lhs_expand);
108  if (rhs_expand != NULL)
109  {
110  free(rhs_expand);
111  }
112  return result;
113 }
114 
126 static void iocstringtest(const char* resultvar, const char* lhs, int operation, const char* rhs)
127 {
128  if (resultvar == NULL)
129  {
130  errlogPrintf("iocstringtest: ERROR: NULL args");
131  return;
132  }
133  bool verbose = (operation & 0x1);
134  int result = stringtestres(lhs, operation, rhs);
135  char result_str[32];
136  if ( sprintf(result_str, "%s", (result != 0 ? " " : "#")) < 0 )
137  {
138  errlogPrintf("iocstringtest: ERROR: sprintf (hash mode)\n");
139  return;
140  }
141  if (verbose)
142  {
143  printf("iocstringtest: setting %s=\"%s\" (%ld)\n", resultvar, result_str, result);
144  }
145  epicsEnvSet(resultvar, result_str);
146 }
147 
171 static void iocstringiftest(const char* resultvar, const char* lhs, int operation, const char* rhs)
172 {
173  if (resultvar == NULL)
174  {
175  errlogPrintf("iocstringtest: ERROR: NULL args");
176  return;
177  }
178  bool verbose = (operation & 0x1);
179  int result = stringtestres(lhs, operation, rhs);
180  char resultIfVar[128];
181  char resultNotIfVar[128];
182  sprintf(resultIfVar, "IF%s", resultvar);
183  sprintf(resultNotIfVar, "IFNOT%s", resultvar);
184  char resultIf[128];
185  char resultNotIf[128];
186  if (result != 0)
187  {
188  sprintf(resultIf, "%s", " ");
189  sprintf(resultNotIf, "%s", "#");
190  }
191  else
192  {
193  sprintf(resultIf, "%s", "#");
194  sprintf(resultNotIf, "%s", " ");
195  }
196  if (verbose)
197  {
198  printf("iocstringiftest: setting %s=\"%s\"\n", resultIfVar, resultIf);
199  printf("iocstringiftest: setting %s=\"%s\"\n", resultNotIfVar, resultNotIf);
200  }
201  epicsEnvSet(resultIfVar, resultIf);
202  epicsEnvSet(resultNotIfVar, resultNotIf);
203 }
204 
205 extern "C" {
206 
207 // EPICS iocsh shell commands
208 
209 static const iocshArg stringtestInitArg0 = { "resultvar", iocshArgString };
210 static const iocshArg stringtestInitArg1 = { "lhs", iocshArgString };
211 
217 static const iocshArg stringtestInitArg2 = { "operation", iocshArgInt };
218 static const iocshArg stringtestInitArg3 = { "rhs", iocshArgString };
219 static const iocshArg * const stringtestInitArgs[] = { &stringtestInitArg0, &stringtestInitArg1, &stringtestInitArg2, &stringtestInitArg3 };
220 
221 static const iocshFuncDef stringtestInitFuncDef = {"stringtest", sizeof(stringtestInitArgs) / sizeof(iocshArg*), stringtestInitArgs};
222 
223 static void stringtestInitCallFunc(const iocshArgBuf *args)
224 {
225  iocstringtest(args[0].sval, args[1].sval, args[2].ival, args[3].sval);
226 }
227 
228 static const iocshArg stringiftestInitArg0 = { "resultvar", iocshArgString };
229 static const iocshArg stringiftestInitArg1 = { "lhs", iocshArgString };
230 
236 static const iocshArg stringiftestInitArg2 = { "operation", iocshArgInt };
237 static const iocshArg stringiftestInitArg3 = { "rhs", iocshArgString };
238 static const iocshArg * const stringiftestInitArgs[] = { &stringiftestInitArg0, &stringiftestInitArg1, &stringiftestInitArg2, &stringiftestInitArg3 };
239 
240 static const iocshFuncDef stringiftestInitFuncDef = {"stringiftest", sizeof(stringiftestInitArgs) / sizeof(iocshArg*), stringiftestInitArgs};
241 
242 static void stringiftestInitCallFunc(const iocshArgBuf *args)
243 {
244  iocstringiftest(args[0].sval, args[1].sval, args[2].ival, args[3].sval);
245 }
246 
247 static void iocstringtestRegister(void)
248 {
249  iocshRegister(&stringtestInitFuncDef, stringtestInitCallFunc);
250  iocshRegister(&stringiftestInitFuncDef, stringiftestInitCallFunc);
251 }
252 
253 epicsExportRegistrar(iocstringtestRegister); // need to be declared via registrar() in utilities.dbd too
254 
255 // asub callable functions - need to be in utilities.dbd as function()
256 
257 //epicsRegisterFunction(setIOCName);
258 //epicsRegisterFunction(getIOCName);
259 //epicsRegisterFunction(getIOCGroup);
260 
261 }
262 
static const iocshArg stringtestInitArg2
The operation to perform on the string.
static const iocshArg stringiftestInitArg0
The name of the macro to put the result of the calculation into.
static const iocshArg stringtestInitArg0
The name of the macro to put the result of the calculation into.
static void stringiftestInitCallFunc(const iocshArgBuf *args)
static const iocshArg stringtestInitArg3
The right hand side argument.
static void iocstringtestRegister(void)
static void iocstringtest(const char *resultvar, const char *lhs, int operation, const char *rhs)
Compares the given strings based on the operation flag and returns &#39; &#39; on true and &#39;#&#39; on false into ...
static const iocshArg stringiftestInitArg1
The left hand side argument.
static void stringtestInitCallFunc(const iocshArgBuf *args)
static const iocshArg stringiftestInitArg2
The operation to perform on the string.
static const iocshArg stringiftestInitArg3
The right hand side argument.
epicsExportRegistrar(dbLoadRecordsFuncsRegister)
static int stringtestres(const char *lhs, int operation, const char *rhs)
Does the actual calculation comparing the given strings to each other.
static const iocshArg stringtestInitArg1
The left hand side argument.
static const iocshArg *const stringiftestInitArgs[]
static void iocstringiftest(const char *resultvar, const char *lhs, int operation, const char *rhs)
Compares the given strings based on the operation flag, putting the result in two different macros...
static const iocshFuncDef stringtestInitFuncDef
static const iocshFuncDef stringiftestInitFuncDef
static const iocshArg *const stringtestInitArgs[]
Copyright © 2013 Science and Technology Facilities Council | Generated by   doxygen 1.8.5