lib: changed capitalization of member functions to match rest of Barry API
[barry.git] / src / a_osloader.cc
blob58673be30c4dda5e347b18ea7e828af62c9676ed
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 // Not supported...
83 if (alxfile == "BlackBerry.alx")
84 continue;
86 // Ignore all files except ".alx" files
87 if (alxfile.substr(offset, ext.length()) != ALX_FILE_EXT)
88 continue;
90 LoadALXFile(pathname + "/" + alxfile, true);
95 void OSLoader::LoadALXFile(const std::string& alxfile, const bool enable)
97 std::ifstream file(alxfile.c_str());
98 if( !file )
99 throw Barry::Error("Cannot open ALX file: " + alxfile);
101 ALX::ALXParser parser(*this, file);
103 parser.Run(enable);
105 file.close();
109 void OSLoader::Dump(std::ostream &os) const
111 os << "OS Properties :" << std::endl;
114 std::map<std::string, std::string>::const_iterator b = properties.begin(), e = properties.end();
116 for (; b != e; b++) {
117 os << " - " << (*b).first << " = " << (*b).second << std::endl;
121 os << std::endl;
123 os << "SFI File :" << std::endl;
124 os << " " << sfifile << std::endl;
125 os << std::endl;
127 os << "Applications :" << std::endl;
130 std::vector<CODSection *>::const_iterator b = applications.begin(), e = applications.end();
132 for (; b != e; b++) {
133 os << (**b) << std::endl;
137 os << "Libraries :" << std::endl;
140 std::vector<CODSection *>::const_iterator b = libraries.begin(), e = libraries.end();
142 for (; b != e; b++) {
143 os << (**b) << std::endl;
149 void OSLoader::AddProperties(const std::string& property, const std::string& value)
151 properties[property] = value;
153 if (property == "JVMLevel")
154 properties["Java"] = value;
158 void OSLoader::AddProperties(const xmlpp::SaxParser::AttributeList& attrs)
160 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) {
161 std::string attribut(iter->name);
162 std::string value(iter->value);
164 AddProperties(attribut, value);
169 void OSLoader::SetSFIFile(const std::string& name)
171 sfifile = name;
175 bool OSLoader::IsSupported(const xmlpp::SaxParser::AttributeList& attrs)
177 if (properties.empty())
178 return false;
180 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) {
181 std::string attribut(iter->name);
182 std::string value(iter->value);
184 std::string s = properties[attribut];
186 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
187 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
189 if (value[0] == '~') {
190 value = value.substr(1, value.length()-1);
192 if (s == value)
193 return false;
195 else {
196 if (s != value)
197 return false;
201 return true;
205 void OSLoader::AddApplication(CODSection *app)
207 applications.push_back(app);
211 void OSLoader::AddLibrary(CODSection *lib)
213 libraries.push_back(lib);
217 } // namespace ALX
219 } // namespace Barry