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) :
112 inline void begin() {
121 inline void beginDict() {
127 inline void endDict() {
130 inline void beginItem() {
133 inline void beginItem(const char * name) {
137 inline void beginItem(const std::string &name) {
138 beginItem(name.c_str());
141 inline void endItem(void) {
145 inline void beginList() {
152 inline void endList(void) {
156 inline void beginTuple() {
160 inline void endTuple(void) {
164 inline void writeString(const char *s, size_t length) {
171 os.put(SHORT_BINSTRING);
183 inline void writeString(const char *s) {
189 writeString(s, strlen(s));
192 inline void writeString(const std::string &s) {
193 writeString(s.c_str(), s.size());
196 inline void writeNone(void) {
200 inline void writeBool(bool b) {
201 os.put(b ? NEWTRUE : NEWFALSE);
204 inline void writeInt(uint8_t i) {
209 inline void writeInt(uint16_t i) {
211 writeInt((uint8_t)i);
218 inline void writeInt(int32_t i) {
219 if (0 <= i && i < 0x10000) {
220 writeInt((uint16_t)i);
227 inline void writeInt(uint32_t i) {
229 writeInt((int32_t)i);
235 inline void writeInt(long long i) {
236 if (-0x8000000 <= i && i < 0x8000000) {
237 writeInt((int32_t)i);
243 inline void writeInt(unsigned long long i) {
245 writeInt((int32_t)i);
251 inline void writeFloat(double f) {
257 assert(sizeof u.f == sizeof u.c);
271 inline void writeByteArray(const void *buf, size_t length) {
273 os << "__builtin__\nbytearray\n";
276 writeString(static_cast<const char *>(buf), length);
282 inline void putInt16(uint16_t i) {
287 inline void putInt32(uint32_t i) {
289 os.put((i >> 8) & 0xff);
290 os.put((i >> 16) & 0xff);
295 inline void writeLong(T l) {
303 // Same as l >> (8 * sizeof l), but without the warnings
304 T sign = l < 0 ? ~0 : 0;
310 } while (sl >>= 8 != sign);
312 // Add an extra byte if sign bit doesn't match
313 if (((l >> (8 * c - 1)) & 1) != ((l >> (8 * sizeof l - 1)) & 1)) {
318 for (unsigned i = 0; i < c; ++ i) {
325 #endif /* _Pickle_HPP_ */