1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * Python pickle writer
47 * Python pickle opcodes. See pickle.py and pickletools.py from Python
48 * standard library for details.
68 SHORT_BINSTRING = 'U',
108 PickleWriter(std::ostream &_os) :
119 inline void beginDict() {
125 inline void endDict() {
128 inline void beginItem() {
131 inline void beginItem(const char * name) {
135 inline void beginItem(const std::string &name) {
136 beginItem(name.c_str());
139 inline void endItem(void) {
143 inline void beginList() {
150 inline void endList(void) {
154 inline void beginTuple() {
158 inline void endTuple(void) {
162 inline void writeString(const char *s, size_t length) {
169 os.put(SHORT_BINSTRING);
181 inline void writeString(const char *s) {
187 writeString(s, strlen(s));
190 inline void writeString(const std::string &s) {
191 writeString(s.c_str(), s.size());
194 inline void writeNone(void) {
198 inline void writeBool(bool b) {
199 os.put(b ? NEWTRUE : NEWFALSE);
202 inline void writeInt(uint8_t i) {
207 inline void writeInt(uint16_t i) {
209 writeInt((uint8_t)i);
216 inline void writeInt(int32_t i) {
217 if (0 <= i && i < 0x10000) {
218 writeInt((uint16_t)i);
225 inline void writeInt(uint32_t i) {
227 writeInt((int32_t)i);
233 inline void writeInt(long long i) {
234 if (-0x8000000 <= i && i < 0x8000000) {
235 writeInt((int32_t)i);
241 inline void writeInt(unsigned long long i) {
243 writeInt((int32_t)i);
249 inline void writeFloat(double f) {
255 assert(sizeof u.f == sizeof u.c);
270 inline void putInt16(uint16_t i) {
275 inline void putInt32(uint32_t i) {
277 os.put((i >> 8) & 0xff);
278 os.put((i >> 16) & 0xff);
283 inline void writeLong(T l) {
292 // Same as l >> (8 * sizeof l), but without the warnings
293 T sign = l < 0 ? ~0 : 0;
294 while ((l >> (8 * c)) != sign) {
297 // Add an extra byte if sign bit doesn't match
298 if (((l >> (8 * c - 1)) & 1) != ((l >> (8 * sizeof l - 1)) & 1)) {
303 for (unsigned i = 0; i < c; ++ i) {
310 #endif /* _Pickle_HPP_ */