1
0
mirror of https://github.com/preble/libpinproc synced 2026-02-22 18:15:25 +01:00

Moved YAML code to new PRConfig.cpp.

This commit is contained in:
Adam Preble
2009-05-22 11:35:31 -04:00
parent cd1b0462c5
commit 8b0eeecee3
3 changed files with 124 additions and 87 deletions

View File

@@ -15,6 +15,7 @@
668249E30FC0A3960051560E /* pinproctest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 668249E20FC0A3960051560E /* pinproctest.cpp */; };
668249EA0FC0A4280051560E /* libpinproc.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D2AAC046055464E500DB518D /* libpinproc.a */; };
668249ED0FC0A4CD0051560E /* PRHardware.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 668249EC0FC0A4CD0051560E /* PRHardware.cpp */; };
66824DAE0FC6FC690051560E /* PRConfig.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 66824DAD0FC6FC690051560E /* PRConfig.cpp */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -36,6 +37,7 @@
668249D90FC0A30A0051560E /* pinproctest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = pinproctest; sourceTree = BUILT_PRODUCTS_DIR; };
668249E20FC0A3960051560E /* pinproctest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = pinproctest.cpp; path = examples/pinproctest/pinproctest.cpp; sourceTree = "<group>"; };
668249EC0FC0A4CD0051560E /* PRHardware.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PRHardware.cpp; path = src/PRHardware.cpp; sourceTree = "<group>"; };
66824DAD0FC6FC690051560E /* PRConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PRConfig.cpp; path = src/PRConfig.cpp; sourceTree = "<group>"; };
D2AAC046055464E500DB518D /* libpinproc.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libpinproc.a; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
@@ -93,6 +95,7 @@
668249390FC07B2A0051560E /* pinproc.cpp */,
668249400FC07D900051560E /* PRDevice.h */,
668249410FC07D900051560E /* PRDevice.cpp */,
66824DAD0FC6FC690051560E /* PRConfig.cpp */,
6682494A0FC0870B0051560E /* PRHardware.h */,
668249EC0FC0A4CD0051560E /* PRHardware.cpp */,
);
@@ -198,6 +201,7 @@
6682493A0FC07B2A0051560E /* pinproc.cpp in Sources */,
668249430FC07D900051560E /* PRDevice.cpp in Sources */,
668249ED0FC0A4CD0051560E /* PRHardware.cpp in Sources */,
66824DAE0FC6FC690051560E /* PRConfig.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

120
src/PRConfig.cpp Normal file
View File

@@ -0,0 +1,120 @@
/*
* The MIT License
* Copyright (c) 2009 Gerry Stellenberg, Adam Preble
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* PRConfig.cpp
* libpinproc
*
*/
#include "PRDevice.h"
#include <fstream>
#include "yaml.h"
PRResult PRDevice::LoadDefaultsFromYAML(const char *yamlFilePath)
{
try
{
std::ifstream fin(yamlFilePath);
if (fin.is_open() == false)
{
DEBUG(PRLog("YAML file not found: %s\n", yamlFilePath));
return kPRFailure;
}
YAML::Parser parser(fin);
while(parser) {
YAML::Node doc;
parser.GetNextDocument(doc);
for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
std::string key;
it.first() >> key;
DEBUG(PRLog("Parsing key %s...\n", key.c_str()));
if (key.compare("PRGameName") == 0)
{
std::string name;
it.second() >> name;
DEBUG(PRLog(" Machine name: %s\n", name.c_str()));
}
else if (key.compare("PRDriverGlobalConfig") == 0)
{
//const YAML::Node& dict = it.second();
}
else if (key.compare("PRDriverGroupConfigs") == 0)
{
const YAML::Node& groups = it.second();
for(YAML::Iterator it=groups.begin();it!=groups.end();++it)
{
std::string key;
it.first() >> key;
}
}
else if (key.compare("PRDrivers") == 0)
{
const YAML::Node& dict = it.second();
for(YAML::Iterator dictIt=dict.begin(); dictIt != dict.end(); ++dictIt)
{
std::string driverKey;
std::string driverName;
dictIt.first() >> driverKey;
dictIt.second() >> driverName;
DEBUG(PRLog(" Driver: %s -> %s\n", driverKey.c_str(), driverName.c_str()));
}
}
else if (key.compare("PRSwitches") == 0)
{
const YAML::Node& dict = it.second();
for(YAML::Iterator dictIt=dict.begin(); dictIt != dict.end(); ++dictIt)
{
std::string driverKey;
std::string driverName;
dictIt.first() >> driverKey;
dictIt.second() >> driverName;
DEBUG(PRLog(" Switch: %s -> %s\n", driverKey.c_str(), driverName.c_str()));
}
}
}
}
}
catch (YAML::ParserException& ex)
{
DEBUG(PRLog("YAML parse error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str()));
return kPRFailure;
}
catch (YAML::RepresentationException& ex)
{
DEBUG(PRLog("YAML representation error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str()));
return kPRFailure;
}
catch (...)
{
DEBUG(PRLog("Unexpected exception while parsing YAML config.\n"));
return kPRFailure;
}
return kPRSuccess;
}

View File

@@ -99,93 +99,6 @@ void PRDevice::Reset()
// TODO: Assign defaults based on machineType. Some may have already been done above.
}
#include <fstream>
#include "yaml.h"
PRResult PRDevice::LoadDefaultsFromYAML(const char *yamlFilePath)
{
try
{
std::ifstream fin(yamlFilePath);
if (fin.is_open() == false)
{
DEBUG(PRLog("YAML file not found: %s\n", yamlFilePath));
return kPRFailure;
}
YAML::Parser parser(fin);
while(parser) {
YAML::Node doc;
parser.GetNextDocument(doc);
for(YAML::Iterator it=doc.begin();it!=doc.end();++it) {
std::string key;
it.first() >> key;
DEBUG(PRLog("Parsing key %s...\n", key.c_str()));
if (key.compare("PRGameName") == 0)
{
std::string name;
it.second() >> name;
DEBUG(PRLog(" Machine name: %s\n", name.c_str()));
}
else if (key.compare("PRDriverGlobalConfig") == 0)
{
//const YAML::Node& dict = it.second();
}
else if (key.compare("PRDriverGroupConfigs") == 0)
{
const YAML::Node& groups = it.second();
for(YAML::Iterator it=groups.begin();it!=groups.end();++it)
{
std::string key;
it.first() >> key;
}
}
else if (key.compare("PRDrivers") == 0)
{
const YAML::Node& dict = it.second();
for(YAML::Iterator dictIt=dict.begin(); dictIt != dict.end(); ++dictIt)
{
std::string driverKey;
std::string driverName;
dictIt.first() >> driverKey;
dictIt.second() >> driverName;
DEBUG(PRLog(" Driver: %s -> %s\n", driverKey.c_str(), driverName.c_str()));
}
}
else if (key.compare("PRSwitches") == 0)
{
const YAML::Node& dict = it.second();
for(YAML::Iterator dictIt=dict.begin(); dictIt != dict.end(); ++dictIt)
{
std::string driverKey;
std::string driverName;
dictIt.first() >> driverKey;
dictIt.second() >> driverName;
DEBUG(PRLog(" Switch: %s -> %s\n", driverKey.c_str(), driverName.c_str()));
}
}
}
}
}
catch (YAML::ParserException& ex)
{
DEBUG(PRLog("YAML parse error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str()));
return kPRFailure;
}
catch (YAML::RepresentationException& ex)
{
DEBUG(PRLog("YAML representation error at line=%d col=%d: %s\n", ex.line, ex.column, ex.msg.c_str()));
return kPRFailure;
}
catch (...)
{
DEBUG(PRLog("Unexpected exception while parsing YAML config.\n"));
return kPRFailure;
}
return kPRSuccess;
}
int PRDevice::GetEvents(PREvent *events, int maxEvents)
{