build: changed order of configure environment for subpackages
[barry.git] / src / a_osloader.cc
blob92ab2886e737a873309dc974a686c3a727d811d7
1 ///
2 /// \file a_osloader.cc
3 /// OS files parser (multi ALX files parser)
4 ///
6 /*
7 Copyright (C) 2010, Nicolas VIVIEN
8 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program 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.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <iostream>
24 #include <fstream>
25 #include <algorithm>
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <errno.h>
31 #include "a_osloader.h"
32 #include "a_alxparser.h"
33 #include "vsmartptr.h"
34 #include "error.h"
37 namespace Barry {
39 namespace ALX {
42 OSLoader::OSLoader(void)
47 OSLoader::~OSLoader(void)
52 void OSLoader::Load(const std::string& pathname)
54 #define ALX_FILE_EXT ".alx"
56 int offset;
58 struct dirent *entry;
60 std::string alxfile;
61 const std::string ext = ALX_FILE_EXT;
63 // At first, we have to read platform properties...
64 alxfile = pathname + "/Platform.alx";
65 LoadALXFile(alxfile, false);
67 // Then, we can read all ALX files
68 // Wrap it in a smart pointer so exceptions are safe
69 vLateSmartPtr<DIR, int (*)(DIR*)> path(&closedir);
70 path.reset( opendir(pathname.c_str()) );
71 if( path.get() == NULL )
72 throw Barry::ErrnoError("Could not opendir: " + pathname, errno);
74 while ((entry = readdir(path.get())) != NULL) {
75 alxfile = entry->d_name;
77 if (alxfile.length() < ext.length())
78 continue;
80 offset = alxfile.length() - ext.length();
82 // Ignore all files except ".alx" files
83 if (alxfile.substr(offset, ext.length()) != ALX_FILE_EXT)
84 continue;
86 LoadALXFile(pathname + "/" + alxfile, true);
91 void OSLoader::LoadALXFile(const std::string& alxfile, const bool enable)
93 std::ifstream file(alxfile.c_str());
94 if( !file )
95 throw Barry::Error("Cannot open ALX file: " + alxfile);
97 ALX::ALXParser parser(*this, file);
99 parser.Run(enable);
101 file.close();
105 void OSLoader::Dump(std::ostream &os) const
107 os << "OS Properties :" << std::endl;
110 std::map<std::string, std::string>::const_iterator b = properties.begin(), e = properties.end();
112 for (; b != e; b++) {
113 os << " - " << (*b).first << " = " << (*b).second << std::endl;
117 os << std::endl;
119 os << "SFI File :" << std::endl;
120 os << " " << sfifile << std::endl;
121 os << std::endl;
123 os << "Applications :" << std::endl;
126 CODSectionList::const_iterator b = applications.begin(), e = applications.end();
128 for (; b != e; b++) {
129 os << (**b) << std::endl;
133 os << "Libraries :" << std::endl;
136 CODSectionList::const_iterator b = libraries.begin(), e = libraries.end();
138 for (; b != e; b++) {
139 os << (**b) << std::endl;
145 void OSLoader::AddProperties(const std::string& property, const std::string& value)
147 properties[property] = value;
149 if (property == "JVMLevel")
150 properties["Java"] = value;
154 void OSLoader::AddProperties(const xmlpp::SaxParser::AttributeList& attrs)
156 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) {
157 std::string attribut(iter->name);
158 std::string value(iter->value);
160 AddProperties(attribut, value);
165 void OSLoader::SetSFIFile(const std::string& name)
167 sfifile = name;
171 bool OSLoader::IsSupported(const xmlpp::SaxParser::AttributeList& attrs)
173 if (properties.empty())
174 return false;
176 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) {
177 std::string attribut(iter->name);
178 std::string value(iter->value);
180 std::string s = properties[attribut];
182 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
183 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
185 if (value[0] == '~') {
186 value = value.substr(1, value.length()-1);
188 if (s == value)
189 return false;
191 else {
192 if (s != value)
193 return false;
197 return true;
201 void OSLoader::AddApplication(OSLoader::CODSectionPtr app)
203 applications.push_back(app);
207 void OSLoader::AddLibrary(OSLoader::CODSectionPtr lib)
209 libraries.push_back(lib);
213 } // namespace ALX
215 } // namespace Barry