UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
config_file.cpp File Reference
#include "config_msvc.h"
#include "config_file.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for config_file.cpp:

Go to the source code of this file.

Classes

struct  config_file
 

Functions

void config_file_close (struct config_file *s)
 
string config_file_get_alias (struct config_file *s, const char *req_item_class, const char *requested_name)
 
string config_file_get_capture_filter_for_alias (struct config_file *s, const char *alias)
 
struct config_file * config_file_open (const char *name)
 
char * default_config_file (char *buf, int buf_len)
 
std::list< std::pair< std::string, std::string > > get_configured_capture_aliases (struct config_file *s)
 
static string get_nth_word (struct config_file *s, const char *prefix, int index)
 

Function Documentation

◆ config_file_close()

void config_file_close ( struct config_file * s)

Definition at line 102 of file config_file.cpp.

103{
104 if (!s)
105 return;
106 if (s->f)
107 fclose(s->f);
108
109 free(s);
110}
FILE * f
handle to config file, NULL if opening failed

References config_file::f.

Referenced by vidcap_dispatch_alias().

◆ config_file_get_alias()

string config_file_get_alias ( struct config_file * s,
const char * req_item_class,
const char * requested_name )

Returns alias for specified class and item name.

Caller must not deallocate returned value. Returned value is valid until corresponding instance of config_file is destroyed.

Parameters
sconfig_file instance
req_item_classtextual representation of configuration file name
requested_namerequested alias
Return values
NULLif not found
pointerpointer to string representation of the aliased item

Definition at line 157 of file config_file.cpp.

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}
static string get_nth_word(struct config_file *s, const char *prefix, int index)

References get_nth_word().

Referenced by vidcap_dispatch_alias().

Here is the call graph for this function:

◆ config_file_get_capture_filter_for_alias()

string config_file_get_capture_filter_for_alias ( struct config_file * s,
const char * alias )

Definition at line 173 of file config_file.cpp.

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}

References get_nth_word().

Referenced by vidcap_dispatch_alias().

Here is the call graph for this function:

◆ config_file_open()

struct config_file * config_file_open ( const char * name)

Creates config file instance

Parameters
namename of the config file
Returns
newly created instance. If opening of the config file failed, empty instance is returned and no error is reported!

Definition at line 87 of file config_file.cpp.

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}
const char * name
Definition settings.cpp:193
char file_name[1024]

References config_file::f, config_file::file_name, and name.

Referenced by vidcap_dispatch_alias().

◆ default_config_file()

char * default_config_file ( char * buf,
int buf_len )

Returns location of default UltraGrid config file

Parameters
[out]bufbuffer to be written to
[in]buf_lenlength of the buffer
Return values
NULLif path cannot be constructed
bufpointer output buffer pointer - if successfully performed

Definition at line 67 of file config_file.cpp.

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}

Referenced by vidcap_dispatch_alias().

◆ get_configured_capture_aliases()

std::list< std::pair< std::string, std::string > > get_configured_capture_aliases ( struct config_file * s)

Definition at line 189 of file config_file.cpp.

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}
#define suffix(x, y)
Definition hd-rum.c:27
const char * description
Definition cineform.cpp:126

References description, config_file::f, name, and suffix.

◆ get_nth_word()

static string get_nth_word ( struct config_file * s,
const char * prefix,
int index )
static

Definition at line 112 of file config_file.cpp.

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}

References config_file::f, and suffix.

Referenced by config_file_get_alias(), and config_file_get_capture_filter_for_alias().