2 /// \file a_osloader.cc
3 /// OS files parser (multi ALX files parser)
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.
27 #include <sys/types.h>
31 #include "a_osloader.h"
32 #include "a_alxparser.h"
33 #include "vsmartptr.h"
42 OSLoader::OSLoader(void)
47 OSLoader::~OSLoader(void)
52 void OSLoader::Load(const std::string
& pathname
)
54 #define ALX_FILE_EXT ".alx"
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())
80 offset
= alxfile
.length() - ext
.length();
83 if (alxfile
== "BlackBerry.alx")
86 // Ignore all files except ".alx" files
87 if (alxfile
.substr(offset
, ext
.length()) != ALX_FILE_EXT
)
90 LoadALXFile(pathname
+ "/" + alxfile
, true);
95 void OSLoader::LoadALXFile(const std::string
& alxfile
, const bool enable
)
97 std::ifstream
file(alxfile
.c_str());
99 throw Barry::Error("Cannot open ALX file: " + alxfile
);
101 ALX::ALXParser
parser(*this, file
);
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
;
123 os
<< "SFI File :" << std::endl
;
124 os
<< " " << sfifile
<< 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
)
175 bool OSLoader::IsSupported(const xmlpp::SaxParser::AttributeList
& attrs
)
177 if (properties
.empty())
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);
205 void OSLoader::AddApplication(CODSection
*app
)
207 applications
.push_back(app
);
211 void OSLoader::AddLibrary(CODSection
*lib
)
213 libraries
.push_back(lib
);