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:
Baldomo
2020-12-26 17:05:47 +01:00
parent 9fedb03ceb
commit 99b9474f3b
3 changed files with 84 additions and 78 deletions

View File

@@ -1,5 +1,4 @@
#ifndef OIM_IPC_HPP_
#define OIM_IPC_HPP_
#pragma once
#include <cstring>
#include <string>
@@ -56,6 +55,4 @@ bool ipc::send(string cmd) {
return write(sockfd_, cmd.c_str(), cmd.length()) != -1;
}
} // namespace oim
#endif
} // namespace oim

View File

@@ -1,5 +1,4 @@
#ifndef OIM_OPTS_HPP_
#define OIM_OPTS_HPP_
#pragma once
#include "url.hpp"
@@ -27,18 +26,33 @@ class options {
bool new_window_;
public:
/*
* Constructor for oim::options
*/
options();
/*
* Builds a CLI command used to invoke mpv with the appropriate arguments
*/
string build_cmd();
/*
* Builds the IPC command needed to enqueue videos in mpv
*/
string build_ipc();
/*
* Parse a URL and populate the current oim::options
*/
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();
};
/*
* Constructor for oim::options
*/
options::options() {
url_ = "";
flags_ = "";
@@ -48,9 +62,6 @@ options::options() {
enqueue_ = false;
}
/*
* Builds a CLI command used to invoke mpv with the appropriate arguments
*/
string options::build_cmd() {
std::ostringstream ret;
@@ -71,9 +82,6 @@ string options::build_cmd() {
return ret.str();
}
/*
* Builds the IPC command needed to enqueue videos in mpv
*/
string options::build_ipc() {
std::ostringstream ret;
@@ -89,9 +97,6 @@ string options::build_ipc() {
return ret.str();
}
/*
* Parse a URL and populate the current oim::options
*/
void options::parse(const char *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";
}
/*
* Checks wether or not oim::options needs to communicate with mpv via IPC
* instead of the command line interface
*/
bool options::needs_ipc() {
// For now this is needed only when queuing videos
return enqueue_;
}
} // namespace oim
#endif
} // namespace oim

View File

@@ -1,5 +1,4 @@
#ifndef OIM_URL_HPP_
#define OIM_URL_HPP_
#pragma once
#include <algorithm>
#include <cstddef>
@@ -9,6 +8,7 @@
using std::string;
namespace {
/*
* Converts a single character to a percent-decodable byte representation
* Taken from
@@ -30,6 +30,8 @@ inline std::byte alnum_to_hex(char value) {
return static_cast<std::byte>(' ');
}
} // namespace
namespace oim {
/*
@@ -46,28 +48,7 @@ class url {
url(const char *url_s) : url(string(url_s)){};
/* Constructor with C++ std::string 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));
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());
};
url(const string &url_s);
/* Move constructor for oim::url */
url(url &&other) {
@@ -92,36 +73,67 @@ class url {
/*
* Gets a value from a query string given a key
*/
string query_value(string key) {
// Find the beginning of the last occurrence of `key` in `query`
auto pos = query_.rfind(key + "=");
if (pos == string::npos)
return "";
// Offset calculation (beginning of the value string associated with
// `key`):
// pos: positione of the first character of `key`
// key.length(): self explanatory
// 1: length of character '='
int offset = pos + key.length() + 1;
// Return a string starting from the offset and with appropriate length
// (difference between the position of the first '&' char after the
// value and `offset`)
return query_.substr(offset, query_.find('&', pos) - offset);
}
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) {
string ret = query_value(key);
if (ret.empty())
return fallback;
return ret;
}
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`
auto pos = query_.rfind(key + "=");
if (pos == string::npos)
return "";
// Offset calculation (beginning of the value string associated with
// `key`):
// pos: positione of the first character of `key`
// key.length(): self explanatory
// 1: length of character '='
int offset = pos + key.length() + 1;
// Return a string starting from the offset and with appropriate length
// (difference between the position of the first '&' char after the
// value and `offset`)
return query_.substr(offset, query_.find('&', pos) - offset);
}
string url::query_value(string key, string fallback) {
string ret = query_value(key);
if (ret.empty())
return fallback;
return ret;
}
/*
* Percent-decodes a URL
*/
@@ -129,8 +141,8 @@ string url_decode(const string encoded) {
string ret = "";
for (auto i = encoded.begin(); i < encoded.end(); i++) {
if (*i == '%') {
std::byte b1 = alnum_to_hex(*++i);
std::byte b2 = alnum_to_hex(*++i);
std::byte b1 = ::alnum_to_hex(*++i);
std::byte b2 = ::alnum_to_hex(*++i);
char parsed =
static_cast<char>((0x10u * std::to_integer<unsigned int>(b1)) +
@@ -144,6 +156,4 @@ string url_decode(const string encoded) {
return ret;
}
} // namespace oim
#endif
} // namespace oim