From 8b0eeecee308417bf7f30e77bc26e4795bdb717b Mon Sep 17 00:00:00 2001 From: Adam Preble Date: Fri, 22 May 2009 11:35:31 -0400 Subject: [PATCH] Moved YAML code to new PRConfig.cpp. --- libpinproc.xcodeproj/project.pbxproj | 4 + src/PRConfig.cpp | 120 +++++++++++++++++++++++++++ src/PRDevice.cpp | 87 ------------------- 3 files changed, 124 insertions(+), 87 deletions(-) create mode 100644 src/PRConfig.cpp diff --git a/libpinproc.xcodeproj/project.pbxproj b/libpinproc.xcodeproj/project.pbxproj index 65642bf..252f07f 100644 --- a/libpinproc.xcodeproj/project.pbxproj +++ b/libpinproc.xcodeproj/project.pbxproj @@ -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 = ""; }; 668249EC0FC0A4CD0051560E /* PRHardware.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PRHardware.cpp; path = src/PRHardware.cpp; sourceTree = ""; }; + 66824DAD0FC6FC690051560E /* PRConfig.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PRConfig.cpp; path = src/PRConfig.cpp; sourceTree = ""; }; 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; }; diff --git a/src/PRConfig.cpp b/src/PRConfig.cpp new file mode 100644 index 0000000..17b34bb --- /dev/null +++ b/src/PRConfig.cpp @@ -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 +#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; +} diff --git a/src/PRDevice.cpp b/src/PRDevice.cpp index 5b70b7b..db14858 100644 --- a/src/PRDevice.cpp +++ b/src/PRDevice.cpp @@ -99,93 +99,6 @@ void PRDevice::Reset() // TODO: Assign defaults based on machineType. Some may have already been done above. } -#include -#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) {