Blender  V2.93
abc_reader_archive.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 Kévin Dietrich.
17  * All rights reserved.
18  */
19 
24 #include "abc_reader_archive.h"
25 
26 #include "BKE_main.h"
27 
28 #include "BLI_path_util.h"
29 #include "BLI_string.h"
30 
31 #ifdef WIN32
32 # include "utfconv.h"
33 #endif
34 
35 #include <fstream>
36 
37 using Alembic::Abc::ErrorHandler;
38 using Alembic::Abc::Exception;
39 using Alembic::Abc::IArchive;
40 using Alembic::Abc::kWrapExisting;
41 
42 namespace blender::io::alembic {
43 
44 static IArchive open_archive(const std::string &filename,
45  const std::vector<std::istream *> &input_streams)
46 {
47  try {
48  Alembic::AbcCoreOgawa::ReadArchive archive_reader(input_streams);
49 
50  return IArchive(archive_reader(filename), kWrapExisting, ErrorHandler::kThrowPolicy);
51  }
52  catch (const Exception &e) {
53  std::cerr << e.what() << '\n';
54 
55  /* Inspect the file to see whether it's actually a HDF5 file. */
56  char header[4]; /* char(0x89) + "HDF" */
57  std::ifstream the_file(filename.c_str(), std::ios::in | std::ios::binary);
58  if (!the_file) {
59  std::cerr << "Unable to open " << filename << std::endl;
60  }
61  else if (!the_file.read(header, sizeof(header))) {
62  std::cerr << "Unable to read from " << filename << std::endl;
63  }
64  else if (strncmp(header + 1, "HDF", 3) != 0) {
65  std::cerr << filename << " has an unknown file format, unable to read." << std::endl;
66  }
67  else {
68  std::cerr << filename << " is in the obsolete HDF5 format, unable to read." << std::endl;
69  }
70 
71  if (the_file.is_open()) {
72  the_file.close();
73  }
74  }
75 
76  return IArchive();
77 }
78 
79 ArchiveReader::ArchiveReader(struct Main *bmain, const char *filename)
80 {
81  char abs_filename[FILE_MAX];
82  BLI_strncpy(abs_filename, filename, FILE_MAX);
83  BLI_path_abs(abs_filename, BKE_main_blendfile_path(bmain));
84 
85 #ifdef WIN32
86  UTF16_ENCODE(abs_filename);
87  std::wstring wstr(abs_filename_16);
88  m_infile.open(wstr.c_str(), std::ios::in | std::ios::binary);
89  UTF16_UN_ENCODE(abs_filename);
90 #else
91  m_infile.open(abs_filename, std::ios::in | std::ios::binary);
92 #endif
93 
94  m_streams.push_back(&m_infile);
95 
96  m_archive = open_archive(abs_filename, m_streams);
97 }
98 
100 {
101  return m_archive.valid();
102 }
103 
104 Alembic::Abc::IObject ArchiveReader::getTop()
105 {
106  return m_archive.getTop();
107 }
108 
109 } // namespace blender::io::alembic
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
#define FILE_MAX
bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL()
Definition: path_util.c:1016
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ArchiveReader(struct Main *bmain, const char *filename)
static IArchive open_archive(const std::string &filename, const std::vector< std::istream * > &input_streams)
Definition: BKE_main.h:116
#define UTF16_ENCODE(in8str)
Definition: utfconv.h:96
#define UTF16_UN_ENCODE(in8str)
Definition: utfconv.h:100