2 /// \file a_osloader.cc
3 /// OS files parser (multi ALX files parser)
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.
27 #include <sys/types.h>
31 #include "a_osloader.h"
32 #include "a_alxparser.h"
33 #include "vsmartptr.h"
35 #include "ios_state.h"
43 OSLoader::OSLoader(void)
48 OSLoader::~OSLoader(void)
53 void OSLoader::Load(const std::string
& pathname
)
55 #define ALX_FILE_EXT ".alx"
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())
81 offset
= alxfile
.length() - ext
.length();
83 // Ignore all files except ".alx" files
84 if (alxfile
.substr(offset
, ext
.length()) != ALX_FILE_EXT
)
87 LoadALXFile(pathname
+ "/" + alxfile
, true);
92 void OSLoader::LoadALXFile(const std::string
& alxfile
, const bool enable
)
94 std::ifstream
file(alxfile
.c_str());
96 throw Barry::Error("Cannot open ALX file: " + alxfile
);
98 ALX::ALXParser
parser(*this, file
);
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
;
122 os
<< "SFI File :" << std::endl
;
123 os
<< " " << sfifile
<< 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
)
174 bool OSLoader::IsSupported(const xmlpp::SaxParser::AttributeList
& attrs
)
176 if (properties
.empty())
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);
204 void OSLoader::AddApplication(OSLoader::CODSectionPtr app
)
206 applications
.push_back(app
);
210 void OSLoader::AddLibrary(OSLoader::CODSectionPtr lib
)
212 libraries
.push_back(lib
);