[cig-commits] r8932 - cs/benchmark/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Wed Dec 19 12:04:52 PST 2007


Author: luis
Date: 2007-12-19 12:04:52 -0800 (Wed, 19 Dec 2007)
New Revision: 8932

Added:
   cs/benchmark/cigma/trunk/src/StringUtils.cpp
   cs/benchmark/cigma/trunk/src/StringUtils.h
Log:
Various string utilities for conversions, or path manipulations

Added: cs/benchmark/cigma/trunk/src/StringUtils.cpp
===================================================================
--- cs/benchmark/cigma/trunk/src/StringUtils.cpp	2007-12-19 20:04:43 UTC (rev 8931)
+++ cs/benchmark/cigma/trunk/src/StringUtils.cpp	2007-12-19 20:04:52 UTC (rev 8932)
@@ -0,0 +1,151 @@
+#include <string>
+#include <vector>
+#include <cassert>
+#include "StringUtils.h"
+#include "convert.h"
+
+
+void string_to_int(const std::string &str, int &val)
+{
+    try
+    {
+        val = convertTo<int>(str);
+    }
+    catch(BadConversion &exc)
+    {
+        val = 0;
+    }
+}
+
+
+void string_to_double(const std::string &str, double &val)
+{
+    try
+    {
+        val = convertTo<double>(str);
+    }
+    catch(BadConversion &exc)
+    {
+        val = 0.0;
+    }
+}
+
+
+void string_to_paths(const std::string &paths,
+                     std::vector<std::string> &filepaths)
+{
+    filepaths.clear();
+    string_tokenize(paths, filepaths, ":");
+}
+
+
+void string_tokenize(const std::string &str,
+                     std::vector<std::string> &tokens,
+                     const std::string &delimiters = " ")
+{
+    // Skip delimiters at beginning
+    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+
+    // Find first "non-delimiter"
+    std::string::size_type pos = str.find_first_of(delimiters, lastPos);
+
+    while (std::string::npos != pos || std::string::npos != lastPos)
+    {
+        // Found a token, add it to the vector
+        tokens.push_back(str.substr(lastPos, pos-lastPos));
+
+        // Skip delimiters. Note the "not_of"
+        lastPos = str.find_first_not_of(delimiters, pos);
+
+        // Find next "non-delimiter"
+        pos = str.find_first_of(delimiters, lastPos);
+    }
+}
+
+
+void path_dirname(const std::string &filepath,
+                  std::string &dirname)
+{
+    std::string::size_type lastSep = filepath.find_last_of("/");
+    if (lastSep != std::string::npos)
+    {
+        dirname = filepath.substr(0, lastSep + 1);
+    }
+    else
+    {
+        dirname = "";
+    }
+}
+
+
+void path_basename(const std::string &filepath,
+                   std::string &basename)
+{
+    std::string::size_type lastSep = filepath.find_last_of("/");
+    basename = filepath.substr(lastSep + 1);
+}
+
+
+void path_splitext(const std::string &filepath,
+                   std::string &root,
+                   std::string &ext)
+{
+    std::string::size_type lastDot = filepath.find_last_of(".");
+    std::string::size_type lastSep = filepath.find_last_of("/");
+
+    if ((lastDot != std::string::npos) && (lastSep < lastDot))
+    {
+        root = filepath.substr(0, lastDot);
+        ext = filepath.substr(lastDot);
+    }
+    else
+    {
+        root = filepath;
+        ext = "";
+    }
+}
+
+
+int parse_path_pair(const std::string &str,
+                    std::string &path1,
+                    std::string &path2)
+{
+    std::vector<std::string> paths;
+
+    string_to_paths(str, paths);
+
+    assert(paths.size() > 0);
+
+    if (paths.size() == 1)
+    {
+        path1 = str;
+        path2 = "";
+        return 2;
+    }
+    else if (paths.size() == 2)
+    {
+        std::vector<std::string>::iterator it;
+        it = paths.begin();
+        path1 = *it;
+        ++it;
+        path2 = *it;
+        return 2;
+    }
+    else
+    {
+        path1 = "";
+        path2 = "";
+    }
+    return paths.size();
+}
+
+void parse_dataset_path(const std::string &str,
+                        std::string &dataset,
+                        std::string &filepath,
+                        std::string &extension)
+{
+    std::string fileroot;
+    std::vector<std::string> paths;
+    parse_path_pair(str, filepath, dataset);
+    path_splitext(filepath, fileroot, extension);
+}

Added: cs/benchmark/cigma/trunk/src/StringUtils.h
===================================================================
--- cs/benchmark/cigma/trunk/src/StringUtils.h	2007-12-19 20:04:43 UTC (rev 8931)
+++ cs/benchmark/cigma/trunk/src/StringUtils.h	2007-12-19 20:04:52 UTC (rev 8932)
@@ -0,0 +1,47 @@
+#ifndef __STRING_UTILS_H__
+#define __STRING_UTILS_H__
+
+#include <string>
+#include <vector>
+
+
+/*
+ * string functions
+ */
+
+void string_to_int(const std::string &str, int &val);
+
+void string_to_double(const std::string &str, double &val);
+
+void string_to_paths(const std::string &paths,
+                     std::vector<std::string> &filepaths);
+
+void string_tokenize(const std::string &str,
+                     std::vector<std::string> &tokens,
+                     const std::string &delimiters);
+
+
+/*
+ * path functions
+ */
+
+void path_dirname(const std::string &filepath,
+                  std::string &dirname);
+
+void path_basename(const std::string &filepath,
+                   std::string &basename);
+
+void path_splitext(const std::string &filepath,
+                   std::string &root,
+                   std::string &ext);
+
+int parse_path_pair(const std::string &str,
+                    std::string &path1,
+                    std::string &path2);
+
+void parse_dataset_path(const std::string &str,
+                        std::string &dataset,
+                        std::string &filepath,
+                        std::string &extension);
+
+#endif



More information about the cig-commits mailing list