rllib  1
rldataacquisitionprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rldataacquisitionprovider.cpp - description
3  -------------------
4  begin : Mon Sep 03 2007
5  copyright : (C) 2007 by pvbrowser
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
17 #include <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20 
21 rlDataAcquisitionProvider::rlDataAcquisitionProvider(int maxNameLength, const char *shared_memory, long shared_memory_size)
22 {
23  shm = new rlSharedMemory(shared_memory,shared_memory_size);
25  shmvalues = ((char *)shmheader) + sizeof(SHM_HEADER);
26  memset(shmheader,0,shared_memory_size);
28  shmheader->maxNameLength = maxNameLength;
29  shmheader->numItems = 0;
32  sharedMemorySize = shared_memory_size;
33  strcpy(shmheader->ident, "daq");
34  allow_add_values = 0;
35 }
36 
38 {
39  delete shm;
40 }
41 
42 int rlDataAcquisitionProvider::readItemList(const char *filename)
43 {
44  if(filename == NULL) return DAQ_PROVIDER_ERROR;
45 
46  FILE *fin;
47  char line[1024], *cptr, *cptr2;
48  int maxItemNameLength,numItems,len,i;
49  int value_offset, delta_index;
50  int ilong;
51 
52  // get maxItemNameLength and numItems
53  fin = fopen(filename,"r");
54  if(fin == NULL)
55  {
56  printf("could not open itemlist %s\n",filename);
57  return DAQ_PROVIDER_ERROR;
58  }
59  maxItemNameLength = numItems = 0;
60  while(fgets(line,sizeof(line)-1,fin) != NULL)
61  {
62  if(line[0] > ' ' && line[0] != '#')
63  {
64  len = strlen(line);
65  if(len > maxItemNameLength) maxItemNameLength = len;
66  numItems++;
67  }
68  }
69  fclose(fin);
70  shmheader->maxItemNameLength = maxItemNameLength;
71  shmheader->numItems = numItems;
72 
73  // read items
74  fin = fopen(filename,"r");
75  if(fin == NULL)
76  {
77  printf("could not open itemlist %s\n",filename);
78  return DAQ_PROVIDER_ERROR;
79  }
80  cptr = shmvalues;
81  value_offset = shmheader->maxItemNameLength + 1;
82  delta_index = value_offset + shmheader->maxNameLength + 1;
83  ilong = sharedMemorySize - sizeof(SHM_HEADER);
84  if(ilong < numItems*delta_index)
85  {
86  printf("rlDataAcquisitionProvider::shared memmory is too small sharedMemorySize=%ld sizeNeeded=%d\n", sharedMemorySize, (int) (numItems*delta_index + sizeof(SHM_HEADER)));
87  return DAQ_PROVIDER_ERROR;
88  }
89  i = 0;
90  while(fgets(line,sizeof(line)-1,fin) != NULL)
91  {
92  if(line[0] > ' ' && line[0] != '#')
93  {
94  len = strlen(line);
95  if(len > shmheader->maxNameLength) line[shmheader->maxNameLength] = '\0';
96  cptr2 = strchr(line,'\n');
97  if(cptr2 != NULL) *cptr2 = '\0';
98  cptr2 = strchr(line,' ');
99  if(cptr2 != NULL) *cptr2 = '\0';
100  cptr2 = strchr(line,'\t');
101  if(cptr2 != NULL) *cptr2 = '\0';
102  strcpy(cptr + (i*delta_index), line);
103  i++;
104  }
105  }
106  fclose(fin);
107  strcpy(shmheader->ident, "daq");
108 
109  return 0;
110 }
111 
113 {
114  int value_offset, delta_index;
115  const char *cptr;
116 
117  current_item = 0;
118  if(shmheader == NULL) return NULL;
119  if(shmheader->numItems <= 0) return NULL;
120  if(strcmp(shmheader->ident,"daq") != 0) return NULL;
121 
122  value_offset = shmheader->maxItemNameLength + 1;
123  delta_index = value_offset + shmheader->maxNameLength + 1;
124 
125  cptr = shmvalues;
126  return cptr + (current_item * delta_index);
127 }
128 
130 {
131  int value_offset, delta_index;
132  const char *cptr;
133 
134  current_item++;
135  if(shmheader == NULL) return NULL;
136  if(shmheader->numItems <= 0) return NULL;
137  if(strcmp(shmheader->ident,"daq") != 0) return NULL;
138  if(shmheader->numItems <= current_item) return NULL;
139 
140  value_offset = shmheader->maxItemNameLength + 1;
141  delta_index = value_offset + shmheader->maxNameLength + 1;
142 
143  cptr = shmvalues;
144  return cptr + (current_item * delta_index);
145 }
146 
147 const char *rlDataAcquisitionProvider::stringValue(const char *variable)
148 {
149  int value_offset, delta_index, nmax, i;
150  const char *cptr;
151 
152  if(shmheader == NULL) return "DAQ_ERROR";
153  if(strcmp(shmheader->ident,"daq") != 0) return "DAQ_ERROR";
154  value_offset = shmheader->maxItemNameLength + 1;
155  delta_index = value_offset + shmheader->maxNameLength + 1;
156  nmax = shmheader->numItems;
157 
158  cptr = shmvalues;
159  for(i=0; i<nmax; i++)
160  {
161  if(strcmp(cptr,variable) == 0)
162  {
163  return cptr + value_offset;
164  }
165  cptr += delta_index;
166  }
167 
168  return "DAQ_ERROR";
169 }
170 
171 int rlDataAcquisitionProvider::intValue(const char *variable)
172 {
173  const char *cptr;
174  int ret;
175 
176  cptr = stringValue(variable);
177  if(isdigit(*cptr))
178  {
179  ret = DAQ_PROVIDER_ERROR;
180  sscanf(cptr,"%d",&ret);
181  return ret;
182  }
183  return DAQ_PROVIDER_ERROR;
184 }
185 
186 float rlDataAcquisitionProvider::floatValue(const char *variable)
187 {
188  const char *cptr;
189  float ret;
190 
191  cptr = stringValue(variable);
192  if(isdigit(*cptr) || *cptr == '-')
193  {
194  ret = DAQ_PROVIDER_ERROR;
195  sscanf(cptr,"%f",&ret);
196  return ret;
197  }
198  return DAQ_PROVIDER_ERROR;
199 }
200 
201 int rlDataAcquisitionProvider::setStringValue(const char *variable, const char *value)
202 {
203  if(variable == NULL || value == NULL || shmheader == NULL) return DAQ_PROVIDER_ERROR;
204  if(strcmp(shmheader->ident,"daq") != 0) return DAQ_PROVIDER_ERROR;
205 
206  int value_offset, delta_index, nmax, i;
207  char *cptr;
208 
209  if((int) strlen(variable) > shmheader->maxItemNameLength) return DAQ_PROVIDER_ERROR;
210 
211  int len = strlen(value);
212  char *val = new char[len+1];
213  strcpy(val,value);
214  if(len > shmheader->maxNameLength) val[shmheader->maxNameLength] = '\0';
215 
216  value_offset = shmheader->maxItemNameLength + 1;
217  delta_index = value_offset + shmheader->maxNameLength + 1;
218  nmax = shmheader->numItems;
219 
220  cptr = shmvalues;
221  for(i=0; i<nmax; i++)
222  {
223  if(strcmp(cptr,variable) == 0)
224  {
225  strcpy(cptr + value_offset, val); // update existing value
226  delete [] val;
227  return 0;
228  }
229  cptr += delta_index;
230  }
231 
232  if(allow_add_values == 1) // insert a new value
233  {
234  if(sharedMemorySize > ( (long) sizeof(SHM_HEADER) + (i*delta_index) + delta_index ) )
235  {
236  strcpy(cptr, variable); // insert the value
237  strcpy(cptr + value_offset, val); // insert the value
238  delete [] val;
239  shmheader->numItems += 1;
240  return 0;
241  }
242  else
243  {
244  ::printf("rlDataAcquisitionProvider: ERROR shared memory too small to add variable=%s val=%s please increase shared memory size", variable, val);
245  }
246  }
247 
248  delete [] val;
249  return DAQ_PROVIDER_ERROR;
250 }
251 
252 int rlDataAcquisitionProvider::setIntValue(const char *variable, int value)
253 {
254  char cval[128];
255 
256  sprintf(cval,"%d",value);
257  return setStringValue(variable,cval);
258 }
259 
260 int rlDataAcquisitionProvider::setFloatValue(const char *variable, float value)
261 {
262  char cval[128];
263 
264  sprintf(cval,"%f",value);
265  return setStringValue(variable,cval);
266 }
267 
269 {
270  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
271  return shmheader->readErrorCount;
272 }
273 
275 {
276  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
277  return shmheader->writeErrorCount;
278 }
279 
281 {
282  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
283  return shmheader->lifeCounter;
284 }
285 
287 {
288  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
289  shmheader->readErrorCount = count;
290  return 0;
291 }
292 
294 {
295  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
296  shmheader->writeErrorCount = count;
297  return 0;
298 }
299 
301 {
302  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
303  shmheader->lifeCounter = count;
304  return 0;
305 }
306 
308 {
309  if(shmheader == NULL) return DAQ_PROVIDER_ERROR;
310  if(shm->status == rlSharedMemory::OK) return 0;
311  return DAQ_PROVIDER_ERROR;
312 }
313 
314 int rlDataAcquisitionProvider::setAllowAddValues(int allow, int maxItemNameLength)
315 {
316  if(allow == 0) allow_add_values = 0;
317  else allow_add_values = 1;
318  if(maxItemNameLength > 0) shmheader->maxItemNameLength = maxItemNameLength;
319  return 0;
320 }
321 
322 
int intValue(const char *variable)
int setIntValue(const char *variable, int value)
const char * stringValue(const char *variable)
int readItemList(const char *filename)
int setFloatValue(const char *variable, float value)
rlDataAcquisitionProvider(int maxNameLength=31, const char *shared_memory="c:\utomation\hm\ataacquisition.shm", long shared_memory_size=65536)
int setAllowAddValues(int allow, int maxItemNameLength)
float floatValue(const char *variable)
int setStringValue(const char *variable, const char *value)