Fix tg_termpos1 for 64-bit termpos
[xapian.git] / xapian-applications / omega / values.h
blob979b5d3397cb872c66ea772c63fafccd6fc0b35c
1 /** @file
2 * @brief constants and functions for document value handling.
3 */
4 /* Copyright (C) 2006,2010,2015,2019,2023 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 #ifndef OMEGA_INCLUDED_VALUES_H
22 #define OMEGA_INCLUDED_VALUES_H
24 #ifndef PACKAGE
25 # error config.h must be included first in each C++ source file
26 #endif
28 #include <cstring>
29 #include <string>
31 #include <cstdint>
33 enum value_slot {
34 VALUE_LASTMOD = 0, // 4 byte big endian value - seconds since 1970.
35 VALUE_MD5 = 1, // 16 byte MD5 checksum of original document.
36 VALUE_SIZE = 2, // sortable_serialise(<file size in bytes>).
37 VALUE_CTIME = 3, // Like VALUE_LASTMOD, but for last metadata change.
38 VALUE_CREATED = 4 // Like VALUE_LASTMOD, but for created timestamp.
41 #ifndef WORDS_BIGENDIAN
42 inline std::uint32_t swap_endianness_32(std::uint32_t v) {
43 # if HAVE_DECL___BUILTIN_BSWAP32
44 return __builtin_bswap32(v);
45 # elif HAVE_DECL__BYTESWAP_ULONG
46 return _byteswap_ulong(v);
47 # else
48 return (v << 24) | ((v & 0xff00) << 8) | ((v >> 8) & 0xff00) | (v >> 24);
49 # endif
51 #endif
53 inline std::uint32_t binary_string_to_int(const std::string &s)
55 if (s.size() != 4) return static_cast<std::uint32_t>(-1);
56 std::uint32_t v;
57 std::memcpy(&v, s.data(), 4);
58 #ifndef WORDS_BIGENDIAN
59 v = swap_endianness_32(v);
60 #endif
61 return v;
64 inline std::string int_to_binary_string(std::uint32_t v)
66 #ifndef WORDS_BIGENDIAN
67 v = swap_endianness_32(v);
68 #endif
69 return std::string(reinterpret_cast<const char*>(&v), 4);
72 #endif