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
48 * Python pickle opcodes. See pickle.py and pickletools.py from Python
49 * standard library for details.
69 SHORT_BINSTRING = 'U',
109 PickleWriter(std::ostream &_os) :
113 inline void begin() {
122 inline void beginDict() {
128 inline void endDict() {
131 inline void beginItem() {
134 inline void beginItem(const char * name) {
138 inline void beginItem(const std::string &name) {
139 beginItem(name.c_str());
142 inline void endItem(void) {
146 inline void beginList() {
153 inline void endList(void) {
157 inline void beginTuple() {
161 inline void endTuple(void) {
165 inline void writeString(const char *s, size_t length) {
172 os.put(SHORT_BINSTRING);
184 inline void writeString(const char *s) {
190 writeString(s, strlen(s));
193 inline void writeString(const std::string &s) {
194 writeString(s.c_str(), s.size());
197 inline void writeNone(void) {
201 inline void writeBool(bool b) {
202 os.put(b ? NEWTRUE : NEWFALSE);
205 inline void writeInt(uint8_t i) {
210 inline void writeInt(uint16_t i) {
212 writeInt((uint8_t)i);
219 inline void writeInt(int32_t i) {
220 if (0 <= i && i < 0x10000) {
221 writeInt((uint16_t)i);
228 inline void writeInt(uint32_t i) {
230 writeInt((int32_t)i);
236 inline void writeInt(long long i) {
237 if (-0x8000000 <= i && i < 0x8000000) {
238 writeInt((int32_t)i);
244 inline void writeInt(unsigned long long i) {
246 writeInt((int32_t)i);
252 inline void writeFloat(double f) {
258 assert(sizeof u.f == sizeof u.c);
272 inline void writeByteArray(const void *buf, size_t length) {
274 os << "__builtin__\nbytearray\n";
277 writeString(static_cast<const char *>(buf), length);
283 inline void putInt16(uint16_t i) {
288 inline void putInt32(uint32_t i) {
290 os.put((i >> 8) & 0xff);
291 os.put((i >> 16) & 0xff);
296 inline void writeLong(T l) {
304 // Same as l >> (8 * sizeof l), but without the warnings
306 if (std::numeric_limits<T>::is_signed) {
307 sign = l < 0 ? ~0 : 0;
316 } while (sl >>= 8 != sign);
318 // Add an extra byte if sign bit doesn't match
319 if (((l >> (8 * c - 1)) & 1) != ((l >> (8 * sizeof l - 1)) & 1)) {
324 for (unsigned i = 0; i < c; ++ i) {
331 #endif /* _Pickle_HPP_ */