cyson.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Export visible API
  2. #include "cyson.h"
  3. #include <library/cpp/yson_pull/yson.h>
  4. #include <library/cpp/yson_pull/detail/reader.h>
  5. #include <library/cpp/yson_pull/detail/writer.h>
  6. #include <library/cpp/yson_pull/detail/input/stream.h>
  7. #include <library/cpp/yson_pull/detail/input/stdio_file.h>
  8. #include <library/cpp/yson_pull/detail/output/buffered.h>
  9. #include <library/cpp/yson_pull/detail/output/stream.h>
  10. #include <library/cpp/yson_pull/detail/output/stdio_file.h>
  11. #include <util/generic/ptr.h>
  12. #include <util/generic/strbuf.h>
  13. #include <util/generic/string.h>
  14. namespace {
  15. template <typename T>
  16. void safe_assign_string(TString& dest, T&& value) noexcept {
  17. try {
  18. dest = std::forward<T>(value);
  19. } catch (...) {
  20. // Suppress exception
  21. }
  22. }
  23. } // anonymous namespace
  24. struct yson_reader {
  25. NYsonPull::NDetail::gen_reader_impl<false> impl;
  26. TString error_message;
  27. yson_reader(NYsonPull::NInput::IStream* stream, NYsonPull::EStreamType mode)
  28. : impl(*stream, mode)
  29. {
  30. error_message.reserve(64);
  31. }
  32. yson_event_type safe_get_next_event() noexcept {
  33. try {
  34. auto& event = impl.next_event();
  35. return static_cast<yson_event_type>(event.Type());
  36. } catch (...) {
  37. safe_assign_string(error_message, CurrentExceptionMessage());
  38. return YSON_EVENT_ERROR;
  39. }
  40. }
  41. };
  42. struct yson_writer {
  43. THolder<NYsonPull::IConsumer> consumer;
  44. TString error_message;
  45. yson_writer(THolder<NYsonPull::IConsumer> consumer_)
  46. : consumer{std::move(consumer_)} {
  47. error_message.reserve(64);
  48. }
  49. template <typename T>
  50. yson_writer_result safe_write(T&& func) noexcept {
  51. try {
  52. func(*consumer);
  53. return YSON_WRITER_RESULT_OK;
  54. } catch (const NYsonPull::NException::TBadOutput& err) {
  55. safe_assign_string(error_message, err.what());
  56. return YSON_WRITER_RESULT_BAD_STREAM;
  57. } catch (...) {
  58. safe_assign_string(error_message, CurrentExceptionMessage());
  59. return YSON_WRITER_RESULT_ERROR;
  60. }
  61. }
  62. };
  63. namespace {
  64. class callback_error: public std::exception {
  65. public:
  66. const char* what() const noexcept override {
  67. return "User callback returned error result code";
  68. }
  69. };
  70. class c_yson_input_stream: public NYsonPull::NInput::IStream {
  71. void* ctx_;
  72. yson_input_stream_func callback_;
  73. public:
  74. c_yson_input_stream(void* ctx, yson_input_stream_func callback)
  75. : ctx_{ctx}
  76. , callback_{callback} {
  77. }
  78. protected:
  79. result do_fill_buffer() override {
  80. const char* ptr;
  81. size_t length;
  82. switch (callback_(ctx_, &ptr, &length)) {
  83. case YSON_INPUT_STREAM_RESULT_OK:
  84. buffer().reset(
  85. reinterpret_cast<const ui8*>(ptr),
  86. reinterpret_cast<const ui8*>(ptr) + length);
  87. return result::have_more_data;
  88. case YSON_INPUT_STREAM_RESULT_EOF:
  89. return result::at_end;
  90. default:
  91. case YSON_INPUT_STREAM_RESULT_ERROR:
  92. throw callback_error();
  93. }
  94. }
  95. };
  96. class c_yson_output_stream: public NYsonPull::NDetail::NOutput::TBuffered<c_yson_output_stream> {
  97. using base_type = NYsonPull::NDetail::NOutput::TBuffered<c_yson_output_stream>;
  98. void* ctx_;
  99. yson_output_stream_func callback_;
  100. public:
  101. c_yson_output_stream(void* ctx, yson_output_stream_func callback, size_t buffer_size)
  102. : base_type(buffer_size)
  103. , ctx_{ctx}
  104. , callback_{callback} {
  105. }
  106. void write(TStringBuf data) {
  107. switch (callback_(ctx_, data.data(), data.size())) {
  108. case YSON_OUTPUT_STREAM_RESULT_OK:
  109. return;
  110. default:
  111. case YSON_OUTPUT_STREAM_RESULT_ERROR:
  112. throw callback_error();
  113. }
  114. }
  115. };
  116. // Type marshalling
  117. const yson_string* to_yson_string(const NYsonPull::TScalar& value) {
  118. assert(value.Type() == NYsonPull::EScalarType::String);
  119. auto* result = &value.AsUnsafeValue().AsString;
  120. return reinterpret_cast<const yson_string*>(result);
  121. }
  122. yson_input_stream* to_yson_input_stream(NYsonPull::NInput::IStream* ptr) {
  123. return reinterpret_cast<yson_input_stream*>(ptr);
  124. }
  125. NYsonPull::NInput::IStream* from_yson_input_stream(yson_input_stream* ptr) {
  126. return reinterpret_cast<NYsonPull::NInput::IStream*>(ptr);
  127. }
  128. yson_output_stream* to_yson_output_stream(NYsonPull::NOutput::IStream* ptr) {
  129. return reinterpret_cast<yson_output_stream*>(ptr);
  130. }
  131. NYsonPull::NOutput::IStream* from_yson_output_stream(yson_output_stream* ptr) {
  132. return reinterpret_cast<NYsonPull::NOutput::IStream*>(ptr);
  133. }
  134. // Exception-handling new/delete wrappers
  135. template <typename T, typename... Args>
  136. T* safe_new(Args&&... args) noexcept {
  137. try {
  138. return new T(std::forward<Args>(args)...);
  139. } catch (...) {
  140. return nullptr;
  141. }
  142. }
  143. template <typename T>
  144. void safe_delete(T* ptr) noexcept {
  145. assert(ptr != nullptr);
  146. try {
  147. delete ptr;
  148. } catch (...) {
  149. // Suppress destructor exceptions
  150. }
  151. }
  152. template <typename T, typename... Args>
  153. yson_writer* safe_new_writer(yson_output_stream* stream, Args&&... args) noexcept {
  154. try {
  155. auto impl = MakeHolder<T>(
  156. *from_yson_output_stream(stream),
  157. std::forward<Args>(args)...);
  158. return new yson_writer(std::move(impl));
  159. } catch (...) {
  160. return nullptr;
  161. }
  162. }
  163. } // anonymous namespace
  164. extern "C" {
  165. // Input stream
  166. yson_input_stream* yson_input_stream_from_string(const char* ptr, size_t length) {
  167. auto buf = TStringBuf{ptr, length};
  168. auto* result = safe_new<NYsonPull::NDetail::NInput::TOwned<TMemoryInput>>(buf);
  169. return to_yson_input_stream(result);
  170. }
  171. yson_input_stream* yson_input_stream_from_file(FILE* file, size_t buffer_size) {
  172. auto* result = safe_new<NYsonPull::NDetail::NInput::TStdioFile>(file, buffer_size);
  173. return to_yson_input_stream(result);
  174. }
  175. yson_input_stream* yson_input_stream_from_fd(int fd, size_t buffer_size) {
  176. auto* result = safe_new<NYsonPull::NDetail::NInput::TFHandle>(fd, buffer_size);
  177. return to_yson_input_stream(result);
  178. }
  179. yson_input_stream* yson_input_stream_new(void* ctx, yson_input_stream_func callback) {
  180. auto* result = safe_new<c_yson_input_stream>(ctx, callback);
  181. return to_yson_input_stream(result);
  182. }
  183. void yson_input_stream_delete(yson_input_stream* stream) {
  184. assert(stream != nullptr);
  185. safe_delete(from_yson_input_stream(stream));
  186. }
  187. // Reader
  188. yson_reader* yson_reader_new(yson_input_stream* stream, yson_stream_type mode) {
  189. assert(stream != nullptr);
  190. return safe_new<yson_reader>(
  191. from_yson_input_stream(stream),
  192. static_cast<NYsonPull::EStreamType>(mode));
  193. }
  194. void yson_reader_delete(yson_reader* reader) {
  195. assert(reader != nullptr);
  196. safe_delete(reader);
  197. }
  198. yson_event_type yson_reader_get_next_event(yson_reader* reader) {
  199. assert(reader != nullptr);
  200. return reader->safe_get_next_event();
  201. }
  202. const char* yson_reader_get_error_message(yson_reader* reader) {
  203. assert(reader != nullptr);
  204. return reader->error_message.c_str();
  205. }
  206. yson_scalar_type yson_reader_get_scalar_type(yson_reader* reader) {
  207. assert(reader != nullptr);
  208. auto& event = reader->impl.last_event();
  209. return static_cast<yson_scalar_type>(event.AsScalar().Type());
  210. }
  211. int yson_reader_get_boolean(yson_reader* reader) {
  212. assert(reader != nullptr);
  213. auto& event = reader->impl.last_event();
  214. return static_cast<int>(event.AsScalar().AsBoolean());
  215. }
  216. i64 yson_reader_get_int64(yson_reader* reader) {
  217. assert(reader != nullptr);
  218. auto& event = reader->impl.last_event();
  219. return event.AsScalar().AsInt64();
  220. }
  221. ui64 yson_reader_get_uint64(yson_reader* reader) {
  222. assert(reader != nullptr);
  223. auto& event = reader->impl.last_event();
  224. return event.AsScalar().AsUInt64();
  225. }
  226. double yson_reader_get_float64(yson_reader* reader) {
  227. assert(reader != nullptr);
  228. auto& event = reader->impl.last_event();
  229. return event.AsScalar().AsFloat64();
  230. }
  231. const yson_string* yson_reader_get_string(yson_reader* reader) {
  232. assert(reader != nullptr);
  233. return to_yson_string(reader->impl.last_event().AsScalar());
  234. }
  235. // Output stream
  236. yson_output_stream* yson_output_stream_from_file(FILE* file, size_t buffer_size) {
  237. auto* result = safe_new<NYsonPull::NDetail::NOutput::TStdioFile>(file, buffer_size);
  238. return to_yson_output_stream(result);
  239. }
  240. yson_output_stream* yson_output_stream_from_fd(int fd, size_t buffer_size) {
  241. auto* result = safe_new<NYsonPull::NDetail::NOutput::TFHandle>(fd, buffer_size);
  242. return to_yson_output_stream(result);
  243. }
  244. yson_output_stream* yson_output_stream_new(void* ctx, yson_output_stream_func callback, size_t buffer_size) {
  245. auto* result = safe_new<c_yson_output_stream>(ctx, callback, buffer_size);
  246. return to_yson_output_stream(result);
  247. }
  248. void yson_output_stream_delete(yson_output_stream* stream) {
  249. assert(stream != nullptr);
  250. safe_delete(from_yson_output_stream(stream));
  251. }
  252. // Writer
  253. yson_writer* yson_writer_new_binary(yson_output_stream* stream, yson_stream_type mode) {
  254. assert(stream != nullptr);
  255. return safe_new_writer<NYsonPull::NDetail::TBinaryWriterImpl>(
  256. stream,
  257. static_cast<NYsonPull::EStreamType>(mode));
  258. }
  259. yson_writer* yson_writer_new_text(yson_output_stream* stream, yson_stream_type mode) {
  260. assert(stream != nullptr);
  261. return safe_new_writer<NYsonPull::NDetail::TTextWriterImpl>(
  262. stream,
  263. static_cast<NYsonPull::EStreamType>(mode));
  264. }
  265. yson_writer* yson_writer_new_pretty_text(yson_output_stream* stream, yson_stream_type mode, size_t indent) {
  266. assert(stream != nullptr);
  267. return safe_new_writer<NYsonPull::NDetail::TPrettyWriterImpl>(
  268. stream,
  269. static_cast<NYsonPull::EStreamType>(mode),
  270. indent);
  271. }
  272. void yson_writer_delete(yson_writer* writer) {
  273. assert(writer != nullptr);
  274. safe_delete(writer);
  275. }
  276. const char* yson_writer_get_error_message(yson_writer* writer) {
  277. assert(writer != nullptr);
  278. return writer->error_message.c_str();
  279. }
  280. yson_writer_result yson_writer_write_begin_stream(yson_writer* writer) {
  281. assert(writer != nullptr);
  282. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  283. consumer.OnBeginStream();
  284. });
  285. }
  286. yson_writer_result yson_writer_write_end_stream(yson_writer* writer) {
  287. assert(writer != nullptr);
  288. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  289. consumer.OnEndStream();
  290. });
  291. }
  292. yson_writer_result yson_writer_write_begin_list(yson_writer* writer) {
  293. assert(writer != nullptr);
  294. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  295. consumer.OnBeginList();
  296. });
  297. }
  298. yson_writer_result yson_writer_write_end_list(yson_writer* writer) {
  299. assert(writer != nullptr);
  300. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  301. consumer.OnEndList();
  302. });
  303. }
  304. yson_writer_result yson_writer_write_begin_map(yson_writer* writer) {
  305. assert(writer != nullptr);
  306. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  307. consumer.OnBeginMap();
  308. });
  309. }
  310. yson_writer_result yson_writer_write_end_map(yson_writer* writer) {
  311. assert(writer != nullptr);
  312. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  313. consumer.OnEndMap();
  314. });
  315. }
  316. yson_writer_result yson_writer_write_begin_attributes(yson_writer* writer) {
  317. assert(writer != nullptr);
  318. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  319. consumer.OnBeginAttributes();
  320. });
  321. }
  322. yson_writer_result yson_writer_write_end_attributes(yson_writer* writer) {
  323. assert(writer != nullptr);
  324. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  325. consumer.OnEndAttributes();
  326. });
  327. }
  328. yson_writer_result yson_writer_write_entity(yson_writer* writer) {
  329. assert(writer != nullptr);
  330. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  331. consumer.OnEntity();
  332. });
  333. }
  334. yson_writer_result yson_writer_write_key(yson_writer* writer, const char* ptr, size_t length) {
  335. assert(writer != nullptr);
  336. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  337. consumer.OnKey({ptr, length});
  338. });
  339. }
  340. yson_writer_result yson_writer_write_string(yson_writer* writer, const char* ptr, size_t length) {
  341. assert(writer != nullptr);
  342. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  343. consumer.OnScalarString({ptr, length});
  344. });
  345. }
  346. yson_writer_result yson_writer_write_int64(yson_writer* writer, i64 value) {
  347. assert(writer != nullptr);
  348. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  349. consumer.OnScalarInt64(value);
  350. });
  351. }
  352. yson_writer_result yson_writer_write_uint64(yson_writer* writer, ui64 value) {
  353. assert(writer != nullptr);
  354. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  355. consumer.OnScalarUInt64(value);
  356. });
  357. }
  358. yson_writer_result yson_writer_write_boolean(yson_writer* writer, int value) {
  359. assert(writer != nullptr);
  360. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  361. consumer.OnScalarBoolean(static_cast<bool>(value));
  362. });
  363. }
  364. yson_writer_result yson_writer_write_float64(yson_writer* writer, double value) {
  365. assert(writer != nullptr);
  366. return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
  367. consumer.OnScalarFloat64(value);
  368. });
  369. }
  370. } // extern "C"