cJSON_Utils.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <ctype.h>
  2. #include "cJSON_Utils.h"
  3. static int cJSONUtils_Pstrcasecmp(const char *a,const char *e)
  4. {
  5. if (!a || !e) return (a==e)?0:1;
  6. for (;*a && *e && *e!='/';a++,e++) {
  7. if (*e=='~') {if (!(e[1]=='0' && *a=='~') && !(e[1]=='1' && *a=='/')) return 1; else e++;}
  8. else if (tolower(*a)!=tolower(*e)) return 1;
  9. }
  10. if ((*e!=0 && *e!='/') != (*a!=0)) return 1;
  11. return 0;
  12. }
  13. cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer)
  14. {
  15. cJSON *target=object;int which=0;const char *element=0;
  16. while (*pointer=='/' && object)
  17. {
  18. pointer++;
  19. if (object->type==cJSON_Array)
  20. {
  21. which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0';
  22. if (*pointer && *pointer!='/') return 0;
  23. object=cJSON_GetArrayItem(object,which);
  24. }
  25. else if (object->type==cJSON_Object)
  26. {
  27. element=pointer; while (*pointer && *pointer!='/') pointer++;
  28. object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,element)) object=object->next; // GetObjectItem.
  29. }
  30. else return 0;
  31. }
  32. return object;
  33. }