PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/vale/src/json/loadj.c

https://gitlab.com/libre-mesh/lime-packages
C | 185 lines | 154 code | 28 blank | 3 comment | 34 complexity | 06d160e07d34fa2491eb1c7edb7e7f0c MD5 | raw file
  1. #include "jsmn.h"
  2. #define _XOPEN_SOURCE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <time.h>
  10. #define DATE_FORMAT "%Y-%m-%d %H:%M:%S"
  11. char *load_file(char *path);
  12. typedef struct group {
  13. int from;
  14. int to;
  15. char *key;
  16. } group;
  17. typedef struct client {
  18. unsigned char id[24];
  19. time_t expire;
  20. } client;
  21. group *load_groups(jsmntok_t * tokens, char *data);
  22. client *load_clients(jsmntok_t * tokens, char *data);
  23. void print_clients(FILE *out, client *clients);
  24. int main(int argc, char **argv)
  25. {
  26. jsmntok_t tokens[1024] = {{0}};
  27. jsmn_parser parser;
  28. jsmnerr_t err;
  29. if (argc < 2)
  30. return 1;
  31. char *data = load_file(argv[1]);
  32. jsmn_init(&parser);
  33. err = jsmn_parse(&parser, data, tokens, sizeof(tokens));
  34. if (err != JSMN_SUCCESS)
  35. return 1;
  36. client *clients = load_clients(tokens, data);
  37. int i;
  38. client p = {"test2",time(NULL)};
  39. client_insert(clients,&p);
  40. save_clients("./clients.new",clients);
  41. rename("./clients.new","./clients");
  42. return 0;
  43. }
  44. group *load_groups(jsmntok_t * tokens, char *data)
  45. {
  46. group *groups;
  47. int i, g = 0;
  48. int total = tokens[0].size;
  49. if (total <= 0)
  50. return NULL;
  51. groups = malloc(total * sizeof(group));
  52. for (i = 1; i <= total; i++) {
  53. if (tokens[i].size == 3 && tokens[i].type == JSMN_ARRAY) {
  54. /*next three tokens have the group data */
  55. groups[g].from = atoi(&data[tokens[i + 1].start]);
  56. groups[g].to = atoi(&data[tokens[i + 2].start]);
  57. groups[g].key = &data[tokens[i + 3].start];
  58. data[tokens[i + 3].end] = '\0';
  59. group *gr = &groups[g];
  60. g++;
  61. printf("%d %d %s\n", gr->from, gr->to, gr->key);
  62. }
  63. if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
  64. total += tokens[i].size;
  65. i += tokens[i].size;
  66. }
  67. }
  68. return groups;
  69. }
  70. client *load_clients(jsmntok_t * tokens, char *data)
  71. {
  72. client *clients;
  73. int i, c = 0;
  74. int total = tokens[0].size;
  75. struct tm tm;
  76. if (total <= 0)
  77. return NULL;
  78. //XXX +100, expand array
  79. clients = malloc(total+100 * sizeof(client));
  80. for (i = 1; i <= total; i++) {
  81. if (tokens[i].size == 2 && tokens[i].type == JSMN_ARRAY) {
  82. size_t idlen = tokens[i + 1].end - tokens[i + 1].start;
  83. idlen = idlen > 24-1 ? 24-1 : idlen;
  84. data[tokens[i + 1].start+idlen] = '\0';
  85. memcpy(clients[c].id, &data[tokens[i + 1].start],idlen);
  86. memset(&tm, 0, sizeof(tm));
  87. if (!strptime(&data[tokens[i + 2].start], DATE_FORMAT, &tm))
  88. clients[c].expire = -1;
  89. else
  90. clients[c].expire = mktime(&tm);
  91. c++;
  92. }
  93. if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
  94. total += tokens[i].size;
  95. i += tokens[i].size;
  96. }
  97. }
  98. clients[c].expire = 0;
  99. return clients;
  100. }
  101. //XXX replace by dynamic array
  102. void client_insert(client *clients, client *newclient) {
  103. int i = 0;
  104. while(clients[i].expire && strcmp((char *)clients[i].id,(char *)newclient->id)) i++;
  105. memcpy(&clients[i],newclient,sizeof(client));
  106. clients[i+1].expire = 0;
  107. }
  108. int save_clients(char *path, client *clients) {
  109. FILE *out = fopen(path,"w");
  110. if(!out)
  111. return -1;
  112. print_clients(out,clients);
  113. fclose(out);
  114. return 0;
  115. }
  116. void print_clients(FILE *out, client *clients) {
  117. struct tm tm;
  118. char buf[256];
  119. int i = 0;
  120. fprintf(out,"[\n");
  121. while(clients[i].expire) {
  122. gmtime_r(&clients[i].expire, &tm);
  123. strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
  124. fprintf(out,"[\"%s\",\"%s\"],\n", clients[i].id, buf);
  125. i++;
  126. }
  127. fprintf(out,"]\n");
  128. }
  129. char *load_file(char *path)
  130. {
  131. int fd = open(path, O_RDONLY);
  132. char *buf;
  133. off_t size;
  134. if (fd == -1)
  135. return NULL;
  136. size = lseek(fd, 0, SEEK_END);
  137. lseek(fd, 0, SEEK_SET);
  138. buf = malloc(size + 1);
  139. buf[size] = '\0';
  140. if (read(fd, buf, size) == size) {
  141. close(fd);
  142. return buf;
  143. }
  144. close(fd);
  145. free(buf);
  146. return NULL;
  147. }
  148. int save_file(char *path, char *data, size_t size) {
  149. int fd = open(path,O_CREAT|O_WRONLY|O_TRUNC);
  150. if (fd == -1) return fd;
  151. if (write(fd,data,size) == size) {
  152. close(fd);
  153. return size;
  154. }
  155. close(fd);
  156. return -1;
  157. }