VladimirLavrenchuk icon

rapidjson static/dynamic memory with/without m64 flag

VladimirLavrenchuk | PRO | 11/02/17 12:51:12 PM UTC | 0 ⭐ | 392 👁️ | Never ⏰ | []
C++ |

1.13 KB

|

None

|

0 👍

/

0 👎

    // Read whole file into a buffer
    FILE* fp = fopen("test.json", "r");
    fseek(fp, 0, SEEK_END);
    size_t filesize = (size_t)ftell(fp);
    fseek(fp, 0, SEEK_SET);
    //char* buffer = (char*)malloc(filesize + 1);
    char buffer[1024 * 10];// = (char*)malloc(filesize + 1);
    size_t readLength = fread(buffer, 1, filesize, fp);
    buffer[readLength] = '\0';
    fclose(fp);
 
    // In situ parsing the buffer into d, buffer will also be modified
    typedef GenericDocument<UTF8<>, MemoryPoolAllocator<>, MemoryPoolAllocator<>> MemDocument;
    MemDocument d;
    d.ParseInsitu(buffer);
    
    const char* key = "id";
    Value::ConstMemberIterator itr = d.FindMember(key);
    
    if (!d.IsObject()){
        printf("Not object!");
        return nullptr;
    }
    
    if (itr != d.MemberEnd())
        std::cout << &itr->value << std::endl;
    else
        printf("NULL pointer!");
        
   
    // 3. Stringify the DOM
    StringBuffer buff;
    Writer<StringBuffer> writer(buff);
    d.Accept(writer);
 
    std::cout << buff.GetString() << std::endl;  
 
    // Query/manipulate the DOM here...
 
    //free(buffer);

Comments