blob: 9ccea08c6a4c77d59a9d76f64af634e766170fdf [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef9831082007-02-20 20:59:21 +00007#include <Thrift.h>
David Reissa1771092008-04-11 22:36:31 +00008#include <boost/lexical_cast.hpp>
Mark Sleef9831082007-02-20 20:59:21 +00009#include <protocol/TProtocol.h>
10
11namespace facebook { namespace thrift {
12
boz6ded7752007-06-05 22:41:18 +000013TOutput GlobalOutput;
14
David Reiss9b209552008-04-08 06:26:05 +000015std::string TOutput::strerror_s(int errno_copy) {
16#ifndef HAVE_STRERROR_R
David Reissa1771092008-04-11 22:36:31 +000017 return "errno = " + boost::lexical_cast<string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +000018#else // HAVE_STRERROR_R
19
20 char b_errbuf[1024] = { '\0' };
21#ifdef STRERROR_R_CHAR_P
22 char *b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
23#else
24 char *b_error = b_errbuf;
25 int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
26 if (rv == -1) {
27 // strerror_r failed. omgwtfbbq.
28 return "XSI-compliant strerror_r() failed with errno = " +
David Reissa1771092008-04-11 22:36:31 +000029 boost::lexical_cast<std::string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +000030 }
31#endif
32 // Can anyone prove that explicit cast is probably not necessary
33 // to ensure that the string object is constructed before
34 // b_error becomes invalid?
35 return std::string(b_error);
36
37#endif // HAVE_STRERROR_R
38}
39
Mark Sleef9831082007-02-20 20:59:21 +000040uint32_t TApplicationException::read(facebook::thrift::protocol::TProtocol* iprot) {
41 uint32_t xfer = 0;
42 std::string fname;
43 facebook::thrift::protocol::TType ftype;
44 int16_t fid;
45
46 xfer += iprot->readStructBegin(fname);
47
48 while (true) {
49 xfer += iprot->readFieldBegin(fname, ftype, fid);
David Reiss0c90f6f2008-02-06 22:18:40 +000050 if (ftype == facebook::thrift::protocol::T_STOP) {
Mark Sleef9831082007-02-20 20:59:21 +000051 break;
52 }
53 switch (fid) {
54 case 1:
55 if (ftype == facebook::thrift::protocol::T_STRING) {
56 xfer += iprot->readString(message_);
57 } else {
58 xfer += iprot->skip(ftype);
59 }
60 break;
61 case 2:
62 if (ftype == facebook::thrift::protocol::T_I32) {
63 int32_t type;
64 xfer += iprot->readI32(type);
65 type_ = (TApplicationExceptionType)type;
66 } else {
67 xfer += iprot->skip(ftype);
68 }
69 break;
70 default:
71 xfer += iprot->skip(ftype);
72 break;
73 }
74 xfer += iprot->readFieldEnd();
75 }
76
77 xfer += iprot->readStructEnd();
78 return xfer;
79}
80
81uint32_t TApplicationException::write(facebook::thrift::protocol::TProtocol* oprot) const {
82 uint32_t xfer = 0;
83 xfer += oprot->writeStructBegin("TApplicationException");
84 xfer += oprot->writeFieldBegin("message", facebook::thrift::protocol::T_STRING, 1);
85 xfer += oprot->writeString(message_);
86 xfer += oprot->writeFieldEnd();
87 xfer += oprot->writeFieldBegin("type", facebook::thrift::protocol::T_I32, 2);
88 xfer += oprot->writeI32(type_);
89 xfer += oprot->writeFieldEnd();
90 xfer += oprot->writeFieldStop();
91 xfer += oprot->writeStructEnd();
92 return xfer;
93}
94
95}} // facebook::thrift