Ejemplo n.º 1
0
char*
stringBufferToString(string_buffer* buffer)
{
  //this is the realloc dependent function - it does not work
  //  char* result = buffer->string;
  //ensure that the string ends with 0
  if (buffer->string_length == 0 || buffer->string[(buffer->string_length - 1)]
      != 0)
    {
      stringBufferAdd(0, buffer);
    }
  /*  char* string = realloc(result, buffer->string_length);
   if (string==NULL) {
   free(result);
   }
   buffer->string=NULL;
   free(buffer);
   return string;*/
  char* result = malloc(buffer->string_length * sizeof(char));
  if (result == NULL)
    {
      return NULL;
    }
  strcpy(result, global_buffer);
  buffer->string = NULL;
  free(buffer);
  return result;
}
Ejemplo n.º 2
0
// Parse the input text into an unescaped cstring, and populate item.
int
aJsonClass::parseString(aJsonObject *item, FILE* stream)
{
  //we do not need to skip here since the first byte should be '\"'
  int in = fgetc(stream);
  if (in != '\"')
    {
      return EOF; // not a string!
    }
  item->type = aJson_String;
  //allocate a buffer & track how long it is and how much we have read
  string_buffer* buffer = stringBufferCreate();
  if (buffer == NULL)
    {
      //unable to allocate the string
      return EOF;
    }
  in = fgetc(stream);
  if (in == EOF)
    {
      stringBufferFree(buffer);
      return EOF;
    }
  while (in != EOF)
    {
      while (in != '\"' && in >= 32)
        {
          if (in != '\\')
            {
              stringBufferAdd((char) in, buffer);
            }
          else
            {
              in = fgetc(stream);
              if (in == EOF)
                {
                  stringBufferFree(buffer);
                  return EOF;
                }
              switch (in)
                {
              case '\\':
                stringBufferAdd('\\', buffer);
                break;
              case '\"':
                stringBufferAdd('\"', buffer);
                break;
              case 'b':
                stringBufferAdd('\b', buffer);
                break;
              case 'f':
                stringBufferAdd('\f', buffer);
                break;
              case 'n':
                stringBufferAdd('\n', buffer);
                break;
              case 'r':
                stringBufferAdd('\r', buffer);
                break;
              case 't':
                stringBufferAdd('\t', buffer);
                break;
              default:
                //we do not understand it so we skip it
                break;
                }
            }
          in = fgetc(stream);
          if (in == EOF)
            {
              stringBufferFree(buffer);
              return EOF;
            }
        }
      //the string ends here
      item->valuestring = stringBufferToString(buffer);
      return 0;
    }
  //we should not be here but it is ok
  return 0;
}