Skip to content
Ayush Joshi edited this page Oct 7, 2022 · 5 revisions

This page shows how you can use cjson and its internal libraries in your project.

StringStream

The instance instantiated through this structure allows you to stream data of any type to itself. This functionality is useful for parsing data which JSON parsing requires a lot.

  • Using StringStream to allocate empty string.

    #include <stdio.h>
    #include <cjson/data/sstream/sstream.h>
    
    int main(void) {
      // Returns a (null) terminated string that is allocated dynamically.
      StringStream sstream = StringStreamAlloc();
      // Print a (null) terminated string.
      PrintSstream(sstream, FALSE);
      return 0;
    }
  • Using StringStream to allocate const char*.

    #include <stdio.h>
    #include <cjson/data/sstream/sstream.h>
    
    int main(void) {
      StringStream sstream = StringStreamStrAlloc("foo");
      // Print "foo".
      PrintSstream(sstream, FALSE);
      return 0;
    }
  • Using StringStream to allocate exactly N bytes of a const char*.

    #include <stdio.h>
    #include <cjson/data/sstream/sstream.h>
    
    int main(void) {
      StringStream sstream = StringStreamStrNAlloc("foobar", 3);
      // Print "foo".
      PrintSstream(sstream, FALSE);
      return 0;
    }

Modifiers

  • Concatenating string on the existing StringStream instance using StringStreamConcat.

    #include <stdio.h>
    #include <cjson/data/sstream/sstream.h>
    
    int main(void) {
      StringStream sstream = StringStreamStrAlloc("foo");
      // Concatenating "bar", "fuzz", 101.
      StringStreamConcat(&sstream, " %s %s %d", "bar", "fuzz", 101);
      // Prints "foo bar fuzz 101".
      PrintSstream(sstream, FALSE);
      return 0;
    }

Iterators

const char* StringStreamBegin(const StringStream* const sstream)

Returns a const char* to the beginning of the data instance of the StringStream instance.

const char* StringStreamEnd(const StringStream *sstream)

Returns a iterator to the end of the data instance of the StringStream instance.

Macros

_GET_STRING_STREAM_AVAILABLE_SPACE(sstream)

Calculates the number of memory blocks available in the data instance of the StringStream instance. The terminating \0 byte is not counted in available blocks.

Clone this wiki locally