comparison Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 35:81262707d68e

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 12 Jun 2021 09:35:17 +0200
parents 3bd0ec7c5a7c
children 1256194e1c08
comparison
equal deleted inserted replaced
34:ff52b3e49d22 35:81262707d68e
311 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); 311 return CheckHttp(OrthancPluginRestApiPut(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize));
312 } 312 }
313 } 313 }
314 314
315 315
316 static bool ReadJsonInternal(Json::Value& target,
317 const void* buffer,
318 size_t size,
319 bool collectComments)
320 {
321 #if JSONCPP_USE_DEPRECATED == 1
322 Json::Reader reader;
323 return reader.parse(reinterpret_cast<const char*>(buffer),
324 reinterpret_cast<const char*>(buffer) + size, target, collectComments);
325 #else
326 Json::CharReaderBuilder builder;
327 builder.settings_["collectComments"] = collectComments;
328
329 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
330 assert(reader.get() != NULL);
331
332 JSONCPP_STRING err;
333 if (reader->parse(reinterpret_cast<const char*>(buffer),
334 reinterpret_cast<const char*>(buffer) + size, &target, &err))
335 {
336 return true;
337 }
338 else
339 {
340 LogError("Cannot parse JSON: " + std::string(err));
341 return false;
342 }
343 #endif
344 }
345
346
316 bool ReadJson(Json::Value& target, 347 bool ReadJson(Json::Value& target,
317 const std::string& source) 348 const std::string& source)
318 { 349 {
319 return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size()); 350 return ReadJson(target, source.empty() ? NULL : source.c_str(), source.size());
320 } 351 }
322 353
323 bool ReadJson(Json::Value& target, 354 bool ReadJson(Json::Value& target,
324 const void* buffer, 355 const void* buffer,
325 size_t size) 356 size_t size)
326 { 357 {
327 #if JSONCPP_USE_DEPRECATED == 1 358 return ReadJsonInternal(target, buffer, size, true);
328 Json::Reader reader;
329 return reader.parse(reinterpret_cast<const char*>(buffer),
330 reinterpret_cast<const char*>(buffer) + size, target);
331 #else
332 Json::CharReaderBuilder builder;
333 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
334 assert(reader.get() != NULL);
335 JSONCPP_STRING err;
336 if (reader->parse(reinterpret_cast<const char*>(buffer),
337 reinterpret_cast<const char*>(buffer) + size, &target, &err))
338 {
339 return true;
340 }
341 else
342 {
343 LogError("Cannot parse JSON: " + err);
344 return false;
345 }
346 #endif
347 } 359 }
348 360
361
362 bool ReadJsonWithoutComments(Json::Value& target,
363 const std::string& source)
364 {
365 return ReadJsonWithoutComments(target, source.empty() ? NULL : source.c_str(), source.size());
366 }
367
368
369 bool ReadJsonWithoutComments(Json::Value& target,
370 const void* buffer,
371 size_t size)
372 {
373 return ReadJsonInternal(target, buffer, size, false);
374 }
375
349 376
350 void WriteFastJson(std::string& target, 377 void WriteFastJson(std::string& target,
351 const Json::Value& source) 378 const Json::Value& source)
352 { 379 {
353 #if JSONCPP_USE_DEPRECATED == 1 380 #if JSONCPP_USE_DEPRECATED == 1
2697 { 2724 {
2698 assert(*it != NULL); 2725 assert(*it != NULL);
2699 delete *it; 2726 delete *it;
2700 } 2727 }
2701 2728
2729 size_ = 0;
2702 content_.clear(); 2730 content_.clear();
2703 } 2731 }
2704 2732
2705 void Flatten(std::string& target) const 2733 /**
2734 * Since Orthanc 1.9.3, this function also clears the content of
2735 * the ChunkedBuffer in order to mimic the behavior of the
2736 * original class "Orthanc::ChunkedBuffer". This prevents the
2737 * forgetting of calling "Clear()" in order to reduce memory
2738 * consumption.
2739 **/
2740 void Flatten(std::string& target)
2706 { 2741 {
2707 target.resize(size_); 2742 target.resize(size_);
2708 2743
2709 size_t pos = 0; 2744 size_t pos = 0;
2710 2745
2716 if (s != 0) 2751 if (s != 0)
2717 { 2752 {
2718 memcpy(&target[pos], (*it)->c_str(), s); 2753 memcpy(&target[pos], (*it)->c_str(), s);
2719 pos += s; 2754 pos += s;
2720 } 2755 }
2756
2757 delete *it;
2721 } 2758 }
2722 2759
2723 assert(size_ == 0 || 2760 assert(pos == target.size());
2724 pos == target.size()); 2761
2762 size_ = 0;
2763 content_.clear();
2725 } 2764 }
2726 2765
2727 void AddChunk(const void* data, 2766 void AddChunk(const void* data,
2728 size_t size) 2767 size_t size)
2729 { 2768 {
2750 const HttpClient::HttpHeaders& GetHeaders() const 2789 const HttpClient::HttpHeaders& GetHeaders() const
2751 { 2790 {
2752 return headers_; 2791 return headers_;
2753 } 2792 }
2754 2793
2755 const ChunkedBuffer& GetBody() const 2794 ChunkedBuffer& GetBody()
2756 { 2795 {
2757 return body_; 2796 return body_;
2758 } 2797 }
2759 2798
2760 virtual void AddHeader(const std::string& key, 2799 virtual void AddHeader(const std::string& key,