blob: da21d33d40c5a3b0de3ba9248cca9fa4dc616f74 [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 Reissa4d7eef2008-06-11 01:18:20 +00008#include <cstring>
David Reissa1771092008-04-11 22:36:31 +00009#include <boost/lexical_cast.hpp>
Mark Sleef9831082007-02-20 20:59:21 +000010#include <protocol/TProtocol.h>
11
12namespace facebook { namespace thrift {
13
boz6ded7752007-06-05 22:41:18 +000014TOutput GlobalOutput;
15
David Reiss9b209552008-04-08 06:26:05 +000016std::string TOutput::strerror_s(int errno_copy) {
17#ifndef HAVE_STRERROR_R
David Reissa1771092008-04-11 22:36:31 +000018 return "errno = " + boost::lexical_cast<string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +000019#else // HAVE_STRERROR_R
20
21 char b_errbuf[1024] = { '\0' };
22#ifdef STRERROR_R_CHAR_P
23 char *b_error = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
24#else
25 char *b_error = b_errbuf;
26 int rv = strerror_r(errno_copy, b_errbuf, sizeof(b_errbuf));
27 if (rv == -1) {
28 // strerror_r failed. omgwtfbbq.
29 return "XSI-compliant strerror_r() failed with errno = " +
David Reissa1771092008-04-11 22:36:31 +000030 boost::lexical_cast<std::string>(errno_copy);
David Reiss9b209552008-04-08 06:26:05 +000031 }
32#endif
33 // Can anyone prove that explicit cast is probably not necessary
34 // to ensure that the string object is constructed before
35 // b_error becomes invalid?
36 return std::string(b_error);
37
38#endif // HAVE_STRERROR_R
39}
40
Mark Sleef9831082007-02-20 20:59:21 +000041uint32_t TApplicationException::read(facebook::thrift::protocol::TProtocol* iprot) {
42 uint32_t xfer = 0;
43 std::string fname;
44 facebook::thrift::protocol::TType ftype;
45 int16_t fid;
46
47 xfer += iprot->readStructBegin(fname);
48
49 while (true) {
50 xfer += iprot->readFieldBegin(fname, ftype, fid);
David Reiss0c90f6f2008-02-06 22:18:40 +000051 if (ftype == facebook::thrift::protocol::T_STOP) {
Mark Sleef9831082007-02-20 20:59:21 +000052 break;
53 }
54 switch (fid) {
55 case 1:
56 if (ftype == facebook::thrift::protocol::T_STRING) {
57 xfer += iprot->readString(message_);
58 } else {
59 xfer += iprot->skip(ftype);
60 }
61 break;
62 case 2:
63 if (ftype == facebook::thrift::protocol::T_I32) {
64 int32_t type;
65 xfer += iprot->readI32(type);
66 type_ = (TApplicationExceptionType)type;
67 } else {
68 xfer += iprot->skip(ftype);
69 }
70 break;
71 default:
72 xfer += iprot->skip(ftype);
73 break;
74 }
75 xfer += iprot->readFieldEnd();
76 }
77
78 xfer += iprot->readStructEnd();
79 return xfer;
80}
81
82uint32_t TApplicationException::write(facebook::thrift::protocol::TProtocol* oprot) const {
83 uint32_t xfer = 0;
84 xfer += oprot->writeStructBegin("TApplicationException");
85 xfer += oprot->writeFieldBegin("message", facebook::thrift::protocol::T_STRING, 1);
86 xfer += oprot->writeString(message_);
87 xfer += oprot->writeFieldEnd();
88 xfer += oprot->writeFieldBegin("type", facebook::thrift::protocol::T_I32, 2);
89 xfer += oprot->writeI32(type_);
90 xfer += oprot->writeFieldEnd();
91 xfer += oprot->writeFieldStop();
92 xfer += oprot->writeStructEnd();
93 return xfer;
94}
95
96}} // facebook::thrift