~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to arts/runtime/artsbuilderloader_impl.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    /*
 
2
 
 
3
    Copyright (C) 2001 Stefan Westerfeld
 
4
                       stefan@space.twc.de
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of 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
    Library General Public License for more details.
 
15
   
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
    Boston, MA 02111-1307, USA.
 
20
 
 
21
    */
 
22
 
 
23
#include "artsbuilder.h"
 
24
#include "debug.h"
 
25
#include <fstream.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <stdio.h>
 
29
#include <unistd.h>
 
30
#include <dirent.h>
 
31
#include <set>
 
32
 
 
33
 
 
34
using namespace Arts;
 
35
using namespace std;
 
36
 
 
37
namespace Arts {
 
38
 
 
39
class ArtsBuilderLoader_impl : virtual public ArtsBuilderLoader_skel {
 
40
protected:
 
41
        set<string> sourceDirs;
 
42
 
 
43
        string lastDataVersion;
 
44
        vector<TraderEntry> _traderEntries;
 
45
        vector<ModuleDef> _modules;
 
46
 
 
47
public:
 
48
        Object loadObject(Arts::TraderOffer offer)
 
49
        {
 
50
                StructureDesc structureDesc;
 
51
 
 
52
                vector<string> strseq;
 
53
 
 
54
                // load file
 
55
                vector<string> *filenames = offer.getProperty("File");
 
56
                if(filenames->size() == 1)
 
57
                {
 
58
                        string& filename = filenames->front();
 
59
                        arts_info("ArtsBuilderLoader: filename = %s", filename.c_str());
 
60
 
 
61
                        ifstream infile(filename.c_str());
 
62
                        string line;
 
63
                        while(getline(infile,line)) strseq.push_back(line);
 
64
                }
 
65
                delete filenames;
 
66
 
 
67
                structureDesc.loadFromList(strseq);
 
68
                if(structureDesc.name() != offer.interfaceName())
 
69
                {
 
70
                        arts_warning("failed (name = %s).",structureDesc.name().c_str());
 
71
                        return Object::null();
 
72
                }
 
73
 
 
74
                StructureBuilder builder;
 
75
                builder.addFactory(LocalFactory());
 
76
 
 
77
                return builder.createObject(structureDesc);
 
78
        }
 
79
 
 
80
        vector<string> *listFiles(const string& pathname, const char *extension)
 
81
        {
 
82
                vector<string> *result = new vector<string>();
 
83
 
 
84
                unsigned long extlen = strlen(extension);
 
85
                DIR *dir = opendir(pathname.c_str());
 
86
                if(dir != 0)
 
87
                {
 
88
                        struct dirent *de;
 
89
                        while((de = readdir(dir)) != 0)
 
90
                        {
 
91
                                if(strlen(de->d_name) > extlen &&
 
92
                                                strncmp(&de->d_name[strlen(de->d_name)-extlen],
 
93
                                                        extension,extlen) == 0)
 
94
                                        result->push_back(de->d_name);
 
95
                        }
 
96
                        closedir(dir);
 
97
                }
 
98
                return result;
 
99
        }
 
100
 
 
101
        void scanArtsFile(const string& filename)
 
102
        {
 
103
                StructureDesc structureDesc;
 
104
                vector<string> strseq;
 
105
 
 
106
                // load file
 
107
                {
 
108
                        ifstream infile(filename.c_str());
 
109
                        string line;
 
110
                        int inmodule = 0;
 
111
 
 
112
                        while(getline(infile,line))
 
113
                        {
 
114
                                /*
 
115
                                 * TODO - maybe there is a cleaner way?
 
116
                                 *
 
117
                                 * the following six lines are a bit hackish code to skip
 
118
                                 * the module sections of the structures
 
119
                                 *
 
120
                                 * the problem with the module sections is this:
 
121
                                 * we can't be sure that every module is known to the type
 
122
                                 * system before we registered them with the type system,
 
123
                                 * but as this code should be able to initially register .arts
 
124
                                 * files with the type system, we can't rely that it has been
 
125
                                 * done already (if we could, what would be the point of
 
126
                                 * running this?)
 
127
                                 */
 
128
                                if(strncmp(line.c_str(), "module=", 7) == 0)
 
129
                                        inmodule = 1;
 
130
 
 
131
                                if(strncmp(line.c_str(), "{", 1) == 0 && inmodule == 1)
 
132
                                        inmodule = 2;
 
133
 
 
134
                                if(strncmp(line.c_str(), "}", 1) == 0 && inmodule == 2)
 
135
                                        inmodule = 0;
 
136
 
 
137
                                if(inmodule == 0)
 
138
                                        strseq.push_back(line);
 
139
                        }
 
140
                }
 
141
 
 
142
                structureDesc.loadFromList(strseq);
 
143
                string name = structureDesc.name();
 
144
 
 
145
 
 
146
                arts_debug("%s [%s]\n",filename.c_str(),name.c_str());
 
147
 
 
148
                /* add to _modules */
 
149
                StructureBuilder builder;
 
150
                ModuleDef md = builder.createTypeInfo(structureDesc);
 
151
                _modules.push_back(md);
 
152
 
 
153
                arts_assert(md.moduleName == name);
 
154
 
 
155
                /* add to _traderEntries */
 
156
                
 
157
                TraderEntry entry;
 
158
                entry.interfaceName = name;
 
159
                entry.lines.push_back("Buildable=true");
 
160
                entry.lines.push_back("Interface="+name+",Arts::SynthModule,Arts::Object");
 
161
                entry.lines.push_back("Language=aRts");
 
162
                entry.lines.push_back("File="+filename);
 
163
 
 
164
                _traderEntries.push_back(entry);
 
165
                /*
 
166
                 * TODO: more entries like
 
167
                 * Author="Stefan Westerfeld <stefan@space.twc.de>"
 
168
                 * URL="http://www.arts-project.org"
 
169
                 * License=...
 
170
                 */
 
171
        }
 
172
 
 
173
        void rescan()
 
174
        {
 
175
                lastDataVersion = dataVersion();
 
176
                
 
177
                _traderEntries.clear();
 
178
                _modules.clear();
 
179
 
 
180
                set<string>::iterator si;
 
181
                for(si = sourceDirs.begin(); si != sourceDirs.end(); si++)
 
182
                {
 
183
                        vector<string> *files = listFiles(*si, ".arts");
 
184
                        vector<string>::iterator i;
 
185
                        for(i = files->begin(); i != files->end(); i++)
 
186
                                scanArtsFile(*si + "/" +*i);
 
187
                        delete files;
 
188
                }
 
189
        }
 
190
 
 
191
        string dataVersion()
 
192
        {
 
193
                /*
 
194
                 * change this string if you change the loading algorithm to force
 
195
                 * rescanning even with the same data
 
196
                 */
 
197
                string result = "ArtsBuilderLoader:1.0:";
 
198
 
 
199
                bool first = true;
 
200
 
 
201
                set<string>::iterator i;
 
202
                for(i = sourceDirs.begin(); i != sourceDirs.end(); i++)
 
203
                {
 
204
                        const string& filename = *i;
 
205
 
 
206
                        struct stat st;
 
207
                        stat(filename.c_str(), &st);
 
208
                
 
209
                        if(!first) result += ",";
 
210
                        first = false;
 
211
 
 
212
                        char mtime[32];
 
213
                        sprintf(mtime,"[%ld]",st.st_mtime);
 
214
                        result += filename + mtime;
 
215
                }
 
216
                return result;
 
217
        }
 
218
 
 
219
        vector<TraderEntry> *traderEntries()
 
220
        {
 
221
                if(dataVersion() != lastDataVersion)
 
222
                        rescan();
 
223
 
 
224
                return new vector<TraderEntry>(_traderEntries);
 
225
        }
 
226
 
 
227
        vector<ModuleDef> *modules()
 
228
        {
 
229
                if(dataVersion() != lastDataVersion)
 
230
                        rescan();
 
231
 
 
232
                return new vector<ModuleDef>(_modules);
 
233
        }
 
234
 
 
235
        ArtsBuilderLoader_impl()
 
236
        {
 
237
                sourceDirs.insert(EXAMPLES_DIR);
 
238
 
 
239
                const char *home = getenv("HOME");
 
240
                if(home) sourceDirs.insert(home+string("/arts/structures"));
 
241
        }
 
242
};
 
243
 
 
244
REGISTER_IMPLEMENTATION(ArtsBuilderLoader_impl);
 
245
};