desktop: show the modem pppd command in the modem script
[barry.git] / src / a_osloader.cc
blobd9dfeb6d8788d58a791dbf59d8a83a649f8fc6ba
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-2012, 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"
35 #include "ios_state.h"
38 namespace Barry {
40 namespace ALX {
43 OSLoader::OSLoader(void)
48 OSLoader::~OSLoader(void)
53 void OSLoader::Load(const std::string& pathname)
55 #define ALX_FILE_EXT ".alx"
57 int offset;
59 struct dirent *entry;
61 std::string alxfile;
62 const std::string ext = ALX_FILE_EXT;
64 // At first, we have to read platform properties...
65 alxfile = pathname + "/Platform.alx";
66 LoadALXFile(alxfile, false);
68 // Then, we can read all ALX files
69 // Wrap it in a smart pointer so exceptions are safe
70 vLateSmartPtr<DIR, int (*)(DIR*)> path(&closedir);
71 path.reset( opendir(pathname.c_str()) );
72 if( path.get() == NULL )
73 throw Barry::ErrnoError("Could not opendir: " + pathname, errno);
75 while ((entry = readdir(path.get())) != NULL) {
76 alxfile = entry->d_name;
78 if (alxfile.length() < ext.length())
79 continue;
81 offset = alxfile.length() - ext.length();
83 // Ignore all files except ".alx" files
84 if (alxfile.substr(offset, ext.length()) != ALX_FILE_EXT)
85 continue;
87 LoadALXFile(pathname + "/" + alxfile, true);
92 void OSLoader::LoadALXFile(const std::string& alxfile, const bool enable)
94 std::ifstream file(alxfile.c_str());
95 if( !file )
96 throw Barry::Error("Cannot open ALX file: " + alxfile);
98 ALX::ALXParser parser(*this, file);
100 parser.Run(enable);
102 file.close();
106 void OSLoader::Dump(std::ostream &os) const
108 ios_format_state state(os);
110 os << "OS Properties :" << std::endl;
113 std::map<std::string, std::string>::const_iterator b = properties.begin(), e = properties.end();
115 for (; b != e; b++) {
116 os << " - " << (*b).first << " = " << (*b).second << std::endl;
120 os << std::endl;
122 os << "SFI File :" << std::endl;
123 os << " " << sfifile << std::endl;
124 os << std::endl;
126 os << "Applications :" << std::endl;
129 CODSectionList::const_iterator b = applications.begin(), e = applications.end();
131 for (; b != e; b++) {
132 os << (**b) << std::endl;
136 os << "Libraries :" << std::endl;
139 CODSectionList::const_iterator b = libraries.begin(), e = libraries.end();
141 for (; b != e; b++) {
142 os << (**b) << std::endl;
148 void OSLoader::AddProperties(const std::string& property, const std::string& value)
150 properties[property] = value;
152 if (property == "JVMLevel")
153 properties["Java"] = value;
157 void OSLoader::AddProperties(const xmlpp::SaxParser::AttributeList& attrs)
159 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) {
160 std::string attribut(iter->name);
161 std::string value(iter->value);
163 AddProperties(attribut, value);
168 void OSLoader::SetSFIFile(const std::string& name)
170 sfifile = name;
174 bool OSLoader::IsSupported(const xmlpp::SaxParser::AttributeList& attrs)
176 if (properties.empty())
177 return false;
179 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attrs.begin(); iter != attrs.end(); ++iter) {
180 std::string attribut(iter->name);
181 std::string value(iter->value);
183 std::string s = properties[attribut];
185 std::transform(value.begin(), value.end(), value.begin(), ::tolower);
186 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
188 if (value[0] == '~') {
189 value = value.substr(1, value.length()-1);
191 if (s == value)
192 return false;
194 else {
195 if (s != value)
196 return false;
200 return true;
204 void OSLoader::AddApplication(OSLoader::CODSectionPtr app)
206 applications.push_back(app);
210 void OSLoader::AddLibrary(OSLoader::CODSectionPtr lib)
212 libraries.push_back(lib);
216 } // namespace ALX
218 } // namespace Barry