00001 /* @file serialise.h 00002 * @brief functions to convert classes to strings and back 00003 * 00004 * Copyright (C) 2006,2007,2008,2009 Olly Betts 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #ifndef XAPIAN_INCLUDED_SERIALISE_H 00022 #define XAPIAN_INCLUDED_SERIALISE_H 00023 00024 #include <string> 00025 #include "noreturn.h" 00026 #include "xapian/visibility.h" 00027 #include "xapian/weight.h" 00028 00029 // Forward class declarations: 00030 00031 namespace Xapian { 00032 class Document; 00033 class Error; 00034 class MSet; 00035 class RSet; 00036 } 00037 00046 template<class T> 00047 std::string 00048 encode_length(T len) 00049 { 00050 std::string result; 00051 if (len < 255) { 00052 result += static_cast<unsigned char>(len); 00053 } else { 00054 result += '\xff'; 00055 len -= 255; 00056 while (true) { 00057 unsigned char b = static_cast<unsigned char>(len & 0x7f); 00058 len >>= 7; 00059 if (!len) { 00060 result += (b | static_cast<unsigned char>(0x80)); 00061 break; 00062 } 00063 result += b; 00064 } 00065 } 00066 return result; 00067 } 00068 00079 XAPIAN_VISIBILITY_DEFAULT 00080 size_t decode_length(const char ** p, const char *end, bool check_remaining); 00081 00088 XAPIAN_VISIBILITY_DEFAULT 00089 std::string serialise_error(const Xapian::Error &e); 00090 00103 XAPIAN_VISIBILITY_DEFAULT 00104 XAPIAN_NORETURN( 00105 void unserialise_error(const std::string &error_string, 00106 const std::string &prefix, 00107 const std::string &new_context)); 00108 00115 std::string serialise_stats(const Xapian::Weight::Internal &stats); 00116 00123 Xapian::Weight::Internal unserialise_stats(const std::string &s); 00124 00131 std::string serialise_mset(const Xapian::MSet &mset); 00132 00140 Xapian::MSet unserialise_mset(const char * p, const char * p_end); 00141 00148 std::string serialise_rset(const Xapian::RSet &omrset); 00149 00156 Xapian::RSet unserialise_rset(const std::string &s); 00157 00164 XAPIAN_VISIBILITY_DEFAULT 00165 std::string serialise_document(const Xapian::Document &doc); 00166 00173 XAPIAN_VISIBILITY_DEFAULT 00174 Xapian::Document unserialise_document(const std::string &s); 00175 00176 #endif