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