cJSON_Utils.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "cJSON_Utils.h"
  5. cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
  6. {
  7. cJSON *target=object;int which=0;const char *s=0;int len=0;char *element=0,*e=0;
  8. while (*pointer=='/' && object)
  9. {
  10. pointer++;
  11. if (object->type==cJSON_Array)
  12. {
  13. which=0;
  14. while (*pointer>='0' && *pointer<='9') {which*=10;which+=*pointer++ - '0';}
  15. if (*pointer && *pointer!='/') return 0;
  16. object=cJSON_GetArrayItem(object,which);
  17. }
  18. else if (object->type==cJSON_Object)
  19. {
  20. s=pointer;len=0;
  21. while (*s && *s!='/') {if (*s!='~') len++; s++;}
  22. e=element=malloc(len+1); if (!element) return 0;
  23. element[len]=0;
  24. while (*pointer && *pointer!='/')
  25. {
  26. if (*pointer=='~' && pointer[1]=='0') *e++='~',pointer+=2;
  27. else if (*pointer=='~' && pointer[1]=='1') *e++='/',pointer+=2;
  28. else if (*pointer=='~') {free(element); return 0;} // Invalid encoding.
  29. else *e++=*pointer++;
  30. }
  31. object=cJSON_GetObjectItem(object,element);
  32. free(element);
  33. }
  34. else return 0;
  35. }
  36. return object;
  37. }