source: hvcontrol/src/HVConfig.cc@ 103

Last change on this file since 103 was 100, checked in by ogrimm, 15 years ago
Number of HV boards (almost) unlimited.
File size: 3.5 KB
Line 
1
2/********************************************************************\
3
4 Name: HVConfig.cc
5
6 Created by: Sebastian Commichau, November 2008
7 commichau@phys.ethz.ch
8
9 Contents: Class reading the HV utility configuration file
10
11\********************************************************************/
12
13#include "HVConfig.h"
14
15
16HVConfig::HVConfig(const char *configfile) {
17
18 // Read configuration file
19 FILE *f;
20
21 if ((f = fopen(configfile,"r")) == NULL) {
22 printf("Could not open configuration file: %s\n", configfile);
23 throw;
24 }
25 printf("Opening configuration file: %s\n", configfile);
26
27 // Determine number of entries in 'Boards' card and allocate memeory
28 NumHVBoards = (int) ReadCard("Boards", NULL, 'n', f);
29 fUSBDevice = new char* [NumHVBoards];
30 for (int i=0; i<NumHVBoards; i++) fUSBDevice[i] = new char [BUFFER_LENGTH];
31 ReadCard("Boards", fUSBDevice, 'S', f, NumHVBoards);
32
33 ReadCard("LogFile", fLogFile, 's',f);
34 ReadCard("PixMapTable", fPixMapTable,'s',f);
35 ReadCard("SlowDirectory", fSlowDir, 's',f);
36 ReadCard("TimeOut", &fTimeOut, 'f', f);
37 ReadCard("StatusRefreshRate", &fStatusRefreshRate, 'f', f);
38 ReadCard("CCPort", &fCCPort, 'I', f);
39 ReadCard("DACMin", &DACMin, 'I', f);
40 ReadCard("DACMax", &DACMax, 'I', f);
41 ReadCard("HVCalibOffset", &fHVCalibOffset, 'f', f);
42 ReadCard("HVCalibSlope", &fHVCalibSlope, 'f', f);
43 ReadCard("HVMaxDiff", &fHVMaxDiff, 'U', f);
44
45 fclose(f);
46}
47
48
49HVConfig::~HVConfig() {
50
51 for (int i=0; i<NumHVBoards; i++) delete[] fUSBDevice[i];
52 delete[] fUSBDevice;
53}
54
55
56// ReadCard function (original version by F. Goebel)
57// Data is read into an array if MaxNum is larger than 1
58// If Type is 'n', the number of elements for the card is counted.
59// Type 'S' is for reading an array of strings
60unsigned int ReadCard(const char *card_flag, void *store, char Type, FILE *File, unsigned int MaxNum) {
61
62 char *card_name, *card_val, Buffer[MAX_COM_SIZE];
63 unsigned int Count=0;
64
65 rewind(File);
66
67 while (fgets(Buffer, sizeof(Buffer), File) != NULL) { // Read line by line
68 card_name = strtok(Buffer," \t\n");
69
70 // Ignore empty lines, comments, and skip if card name does not match
71 if (card_name==NULL || card_name[0]=='#' || strcmp(card_name, card_flag)!=0) continue;
72
73 // Read numbers of given type (if MaxNum>1 read array)
74 while ((card_val=strtok(NULL," \t\n"))!=NULL && card_val[0]!='#' && Count++<MaxNum) {
75 switch (Type) {
76 case 'I': *(((int *&) store)++) = (int) strtol(card_val, NULL, 10);
77 break;
78 case 'i': *(((short *&) store)++) = (short) strtol(card_val, NULL, 10);
79 break;
80 case 'U': *(((unsigned int *&) store)++) = (unsigned int) strtoul(card_val, NULL, 10);
81 break;
82 case 'u': *(((unsigned short *&) store)++) = (unsigned short) strtoul(card_val, NULL, 10);
83 break;
84 case 'f': *(((float *&) store)++) = atof(card_val);
85 break;
86 case 'd': *(((double *&) store)++) = atof(card_val);
87 break;
88 case 's': sprintf((char *) store, "%s", card_val);
89 break;
90 case 'S': sprintf(*(((char **&) store)++), "%s", card_val);
91 break;
92 case 'c': *(((char *&) store)++) = card_val[0];
93 break;
94 case 'n': MaxNum = UINT_MAX;
95 break;
96 default: fprintf(stderr,"Warning: Unknown type '%c' for reading of configuration file\n", Type);
97 return 0;
98 }
99 }
100 return Count; // Finished reading data for card name
101 }
102 return 0;
103}
104
Note: See TracBrowser for help on using the repository browser.