libopenraw
teststream.cpp
1 /*
2  * libopenraw - teststream.cpp
3  *
4  * Copyright (C) 2006-2016 Hubert Figuiere
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
23 #include <string.h>
24 
25 #include <string>
26 #include <iostream>
27 #include <cstdlib>
28 
29 #include <boost/test/minimal.hpp>
30 
31 #include "stream.hpp"
32 #include "file.hpp"
33 #include "streamclone.hpp"
34 
35 using namespace OpenRaw;
36 
37 std::string g_testfile;
38 
39 int test_main (int argc, char * argv[])
40 {
41  if (argc == 1) {
42  // no argument, lets run like we are in "check"
43  const char * srcdir = getenv("srcdir");
44 
45  BOOST_ASSERT(srcdir != NULL);
46  g_testfile = std::string(srcdir);
47  g_testfile += "/io/testfile.tmp";
48  }
49  else {
50  g_testfile = argv[1];
51  }
52  auto file = IO::Stream::Ptr(new IO::File(g_testfile.c_str()));
53  char buf1[128];
54  int ret = file->open();
55  BOOST_CHECK(ret == 0);
56 
57  size_t r = file->read(buf1, 6);
58  BOOST_CHECK(r == 6);
59 
60  BOOST_CHECK(memcmp(buf1, "abcdef", 6) == 0);
61 
62  off_t file_size = file->filesize();
63  BOOST_CHECK(file_size == 63);
64 
65  const off_t clone_offset = 2;
66 
67  auto clone = new IO::StreamClone(file, clone_offset);
68 
69  ret = clone->open();
70  BOOST_CHECK(ret == 0);
71 
72  BOOST_CHECK(clone->filesize() == (file_size - clone_offset));
73 
74  char buf2[128];
75  r = clone->read(buf2, 4);
76  BOOST_CHECK(r == 4);
77 
78  BOOST_CHECK(strncmp(buf1 + clone_offset, buf2, 4) == 0);
79 
80  uint8_t c = file->readByte();
81 
82  BOOST_CHECK(c == 'g');
83 
84  // seek
85 
86  int new_pos = clone->seek(0, SEEK_CUR);
87  BOOST_CHECK(new_pos == 5);
88 
89  new_pos = clone->seek(1, SEEK_CUR);
90  BOOST_CHECK(new_pos == 6);
91 
92  new_pos = clone->seek(2, SEEK_SET);
93  BOOST_CHECK(new_pos == 2);
94 
95  c = file->readByte();
96  BOOST_CHECK(c == 'e');
97 
98  new_pos = clone->seek(0, SEEK_CUR);
99  BOOST_CHECK(new_pos == 3);
100 
101  c = file->readByte();
102  BOOST_CHECK(c == 'f');
103 
104  new_pos = clone->seek(-2, SEEK_END);
105  BOOST_CHECK(new_pos == 59);
106 
107  c = file->readByte();
108  BOOST_CHECK(c == 'Z');
109 
110 
111  clone->close();
112  delete clone;
113  file->close();
114  return 0;
115 }
116 
117 /*
118  Local Variables:
119  mode:c++
120  c-file-style:"stroustrup"
121  c-file-offsets:((innamespace . 0))
122  indent-tabs-mode:nil
123  fill-column:80
124  End:
125 */
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:30
cloned stream. Allow reading from a different offset
Definition: streamclone.hpp:36