[honey] Fix portability to systems without pread()
[xapian.git] / xapian-core / net / serialise-error.cc
blob61df8d1961bdebcdcc31cf4a4751a074a368b3fd
1 /** @file serialise-error.cc
2 * @brief functions to convert Xapian::Error objects to strings and back
3 */
4 /* Copyright (C) 2006,2007,2008,2009,2010,2011,2014,2015,2016 Olly Betts
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
23 #include <xapian/error.h>
25 #include "length.h"
26 #include "serialise-error.h"
28 #include <string>
30 using namespace std;
32 string
33 serialise_error(const Xapian::Error &e)
35 // The byte before the type name is the type code.
36 string result(1, (e.get_type())[-1]);
37 result += encode_length(e.get_context().length());
38 result += e.get_context();
39 result += encode_length(e.get_msg().length());
40 result += e.get_msg();
41 // The "error string" goes last so we don't need to store its length.
42 const char * err = e.get_error_string();
43 if (err) result += err;
44 return result;
47 void
48 unserialise_error(const string &serialised_error, const string &prefix,
49 const string &new_context)
51 // Use c_str() so last string is nul-terminated.
52 const char * p = serialised_error.c_str();
53 const char * end = p + serialised_error.size();
54 if (p != end) {
55 char type = *p++;
57 size_t len;
58 decode_length_and_check(&p, end, len);
59 string context(p, len);
60 p += len;
62 decode_length_and_check(&p, end, len);
63 string msg(prefix);
64 msg.append(p, len);
65 p += len;
67 const char * error_string = (p == end) ? NULL : p;
69 if (!new_context.empty()) {
70 if (!context.empty()) {
71 msg += "; context was: ";
72 msg += context;
74 context = new_context;
77 switch (type) {
78 #include "xapian/errordispatch.h"
82 throw Xapian::InternalError("Unknown remote exception type", new_context);