Merge branch 'master' of ssh://carlh.dyndns.org/home/carl/git/libdcp
[libdcp.git] / src / picture_asset_writer.cc
1 /*
2     Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <inttypes.h>
21 #include <stdint.h>
22 #include "AS_DCP.h"
23 #include "KM_fileio.h"
24 #include "picture_asset_writer.h"
25 #include "exceptions.h"
26 #include "picture_asset.h"
27
28 using std::istream;
29 using std::ostream;
30 using std::string;
31 using boost::shared_ptr;
32 using namespace libdcp;
33
34 FrameInfo::FrameInfo (istream& s)
35         : offset (0)
36         , size (0)
37 {
38         s >> offset >> size;
39
40         if (!s.good ()) {
41                 /* Make sure we zero these if something bad happened, otherwise
42                    the caller might try to alloc lots of RAM.
43                 */
44                 offset = size = 0;
45         }
46
47         s >> hash;
48 }
49
50 FrameInfo::FrameInfo (FILE* f)
51 {
52         fscanf (f, "%" PRId64, &offset);
53         fscanf (f, "%" PRId64, &size);
54
55         if (ferror (f)) {
56                 offset = size = 0;
57         }
58
59         char hash_buffer[128];
60         fscanf (f, "%s", hash_buffer);
61         hash = hash_buffer;
62 }
63
64 void
65 FrameInfo::write (ostream& s) const
66 {
67         s << offset << " " << size << " " << hash;
68 }
69
70 void
71 FrameInfo::write (FILE* f) const
72 {
73         fprintf (f, "%" PRId64 " %" PRId64 " %s", offset, size, hash.c_str ());
74 }
75
76
77 PictureAssetWriter::PictureAssetWriter (PictureAsset* asset, bool overwrite)
78         : _asset (asset)
79         , _frames_written (0)
80         , _started (false)
81         , _finalized (false)
82         , _overwrite (overwrite)
83 {
84
85 }