-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMSspex.cc
165 lines (122 loc) · 3.45 KB
/
PMSspex.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
-------------------------------------------------------------------------
OBJECT NAME: PMSspex.cc
FULL NAME:
ENTRY POINTS: InitPMSspecs()
GetParameter()
GetList()
DESCRIPTION:
COPYRIGHT: University Corporation for Atmospheric Research, 1995-2020
-------------------------------------------------------------------------
*/
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include "PMSspex.h"
const char PMSspex::COMMENT = '#';
/* -------------------------------------------------------------------- */
PMSspex::PMSspex(const std::string fileName)
{
int i, cnt;
char *file[2048], PMSbuffer[512], *p;
FILE *fp;
if ((fp = fopen(fileName.c_str(), "r")) == NULL)
std::cerr << "PMSspex: Can't open " << fileName << ".\n";
/* Read file and strip out comments.
*/
for (cnt = 0; fp && fgets(PMSbuffer, 512, fp); )
{
if (PMSbuffer[0] == COMMENT)
continue;
PMSbuffer[strlen(PMSbuffer)] = '\0'; /* Ditch newline */
if ( (p = strchr(PMSbuffer, COMMENT)) )
*p = '\0';
else
p = &PMSbuffer[strlen(PMSbuffer)];
while ( isspace(*(--p)) ) /* Strip trailing white-space */
*p = '\0';
if ( isspace(PMSbuffer[0]) ) /* Belongs to previous line */
{
char *p1;
p1 = new char [strlen(file[cnt-1]) + strlen(PMSbuffer)+1];
strcpy(p1, file[cnt-1]);
strcat(p1, PMSbuffer);
delete [] file[cnt-1];
file[cnt-1] = p1;
}
else /* Fresh line */
{
file[cnt] = new char [strlen(PMSbuffer)+1];
strcpy(file[cnt], PMSbuffer);
++cnt;
}
}
if (fp) fclose(fp);
PMSfile = new char *[cnt+1];
for (i = 0; i < cnt; ++i)
PMSfile[i] = file[i];
PMSfile[i] = NULL;
} /* END INITPMSSPECS */
/* -------------------------------------------------------------------- */
std::string PMSspex::GetParameter(const std::string serialNumber, const char parameter[])
{
int i;
char *p;
for (i = 0; PMSfile[i]; ++i)
{
if (strncmp(PMSfile[i], "START", 5) == 0)
{
for (p = &PMSfile[i][5]; *p == ' ' || *p == '\t'; ++p)
;
if (strcmp(serialNumber.c_str(), p) == 0)
{
int len = strlen(parameter);
for (; PMSfile[i]; ++i)
{
if (strcmp(PMSfile[i], "END") == 0)
return std::string("");
if (strncmp(PMSfile[i], parameter, len) == 0)
{
if (PMSfile[i][len] != ' ' && PMSfile[i][len] != '\t')
continue;
for (p = &PMSfile[i][len]; *p == ' ' || *p == '\t'; ++p)
;
return std::string(p);
}
}
}
}
}
return std::string("");
} /* END GETPMSPARAMETER */
/* -------------------------------------------------------------------- */
PMSspex::~PMSspex()
{
if (PMSfile)
{
int i;
for (i = 0; PMSfile[i]; ++i)
delete [] PMSfile[i];
delete [] PMSfile;
}
} /* END RELEASEPMSSPECS */
/* -------------------------------------------------------------------- */
int PMSspex::GetSerialNumList(char *list[]) /* returns list of serial numbers */
{
int i, cnt = 0;
char *p;
list[0] = NULL;
for (i = 0; PMSfile[i]; ++i)
if (strncmp(PMSfile[i], "START", 5) == 0)
{
for (p = &PMSfile[i][5]; *p == ' ' || *p == '\t'; ++p)
;
list[cnt] = new char [strlen(p)+1];
strcpy(list[cnt], p);
list[++cnt] = NULL;
}
return(cnt);
} /* END GETPMSLIST */
/* END INIT.C */