settings.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * lws_settings
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  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. #include <private-lib-core.h>
  25. lws_settings_instance_t *
  26. lws_settings_init(const lws_settings_ops_t *so, void *opaque_plat)
  27. {
  28. lws_settings_instance_t *si = lws_zalloc(sizeof(*si), __func__);
  29. if (!si)
  30. return NULL;
  31. si->so = so;
  32. si->opaque_plat = opaque_plat;
  33. return si;
  34. }
  35. void
  36. lws_settings_deinit(lws_settings_instance_t **si)
  37. {
  38. lws_free(*si);
  39. *si = NULL;
  40. }
  41. int
  42. lws_settings_plat_printf(lws_settings_instance_t *si, const char *name,
  43. const char *format, ...)
  44. {
  45. va_list ap;
  46. uint8_t *p;
  47. int n;
  48. va_start(ap, format);
  49. n = vsnprintf(NULL, 0, format, ap);
  50. va_end(ap);
  51. p = lws_malloc(n + 2, __func__);
  52. va_start(ap, format);
  53. vsnprintf((char *)p, n + 2, format, ap);
  54. va_end(ap);
  55. n = si->so->set(si, name, p, n);
  56. lws_free(p);
  57. return n;
  58. }