File: mmap-test.cpp

package info (click to toggle)
exiv2 0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,080 kB
  • ctags: 6,382
  • sloc: cpp: 56,011; sh: 10,092; ansic: 1,622; makefile: 594; awk: 92; python: 36; sed: 16
file content (43 lines) | stat: -rw-r--r-- 1,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// ***************************************************************** -*- C++ -*-
// mmap-test.cpp, $Rev: 1967 $
// Simple mmap tests

#include <exiv2/basicio.hpp>
#include <exiv2/error.hpp>
#include <exiv2/futils.hpp>

#include <iostream>
#include <cstring>

using namespace Exiv2;

int main(int argc, char* const argv[])
try {
    if (argc != 2) {
        std::cout << "Usage: " << argv[0] << " file\n";
        return 1;
    }
    const char* path = argv[1];

    FileIo file(path);
    // Open the file in read mode
    if (file.open("rb") != 0) {
        throw Error(10, path, "rb", strError());
    }
    // Map it to memory
    const Exiv2::byte* pData = file.mmap();
    long size = file.size();
    DataBuf buf(size);
    // Read from the memory mapped region
    memcpy(buf.pData_, pData, buf.size_);
    // Reopen file in write mode and write to it
    file.write(buf.pData_, buf.size_);
    // Read from the mapped region again
    memcpy(buf.pData_, pData, buf.size_);
    file.close();

    return 0;
}
catch (const AnyError& e) {
    std::cout << e << "\n";
}