Moved alnum_to_hex to own unnamed namespace, moved documentation comments to class definitions and function bodies outside of definitions, reformatted code
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
#ifndef OIM_IPC_HPP_
|
#pragma once
|
||||||
#define OIM_IPC_HPP_
|
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -57,5 +56,3 @@ bool ipc::send(string cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace oim
|
} // namespace oim
|
||||||
|
|
||||||
#endif
|
|
@@ -1,5 +1,4 @@
|
|||||||
#ifndef OIM_OPTS_HPP_
|
#pragma once
|
||||||
#define OIM_OPTS_HPP_
|
|
||||||
|
|
||||||
#include "url.hpp"
|
#include "url.hpp"
|
||||||
|
|
||||||
@@ -27,18 +26,33 @@ class options {
|
|||||||
bool new_window_;
|
bool new_window_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/*
|
||||||
|
* Constructor for oim::options
|
||||||
|
*/
|
||||||
options();
|
options();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Builds a CLI command used to invoke mpv with the appropriate arguments
|
||||||
|
*/
|
||||||
string build_cmd();
|
string build_cmd();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Builds the IPC command needed to enqueue videos in mpv
|
||||||
|
*/
|
||||||
string build_ipc();
|
string build_ipc();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Parse a URL and populate the current oim::options
|
||||||
|
*/
|
||||||
void parse(const char *url);
|
void parse(const char *url);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Checks wether or not oim::options needs to communicate with mpv via IPC
|
||||||
|
* instead of the command line interface
|
||||||
|
*/
|
||||||
bool needs_ipc();
|
bool needs_ipc();
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* Constructor for oim::options
|
|
||||||
*/
|
|
||||||
options::options() {
|
options::options() {
|
||||||
url_ = "";
|
url_ = "";
|
||||||
flags_ = "";
|
flags_ = "";
|
||||||
@@ -48,9 +62,6 @@ options::options() {
|
|||||||
enqueue_ = false;
|
enqueue_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Builds a CLI command used to invoke mpv with the appropriate arguments
|
|
||||||
*/
|
|
||||||
string options::build_cmd() {
|
string options::build_cmd() {
|
||||||
std::ostringstream ret;
|
std::ostringstream ret;
|
||||||
|
|
||||||
@@ -71,9 +82,6 @@ string options::build_cmd() {
|
|||||||
return ret.str();
|
return ret.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Builds the IPC command needed to enqueue videos in mpv
|
|
||||||
*/
|
|
||||||
string options::build_ipc() {
|
string options::build_ipc() {
|
||||||
std::ostringstream ret;
|
std::ostringstream ret;
|
||||||
|
|
||||||
@@ -89,9 +97,6 @@ string options::build_ipc() {
|
|||||||
return ret.str();
|
return ret.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Parse a URL and populate the current oim::options
|
|
||||||
*/
|
|
||||||
void options::parse(const char *url_s) {
|
void options::parse(const char *url_s) {
|
||||||
oim::url u(url_s);
|
oim::url u(url_s);
|
||||||
|
|
||||||
@@ -114,15 +119,9 @@ void options::parse(const char *url_s) {
|
|||||||
new_window_ = u.query_value("new_window") == "1";
|
new_window_ = u.query_value("new_window") == "1";
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* Checks wether or not oim::options needs to communicate with mpv via IPC
|
|
||||||
* instead of the command line interface
|
|
||||||
*/
|
|
||||||
bool options::needs_ipc() {
|
bool options::needs_ipc() {
|
||||||
// For now this is needed only when queuing videos
|
// For now this is needed only when queuing videos
|
||||||
return enqueue_;
|
return enqueue_;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace oim
|
} // namespace oim
|
||||||
|
|
||||||
#endif
|
|
84
src/url.hpp
84
src/url.hpp
@@ -1,5 +1,4 @@
|
|||||||
#ifndef OIM_URL_HPP_
|
#pragma once
|
||||||
#define OIM_URL_HPP_
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@@ -9,6 +8,7 @@
|
|||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
namespace {
|
||||||
/*
|
/*
|
||||||
* Converts a single character to a percent-decodable byte representation
|
* Converts a single character to a percent-decodable byte representation
|
||||||
* Taken from
|
* Taken from
|
||||||
@@ -30,6 +30,8 @@ inline std::byte alnum_to_hex(char value) {
|
|||||||
return static_cast<std::byte>(' ');
|
return static_cast<std::byte>(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
namespace oim {
|
namespace oim {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -46,28 +48,7 @@ class url {
|
|||||||
url(const char *url_s) : url(string(url_s)){};
|
url(const char *url_s) : url(string(url_s)){};
|
||||||
|
|
||||||
/* Constructor with C++ std::string URL */
|
/* Constructor with C++ std::string URL */
|
||||||
url(const string &url_s) {
|
url(const string &url_s);
|
||||||
const string prot_end("://");
|
|
||||||
string::const_iterator prot_i = std::search(
|
|
||||||
url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end());
|
|
||||||
protocol_.reserve(std::distance(url_s.begin(), prot_i));
|
|
||||||
std::transform(url_s.begin(), prot_i, std::back_inserter(protocol_),
|
|
||||||
std::ptr_fun<int, int>(tolower)); // protocol is icase
|
|
||||||
if (prot_i == url_s.end())
|
|
||||||
return;
|
|
||||||
std::advance(prot_i, prot_end.length());
|
|
||||||
|
|
||||||
string::const_iterator path_i = std::find(prot_i, url_s.end(), '/');
|
|
||||||
host_.reserve(std::distance(prot_i, path_i));
|
|
||||||
std::transform(prot_i, path_i, std::back_inserter(host_),
|
|
||||||
std::ptr_fun<int, int>(tolower)); // host is icase
|
|
||||||
|
|
||||||
string::const_iterator query_i = std::find(path_i, url_s.end(), '?');
|
|
||||||
path_.assign(path_i, query_i);
|
|
||||||
if (query_i != url_s.end())
|
|
||||||
++query_i;
|
|
||||||
query_.assign(query_i, url_s.end());
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Move constructor for oim::url */
|
/* Move constructor for oim::url */
|
||||||
url(url &&other) {
|
url(url &&other) {
|
||||||
@@ -92,7 +73,43 @@ class url {
|
|||||||
/*
|
/*
|
||||||
* Gets a value from a query string given a key
|
* Gets a value from a query string given a key
|
||||||
*/
|
*/
|
||||||
string query_value(string key) {
|
string query_value(string key);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Gets a value from a query string given a key (overload with optional
|
||||||
|
* fallback if value isn't found)
|
||||||
|
*/
|
||||||
|
string query_value(string key, string fallback);
|
||||||
|
};
|
||||||
|
|
||||||
|
url::url(const string &url_s) {
|
||||||
|
const string prot_end("://");
|
||||||
|
string::const_iterator prot_i = std::search(
|
||||||
|
url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end());
|
||||||
|
protocol_.reserve(std::distance(url_s.begin(), prot_i));
|
||||||
|
// The protocol is case insensitive
|
||||||
|
std::transform(url_s.begin(), prot_i, std::back_inserter(protocol_),
|
||||||
|
std::ptr_fun<int, int>(tolower));
|
||||||
|
if (prot_i == url_s.end())
|
||||||
|
return;
|
||||||
|
std::advance(prot_i, prot_end.length());
|
||||||
|
|
||||||
|
// The path starts with '/'
|
||||||
|
string::const_iterator path_i = std::find(prot_i, url_s.end(), '/');
|
||||||
|
host_.reserve(std::distance(prot_i, path_i));
|
||||||
|
// The host is also case insensitive
|
||||||
|
std::transform(prot_i, path_i, std::back_inserter(host_),
|
||||||
|
std::ptr_fun<int, int>(tolower));
|
||||||
|
|
||||||
|
// Everything else is query
|
||||||
|
string::const_iterator query_i = std::find(path_i, url_s.end(), '?');
|
||||||
|
path_.assign(path_i, query_i);
|
||||||
|
if (query_i != url_s.end())
|
||||||
|
++query_i;
|
||||||
|
query_.assign(query_i, url_s.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
string url::query_value(string key) {
|
||||||
// Find the beginning of the last occurrence of `key` in `query`
|
// Find the beginning of the last occurrence of `key` in `query`
|
||||||
auto pos = query_.rfind(key + "=");
|
auto pos = query_.rfind(key + "=");
|
||||||
if (pos == string::npos)
|
if (pos == string::npos)
|
||||||
@@ -108,19 +125,14 @@ class url {
|
|||||||
// (difference between the position of the first '&' char after the
|
// (difference between the position of the first '&' char after the
|
||||||
// value and `offset`)
|
// value and `offset`)
|
||||||
return query_.substr(offset, query_.find('&', pos) - offset);
|
return query_.substr(offset, query_.find('&', pos) - offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
string url::query_value(string key, string fallback) {
|
||||||
* Gets a value from a query string given a key (overload with optional
|
|
||||||
* fallback if value isn't found)
|
|
||||||
*/
|
|
||||||
string query_value(string key, string fallback) {
|
|
||||||
string ret = query_value(key);
|
string ret = query_value(key);
|
||||||
if (ret.empty())
|
if (ret.empty())
|
||||||
return fallback;
|
return fallback;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Percent-decodes a URL
|
* Percent-decodes a URL
|
||||||
@@ -129,8 +141,8 @@ string url_decode(const string encoded) {
|
|||||||
string ret = "";
|
string ret = "";
|
||||||
for (auto i = encoded.begin(); i < encoded.end(); i++) {
|
for (auto i = encoded.begin(); i < encoded.end(); i++) {
|
||||||
if (*i == '%') {
|
if (*i == '%') {
|
||||||
std::byte b1 = alnum_to_hex(*++i);
|
std::byte b1 = ::alnum_to_hex(*++i);
|
||||||
std::byte b2 = alnum_to_hex(*++i);
|
std::byte b2 = ::alnum_to_hex(*++i);
|
||||||
|
|
||||||
char parsed =
|
char parsed =
|
||||||
static_cast<char>((0x10u * std::to_integer<unsigned int>(b1)) +
|
static_cast<char>((0x10u * std::to_integer<unsigned int>(b1)) +
|
||||||
@@ -145,5 +157,3 @@ string url_decode(const string encoded) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace oim
|
} // namespace oim
|
||||||
|
|
||||||
#endif
|
|
Reference in New Issue
Block a user