UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
config_file.cpp
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2013 CESNET z.s.p.o.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * 3. Neither the name of CESNET nor the names of its contributors may be
23 * used to endorse or promote products derived from this software without
24 * specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
28 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#ifdef HAVE_CONFIG_H
41#include "config.h"
42#include "config_win32.h"
43#include "config_unix.h"
44#endif // HAVE_CONFIG_H
45#include "config_msvc.h"
46
47#include "config_file.h"
48
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53using namespace std;
54
56 FILE *f;
57 char file_name[1024];
58};
59
67char *default_config_file(char *buf, int buf_len)
68{
69 const char *rc_suffix = "/.ug.rc";
70
71 if(!getenv("HOME")) {
72 return NULL;
73 }
74
75 strncpy(buf, getenv("HOME"), buf_len - 1);
76 strncat(buf, rc_suffix, buf_len - 1);
77 return buf;
78}
79
87struct config_file *config_file_open(const char *name)
88{
89 if (name == NULL) {
90 return NULL;
91 }
92 FILE *f = fopen(name, "r");
93 if (f == NULL) {
94 return NULL;
95 }
96 struct config_file *s = (struct config_file *) calloc(1, sizeof(struct config_file));
97 s->f = f;
98 strncpy(s->file_name, name, sizeof(s->file_name) - 1);
99 return s;
100}
101
103{
104 if (!s)
105 return;
106 if (s->f)
107 fclose(s->f);
108
109 free(s);
110}
111
112static string get_nth_word(struct config_file *s, const char *prefix, int index)
113{
114 if (!s->f)
115 return {};
116
117 char line[1024];
118 fseek(s->f, 0, SEEK_SET); // rewind
119 while (fgets(line, sizeof(line), s->f)) {
120 if (strncmp(prefix, line, strlen(prefix)) == 0) {
121 char *suffix = line + strlen(prefix);
122 if (suffix[strlen(suffix) - 1] == '\n')
123 suffix[strlen(suffix) - 1] = '\0';
124
125 char *item, *save_ptr, *tmp;
126 tmp = suffix;
127 int i = 0;
128 while ((item = strtok_r(tmp, " ", &save_ptr))) {
129 tmp = NULL;
130 if (i++ == index) {
131 if (strlen(item) == 0) {
132 return {};
133 }
134 return item;
135 }
136 }
137 }
138 }
139
140 return string();
141}
142
157string config_file_get_alias(struct config_file *s, const char *req_item_class,
158 const char *requested_name)
159{
160 if (!s) {
161 return {};
162 }
163
164 char prefix[1024];
165 memset(prefix, 0, sizeof(prefix));
166
167 snprintf(prefix, sizeof(prefix) - 1, "alias %s %s ",
168 req_item_class, requested_name);
169
170 return get_nth_word(s, prefix, 0);
171}
172
174 const char *alias)
175{
176 if (!s) {
177 return {};
178 }
179
180 char prefix[1024];
181 memset(prefix, 0, sizeof(prefix));
182
183 snprintf(prefix, sizeof(prefix) - 1, "capture-filter %s ",
184 alias);
185
186 return get_nth_word(s, prefix, 0);
187}
188
189std::list<std::pair<std::string, std::string>> get_configured_capture_aliases(struct config_file *s)
190{
191 if (!s) {
192 return {};
193 }
194
195 fseek(s->f, 0, SEEK_SET); // rewind
196
197 std::list<std::pair<std::string, std::string>> ret;
198
199 char line[1024];
200 char prefix[1024] = "";
201 snprintf(prefix, sizeof prefix, "alias capture ");
202 fseek(s->f, 0, SEEK_SET); // rewind
203 while (fgets(line, sizeof(line), s->f)) {
204 if (line[strlen(line) - 1] == '\n')
205 line[strlen(line) - 1] = '\0';
206
207 if (strncasecmp(prefix, line, strlen(prefix)) == 0) {
208 char *suffix = line + strlen(prefix);
209 char *save_ptr, *item;
210 if ((item = strtok_r(suffix, " ", &save_ptr))) {
211 string name(item);
212 string description;
213 char *cfg = strtok_r(NULL, " ", &save_ptr); // skip config string
214 item = strtok_r(NULL, " ", &save_ptr); // skip config string
215 if (item) {
216 description += item;
217 while ((item = strtok_r(NULL, " ", &save_ptr))) {
218 description += string(" ") + item;
219 }
220 } else {
221 description = cfg; // else use config string as description (grrr)
222 }
223 ret.emplace_back(name, description);
224 }
225 }
226 }
227
228 return ret;
229}
230
struct config_file * config_file_open(const char *name)
void config_file_close(struct config_file *s)
static string get_nth_word(struct config_file *s, const char *prefix, int index)
string config_file_get_capture_filter_for_alias(struct config_file *s, const char *alias)
std::list< std::pair< std::string, std::string > > get_configured_capture_aliases(struct config_file *s)
char * default_config_file(char *buf, int buf_len)
string config_file_get_alias(struct config_file *s, const char *req_item_class, const char *requested_name)
#define suffix(x, y)
const char * name
Definition settings.cpp:193
char file_name[1024]
FILE * f
handle to config file, NULL if opening failed
Definition hd-rum.c:27
const char * description
Definition cineform.cpp:126