menu: added new Keywords tag to .desktop files
[barry.git] / src / a_osloader.cc
blob6f71e229146e57e14bb44e1fa6a2bf958b849e3d
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-2013, 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 "i18n.h"
24 #include <iostream>
25 #include <fstream>
26 #include <algorithm>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <errno.h>
32 #include "a_osloader.h"
33 #include "a_alxparser.h"
34 #include "vsmartptr.h"
35 #include "error.h"
36 #include "ios_state.h"
39 namespace Barry {
41 namespace ALX {
44 OSLoader::OSLoader(void)
49 OSLoader::~OSLoader(void)
54 void OSLoader::Load(const std::string& pathname)
56 #define ALX_FILE_EXT ".alx"
58 int offset;
60 struct dirent *entry;
62 std::string alxfile;
63 const std::string ext = ALX_FILE_EXT;
65 // At first, we have to read platform properties...
66 alxfile = pathname + "/Platform.alx";
67 LoadALXFile(alxfile, false);
69 // Then, we can read all ALX files
70 // Wrap it in a smart pointer so exceptions are safe
71 vLateSmartPtr<DIR, int (*)(DIR*)> path(&closedir);
72 path.reset( opendir(pathname.c_str()) );
73 if( path.get() == NULL )
74 throw Barry::ErrnoError(_("Could not opendir: ") + pathname, errno);
76 while ((entry = readdir(path.get())) != NULL) {
77 alxfile = entry->d_name;
79 if (alxfile.length() < ext.length())
80 continue;
82 offset = alxfile.length() - ext.length();
84 // Ignore all files except ".alx" files
85 if (alxfile.substr(offset, ext.length()) != ALX_FILE_EXT)
86 continue;
88 LoadALXFile(pathname + "/" + alxfile, true);
93 void OSLoader::LoadALXFile(const std::string& alxfile, const bool enable)
95 std::ifstream file(alxfile.c_str());
96 if( !file )
97 throw Barry::Error(_("Cannot open ALX file: ") + alxfile);
99 ALX::ALXParser parser(*this, file);
101 parser.Run(enable);
103 file.close();
107 void OSLoader::Dump(std::ostream &os) const
109 ios_format_state state(os);
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 CODSectionList::const_iterator b = applications.begin(), e = applications.end();
132 for (; b != e; b++) {
133 os << (**b) << std::endl;
137 os << _("Libraries :") << std::endl;
140 CODSectionList::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(OSLoader::CODSectionPtr app)
207 applications.push_back(app);
211 void OSLoader::AddLibrary(OSLoader::CODSectionPtr lib)
213 libraries.push_back(lib);
217 } // namespace ALX
219 } // namespace Barry