From 53df30459e5c14d433e415db5acd13c405bf7d83 Mon Sep 17 00:00:00 2001
From: Florian Pose <fp@igh-essen.com>
Date: Thu, 11 Mar 2010 15:17:25 +0100
Subject: [PATCH] Added getSingleMasterIndex() for command-line tool.

---
 tool/Command.cpp         | 13 +++++++++++++
 tool/Command.h           |  1 +
 tool/CommandAlias.cpp    |  8 +-------
 tool/CommandDownload.cpp |  8 +-------
 tool/CommandFoeRead.cpp  |  8 +-------
 tool/CommandFoeWrite.cpp |  9 +--------
 tool/CommandGraph.cpp    |  9 +--------
 tool/CommandRegRead.cpp  |  8 +-------
 tool/CommandRegWrite.cpp |  9 +--------
 tool/CommandSiiRead.cpp  |  8 +-------
 tool/CommandSiiWrite.cpp |  9 +--------
 tool/CommandSoeRead.cpp  |  8 +-------
 tool/CommandSoeWrite.cpp |  8 +-------
 tool/CommandUpload.cpp   |  8 +-------
 tool/CommandXml.cpp      |  9 +--------
 15 files changed, 27 insertions(+), 96 deletions(-)

diff --git a/tool/Command.cpp b/tool/Command.cpp
index 8b9da9b7..565999e1 100644
--- a/tool/Command.cpp
+++ b/tool/Command.cpp
@@ -209,6 +209,19 @@ Command::MasterIndexList Command::getMasterIndices() const
 
 /*****************************************************************************/
 
+unsigned int Command::getSingleMasterIndex() const
+{
+	MasterIndexList masterIndices = getMasterIndices();
+    if (masterIndices.size() != 1) {
+		stringstream err;
+        err << getName() << " requires to select a single master!";
+        throwInvalidUsageException(err);
+    }
+	return masterIndices.front();
+}
+
+/*****************************************************************************/
+
 Command::SlaveList Command::selectedSlaves(MasterDevice &m)
 {
     ec_ioctl_master_t master;
diff --git a/tool/Command.h b/tool/Command.h
index b98001f8..14306c53 100644
--- a/tool/Command.h
+++ b/tool/Command.h
@@ -87,6 +87,7 @@ class Command
         typedef list<unsigned int> MasterIndexList;
         void setMasters(const string &);
         MasterIndexList getMasterIndices() const;
+		unsigned int getSingleMasterIndex() const;
 
         enum Verbosity {
             Quiet,
diff --git a/tool/CommandAlias.cpp b/tool/CommandAlias.cpp
index c6d588f3..6c53379e 100644
--- a/tool/CommandAlias.cpp
+++ b/tool/CommandAlias.cpp
@@ -81,7 +81,6 @@ void CommandAlias::execute(const StringVector &args)
     uint16_t alias;
     stringstream err, strAlias;
     int number;
-	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
 
@@ -100,12 +99,7 @@ void CommandAlias::execute(const StringVector &args)
     }
     alias = number;
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     
diff --git a/tool/CommandDownload.cpp b/tool/CommandDownload.cpp
index fbbfcb44..7253d0b5 100644
--- a/tool/CommandDownload.cpp
+++ b/tool/CommandDownload.cpp
@@ -85,7 +85,6 @@ string CommandDownload::helpString() const
 void CommandDownload::execute(const StringVector &args)
 {
     stringstream strIndex, strSubIndex, err;
-	MasterIndexList masterIndices;
     ec_ioctl_slave_sdo_download_t data;
     unsigned int number;
     const DataType *dataType = NULL;
@@ -115,12 +114,7 @@ void CommandDownload::execute(const StringVector &args)
     }
     data.sdo_entry_subindex = number;
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandFoeRead.cpp b/tool/CommandFoeRead.cpp
index 1afc7855..3ab6d913 100644
--- a/tool/CommandFoeRead.cpp
+++ b/tool/CommandFoeRead.cpp
@@ -76,7 +76,6 @@ string CommandFoeRead::helpString() const
 
 void CommandFoeRead::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     SlaveList slaves;
     ec_ioctl_slave_t *slave;
     ec_ioctl_slave_foe_t data;
@@ -88,12 +87,7 @@ void CommandFoeRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/CommandFoeWrite.cpp b/tool/CommandFoeWrite.cpp
index c1c3139b..58094e1d 100644
--- a/tool/CommandFoeWrite.cpp
+++ b/tool/CommandFoeWrite.cpp
@@ -82,7 +82,6 @@ string CommandFoeWrite::helpString() const
 
 void CommandFoeWrite::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     stringstream err;
     ec_ioctl_slave_foe_t data;
     ifstream file;
@@ -94,13 +93,6 @@ void CommandFoeWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
-
     if (args[0] == "-") {
         loadFoeData(&data, cin);
         if (getOutputFile().empty()) {
@@ -128,6 +120,7 @@ void CommandFoeWrite::execute(const StringVector &args)
         }
     }
 
+    MasterDevice m(getSingleMasterIndex());
     try {
         m.open(MasterDevice::ReadWrite);
     } catch (MasterDeviceException &e) {
diff --git a/tool/CommandGraph.cpp b/tool/CommandGraph.cpp
index 78cf2b2f..951c5d27 100644
--- a/tool/CommandGraph.cpp
+++ b/tool/CommandGraph.cpp
@@ -67,7 +67,6 @@ string CommandGraph::helpString() const
 
 void CommandGraph::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     ec_ioctl_master_t master;
     unsigned int i;
     typedef vector<ec_ioctl_slave_t> SlaveVector;
@@ -91,13 +90,7 @@ void CommandGraph::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        stringstream err;
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     m.getMaster(&master);
 
diff --git a/tool/CommandRegRead.cpp b/tool/CommandRegRead.cpp
index 65851865..8af9635f 100644
--- a/tool/CommandRegRead.cpp
+++ b/tool/CommandRegRead.cpp
@@ -79,7 +79,6 @@ string CommandRegRead::helpString() const
 
 void CommandRegRead::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     SlaveList slaves;
     ec_ioctl_slave_reg_t data;
     stringstream strOffset, err;
@@ -141,12 +140,7 @@ void CommandRegRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
     
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/CommandRegWrite.cpp b/tool/CommandRegWrite.cpp
index 6de5034e..964dc1bb 100644
--- a/tool/CommandRegWrite.cpp
+++ b/tool/CommandRegWrite.cpp
@@ -81,7 +81,6 @@ string CommandRegWrite::helpString() const
 
 void CommandRegWrite::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     stringstream strOffset, err;
     ec_ioctl_slave_reg_t data;
     ifstream file;
@@ -101,13 +100,6 @@ void CommandRegWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
   
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
-
     if (getDataType().empty()) {
         if (args[1] == "-") {
             loadRegData(&data, cin);
@@ -157,6 +149,7 @@ void CommandRegWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
+    MasterDevice m(getSingleMasterIndex());
     try {
         m.open(MasterDevice::ReadWrite);
     } catch (MasterDeviceException &e) {
diff --git a/tool/CommandSiiRead.cpp b/tool/CommandSiiRead.cpp
index 368b2e59..24e23ae8 100644
--- a/tool/CommandSiiRead.cpp
+++ b/tool/CommandSiiRead.cpp
@@ -76,7 +76,6 @@ string CommandSiiRead::helpString() const
 
 void CommandSiiRead::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     SlaveList slaves;
     ec_ioctl_slave_t *slave;
     ec_ioctl_slave_sii_t data;
@@ -90,12 +89,7 @@ void CommandSiiRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
diff --git a/tool/CommandSiiWrite.cpp b/tool/CommandSiiWrite.cpp
index 4aa3eddd..de6d0507 100644
--- a/tool/CommandSiiWrite.cpp
+++ b/tool/CommandSiiWrite.cpp
@@ -78,7 +78,6 @@ string CommandSiiWrite::helpString() const
 
 void CommandSiiWrite::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     stringstream err;
     ec_ioctl_slave_sii_t data;
     ifstream file;
@@ -89,13 +88,6 @@ void CommandSiiWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
-
     if (args[0] == "-") {
         loadSiiData(&data, cin);
     } else {
@@ -117,6 +109,7 @@ void CommandSiiWrite::execute(const StringVector &args)
         }
     }
 
+    MasterDevice m(getSingleMasterIndex());
     try {
         m.open(MasterDevice::ReadWrite);
     } catch (MasterDeviceException &e) {
diff --git a/tool/CommandSoeRead.cpp b/tool/CommandSoeRead.cpp
index d46999a0..a528e980 100644
--- a/tool/CommandSoeRead.cpp
+++ b/tool/CommandSoeRead.cpp
@@ -78,7 +78,6 @@ string CommandSoeRead::helpString() const
 
 void CommandSoeRead::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     SlaveList slaves;
     stringstream err;
     const DataType *dataType = NULL;
@@ -96,12 +95,7 @@ void CommandSoeRead::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandSoeWrite.cpp b/tool/CommandSoeWrite.cpp
index 6c348521..ab5f1740 100644
--- a/tool/CommandSoeWrite.cpp
+++ b/tool/CommandSoeWrite.cpp
@@ -80,7 +80,6 @@ string CommandSoeWrite::helpString() const
 
 void CommandSoeWrite::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     stringstream strIdn, err;
     const DataType *dataType = NULL;
     ec_ioctl_slave_soe_write_t ioctl;
@@ -99,12 +98,7 @@ void CommandSoeWrite::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::ReadWrite);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandUpload.cpp b/tool/CommandUpload.cpp
index 38d3fbf7..4146ab69 100644
--- a/tool/CommandUpload.cpp
+++ b/tool/CommandUpload.cpp
@@ -82,7 +82,6 @@ string CommandUpload::helpString() const
 
 void CommandUpload::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     SlaveList slaves;
     stringstream err, strIndex, strSubIndex;
     ec_ioctl_slave_sdo_upload_t data;
@@ -113,12 +112,7 @@ void CommandUpload::execute(const StringVector &args)
     }
     data.sdo_entry_subindex = uval;
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
     if (slaves.size() != 1) {
diff --git a/tool/CommandXml.cpp b/tool/CommandXml.cpp
index 4969fa59..baf42a3d 100644
--- a/tool/CommandXml.cpp
+++ b/tool/CommandXml.cpp
@@ -71,7 +71,6 @@ string CommandXml::helpString() const
 
 void CommandXml::execute(const StringVector &args)
 {
-	MasterIndexList masterIndices;
     SlaveList slaves;
     SlaveList::const_iterator si;
 
@@ -81,13 +80,7 @@ void CommandXml::execute(const StringVector &args)
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    if (masterIndices.size() != 1) {
-        stringstream err;
-        err << getName() << " requires to select a single master!";
-        throwInvalidUsageException(err);
-    }
-    MasterDevice m(masterIndices.front());
+    MasterDevice m(getSingleMasterIndex());
     m.open(MasterDevice::Read);
     slaves = selectedSlaves(m);
 
-- 
GitLab