dvbcfg_common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * dvbcfg - support for linuxtv configuration files
  3. * common functions
  4. *
  5. * Copyright (C) 2006 Christoph Pfister <christophpfister@gmail.com>
  6. * Copyright (C) 2005 Andrew de Quincey <adq_dvb@lidskialf.net>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "dvbcfg_common.h"
  26. int dvbcfg_parse_int(char **text, char *tokens)
  27. {
  28. char *start = *text;
  29. char *stop = *text;
  30. int value;
  31. while (*stop != '\0') {
  32. if (strchr(tokens, *stop) != NULL) {
  33. *stop = '\0';
  34. stop++;
  35. break;
  36. }
  37. stop++;
  38. }
  39. if (sscanf(start, "%i", &value) == 1) {
  40. *text = stop;
  41. return value;
  42. }
  43. *text = NULL;
  44. return -1;
  45. }
  46. int dvbcfg_parse_char(char **text, char *tokens)
  47. {
  48. char *start = *text;
  49. char *stop = *text;
  50. char value;
  51. while (*stop != '\0') {
  52. if (strchr(tokens, *stop) != NULL) {
  53. *stop = '\0';
  54. stop++;
  55. break;
  56. }
  57. stop++;
  58. }
  59. if (sscanf(start, "%c", &value) == 1) {
  60. *text = stop;
  61. return value;
  62. }
  63. *text = NULL;
  64. return -1;
  65. }
  66. int dvbcfg_parse_setting(char **text, char *tokens, const struct dvbcfg_setting *settings)
  67. {
  68. char *start = *text;
  69. char *stop = *text;
  70. while (*stop != '\0') {
  71. if (strchr(tokens, *stop) != NULL) {
  72. *stop = '\0';
  73. stop++;
  74. break;
  75. }
  76. stop++;
  77. }
  78. while (settings->name) {
  79. if (strcmp(start, settings->name) == 0) {
  80. *text = stop;
  81. return settings->value;
  82. }
  83. settings++;
  84. }
  85. *text = NULL;
  86. return -1;
  87. }
  88. void dvbcfg_parse_string(char **text, char *tokens, char *dest, unsigned long size)
  89. {
  90. char *start = *text;
  91. char *stop = *text;
  92. unsigned long length;
  93. while ((*stop != '\0') && (strchr(tokens, *stop) == NULL))
  94. stop++;
  95. length = (stop - start) + 1;
  96. if (length <= size) {
  97. if (strchr(tokens, *stop) != NULL) {
  98. *stop = '\0';
  99. *text = stop + 1;
  100. } else
  101. *text = stop;
  102. memcpy(dest, start, length);
  103. return;
  104. }
  105. *text = NULL;
  106. return;
  107. }
  108. const char *dvbcfg_lookup_setting(unsigned int setting, const struct dvbcfg_setting *settings)
  109. {
  110. while (settings->name) {
  111. if (setting == settings->value)
  112. return settings->name;
  113. settings++;
  114. }
  115. return NULL;
  116. }