From 0e03940802cebefdf6b0597a154bd9395e1af4d2 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Wed, 18 Jun 2014 15:45:45 +0200 Subject: Add the vanilla version of the project This version can still be found here: http://www.roland-riegel.de/nload/index.html --- src/devreaderfactory.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/devreaderfactory.cpp (limited to 'src/devreaderfactory.cpp') diff --git a/src/devreaderfactory.cpp b/src/devreaderfactory.cpp new file mode 100644 index 0000000..c2fc656 --- /dev/null +++ b/src/devreaderfactory.cpp @@ -0,0 +1,128 @@ +/*************************************************************************** + devreaderfactory.cpp + -------------------- + begin : Fri Nov 16 2007 + copyright : (C) 2007 - 2012 by Roland Riegel + email : feedback@roland-riegel.de + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "devreaderfactory.h" + +#include "config.h" + +#include "devreader.h" +#include "devreader-bsd.h" +#include "devreader-hpux.h" +#include "devreader-linux-proc.h" +#include "devreader-linux-sys.h" +#include "devreader-linux.h" +#include "devreader-solaris.h" + +#include +#include +#include + +using namespace std; + +map DevReaderFactory::m_devReaders; +DevReaderFactory DevReaderFactory::m_instance; + +DevReaderFactory::DevReaderFactory() +{ +} + +DevReaderFactory::~DevReaderFactory() +{ + for(map::const_iterator it = m_devReaders.begin(); it != m_devReaders.end(); ++it) + delete it->second; + + m_devReaders.clear(); +} + +int DevReaderFactory::findAllDevices() +{ +#if defined HAVE_BSD + list interfaceNames = DevReaderBsd::findAllDevices(); +#elif defined HAVE_HPUX + list interfaceNames = DevReaderHpux::findAllDevices(); +#elif defined HAVE_LINUX + list interfaceNames = DevReaderLinux::findAllDevices(); +#elif defined HAVE_SOLARIS + list interfaceNames = DevReaderSolaris::findAllDevices(); +#endif + + map::iterator devReaderIt = m_devReaders.begin(); + while(devReaderIt != m_devReaders.end()) + { + list::iterator interfaceIt = interfaceNames.begin(); + list::iterator interfaceItEnd = interfaceNames.end(); + + while(*interfaceIt != devReaderIt->first && interfaceIt != interfaceItEnd) + ++interfaceIt; + + // delete all devices which disappeared + if(interfaceIt == interfaceItEnd) + { + delete devReaderIt->second; + m_devReaders.erase(devReaderIt++); + } + // delete all entries in the interface name list which we know of already + else + { + interfaceNames.erase(interfaceIt); + devReaderIt++; + } + } + + // the interface name list now contains only devices which just appeared in the system + for(list::const_iterator it = interfaceNames.begin(); it != interfaceNames.end(); ++it) + { + DevReader* newReader = createDevReader(*it); + if(newReader) + m_devReaders[*it] = newReader; + } + + return m_devReaders.size(); +} + +int DevReaderFactory::getDeviceCount() +{ + return m_devReaders.size(); +} + +const map& DevReaderFactory::getAllDevReaders() +{ + return m_devReaders; +} + +DevReader* DevReaderFactory::createDevReader(const string& deviceName) +{ + DevReader* reader = 0; + +#if defined HAVE_BSD + reader = new DevReaderBsd(deviceName); +#elif defined HAVE_HPUX + reader = new DevReaderHpux(deviceName); +#elif defined HAVE_LINUX + if(DevReaderLinuxSys::isAvailable()) + reader = new DevReaderLinuxSys(deviceName); + else if(DevReaderLinuxProc::isAvailable()) + reader = new DevReaderLinuxProc(deviceName); + else + reader = 0; +#elif defined HAVE_SOLARIS + reader = new DevReaderSolaris(deviceName); +#endif + + return reader; +} + -- cgit v1.2.3