Advertisement
chayanforyou

json_parser

Jul 25th, 2021
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include "Parser.h"
  2. #include <string.h>
  3.  
  4. void parseATResponse(const char *buffer, unsigned int size, unsigned int offset, char *response)
  5. {
  6.   const char *twoPointsPointer = strchr(buffer, ':');
  7.   unsigned int twoPointsIndex = (int)(twoPointsPointer - buffer);
  8.   unsigned int valueStartIndex = twoPointsIndex + offset;
  9.   for (unsigned int i = valueStartIndex; i < valueStartIndex + size; ++i)
  10.   {
  11.     response[i - valueStartIndex] = buffer[i];
  12.     response[i - valueStartIndex + 1] = '\0';
  13.   }
  14. }
  15.  
  16. void parseJSONResponse(const char *buffer, unsigned int bufferSize, char *response)
  17. {
  18.   unsigned int start_index = 0;
  19.   unsigned int i = 0;
  20.   while (i < bufferSize - 1 && start_index == 0)
  21.   {
  22.     char c = buffer[i];
  23.     if ('{' == c)
  24.     {
  25.       start_index = i;
  26.     }
  27.     ++i;
  28.   }
  29.  
  30.   unsigned int end_index = 0;
  31.   int j = bufferSize - 1;
  32.   while (j >= 0 && end_index == 0)
  33.   {
  34.     char c = buffer[j];
  35.     if ('}' == c)
  36.     {
  37.       end_index = j;
  38.     }
  39.     --j;
  40.   }
  41.  
  42.   for (int k = 0; k < (end_index - start_index) + 2; ++k)
  43.   {
  44.     response[k] = buffer[start_index + k];
  45.     response[k + 1] = '\0';
  46.   }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement