[cig-commits] r12955 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Sep 24 04:10:45 PDT 2008


Author: luis
Date: 2008-09-24 04:10:45 -0700 (Wed, 24 Sep 2008)
New Revision: 12955

Added:
   cs/cigma/trunk/src/DataPath.cpp
   cs/cigma/trunk/src/DataPath.h
Log:
Refactor access to file/data

Added: cs/cigma/trunk/src/DataPath.cpp
===================================================================
--- cs/cigma/trunk/src/DataPath.cpp	                        (rev 0)
+++ cs/cigma/trunk/src/DataPath.cpp	2008-09-24 11:10:45 UTC (rev 12955)
@@ -0,0 +1,113 @@
+#include "DataPath.h"
+
+#include "Common.h"
+#include "Filesystem.h"
+#include <boost/algorithm/string.hpp>
+#include <vector>
+
+using namespace std;
+
+
+// ----------------------------------------------------------------------------
+
+static void colon_split(const string &s, vector<string> &ss)
+{
+    boost::split(ss, s, boost::is_any_of(":"), boost::token_compress_on);
+}
+
+
+// ----------------------------------------------------------------------------
+
+DataPath::DataPath(std::string p)
+{
+    if (p != "")
+    {
+        size_t colon = p.find(':');
+        if (colon != string::npos)
+        {
+            /*
+             * Split p into fname and dname.
+             */
+            vector<string> ps;
+            colon_split(p, ps);
+            if (ps.size() == 2)
+            {
+                fname = ps[0];
+                dname = ps[1];
+            }
+            else
+            {
+                throw cigma::Exception(string("dname"), string("Too many colons in path"));
+            }
+        }
+        else
+        {
+            /*
+             * If no colon character is present in p,
+             * then assume that p refers to a file path.
+             */
+            fname = p;
+            dname = "";
+        }
+    }
+}
+
+DataPath::DataPath(const DataPath &dp)
+{
+    fname = dp.fname;
+    dname = dp.dname;
+}
+
+DataPath::~DataPath()
+{
+}
+
+
+// ----------------------------------------------------------------------------
+
+bool DataPath::exists()
+{
+    return boost::filesystem::exists(fname);
+}
+
+bool DataPath::empty()
+{
+    return (fname == "") && (dname == "");
+}
+
+std::string DataPath::extension() const
+{
+    return boost::filesystem::extension(fname);
+}
+
+std::string DataPath::filename() const
+{
+    return fname;
+}
+
+std::string DataPath::location() const
+{
+    return dname;
+}
+
+void DataPath::set_filename(std::string fname)
+{
+    this->fname = fname;
+}
+
+void DataPath::set_location(std::string dname)
+{
+    this->dname = dname;
+}
+
+
+// ----------------------------------------------------------------------------
+
+std::ostream &operator<<(std::ostream &os, const DataPath &dp)
+{
+    os << "<" << dp.filename() << ":" << dp.location() << ">";
+    return os;
+}
+
+
+// ----------------------------------------------------------------------------

Added: cs/cigma/trunk/src/DataPath.h
===================================================================
--- cs/cigma/trunk/src/DataPath.h	                        (rev 0)
+++ cs/cigma/trunk/src/DataPath.h	2008-09-24 11:10:45 UTC (rev 12955)
@@ -0,0 +1,33 @@
+#ifndef __DATA_PATH_H__
+#define __DATA_PATH_H__
+
+#include <string>
+#include <iostream>
+
+class DataPath
+{
+public:
+    
+    DataPath(std::string p);
+    DataPath(const DataPath &dp);
+    ~DataPath();
+
+    bool exists();
+    bool empty();
+
+    std::string extension() const;
+    std::string filename() const;
+    std::string location() const;
+
+    void set_filename(std::string fname);
+    void set_location(std::string dname);
+
+public:
+    std::string fname;
+    std::string dname;
+};
+
+
+std::ostream &operator<<(std::ostream &os, const DataPath &dp);
+
+#endif



More information about the cig-commits mailing list