Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / src / synfig / importer.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file importer.cpp
3 **      \brief writeme
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #define SYNFIG_NO_ANGLE
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "canvas.h"
36 #include "importer.h"
37 #include "surface.h"
38 #include <algorithm>
39 #include "string.h"
40 #include <map>
41 #include <ctype.h>
42 #include <functional>
43
44 #endif
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 using namespace etl;
51 using namespace std;
52 using namespace synfig;
53
54 Importer::Book* synfig::Importer::book_;
55
56 map<String,Importer::LooseHandle> *__open_importers;
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 bool
63 Importer::subsys_init()
64 {
65         book_=new Book();
66         __open_importers=new map<String,Importer::LooseHandle>();
67         return true;
68 }
69
70 bool
71 Importer::subsys_stop()
72 {
73         delete book_;
74         delete __open_importers;
75         return true;
76 }
77
78 Importer::Book&
79 Importer::book()
80 {
81         return *book_;
82 }
83
84 Importer::Handle
85 Importer::open(const String &filename)
86 {
87         if(filename.empty())
88         {
89                 synfig::error(_("Importer::open(): Cannot open empty filename"));
90                 return 0;
91         }
92
93         // If we already have an importer open under that filename,
94         // then use it instead.
95         if(__open_importers->count(filename))
96         {
97                 //synfig::info("Found importer already open, using it...");
98                 return (*__open_importers)[filename];
99         }
100
101         if(filename_extension(filename) == "")
102         {
103                 synfig::error(_("Importer::open(): Couldn't find extension"));
104                 return 0;
105         }
106
107         String ext(filename_extension(filename));
108         if (ext.size()) ext = ext.substr(1); // skip initial '.'
109         std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
110
111
112         if(!Importer::book().count(ext))
113         {
114                 synfig::error(_("Importer::open(): Unknown file type -- ")+ext);
115                 return 0;
116         }
117
118         try {
119                 Importer::Handle importer;
120                 importer=Importer::book()[ext](filename.c_str());
121                 (*__open_importers)[filename]=importer;
122                 return importer;
123         }
124         catch (String str)
125         {
126                 synfig::error(str);
127         }
128         return 0;
129 }
130
131 Importer::Importer():
132         gamma_(2.2)
133 {
134 }
135
136
137 Importer::~Importer()
138 {
139         // Remove ourselves from the open importer list
140         map<String,Importer::LooseHandle>::iterator iter;
141         for(iter=__open_importers->begin();iter!=__open_importers->end();++iter)
142                 if(iter->second==this)
143                 {
144                         __open_importers->erase(iter);
145                 }
146 }