]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/endian/example/endian_example.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / endian / example / endian_example.cpp
CommitLineData
7c673cae
FG
1// endian_example.cpp -------------------------------------------------------//
2
3// Copyright Beman Dawes, 2006
4
5// Distributed under the Boost Software License, Version 1.0.
6// See http://www.boost.org/LICENSE_1_0.txt
7
8// See library home page at http://www.boost.org/libs/endian
9
10//----------------------------------------------------------------------------//
11
12#include <boost/endian/detail/disable_warnings.hpp>
13
14#include <iostream>
15#include <cstdio>
16#include <boost/endian/buffers.hpp>
17#include <boost/static_assert.hpp>
18
19using namespace boost::endian;
20
11fdf7f2 21namespace
7c673cae
FG
22{
23 // This is an extract from a very widely used GIS file format. Why the designer
24 // decided to mix big and little endians in the same file is not known. But
25 // this is a real-world format and users wishing to write low level code
26 // manipulating these files have to deal with the mixed endianness.
27
28 struct header
29 {
30 big_int32_buf_at file_code;
31 big_int32_buf_at file_length;
32 little_int32_buf_at version;
33 little_int32_buf_at shape_type;
34 };
35
36 const char* filename = "test.dat";
37}
38
39int main(int, char* [])
40{
41 header h;
11fdf7f2 42
7c673cae
FG
43 BOOST_STATIC_ASSERT(sizeof(h) == 16U); // reality check
44
45 h.file_code = 0x01020304;
46 h.file_length = sizeof(header);
47 h.version = 1;
48 h.shape_type = 0x01020304;
49
50 // Low-level I/O such as POSIX read/write or <cstdio> fread/fwrite is sometimes
51 // used for binary file operations when ultimate efficiency is important.
52 // Such I/O is often performed in some C++ wrapper class, but to drive home the
53 // point that endian integers are often used in fairly low-level code that
54 // does bulk I/O operations, <cstdio> fopen/fwrite is used for I/O in this example.
55
56 std::FILE* fi = std::fopen(filename, "wb"); // MUST BE BINARY
11fdf7f2 57
7c673cae
FG
58 if (!fi)
59 {
60 std::cout << "could not open " << filename << '\n';
61 return 1;
62 }
63
64 if (std::fwrite(&h, sizeof(header), 1, fi)!= 1)
65 {
66 std::cout << "write failure for " << filename << '\n';
67 return 1;
68 }
69
70 std::fclose(fi);
71
72 std::cout << "created file " << filename << '\n';
73
74 return 0;
75}