3
Copyright (C) 2001 Stefan Westerfeld
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.
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.
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.
23
#include "artsbuilder.h"
26
#include <sys/types.h>
39
class ArtsBuilderLoader_impl : virtual public ArtsBuilderLoader_skel {
41
set<string> sourceDirs;
43
string lastDataVersion;
44
vector<TraderEntry> _traderEntries;
45
vector<ModuleDef> _modules;
48
Object loadObject(Arts::TraderOffer offer)
50
StructureDesc structureDesc;
52
vector<string> strseq;
55
vector<string> *filenames = offer.getProperty("File");
56
if(filenames->size() == 1)
58
string& filename = filenames->front();
59
arts_info("ArtsBuilderLoader: filename = %s", filename.c_str());
61
ifstream infile(filename.c_str());
63
while(getline(infile,line)) strseq.push_back(line);
67
structureDesc.loadFromList(strseq);
68
if(structureDesc.name() != offer.interfaceName())
70
arts_warning("failed (name = %s).",structureDesc.name().c_str());
71
return Object::null();
74
StructureBuilder builder;
75
builder.addFactory(LocalFactory());
77
return builder.createObject(structureDesc);
80
vector<string> *listFiles(const string& pathname, const char *extension)
82
vector<string> *result = new vector<string>();
84
unsigned long extlen = strlen(extension);
85
DIR *dir = opendir(pathname.c_str());
89
while((de = readdir(dir)) != 0)
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);
101
void scanArtsFile(const string& filename)
103
StructureDesc structureDesc;
104
vector<string> strseq;
108
ifstream infile(filename.c_str());
112
while(getline(infile,line))
115
* TODO - maybe there is a cleaner way?
117
* the following six lines are a bit hackish code to skip
118
* the module sections of the structures
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
128
if(strncmp(line.c_str(), "module=", 7) == 0)
131
if(strncmp(line.c_str(), "{", 1) == 0 && inmodule == 1)
134
if(strncmp(line.c_str(), "}", 1) == 0 && inmodule == 2)
138
strseq.push_back(line);
142
structureDesc.loadFromList(strseq);
143
string name = structureDesc.name();
146
arts_debug("%s [%s]\n",filename.c_str(),name.c_str());
148
/* add to _modules */
149
StructureBuilder builder;
150
ModuleDef md = builder.createTypeInfo(structureDesc);
151
_modules.push_back(md);
153
arts_assert(md.moduleName == name);
155
/* add to _traderEntries */
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);
164
_traderEntries.push_back(entry);
166
* TODO: more entries like
167
* Author="Stefan Westerfeld <stefan@space.twc.de>"
168
* URL="http://www.arts-project.org"
175
lastDataVersion = dataVersion();
177
_traderEntries.clear();
180
set<string>::iterator si;
181
for(si = sourceDirs.begin(); si != sourceDirs.end(); si++)
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);
194
* change this string if you change the loading algorithm to force
195
* rescanning even with the same data
197
string result = "ArtsBuilderLoader:1.0:";
201
set<string>::iterator i;
202
for(i = sourceDirs.begin(); i != sourceDirs.end(); i++)
204
const string& filename = *i;
207
stat(filename.c_str(), &st);
209
if(!first) result += ",";
213
sprintf(mtime,"[%ld]",st.st_mtime);
214
result += filename + mtime;
219
vector<TraderEntry> *traderEntries()
221
if(dataVersion() != lastDataVersion)
224
return new vector<TraderEntry>(_traderEntries);
227
vector<ModuleDef> *modules()
229
if(dataVersion() != lastDataVersion)
232
return new vector<ModuleDef>(_modules);
235
ArtsBuilderLoader_impl()
237
sourceDirs.insert(EXAMPLES_DIR);
239
const char *home = getenv("HOME");
240
if(home) sourceDirs.insert(home+string("/arts/structures"));
244
REGISTER_IMPLEMENTATION(ArtsBuilderLoader_impl);