libopenraw  0.3.7
rawfilefactory.cpp
1 /*
2  * libopenraw - rawfilefactory.cpp
3  *
4  * Copyright (C) 2006-2023 Hubert Figuière
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 
21 #include <stddef.h>
22 #include <stdlib.h>
23 
24 #include <cassert>
25 #include <utility>
26 
27 #include <libopenraw/debug.h>
28 
29 #include "rawfile.hpp"
30 #include "rawfilefactory.hpp"
31 #include "trace.hpp"
32 
33 using namespace Debug;
34 
35 namespace OpenRaw {
36 
37 namespace Internals {
38 
42 RawFileFactory::Table &RawFileFactory::table_mut()
43 {
45  static Table rawFactoryTable;
46  return rawFactoryTable;
47 }
48 
49 RawFileFactory::Extensions &RawFileFactory::extensions_mut()
50 {
52  static Extensions rawExtensionsTable;
53  return rawExtensionsTable;
54 }
55 
56 void RawFileFactory::registerType(RawFile::Type type,
57  const RawFileFactory::raw_file_factory_t &fn,
58  const char *ext)
59 {
60  if (fn == nullptr) {
61  LOGERR("NULL fn for registerFactory()\n");
62  assert(fn == nullptr);
63  }
64  table_mut()[type] = fn;
65  extensions_mut()[ext] = type;
66 }
67 
68 void RawFileFactory::unRegisterType(RawFile::Type type)
69 {
70  Table::iterator iter = table_mut().find(type);
71  if (iter == table().end()) {
72  LOGERR("attempting to unregisterFactory() in unregistered element\n");
73  assert(true);
74  }
75  table_mut().erase(iter);
76 }
77 
78 const char **RawFileFactory::fileExtensions()
79 {
80  static const char **_fileExtensions = NULL;
81  if (!_fileExtensions) {
82  const auto& ext = extensions();
83  size_t s = ext.size();
84  _fileExtensions = (const char **)calloc((s + 1), sizeof(char *));
85  const char **current = _fileExtensions;
86  Extensions::const_iterator iter(ext.begin());
87  for (; iter != ext.end(); ++iter) {
88  *current = iter->first.c_str();
89  current++;
90  }
91  }
92 
93  return _fileExtensions;
94 }
95 }
96 }
or_rawfile_type
Types of RAW files.
Definition: consts.h:59
Global namespace for libopenraw.
Definition: arwfile.cpp:29