From a0a13705c12b7c1e7fa6c4312315603964cbbea0 Mon Sep 17 00:00:00 2001
From: William F Godoy <williamfgc@yahoo.com>
Date: Tue, 24 Oct 2017 16:21:19 -0400
Subject: [PATCH] Initial bpls2

---
 bindings/fortran/CMakeLists.txt               |   2 +-
 source/CMakeLists.txt                         |   1 +
 source/adios2/utils/bpls2/BPLS2.h             |  22 ---
 source/adios2/utils/bpls2/main.cpp            |  13 --
 source/utils/CMakeLists.txt                   |  13 ++
 .../utils/bpls2/BPLS2.cpp => utils/Utils.cpp} |  11 +-
 source/utils/Utils.h                          |  47 +++++
 source/utils/bpls2/BPLS2.cpp                  | 179 ++++++++++++++++++
 source/utils/bpls2/BPLS2.h                    |  46 +++++
 source/utils/bpls2/main.cpp                   |  28 +++
 10 files changed, 323 insertions(+), 39 deletions(-)
 delete mode 100644 source/adios2/utils/bpls2/BPLS2.h
 delete mode 100644 source/adios2/utils/bpls2/main.cpp
 create mode 100644 source/utils/CMakeLists.txt
 rename source/{adios2/utils/bpls2/BPLS2.cpp => utils/Utils.cpp} (57%)
 create mode 100644 source/utils/Utils.h
 create mode 100644 source/utils/bpls2/BPLS2.cpp
 create mode 100644 source/utils/bpls2/BPLS2.h
 create mode 100644 source/utils/bpls2/main.cpp

diff --git a/bindings/fortran/CMakeLists.txt b/bindings/fortran/CMakeLists.txt
index 89b2de128..6107f1fcd 100644
--- a/bindings/fortran/CMakeLists.txt
+++ b/bindings/fortran/CMakeLists.txt
@@ -32,7 +32,7 @@ else()
 endif()
 
 install(
-  TARGETS ${adios2_f} EXPORT adios2
+  TARGETS adios2_f EXPORT adios2
   RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt
index 9a02a7534..06132c61d 100644
--- a/source/CMakeLists.txt
+++ b/source/CMakeLists.txt
@@ -8,6 +8,7 @@ if(ADIOS2_HAVE_DataMan)
 endif()
 
 add_subdirectory(adios2)
+add_subdirectory(utils)
 
 install(
   FILES adios2.h
diff --git a/source/adios2/utils/bpls2/BPLS2.h b/source/adios2/utils/bpls2/BPLS2.h
deleted file mode 100644
index 84834c584..000000000
--- a/source/adios2/utils/bpls2/BPLS2.h
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Distributed under the OSI-approved Apache License, Version 2.0.  See
- * accompanying file Copyright.txt for details.
- *
- * bpls2.h
- *
- *  Created on: Oct 5, 2017
- *      Author: William F Godoy godoywf@ornl.gov
- */
-
-#ifndef ADIOS2_UTILS_BPLS2_BPLS2_H_
-#define ADIOS2_UTILS_BPLS2_BPLS2_H_
-
-namespace adios2
-{
-namespace utils
-{
-
-} // end namespace utils
-} // end namespace adios2
-
-#endif /* ADIOS2_UTILS_BPLS2_BPLS2_H_ */
diff --git a/source/adios2/utils/bpls2/main.cpp b/source/adios2/utils/bpls2/main.cpp
deleted file mode 100644
index 7ed35c9a4..000000000
--- a/source/adios2/utils/bpls2/main.cpp
+++ /dev/null
@@ -1,13 +0,0 @@
-/*
- * Distributed under the OSI-approved Apache License, Version 2.0.  See
- * accompanying file Copyright.txt for details.
- *
- * bpls2Main.cpp
- *
- *  Created on: Oct 5, 2017
- *      Author: William F Godoy godoywf@ornl.gov
- */
-
-#include "adios2/utils/bpls2/BPLS2.h"
-
-int main(int argc, char *argv[]) { return 0; }
diff --git a/source/utils/CMakeLists.txt b/source/utils/CMakeLists.txt
new file mode 100644
index 000000000..8bf3f4090
--- /dev/null
+++ b/source/utils/CMakeLists.txt
@@ -0,0 +1,13 @@
+#------------------------------------------------------------------------------#
+# Distributed under the OSI-approved Apache License, Version 2.0.  See
+# accompanying file Copyright.txt for details.
+#------------------------------------------------------------------------------#
+
+#BPLS2
+add_executable(bpls2 ./bpls2/main.cpp ./bpls2/BPLS2.cpp Utils.cpp)
+
+target_link_libraries(bpls2 PRIVATE adios2)
+
+install(TARGETS bpls2
+  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+)
diff --git a/source/adios2/utils/bpls2/BPLS2.cpp b/source/utils/Utils.cpp
similarity index 57%
rename from source/adios2/utils/bpls2/BPLS2.cpp
rename to source/utils/Utils.cpp
index 773a1d2ce..011ae8876 100644
--- a/source/adios2/utils/bpls2/BPLS2.cpp
+++ b/source/utils/Utils.cpp
@@ -2,15 +2,20 @@
  * Distributed under the OSI-approved Apache License, Version 2.0.  See
  * accompanying file Copyright.txt for details.
  *
- * BPLS2.cpp
+ * Utils.cpp
  *
- *  Created on: Oct 5, 2017
+ *  Created on: Oct 24, 2017
  *      Author: William F Godoy godoywf@ornl.gov
  */
 
+#include "Utils.h"
+
 namespace adios2
 {
-namespace utils
+
+Utils::Utils(const std::string name, int argc, char *argv[])
+: m_Name(name), m_Arguments(argv, argv + argc)
 {
 }
+
 } // end namespace adios2
diff --git a/source/utils/Utils.h b/source/utils/Utils.h
new file mode 100644
index 000000000..d17320830
--- /dev/null
+++ b/source/utils/Utils.h
@@ -0,0 +1,47 @@
+/*
+ * Distributed under the OSI-approved Apache License, Version 2.0.  See
+ * accompanying file Copyright.txt for details.
+ *
+ * Utils.h
+ *
+ *  Created on: Oct 24, 2017
+ *      Author: William F Godoy godoywf@ornl.gov
+ */
+
+#ifndef SOURCE_UTILS_UTILS_H_
+#define SOURCE_UTILS_UTILS_H_
+
+#include <string>
+#include <vector>
+
+#include "adios2/ADIOSTypes.h"
+
+namespace adios2
+{
+
+class Utils
+{
+public:
+    const std::string m_Name;
+
+    Utils(const std::string name, int argc, char *argv[]);
+
+    virtual ~Utils() = default;
+
+    virtual void Run() = 0;
+
+protected:
+    const std::vector<std::string> m_Arguments;
+    Params m_Parameters;
+
+    virtual void ParseArguments() = 0;
+    virtual void ProcessParameters() const = 0;
+    virtual void PrintUsage() const noexcept = 0;
+    virtual void PrintExamples() const noexcept = 0;
+    virtual void SetParameters(const std::string argument,
+                               const bool isLong) = 0;
+};
+
+} /// end namespace adios2
+
+#endif /* SOURCE_UTILS_UTILS_H_ */
diff --git a/source/utils/bpls2/BPLS2.cpp b/source/utils/bpls2/BPLS2.cpp
new file mode 100644
index 000000000..00e87201a
--- /dev/null
+++ b/source/utils/bpls2/BPLS2.cpp
@@ -0,0 +1,179 @@
+/*
+ * Distributed under the OSI-approved Apache License, Version 2.0.  See
+ * accompanying file Copyright.txt for details.
+ *
+ * BPLS2.cpp
+ *
+ *  Created on: Oct 5, 2017
+ *      Author: William F Godoy godoywf@ornl.gov
+ */
+
+#include "BPLS2.h"
+
+#include <iostream>
+
+namespace adios2
+{
+namespace utils
+{
+
+const std::string BPLS2::m_HelpMessage = "For usage run either:\n"
+                                         "\t bpls2 --help\n"
+                                         "\t bpls2 -h \n";
+
+const std::map<std::string, std::string> BPLS2::m_Options = {
+    {"long", "l"}, {"attrs", "a"},   {"attrsonly", "A"},
+    {"dump", "d"}, {"verbose", "v"}, {"help", "h"}};
+
+BPLS2::BPLS2(int argc, char *argv[]) : Utils("bpls2", argc, argv) {}
+
+void BPLS2::Run()
+{
+    ParseArguments();
+    ProcessParameters();
+}
+
+// PRIVATE
+void BPLS2::ParseArguments()
+{
+    if (m_Arguments.size() == 1)
+    {
+        throw std::invalid_argument("ERROR: Missing bpfile\n" + m_HelpMessage);
+    }
+
+    bool isFileSet = false;
+
+    for (auto itArg = m_Arguments.begin() + 1; itArg != m_Arguments.end();
+         ++itArg)
+    {
+        if (itArg->find("--") == 0) // long  argument
+        {
+            const std::string argument(itArg->substr(2));
+            if (argument.size() == 1)
+            {
+                throw std::invalid_argument(
+                    "ERROR: unknown single character option, did you mean -" +
+                    argument + " ?\n");
+            }
+            SetParameters(argument, true);
+        }
+        else if (itArg->find("-") == 0) // short argument
+        {
+            const std::string argument(itArg->substr(1));
+            SetParameters(argument, false);
+            if (argument == "s" || argument == "c")
+            {
+                ++itArg;
+                // SetParameters(*itArg);
+            }
+        }
+        else
+        {
+            if (isFileSet)
+            {
+                throw std::invalid_argument(
+                    "ERROR: only 1 bpfile is allowed\n" + m_HelpMessage);
+            }
+
+            m_FileName = *itArg;
+            isFileSet = true;
+        }
+    }
+}
+
+void BPLS2::ProcessParameters() const
+{
+    if (m_Parameters.count("help") == 1)
+    {
+        PrintUsage();
+        if (m_Parameters.size() > 1)
+        {
+            std::cout << "\n";
+            std::cout << "Found --help , -h option, discarding others\n";
+            std::cout << "Rerun without --help , -h option\n";
+        }
+        return;
+    }
+
+    if (m_FileName.empty())
+    {
+        throw std::invalid_argument("ERROR: file not passed to bpls2\n" +
+                                    m_HelpMessage);
+    }
+}
+
+void BPLS2::PrintUsage() const noexcept
+{
+    std::cout << "This is ADIOS2 binary pack listing (bpls2) utility. Usage:\n";
+    std::cout << "\t bpls2 [OPTIONS] bpfile [MASK1 MASK2 ...]\n";
+    std::cout << "\n";
+    std::cout << "[OPTIONS]:\n";
+    std::cout << "\n";
+    std::cout << "-l , --long        Print variables and attributes metadata\n";
+    std::cout << "                   information, no overhead\n";
+    std::cout << "-a , --attributes  List attributes metadata\n";
+
+    std::cout << "Example: bpls2 -lav bpfile\n";
+}
+
+void BPLS2::PrintExamples() const noexcept {}
+
+void BPLS2::SetParameters(const std::string argument, const bool isLong)
+{
+    if (argument == "-" || argument.empty())
+    {
+        throw std::invalid_argument("ERROR: invalid argument: -" + argument +
+                                    "\n");
+    }
+
+    bool isOption = false;
+
+    if (m_Options.count(argument) == 1 && isLong)
+    {
+        isOption = true;
+        m_Parameters[argument] = "";
+    }
+    else if (!isLong)
+    {
+        for (const auto &optionPair : m_Options)
+        {
+            if (argument == optionPair.second)
+            {
+                isOption = true;
+                m_Parameters[optionPair.first] = "";
+                break;
+            }
+        }
+    }
+
+    if (isOption)
+    {
+        return;
+    }
+
+    // look for multiple options by character
+    for (const char argChar : argument)
+    {
+        const std::string argCharString(1, argChar);
+        isOption = false;
+
+        for (const auto &optionPair : m_Options)
+        {
+            if (argCharString == optionPair.second)
+            {
+                m_Parameters[optionPair.first] = "";
+                isOption = true;
+            }
+        }
+
+        if (!isOption)
+        {
+            throw std::invalid_argument("ERROR: unknown option " +
+                                        argCharString + " in argument " +
+                                        argument + "\n");
+        }
+    }
+}
+
+} // end namespace utils
+} // end namespace adios2
diff --git a/source/utils/bpls2/BPLS2.h b/source/utils/bpls2/BPLS2.h
new file mode 100644
index 000000000..37b2e67a2
--- /dev/null
+++ b/source/utils/bpls2/BPLS2.h
@@ -0,0 +1,46 @@
+/*
+ * Distributed under the OSI-approved Apache License, Version 2.0.  See
+ * accompanying file Copyright.txt for details.
+ *
+ * bpls2.h
+ *
+ *  Created on: Oct 5, 2017
+ *      Author: William F Godoy godoywf@ornl.gov
+ */
+
+#ifndef UTILS_BPLS2_BPLS2_H_
+#define UTILS_BPLS2_BPLS2_H_
+
+#include "utils/Utils.h"
+
+namespace adios2
+{
+namespace utils
+{
+
+class BPLS2 : public Utils
+{
+public:
+    BPLS2(int argc, char *argv[]);
+
+    ~BPLS2() = default;
+
+    void Run() final;
+
+private:
+    static const std::string m_HelpMessage;
+    static const std::map<std::string, std::string> m_Options;
+
+    std::string m_FileName;
+
+    void ParseArguments() final;
+    void ProcessParameters() const final;
+    void PrintUsage() const noexcept final;
+    void PrintExamples() const noexcept final;
+    void SetParameters(const std::string argument, const bool isLong) final;
+};
+
+} // end namespace utils
+} // end namespace adios2
+
+#endif /* UTILS_BPLS2_BPLS2_H_ */
diff --git a/source/utils/bpls2/main.cpp b/source/utils/bpls2/main.cpp
new file mode 100644
index 000000000..c9fe8694b
--- /dev/null
+++ b/source/utils/bpls2/main.cpp
@@ -0,0 +1,28 @@
+/*
+ * Distributed under the OSI-approved Apache License, Version 2.0.  See
+ * accompanying file Copyright.txt for details.
+ *
+ * bpls2Main.cpp
+ *
+ *  Created on: Oct 5, 2017
+ *      Author: William F Godoy godoywf@ornl.gov
+ */
+
+#include <iostream>
+#include <stdexcept>
+
+#include "utils/bpls2/BPLS2.h"
+
+int main(int argc, char *argv[])
+{
+    try
+    {
+        adios2::utils::BPLS2 bpls2(argc, argv);
+        bpls2.Run();
+    }
+    catch (std::exception &e)
+    {
+        std::cout << "bpls2 Caught an Exception\n";
+        std::cout << e.what() << "\n";
+    }
+}
-- 
GitLab