This is Unofficial EPICS BASE Doxygen Site
pvutils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright information and license terms for this software can be
3  * found in the file LICENSE that is included with the distribution
4  */
5 #include <stdio.h>
6 #if !defined(_WIN32)
7 #include <signal.h>
8 #define USE_SIGNAL
9 #endif
10 
11 #include <iostream>
12 
13 #include <string>
14 #include <ostream>
15 #include <sstream>
16 #include <iomanip>
17 #include <stdexcept>
18 #include <algorithm>
19 
20 #include <pv/logger.h>
21 #include <pv/pvTimeStamp.h>
22 
23 #include "pvutils.h"
24 
25 double timeout = 5.0;
26 bool debugFlag = false;
27 
28 pvd::PVStructure::Formatter::format_t outmode = pvd::PVStructure::Formatter::NT;
30 
31 std::string request("");
32 std::string defaultProvider("pva");
33 
37 bool Tracker::abort = false;
38 
39 #ifdef USE_SIGNAL
40 static
41 void alldone(int num)
42 {
43  (void)num;
44  Tracker::abort = true;
45  Tracker::doneEvt.signal();
46 }
47 #endif
48 
50 {
51 #ifdef USE_SIGNAL
52  signal(SIGINT, alldone);
53  signal(SIGTERM, alldone);
54  signal(SIGQUIT, alldone);
55 #endif
56 }
57 
58 static
59 void early(const char *inp, unsigned pos)
60 {
61  fprintf(stderr, "Unexpected end of input: %s\n", inp);
62  throw std::runtime_error("Unexpected end of input");
63 }
64 
65 // rudimentory parser for json array
66 // needed as long as Base < 3.15 is supported.
67 // for consistency, used with all version
68 void jarray(pvd::shared_vector<std::string>& out, const char *inp)
69 {
70  assert(inp[0]=='[');
71  const char * const orig = inp;
72  inp++;
73 
74  while(true) {
75  // starting a new token
76 
77  for(; *inp==' '; inp++) {} // skip leading whitespace
78 
79  if(*inp=='\0') early(inp, inp-orig);
80 
81  if(isalnum(*inp) || *inp=='+' || *inp=='-') {
82  // number
83 
84  const char *start = inp;
85 
86  while(isalnum(*inp) || *inp=='.' || *inp=='+' || *inp=='-')
87  inp++;
88 
89  if(*inp=='\0') early(inp, inp-orig);
90 
91  // inp points to first char after token
92 
93  out.push_back(std::string(start, inp-start));
94 
95  } else if(*inp=='"') {
96  // quoted string
97 
98  const char *start = ++inp; // skip quote
99 
100  while(*inp!='\0' && *inp!='"')
101  inp++;
102 
103  if(*inp=='\0') early(inp, inp-orig);
104 
105  // inp points to trailing "
106 
107  out.push_back(std::string(start, inp-start));
108 
109  inp++; // skip trailing "
110 
111  } else if(*inp==']') {
112  // no-op
113  } else {
114  fprintf(stderr, "Unknown token '%c' in \"%s\"", *inp, inp);
115  throw std::runtime_error("Unknown token");
116  }
117 
118  for(; *inp==' '; inp++) {} // skip trailing whitespace
119 
120  if(*inp==',') inp++;
121  else if(*inp==']') break;
122  else {
123  fprintf(stderr, "Unknown token '%c' in \"%s\"", *inp, inp);
124  throw std::runtime_error("Unknown token");
125  }
126  }
127 
128 }
double timeout
Definition: pvutils.cpp:25
static epicsMutex doneLock
Definition: pvutils.h:47
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
static void prepare()
Definition: pvutils.cpp:49
static epicsEvent doneEvt
Definition: pvutils.h:48
std::set< Tracker * > inprog_t
Definition: pvutils.h:49
void jarray(pvd::shared_vector< std::string > &out, const char *inp)
Definition: pvutils.cpp:68
void push_back(param_type v)
Definition: sharedVector.h:602
static inprog_t inprog
Definition: pvutils.h:50
int verbosity
Definition: pvutils.cpp:29
std::string request("")
#define stderr
Definition: epicsStdio.h:32
bool debugFlag
Definition: pvutils.cpp:26
static bool abort
Definition: pvutils.h:51
std::string defaultProvider("pva")
pvd::PVStructure::Formatter::format_t outmode
Definition: pvutils.cpp:28