• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws_settings
3  *
4  * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include <private-lib-core.h>
26 
27 lws_settings_instance_t *
lws_settings_init(const lws_settings_ops_t * so,void * opaque_plat)28 lws_settings_init(const lws_settings_ops_t *so, void *opaque_plat)
29 {
30 	lws_settings_instance_t *si = lws_zalloc(sizeof(*si), __func__);
31 
32 	if (!si)
33 		return NULL;
34 
35 	si->so = so;
36 	si->opaque_plat = opaque_plat;
37 
38 	return si;
39 }
40 
41 void
lws_settings_deinit(lws_settings_instance_t ** si)42 lws_settings_deinit(lws_settings_instance_t **si)
43 {
44 	lws_free(*si);
45 	*si = NULL;
46 }
47 
48 int
lws_settings_plat_printf(lws_settings_instance_t * si,const char * name,const char * format,...)49 lws_settings_plat_printf(lws_settings_instance_t *si, const char *name,
50 			 const char *format, ...)
51 {
52 	va_list ap;
53 	uint8_t *p;
54 	int n;
55 
56 	va_start(ap, format);
57 	n = vsnprintf(NULL, 0, format, ap);
58 	va_end(ap);
59 
60 	p = lws_malloc(n + 2, __func__);
61 	va_start(ap, format);
62 	vsnprintf((char *)p, n + 2, format, ap);
63 	va_end(ap);
64 
65 	n = si->so->set(si, name, p, n);
66 	lws_free(p);
67 
68 	return n;
69 }
70