1 |
|
---|
2 | /********************************************************************\
|
---|
3 |
|
---|
4 | Name: ReadCard.cc
|
---|
5 |
|
---|
6 | Created by: F. Goebel
|
---|
7 |
|
---|
8 | Contents: Read input card
|
---|
9 |
|
---|
10 | \********************************************************************/
|
---|
11 |
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <string.h>
|
---|
14 | #include <stdlib.h>
|
---|
15 | #include "ReadCard.h"
|
---|
16 |
|
---|
17 | int ReadCard(char *card_flag, void *store, char type, FILE *fptr, int id) {
|
---|
18 | char id_flag[100];
|
---|
19 | sprintf(id_flag, "%d", id);
|
---|
20 | return ReadCard(card_flag, store, type, fptr, id_flag);
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | int ReadCard(char *card_flag, void *store, char type, FILE *fptr, char *id_flag) {
|
---|
25 | char *card_name, *card_val, *card_id=NULL;
|
---|
26 | char line[160];
|
---|
27 |
|
---|
28 | rewind(fptr);
|
---|
29 |
|
---|
30 | while (fgets(line, 160, fptr) != NULL) { // Read line by line
|
---|
31 | card_name = strtok(line," \t\n");
|
---|
32 | if (id_flag!=NULL) {
|
---|
33 | card_id = strtok(NULL," \t\n");
|
---|
34 | }
|
---|
35 | card_val = strtok(NULL," \t\n");
|
---|
36 |
|
---|
37 | if (card_name != NULL && card_val != NULL // Comment or empty line?
|
---|
38 | && card_name[0] != '*' && card_name[0] != '#') {
|
---|
39 |
|
---|
40 | if (strcmp(card_name, card_flag)!=0) { // Is this the card name we are looking for?
|
---|
41 | continue;
|
---|
42 | }
|
---|
43 | if ((id_flag!=NULL) && // Is id_flag required
|
---|
44 | (strcmp(card_id, id_flag)!=0)) { // Is it the correct id_flag
|
---|
45 | continue;
|
---|
46 | }
|
---|
47 |
|
---|
48 | switch (type) {
|
---|
49 | case 'I':
|
---|
50 | *((int *) store) = (int) strtol(card_val, (char**)NULL, 10);
|
---|
51 | break;
|
---|
52 | case 'i':
|
---|
53 | *((short *) store) = (short) strtol(card_val, (char**)NULL, 10);
|
---|
54 | break;
|
---|
55 | case 'U':
|
---|
56 | *((unsigned int *) store)
|
---|
57 | = (unsigned int) strtoul(card_val, (char**)NULL, 10);
|
---|
58 | break;
|
---|
59 | case 'u':
|
---|
60 | *((unsigned short *) store)
|
---|
61 | = (unsigned short) strtoul(card_val, (char**)NULL, 10);
|
---|
62 | break;
|
---|
63 | case 'f' :
|
---|
64 | *((float *) store) = atof(card_val);
|
---|
65 | break;
|
---|
66 | case 'd' :
|
---|
67 | *((double *) store) = atof(card_val);
|
---|
68 | break;
|
---|
69 | case 's' :
|
---|
70 | sprintf((char *) store,"%s",card_val);
|
---|
71 | break;
|
---|
72 | case 'c' :
|
---|
73 | *((char *) store) = card_val[0];
|
---|
74 | break;
|
---|
75 | default :
|
---|
76 | fprintf(stderr,"WARNING: ReadCard: unknown type: %c\n", type);
|
---|
77 | return -2;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return 0; // Found card name
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | //fprintf(stderr,"WARNING: ReadCard: card: %s not found\n", card_flag);
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | int ReadCardExt(char *card_flag, void *store, char type, FILE *fptr, char *id_flag) {
|
---|
90 | char *card_name, *card_val, *card_id=NULL;
|
---|
91 | char line[160];
|
---|
92 |
|
---|
93 | rewind(fptr);
|
---|
94 |
|
---|
95 | while (fgets(line, 160, fptr) != NULL) { // Read line by line
|
---|
96 | card_name = strtok(line," \t\n");
|
---|
97 | if (id_flag!=NULL) {
|
---|
98 | card_id = strtok(NULL," \t\n");
|
---|
99 | }
|
---|
100 | card_val = strtok(NULL," \t\n");
|
---|
101 |
|
---|
102 | if (card_name != NULL && card_val != NULL // Comment or empty line?
|
---|
103 | && card_name[0] != '*' && card_name[0] != '#') {
|
---|
104 |
|
---|
105 | if (strcmp(card_name, card_flag)!=0) { // Is this the card name we are looking for?
|
---|
106 | continue;
|
---|
107 | }
|
---|
108 | if ((id_flag!=NULL) && // Is id_flag required
|
---|
109 | (strcmp(card_id, id_flag)!=0)) { // Is it the correct id_flag
|
---|
110 | continue;
|
---|
111 | }
|
---|
112 |
|
---|
113 | switch (type) {
|
---|
114 | case 'I':
|
---|
115 | *((int *) store) = (int) strtol(card_val, (char**)NULL, 10);
|
---|
116 | break;
|
---|
117 | case 'i':
|
---|
118 | *((short *) store) = (short) strtol(card_val, (char**)NULL, 10);
|
---|
119 | break;
|
---|
120 | case 'U':
|
---|
121 | *((unsigned int *) store)
|
---|
122 | = (unsigned int) strtoul(card_val, (char**)NULL, 10);
|
---|
123 | break;
|
---|
124 | case 'u':
|
---|
125 | *((unsigned short *) store)
|
---|
126 | = (unsigned short) strtoul(card_val, (char**)NULL, 10);
|
---|
127 | break;
|
---|
128 | case 'f' :
|
---|
129 | *((float *) store) = atof(card_val);
|
---|
130 | break;
|
---|
131 | case 'd' :
|
---|
132 | *((double *) store) = atof(card_val);
|
---|
133 | break;
|
---|
134 | case 's' :
|
---|
135 | sprintf((char *) store,"%s",card_val);
|
---|
136 | break;
|
---|
137 | case 'c' :
|
---|
138 | *((char *) store) = card_val[0];
|
---|
139 | break;
|
---|
140 | default :
|
---|
141 | fprintf(stderr,"WARNING: ReadCard: unknown type: %c\n", type);
|
---|
142 | return -2;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return 0; // Found card name
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | //fprintf(stderr,"WARNING: ReadCard: card: %s not found\n", card_flag);
|
---|
150 | return -1;
|
---|
151 | }
|
---|