-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.c
111 lines (97 loc) · 3.08 KB
/
config.c
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
/***********************************************************************/ /**
* @file config.c:
*
* File oriented utility routines to use with a DataLink connection
* description.
*
* This file is part of the DataLink Library.
*
* Copyright (c) 2023 Chad Trabant, EarthScope Data Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "libdali.h"
#include "portable.h"
/***********************************************************************/ /**
* @brief Create a compound regular expression from a list in a file
*
* Read a list of stream regular expressions from a file and create a
* compound regular expression. The caller is responsible for
* free'ing the returned string.
*
* @return A composite regex pattern on success and NULL on error.
***************************************************************************/
char *
dl_read_streamlist (DLCP *dlconn, const char *streamfile)
{
char *regex = 0;
char line[100];
char *ptr;
int streamfd;
int count = 0;
int idx;
/* Open the stream list file */
if ((streamfd = dlp_openfile (streamfile, 'r')) < 0)
{
if (errno == ENOENT)
{
dl_log_r (dlconn, 2, 0, "could not find stream list file: %s\n", streamfile);
return NULL;
}
else
{
dl_log_r (dlconn, 2, 0, "opening stream list file, %s\n", strerror (errno));
return NULL;
}
}
dl_log_r (dlconn, 1, 1, "Reading list of streams from %s\n", streamfile);
while ((dl_readline (streamfd, line, sizeof (line))) >= 0)
{
ptr = line;
/* Trim initial white space */
while (isspace ((int)*ptr))
ptr++;
/* Trim trailing white space */
idx = strlen (ptr) - 1;
while (idx >= 0 && isspace ((int)ptr[idx]))
ptr[idx--] = '\0';
/* Ignore blank or comment lines */
if (strlen (ptr) == 0 || ptr[0] == '#' || ptr[0] == '*')
continue;
/* Add this stream to the stream chain */
if (dl_addtostring (®ex, ptr, "|", MAXREGEXSIZE))
{
dl_log_r (dlconn, 2, 0, "no streams defined in %s\n", streamfile);
return NULL;
}
count++;
}
if (count == 0)
{
dl_log_r (dlconn, 2, 0, "no streams defined in %s\n", streamfile);
}
else if (count > 0)
{
dl_log_r (dlconn, 1, 2, "Read %d streams from %s\n", count, streamfile);
}
if (close (streamfd))
{
dl_log_r (dlconn, 2, 0, "closing stream list file, %s\n", strerror (errno));
return NULL;
}
return regex;
} /* End of dl_read_streamlist() */