Url.cpp 2.86 KB
Newer Older
1
2
3
/*
 * Url.cpp
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2014 - VideoLAN and VLC Authors
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#include "Url.hpp"
#include "BaseRepresentation.h"
#include "SegmentTemplate.h"

using namespace adaptative::playlist;

Url::Url()
{
}

Url::Url(const Component & comp)
{
    prepend(comp);
}

Url::Url(const std::string &str)
{
    prepend(Component(str));
}

40
41
42
43
44
bool Url::hasScheme() const
{
    if(components.empty())
        return false;

45
    return components[0].b_scheme;
46
47
}

48
49
50
51
52
bool Url::empty() const
{
    return components.empty();
}

53
54
55
56
57
58
59
60
Url & Url::prepend(const Component & comp)
{
    components.insert(components.begin(), comp);
    return *this;
}

Url & Url::append(const Component & comp)
{
61
62
    if(!components.empty() && !components.back().b_dir)
        components.pop_back();
63
64
65
66
67
68
69
70
71
72
73
74
    components.push_back(comp);
    return *this;
}

Url & Url::prepend(const Url &url)
{
    components.insert(components.begin(), url.components.begin(), url.components.end());
    return *this;
}

Url & Url::append(const Url &url)
{
75
76
    if(!components.empty() && !components.back().b_dir)
        components.pop_back();
77
78
79
80
81
82
83
84
85
86
87
88
89
    components.insert(components.end(), url.components.begin(), url.components.end());
    return *this;
}

std::string Url::toString() const
{
    return toString(0, NULL);
}

std::string Url::toString(size_t index, const BaseRepresentation *rep) const
{
    std::string ret;
    std::vector<Component>::const_iterator it;
90
    for(it = components.begin(); it != components.end(); ++it)
91
92
93
94
95
96
97
98
99
100
101
    {
        const Component *comp = & (*it);
        if(rep)
            ret.append(rep->contextualize(index, comp->component, comp->templ));
        else
            ret.append(comp->component);
    }
    return ret;
}

Url::Component::Component(const std::string & str, const MediaSegmentTemplate *templ_)
102
 : component(str), templ(templ_), b_scheme(false), b_dir(false)
103
{
104
105
    if(!component.empty())
    {
106
        b_dir = (component[component.length()-1]=='/');
107
108
        b_scheme = !component.compare(0, 7, "http://") || !component.compare(0, 8, "https://");
    }
109
110
}