1 /* values.h: constants and functions for document value handling.
2 *
3 * Copyright (C) 2006,2010 Olly Betts
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef OMEGA_INCLUDED_VALUES_H
21 #define OMEGA_INCLUDED_VALUES_H
22
23 #include <cstring>
24 #include <string>
25
26 // Include these to get uint32_t and htonl, etc.
27 #ifdef HAVE_WORKING_STDINT_H
28 # include <stdint.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
32 #endif
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36 #ifdef __WIN32__
37 # ifndef HAVE_WORKING_STDINT_H
38 typedef unsigned int uint32_t;
39 # endif
htonl(uint32_t v)40 inline uint32_t htonl(uint32_t v) {
41 return (v << 24) | ((v & 0xff00) << 8) | ((v >> 8) & 0xff00) | (v >> 24);
42 }
43 # define ntohl(V) htonl(V)
44 #endif
45
46 enum value_slot {
47 VALUE_LASTMOD = 0, // 4 byte big endian value - seconds since 1970.
48 VALUE_MD5 = 1, // 16 byte MD5 checksum of original document.
49 VALUE_SIZE = 2 // sortable_serialise(<file size in bytes>).
50 };
51
binary_string_to_int(const std::string & s)52 inline uint32_t binary_string_to_int(const std::string &s)
53 {
54 if (s.size() != 4) return (uint32_t)-1;
55 uint32_t v;
56 std::memcpy(&v, s.data(), 4);
57 return ntohl(v);
58 }
59
int_to_binary_string(uint32_t v)60 inline std::string int_to_binary_string(uint32_t v)
61 {
62 v = htonl(v);
63 return std::string(reinterpret_cast<const char*>(&v), 4);
64 }
65
66 #endif
67