aboutsummaryrefslogtreecommitdiffstats
path: root/library/c/cyson/cyson.cpp
blob: f8bff01a96caa8290d3e988215ca54b2da6dc6d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Export visible API

#include "cyson.h"

#include <library/cpp/yson_pull/yson.h>
#include <library/cpp/yson_pull/detail/reader.h>
#include <library/cpp/yson_pull/detail/writer.h>
#include <library/cpp/yson_pull/detail/input/stream.h>
#include <library/cpp/yson_pull/detail/input/stdio_file.h>
#include <library/cpp/yson_pull/detail/output/buffered.h>
#include <library/cpp/yson_pull/detail/output/stream.h>
#include <library/cpp/yson_pull/detail/output/stdio_file.h>

#include <util/generic/ptr.h>
#include <util/generic/strbuf.h>
#include <util/generic/string.h>

namespace {
    template <typename T>
    void safe_assign_string(TString& dest, T&& value) noexcept {
        try {
            dest = std::forward<T>(value);
        } catch (...) {
            // Suppress exception
        }
    }

} // anonymous namespace

struct yson_reader {
    NYsonPull::NDetail::gen_reader_impl<false> impl;
    TString error_message;

    yson_reader(NYsonPull::NInput::IStream* stream, NYsonPull::EStreamType mode)
        : impl(*stream, mode)
    {
        error_message.reserve(64);
    }

    yson_event_type safe_get_next_event() noexcept {
        try {
            auto& event = impl.next_event();
            return static_cast<yson_event_type>(event.Type());
        } catch (...) {
            safe_assign_string(error_message, CurrentExceptionMessage());
            return YSON_EVENT_ERROR;
        }
    }
};

struct yson_writer {
    THolder<NYsonPull::IConsumer> consumer;
    TString error_message;

    yson_writer(THolder<NYsonPull::IConsumer> consumer_)
        : consumer{std::move(consumer_)} {
        error_message.reserve(64);
    }

    template <typename T>
    yson_writer_result safe_write(T&& func) noexcept {
        try {
            func(*consumer);
            return YSON_WRITER_RESULT_OK;
        } catch (const NYsonPull::NException::TBadOutput& err) {
            safe_assign_string(error_message, err.what());
            return YSON_WRITER_RESULT_BAD_STREAM;
        } catch (...) {
            safe_assign_string(error_message, CurrentExceptionMessage());
            return YSON_WRITER_RESULT_ERROR;
        }
    }
};

namespace {
    class callback_error: public std::exception {
    public:
        const char* what() const noexcept override {
            return "User callback returned error result code";
        }
    };

    class c_yson_input_stream: public NYsonPull::NInput::IStream {
        void* ctx_;
        yson_input_stream_func callback_;

    public:
        c_yson_input_stream(void* ctx, yson_input_stream_func callback)
            : ctx_{ctx}
            , callback_{callback} {
        }

    protected:
        result do_fill_buffer() override {
            const char* ptr;
            size_t length;
            switch (callback_(ctx_, &ptr, &length)) {
                case YSON_INPUT_STREAM_RESULT_OK:
                    buffer().reset(
                        reinterpret_cast<const ui8*>(ptr),
                        reinterpret_cast<const ui8*>(ptr) + length);
                    return result::have_more_data;

                case YSON_INPUT_STREAM_RESULT_EOF:
                    return result::at_end;

                default:
                case YSON_INPUT_STREAM_RESULT_ERROR:
                    throw callback_error();
            }
        }
    };

    class c_yson_output_stream: public NYsonPull::NDetail::NOutput::TBuffered<c_yson_output_stream> {
        using base_type = NYsonPull::NDetail::NOutput::TBuffered<c_yson_output_stream>;

        void* ctx_;
        yson_output_stream_func callback_;

    public:
        c_yson_output_stream(void* ctx, yson_output_stream_func callback, size_t buffer_size)
            : base_type(buffer_size)
            , ctx_{ctx}
            , callback_{callback} {
        }

        void write(TStringBuf data) {
            switch (callback_(ctx_, data.data(), data.size())) {
                case YSON_OUTPUT_STREAM_RESULT_OK:
                    return;

                default:
                case YSON_OUTPUT_STREAM_RESULT_ERROR:
                    throw callback_error();
            }
        }
    };

    // Type marshalling

    const yson_string* to_yson_string(const NYsonPull::TScalar& value) {
        assert(value.Type() == NYsonPull::EScalarType::String);
        auto* result = &value.AsUnsafeValue().AsString;
        return reinterpret_cast<const yson_string*>(result);
    }

    yson_input_stream* to_yson_input_stream(NYsonPull::NInput::IStream* ptr) {
        return reinterpret_cast<yson_input_stream*>(ptr);
    }

    NYsonPull::NInput::IStream* from_yson_input_stream(yson_input_stream* ptr) {
        return reinterpret_cast<NYsonPull::NInput::IStream*>(ptr);
    }

    yson_output_stream* to_yson_output_stream(NYsonPull::NOutput::IStream* ptr) {
        return reinterpret_cast<yson_output_stream*>(ptr);
    }

    NYsonPull::NOutput::IStream* from_yson_output_stream(yson_output_stream* ptr) {
        return reinterpret_cast<NYsonPull::NOutput::IStream*>(ptr);
    }

    // Exception-handling new/delete wrappers

    template <typename T, typename... Args>
    T* safe_new(Args&&... args) noexcept {
        try {
            return new T(std::forward<Args>(args)...);
        } catch (...) {
            return nullptr;
        }
    }

    template <typename T>
    void safe_delete(T* ptr) noexcept {
        assert(ptr != nullptr);
        try {
            delete ptr;
        } catch (...) {
            // Suppress destructor exceptions
        }
    }

    template <typename T, typename... Args>
    yson_writer* safe_new_writer(yson_output_stream* stream, Args&&... args) noexcept {
        try {
            auto impl = MakeHolder<T>(
                *from_yson_output_stream(stream),
                std::forward<Args>(args)...);
            return new yson_writer(std::move(impl));
        } catch (...) {
            return nullptr;
        }
    }

} // anonymous namespace

extern "C" {
// Input stream

yson_input_stream* yson_input_stream_from_string(const char* ptr, size_t length) {
    auto buf = TStringBuf{ptr, length};
    auto* result = safe_new<NYsonPull::NDetail::NInput::TOwned<TMemoryInput>>(buf);
    return to_yson_input_stream(result);
}

yson_input_stream* yson_input_stream_from_file(FILE* file, size_t buffer_size) {
    auto* result = safe_new<NYsonPull::NDetail::NInput::TStdioFile>(file, buffer_size);
    return to_yson_input_stream(result);
}

yson_input_stream* yson_input_stream_from_fd(int fd, size_t buffer_size) {
    auto* result = safe_new<NYsonPull::NDetail::NInput::TFHandle>(fd, buffer_size);
    return to_yson_input_stream(result);
}

yson_input_stream* yson_input_stream_new(void* ctx, yson_input_stream_func callback) {
    auto* result = safe_new<c_yson_input_stream>(ctx, callback);
    return to_yson_input_stream(result);
}

void yson_input_stream_delete(yson_input_stream* stream) {
    assert(stream != nullptr);
    safe_delete(from_yson_input_stream(stream));
}

// Reader

yson_reader* yson_reader_new(yson_input_stream* stream, yson_stream_type mode) {
    assert(stream != nullptr);
    return safe_new<yson_reader>(
        from_yson_input_stream(stream),
        static_cast<NYsonPull::EStreamType>(mode));
}

void yson_reader_delete(yson_reader* reader) {
    assert(reader != nullptr);
    safe_delete(reader);
}

yson_event_type yson_reader_get_next_event(yson_reader* reader) {
    assert(reader != nullptr);
    return reader->safe_get_next_event();
}

const char* yson_reader_get_error_message(yson_reader* reader) {
    assert(reader != nullptr);
    return reader->error_message.c_str();
}

yson_scalar_type yson_reader_get_scalar_type(yson_reader* reader) {
    assert(reader != nullptr);
    auto& event = reader->impl.last_event();
    return static_cast<yson_scalar_type>(event.AsScalar().Type());
}

int yson_reader_get_boolean(yson_reader* reader) {
    assert(reader != nullptr);
    auto& event = reader->impl.last_event();
    return static_cast<int>(event.AsScalar().AsBoolean());
}

i64 yson_reader_get_int64(yson_reader* reader) {
    assert(reader != nullptr);
    auto& event = reader->impl.last_event();
    return event.AsScalar().AsInt64();
}

ui64 yson_reader_get_uint64(yson_reader* reader) {
    assert(reader != nullptr);
    auto& event = reader->impl.last_event();
    return event.AsScalar().AsUInt64();
}

double yson_reader_get_float64(yson_reader* reader) {
    assert(reader != nullptr);
    auto& event = reader->impl.last_event();
    return event.AsScalar().AsFloat64();
}

const yson_string* yson_reader_get_string(yson_reader* reader) {
    assert(reader != nullptr);
    return to_yson_string(reader->impl.last_event().AsScalar());
}

// Output stream

yson_output_stream* yson_output_stream_from_file(FILE* file, size_t buffer_size) {
    auto* result = safe_new<NYsonPull::NDetail::NOutput::TStdioFile>(file, buffer_size);
    return to_yson_output_stream(result);
}

yson_output_stream* yson_output_stream_from_fd(int fd, size_t buffer_size) {
    auto* result = safe_new<NYsonPull::NDetail::NOutput::TFHandle>(fd, buffer_size);
    return to_yson_output_stream(result);
}

yson_output_stream* yson_output_stream_new(void* ctx, yson_output_stream_func callback, size_t buffer_size) {
    auto* result = safe_new<c_yson_output_stream>(ctx, callback, buffer_size);
    return to_yson_output_stream(result);
}

void yson_output_stream_delete(yson_output_stream* stream) {
    assert(stream != nullptr);
    safe_delete(from_yson_output_stream(stream));
}

// Writer

yson_writer* yson_writer_new_binary(yson_output_stream* stream, yson_stream_type mode) {
    assert(stream != nullptr);
    return safe_new_writer<NYsonPull::NDetail::TBinaryWriterImpl>(
        stream,
        static_cast<NYsonPull::EStreamType>(mode));
}

yson_writer* yson_writer_new_text(yson_output_stream* stream, yson_stream_type mode) {
    assert(stream != nullptr);
    return safe_new_writer<NYsonPull::NDetail::TTextWriterImpl>(
        stream,
        static_cast<NYsonPull::EStreamType>(mode));
}

yson_writer* yson_writer_new_pretty_text(yson_output_stream* stream, yson_stream_type mode, size_t indent) {
    assert(stream != nullptr);
    return safe_new_writer<NYsonPull::NDetail::TPrettyWriterImpl>(
        stream,
        static_cast<NYsonPull::EStreamType>(mode),
        indent);
}

void yson_writer_delete(yson_writer* writer) {
    assert(writer != nullptr);
    safe_delete(writer);
}

const char* yson_writer_get_error_message(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->error_message.c_str();
}

yson_writer_result yson_writer_write_begin_stream(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnBeginStream();
    });
}

yson_writer_result yson_writer_write_end_stream(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnEndStream();
    });
}

yson_writer_result yson_writer_write_begin_list(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnBeginList();
    });
}

yson_writer_result yson_writer_write_end_list(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnEndList();
    });
}

yson_writer_result yson_writer_write_begin_map(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnBeginMap();
    });
}

yson_writer_result yson_writer_write_end_map(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnEndMap();
    });
}

yson_writer_result yson_writer_write_begin_attributes(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnBeginAttributes();
    });
}

yson_writer_result yson_writer_write_end_attributes(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnEndAttributes();
    });
}

yson_writer_result yson_writer_write_entity(yson_writer* writer) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnEntity();
    });
}

yson_writer_result yson_writer_write_key(yson_writer* writer, const char* ptr, size_t length) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnKey({ptr, length});
    });
}

yson_writer_result yson_writer_write_string(yson_writer* writer, const char* ptr, size_t length) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnScalarString({ptr, length});
    });
}

yson_writer_result yson_writer_write_int64(yson_writer* writer, i64 value) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnScalarInt64(value);
    });
}

yson_writer_result yson_writer_write_uint64(yson_writer* writer, ui64 value) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnScalarUInt64(value);
    });
}

yson_writer_result yson_writer_write_boolean(yson_writer* writer, int value) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnScalarBoolean(static_cast<bool>(value));
    });
}

yson_writer_result yson_writer_write_float64(yson_writer* writer, double value) {
    assert(writer != nullptr);
    return writer->safe_write([=](NYsonPull::IConsumer& consumer) {
        consumer.OnScalarFloat64(value);
    });
}

} // extern "C"