summaryrefslogtreecommitdiff
path: root/tools/minicargo/helpers.h
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2017-08-19 17:37:07 +0800
committerJohn Hodge <tpg@mutabah.net>2017-08-19 17:39:08 +0800
commita3ca810d74c09235cbd501f606ed1b979691aba3 (patch)
tree50b94b73cd49849c7bab9bde821ba41d8c9d3f62 /tools/minicargo/helpers.h
parent42f772e01d1cae1fa774bb0b670670dbefa813ea (diff)
downloadmrust-a3ca810d74c09235cbd501f606ed1b979691aba3.tar.gz
minicargo - Draft implementation, spawns mrustc on windows
Diffstat (limited to 'tools/minicargo/helpers.h')
-rw-r--r--tools/minicargo/helpers.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/tools/minicargo/helpers.h b/tools/minicargo/helpers.h
new file mode 100644
index 00000000..d811f21a
--- /dev/null
+++ b/tools/minicargo/helpers.h
@@ -0,0 +1,93 @@
+#pragma once
+
+#include <string>
+
+namespace helpers {
+
+
+/// Path helper class (because I don't want to include boost)
+class path
+{
+#ifdef _WIN32
+ const char SEP = '\\';
+#else
+ const char SEP = '/';
+#endif
+
+ ::std::string m_str;
+
+ path()
+ {
+ }
+public:
+ path(const ::std::string& s):
+ path(s.c_str())
+ {
+ }
+ path(const char* s):
+ m_str(s)
+ {
+ // 1. Normalise path separators to the system specified separator
+ for(size_t i = 0; i < m_str.size(); i ++)
+ {
+ if( m_str[i] == '/' || m_str[i] == '\\' )
+ m_str[i] = SEP;
+ }
+
+ // 2. Remove any trailing separators
+ if( !m_str.empty() )
+ {
+ while(!m_str.empty() && m_str.back() == SEP )
+ m_str.pop_back();
+ if(m_str.empty())
+ {
+ m_str.push_back(SEP);
+ }
+ }
+ else
+ {
+ throw ::std::runtime_error("Empty path being constructed");
+ }
+ }
+
+ path operator/(const path& p) const
+ {
+ if(p.m_str[0] == '/')
+ throw ::std::runtime_error("Appending an absolute path to another path");
+ return *this / p.m_str.c_str();
+ }
+ path operator/(const char* o) const
+ {
+ auto rv = *this;
+ rv.m_str.push_back(SEP);
+ rv.m_str.append(o);
+ return rv;
+ }
+
+ path parent() const
+ {
+ auto pos = m_str.find_last_of(SEP);
+ if(pos == ::std::string::npos)
+ {
+ return *this;
+ }
+ else
+ {
+ path rv;
+ rv.m_str = m_str.substr(0, pos);
+ return rv;
+ }
+ }
+
+ operator ::std::string() const
+ {
+ return m_str;
+ }
+
+ friend ::std::ostream& operator<<(::std::ostream& os, const path& p)
+ {
+ return os << p.m_str;
+ }
+};
+
+} // namespace helpers \ No newline at end of file