|
| 1 | +/* |
| 2 | + grive: an GPL program to sync a local directory with Google Drive |
| 3 | + Copyright (C) 2013 Wan Wai Ho |
| 4 | +
|
| 5 | + This program is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU General Public License |
| 7 | + as published by the Free Software Foundation version 2 |
| 8 | + of the License. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + MA 02110-1301, USA. |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "ValWriter.hh" |
| 22 | +#include "util/DataStream.hh" |
| 23 | + |
| 24 | +#include <yajl/yajl_gen.h> |
| 25 | + |
| 26 | +#include <cassert> |
| 27 | + |
| 28 | +namespace gr { |
| 29 | + |
| 30 | +struct ValWriter::Impl |
| 31 | +{ |
| 32 | + yajl_gen gen ; |
| 33 | + DataStream *out ; |
| 34 | +} ; |
| 35 | + |
| 36 | +ValWriter::ValWriter( DataStream *out ) : |
| 37 | + m_impl( new Impl ) |
| 38 | +{ |
| 39 | + assert( out != 0 ) ; |
| 40 | + |
| 41 | + m_impl->out = out ; |
| 42 | + m_impl->gen = yajl_gen_alloc(0) ; |
| 43 | + yajl_gen_config( m_impl->gen, yajl_gen_print_callback, &ValWriter::WriteCallback, this ) ; |
| 44 | +} |
| 45 | + |
| 46 | +ValWriter::~ValWriter() |
| 47 | +{ |
| 48 | + yajl_gen_free( m_impl->gen ) ; |
| 49 | +} |
| 50 | + |
| 51 | +void ValWriter::Visit( long long t ) |
| 52 | +{ |
| 53 | + yajl_gen_integer( m_impl->gen, t ) ; |
| 54 | +} |
| 55 | + |
| 56 | +void ValWriter::Visit( double t ) |
| 57 | +{ |
| 58 | + yajl_gen_double( m_impl->gen, t ) ; |
| 59 | +} |
| 60 | + |
| 61 | +void ValWriter::Visit( const std::string& t ) |
| 62 | +{ |
| 63 | + yajl_gen_string( m_impl->gen, |
| 64 | + reinterpret_cast<const unsigned char*>(t.c_str()), t.size() ) ; |
| 65 | +} |
| 66 | + |
| 67 | +void ValWriter::Visit( bool t ) |
| 68 | +{ |
| 69 | + yajl_gen_bool( m_impl->gen, t ) ; |
| 70 | +} |
| 71 | + |
| 72 | +void ValWriter::VisitNull() |
| 73 | +{ |
| 74 | + yajl_gen_null( m_impl->gen ) ; |
| 75 | +} |
| 76 | + |
| 77 | +void ValWriter::StartArray() |
| 78 | +{ |
| 79 | + yajl_gen_array_open( m_impl->gen ) ; |
| 80 | +} |
| 81 | + |
| 82 | +void ValWriter::EndArray() |
| 83 | +{ |
| 84 | + yajl_gen_array_close( m_impl->gen ) ; |
| 85 | +} |
| 86 | + |
| 87 | +void ValWriter::StartObject() |
| 88 | +{ |
| 89 | + yajl_gen_map_open( m_impl->gen ) ; |
| 90 | +} |
| 91 | + |
| 92 | +void ValWriter::VisitKey( const std::string& t ) |
| 93 | +{ |
| 94 | + Visit(t) ; |
| 95 | +} |
| 96 | + |
| 97 | +void ValWriter::EndObject() |
| 98 | +{ |
| 99 | + yajl_gen_map_close( m_impl->gen ) ; |
| 100 | +} |
| 101 | + |
| 102 | +void ValWriter::WriteCallback( void *ctx, const char *str, std::size_t size ) |
| 103 | +{ |
| 104 | + ValWriter *pthis = reinterpret_cast<ValWriter*>(ctx) ; |
| 105 | + assert( pthis != 0 ) ; |
| 106 | + assert( pthis->m_impl->out != 0 ) ; |
| 107 | + |
| 108 | + pthis->m_impl->out->Write( str, size ) ; |
| 109 | +} |
| 110 | + |
| 111 | +} // end of namespace |
0 commit comments