Skip to content

Commit

Permalink
Organized source codes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiden committed Oct 14, 2024
1 parent 99cbf68 commit 5092cd5
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 160 deletions.
22 changes: 22 additions & 0 deletions src/projects/base/ovlibrary/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,25 @@ namespace ov
return static_cast<std::underlying_type_t<T>>(enum_value);
}
} // namespace ov

#define OV_DEFINE_SETTER(type, setter, member, extra_qualifier) \
void setter(const type &value) extra_qualifier \
{ \
member = value; \
}

#define OV_DEFINE_GETTER(type, getter, member, extra_qualifier) \
type getter() extra_qualifier \
{ \
return member; \
}

#define OV_DEFINE_CONST_GETTER(type, getter, member, extra_qualifier) \
const type &getter() const extra_qualifier \
{ \
return member; \
}

#define OV_DEFINE_SETTER_CONST_GETTER(type, setter, getter, member, extra_qualifier) \
OV_DEFINE_SETTER(type, setter, member, extra_qualifier) \
OV_DEFINE_CONST_GETTER(type, getter, member, extra_qualifier)
132 changes: 105 additions & 27 deletions src/projects/base/ovlibrary/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ namespace ov
return result_string;
}

void Url::SetPath(const ov::String &path)
{
_path = path;

// split <path> to /<app>/<stream>/<file> (4 tokens)
auto tokens = _path.Split("/");

switch (tokens.size())
{
default:
case 4:
_file = tokens[3];
[[fallthrough]];
case 3:
_stream = tokens[2];
[[fallthrough]];
case 2:
_app = tokens[1];
[[fallthrough]];
case 1:
case 0:
// Nothing to do
break;
}
}

std::shared_ptr<Url> Url::Parse(const ov::String &url)
{
auto object = std::make_shared<Url>();
Expand All @@ -139,31 +165,10 @@ namespace ov
object->_password = group_list["password"].GetValue();
object->_host = group_list["host"].GetValue();
object->_port = ov::Converter::ToUInt32(group_list["port"].GetValue());
object->_path = group_list["path"].GetValue();
object->SetPath(group_list["path"].GetValue());
object->_query_string = group_list["qs"].GetValue();
object->_has_query_string = (object->_query_string.IsEmpty() == false);

// split <path> to /<app>/<stream>/<file> (4 tokens)
auto tokens = object->_path.Split("/");

switch (tokens.size())
{
default:
case 4:
object->_file = tokens[3];
[[fallthrough]];
case 3:
object->_stream = tokens[2];
[[fallthrough]];
case 2:
object->_app = tokens[1];
[[fallthrough]];
case 1:
case 0:
// Nothing to do
break;
}

return object;
}

Expand Down Expand Up @@ -284,6 +289,83 @@ namespace ov
}
}

bool Url::HasQueryString() const
{
return _has_query_string;
}

const ov::String &Url::Query() const
{
ParseQueryIfNeeded();
return _query_string;
}

const bool Url::HasQueryKey(ov::String key) const
{
ParseQueryIfNeeded();
if (_query_map.find(key) == _query_map.end())
{
return false;
}

return true;
}

const ov::String Url::GetQueryValue(ov::String key) const
{
if (HasQueryKey(key) == false)
{
return "";
}

return Decode(_query_map[key]);
}

const std::map<ov::String, ov::String> &Url::QueryMap() const
{
ParseQueryIfNeeded();
return _query_map;
}

Url &Url::operator=(const Url &other) noexcept
{
_source = other._source;
_scheme = other._scheme;
_host = other._host;
_port = other._port;
_path = other._path;
_has_query_string = other._has_query_string;
_query_string = other._query_string;
_query_parsed = other._query_parsed;
_query_map = other._query_map;
_app = other._app;
_stream = other._stream;
_file = other._file;

return *this;
}

Url::Url(const Url &other)
{
_source = other._source;
_scheme = other._scheme;
_host = other._host;
_port = other._port;
_path = other._path;
_has_query_string = other._has_query_string;
_query_string = other._query_string;
_query_parsed = other._query_parsed;
_query_map = other._query_map;
_app = other._app;
_stream = other._stream;
_file = other._file;
}

std::shared_ptr<Url> Url::Clone() const
{
return std::make_shared<Url>(*this);
}

void Url::Print() const
{
logi("URL Parser", "%s %s %d %s %s %s %s",
Expand All @@ -302,16 +384,12 @@ namespace ov

ov::String Url::ToString() const
{
ov::String description;

description.AppendFormat(
return ov::String::FormatString(
"%s://%s%s%s%s%s%s (app: %s, stream: %s, file: %s)",
_scheme.CStr(),
_id.IsEmpty() ? "" : ov::String::FormatString("%s:%s@", _id.CStr(), _password.CStr()).CStr(),
_host.CStr(), (_port > 0) ? ov::String::FormatString(":%d", _port).CStr() : "",
_path.CStr(), _query_string.IsEmpty() ? "" : "?", _query_string.CStr(),
_app.CStr(), _stream.CStr(), _file.CStr());

return description;
}
} // namespace ov
154 changes: 21 additions & 133 deletions src/projects/base/ovlibrary/url.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,99 +23,23 @@ namespace ov
// <scheme>://<host>[:<port>][/<path/to/resource>][?<query string>]
static std::shared_ptr<Url> Parse(const ov::String &url);

const ov::String &Source() const
{
return _source;
}

const ov::String &Scheme() const
{
return _scheme;
}

const ov::String &Host() const
{
return _host;
}

void SetPort(uint32_t port)
{
_port = port;
}

const uint32_t &Port() const
{
return _port;
}

const ov::String &Path() const
{
return _path;
}

const ov::String &App() const
{
return _app;
}

const ov::String &Stream() const
{
return _stream;
}

const ov::String &File() const
{
return _file;
}

const ov::String &Id() const
{
return _id;
}

const ov::String &Password() const
{
return _password;
}

bool HasQueryString() const
{
return _has_query_string;
}

const ov::String &Query() const
{
ParseQueryIfNeeded();
return _query_string;
}

const bool HasQueryKey(ov::String key) const
{
ParseQueryIfNeeded();
if(_query_map.find(key) == _query_map.end())
{
return false;
}

return true;
}

const ov::String GetQueryValue(ov::String key) const
{
if(HasQueryKey(key) == false)
{
return "";
}

return Decode(_query_map[key]);
}

const std::map<ov::String, ov::String> &QueryMap() const
{
ParseQueryIfNeeded();
return _query_map;
}

OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetSource, Source, _source, )
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetScheme, Scheme, _scheme, )
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetHost, Host, _host, )
OV_DEFINE_SETTER_CONST_GETTER(uint32_t, SetPort, Port, _port, )
OV_DEFINE_CONST_GETTER(ov::String, Path, _path, )
void SetPath(const ov::String &path);
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetApp, App, _app, )
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetStream, Stream, _stream, )
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetFile, File, _file, )
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetId, Id, _id, )
OV_DEFINE_SETTER_CONST_GETTER(ov::String, SetPassword, Password, _password, )

bool HasQueryString() const;
const ov::String &Query() const;
const bool HasQueryKey(ov::String key) const;
const ov::String GetQueryValue(ov::String key) const;
const std::map<ov::String, ov::String> &QueryMap() const;
bool PushBackQueryKey(const ov::String &key, const ov::String &value);
bool PushBackQueryKey(const ov::String &key);
bool RemoveQueryKey(const ov::String &key);
Expand All @@ -124,48 +48,12 @@ namespace ov
ov::String ToUrlString(bool include_query_string = true) const;
ov::String ToString() const;

Url &operator=(const Url& other) noexcept
{
_source = other._source;
_scheme = other._scheme;
_host = other._host;
_port = other._port;
_path = other._path;
_has_query_string = other._has_query_string;
_query_string = other._query_string;
_query_parsed = other._query_parsed;
_query_map = other._query_map;
_app = other._app;
_stream = other._stream;
_file = other._file;

return *this;
}

Url()
{
}
Url &operator=(const Url &other) noexcept;

Url(const Url &other)
{
_source = other._source;
_scheme = other._scheme;
_host = other._host;
_port = other._port;
_path = other._path;
_has_query_string = other._has_query_string;
_query_string = other._query_string;
_query_parsed = other._query_parsed;
_query_map = other._query_map;
_app = other._app;
_stream = other._stream;
_file = other._file;
}
Url() =default;
Url(const Url &other);

std::shared_ptr<Url> Clone() const
{
return std::make_shared<Url>(*this);
}
std::shared_ptr<Url> Clone() const;

private:
void ParseQueryIfNeeded() const;
Expand Down

0 comments on commit 5092cd5

Please sign in to comment.