diff options
author | Vince Harron <vharron@google.com> | 2015-01-06 23:38:24 +0000 |
---|---|---|
committer | Vince Harron <vharron@google.com> | 2015-01-06 23:38:24 +0000 |
commit | 3218c0fb943a05ffeeeefaffaa0f7f64b2bb3858 (patch) | |
tree | a21b29c98bf6d46a5dabeb20d0dc94497c0bf50a /lldb/source/Utility/UriParser.cpp | |
parent | 009597d0489a61f2dee9a4739865cb0809d2bd2e (diff) | |
download | bcm5719-llvm-3218c0fb943a05ffeeeefaffaa0f7f64b2bb3858.tar.gz bcm5719-llvm-3218c0fb943a05ffeeeefaffaa0f7f64b2bb3858.zip |
Adds UriParser::Parse and unit tests
This can be used to parse URIs passed to 'platform connect'
Differential Revision: http://reviews.llvm.org/D6858
llvm-svn: 225317
Diffstat (limited to 'lldb/source/Utility/UriParser.cpp')
-rw-r--r-- | lldb/source/Utility/UriParser.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lldb/source/Utility/UriParser.cpp b/lldb/source/Utility/UriParser.cpp new file mode 100644 index 00000000000..bf1e601485b --- /dev/null +++ b/lldb/source/Utility/UriParser.cpp @@ -0,0 +1,58 @@ +//===-- UriParser.cpp -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "Utility/UriParser.h" + +// C Includes +#include <stdlib.h> + +// C++ Includes +// Other libraries and framework includes +// Project includes + +//---------------------------------------------------------------------- +// UriParser::Parse +//---------------------------------------------------------------------- +bool +UriParser::Parse(const char* uri, + std::string& scheme, + std::string& hostname, + int& port, + std::string& path + ) +{ + char scheme_buf[100] = {0}; + char hostname_buf[256] = {0}; + char port_buf[11] = {0}; // 10==strlen(2^32) + char path_buf[2049] = {'/', 0}; + + bool ok = false; + if (4==sscanf(uri, "%99[^:/]://%255[^/:]:%[^/]/%2047s", scheme_buf, hostname_buf, port_buf, path_buf+1)) { ok = true; } + else if (3==sscanf(uri, "%99[^:/]://%255[^/:]:%[^/]", scheme_buf, hostname_buf, port_buf)) { ok = true; } + else if (3==sscanf(uri, "%99[^:/]://%255[^/]/%2047s", scheme_buf, hostname_buf, path_buf+1)) { ok = true; } + else if (2==sscanf(uri, "%99[^:/]://%255[^/]", scheme_buf, hostname_buf)) { ok = true; } + + char* end = port_buf; + int port_tmp = strtoul(port_buf, &end, 10); + if (*end != 0) + { + // there are invalid characters in port_buf + return false; + } + + if (ok) + { + scheme.assign(scheme_buf); + hostname.assign(hostname_buf); + port = port_tmp; + path.assign(path_buf); + } + return ok; +} + |