You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.3 KiB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include "utils.h"
  6. #include "jconf.h"
  7. #include "json.h"
  8. #include "string.h"
  9. #define INT_DIGITS 19 /* enough for 64 bit integer */
  10. static char *itoa(int i)
  11. {
  12. /* Room for INT_DIGITS digits, - and '\0' */
  13. static char buf[INT_DIGITS + 2];
  14. char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */
  15. if (i >= 0) {
  16. do {
  17. *--p = '0' + (i % 10);
  18. i /= 10;
  19. } while (i != 0);
  20. return p;
  21. }
  22. else { /* i < 0 */
  23. do {
  24. *--p = '0' - (i % 10);
  25. i /= 10;
  26. } while (i != 0);
  27. *--p = '-';
  28. }
  29. return p;
  30. }
  31. static char *to_string(const json_value *value) {
  32. if (value->type == json_string) {
  33. return strndup(value->u.string.ptr, value->u.string.length);
  34. } else if (value->type == json_integer) {
  35. return strdup(itoa(value->u.integer));
  36. } else if (value->type == json_null) {
  37. return "null";
  38. } else {
  39. LOGE("%d\n", value->type);
  40. FATAL("Invalid config format.\n");
  41. }
  42. return 0;
  43. }
  44. static int to_int(const json_value *value) {
  45. if (value->type == json_string) {
  46. return atoi(value->u.string.ptr);
  47. } else if (value->type == json_integer) {
  48. return value->u.integer;
  49. } else {
  50. FATAL("Invalid config format.\n");
  51. }
  52. return 0;
  53. }
  54. jconf_t *read_jconf(const char* file) {
  55. static jconf_t conf;
  56. char *buf;
  57. json_value *obj;
  58. FILE *f = fopen(file, "r");
  59. if (f == NULL) FATAL("Invalid config path.\n");
  60. fseek(f, 0, SEEK_END);
  61. long pos = ftell(f);
  62. fseek(f, 0, SEEK_SET);
  63. if (pos >= MAX_CONF_SIZE) FATAL("Too large config file.\n");
  64. buf = malloc(pos);
  65. if (buf == NULL) FATAL("No enough memory.\n");
  66. fread(buf, pos, 1, f);
  67. fclose(f);
  68. obj = json_parse(buf);
  69. if (obj->type == json_object) {
  70. int i, j;
  71. for (i = 0; i < obj->u.object.length; i++) {
  72. char *name = obj->u.object.values[i].name;
  73. json_value *value = obj->u.object.values[i].value;
  74. if (strcmp(name, "server") == 0) {
  75. if (value->type == json_array) {
  76. for (j = 0; j < value->u.array.length; j++) {
  77. if (j >= MAX_REMOTE_NUM) break;
  78. json_value *v = value->u.array.values[j];
  79. conf.remote_host[j] = to_string(v);
  80. conf.remote_num = j + 1;
  81. }
  82. } else if (value->type == json_string) {
  83. conf.remote_host[0] = to_string(value);
  84. conf.remote_num = 1;
  85. }
  86. } else if (strcmp(name, "server_port") == 0) {
  87. conf.remote_port = to_string(value);
  88. } else if (strcmp(name, "local_port") == 0) {
  89. conf.local_port = to_string(value);
  90. } else if (strcmp(name, "password") == 0) {
  91. conf.password = to_string(value);
  92. } else if (strcmp(name, "method") == 0) {
  93. conf.method = to_string(value);
  94. } else if (strcmp(name, "timeout") == 0) {
  95. conf.timeout = to_string(value);
  96. }
  97. }
  98. } else {
  99. FATAL("Invalid config file\n");
  100. }
  101. free(buf);
  102. json_value_free(obj);
  103. return &conf;
  104. }