File: TemplatedUri.cpp

package info (click to toggle)
vlc 3.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208,020 kB
  • sloc: ansic: 443,448; cpp: 111,223; objc: 36,399; sh: 6,737; makefile: 6,627; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (170 lines) | stat: -rw-r--r-- 5,717 bytes parent folder | download | duplicates (7)
1
2
3
4
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * TemplatedUri.cpp
 *****************************************************************************
 * Copyright (C) 2010 - 2014 VideoLAN and VLC Authors
 *               2018        VideoLabs
 *
 * 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.
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "TemplatedUri.hpp"

#include <cctype>
#include <sstream>

using namespace dash::mpd;

/* Checks if current char is a token end, including format string tokens */
static std::string::size_type TokenEnd(const std::string &str,
                                       std::string::size_type pos,
                                       bool formatstring,
                                       int &formatwidth)
{
    std::string::size_type remain = str.length() - pos;
    if(remain < 1)
    {
        return std::string::npos;
    }
    else if(str[pos] == '$')
    {
        formatwidth = -1;
        return pos;
    }
    else if(formatstring && str[pos] == '%' && remain >= 3)
    {
        /* parse %[n]d format string if any */
        std::string::size_type fmtend = str.find('$', pos + 1);
        if(fmtend != std::string::npos)
        {
            std::istringstream iss(str.substr(pos + 1, fmtend - pos));
            iss.imbue(std::locale("C"));
            formatwidth = 1; /* %d -> default width = 1 */
            if(std::isdigit(iss.peek())) /* [n]d */
                iss >> formatwidth;
            if (iss.peek() != 'd') /* should be trailing d */
                return std::string::npos;
            /* return end of token position */
            return fmtend;
        }
    }
    return std::string::npos;
}

static int CompareToken(const std::string &str,
                        std::string::size_type pos,
                        const char *token,
                        const size_t token_len,
                        bool formatstring,
                        std::string::size_type &fulllength,
                        int &width)
{
    if(pos + 1 + token_len >= str.length() ||
       str.compare(pos + 1, token_len, token, token_len))
        return -1;

    std::string::size_type end = TokenEnd(str, pos + 1 + token_len, formatstring, width);
    if(end == std::string::npos)
        return -1;

    fulllength = 1 + end - pos;
    return 0;
}

bool TemplatedUri::IsDASHToken(const std::string &str,
                               std::string::size_type pos,
                               TemplatedUri::Token &token)
{
    std::string::size_type remain = str.length() - pos;
    if(remain < 2 || str[pos] != '$')
        return false;
    if(str[pos + 1] == '$') /* escaping */
    {
        token.type = TemplatedUri::Token::TOKEN_ESCAPE;
        token.fulllength = 2;
        token.width = -1;
        return true;
    }
    else if(!CompareToken(str, pos, "RepresentationID", 16, false,
                          token.fulllength, token.width))
    {
        token.type = TemplatedUri::Token::TOKEN_REPRESENTATION;
        return true;
    }
    else if(!CompareToken(str, pos, "Time", 4, true,
                          token.fulllength, token.width))
    {
        token.type = TemplatedUri::Token::TOKEN_TIME;
        return true;
    }
    else if(!CompareToken(str, pos, "Number", 6, true,
                          token.fulllength, token.width))
    {
        token.type = TemplatedUri::Token::TOKEN_NUMBER;
        return true;
    }
    else if(!CompareToken(str, pos, "Bandwidth", 9, true,
                          token.fulllength, token.width))
    {
        token.type = TemplatedUri::Token::TOKEN_BANDWIDTH;
        return true;
    }
    else return false;
}

static std::string FormatValue(uint64_t value, int width)
{
    std::stringstream oss;
    oss.imbue(std::locale("C"));
    if(width > 0)
    {
        oss.width(width); /* set format string length */
        oss.fill('0');
    }
    oss << value;
    return oss.str();
}

std::string::size_type TemplatedUri::ReplaceDASHToken(std::string &str,
                                                      std::string::size_type pos,
                                                      const TemplatedUri::Token &token,
                                                      const TemplatedUri::TokenReplacement &repl)
{
    switch(token.type)
    {
        case TemplatedUri::Token::TOKEN_TIME:
        case TemplatedUri::Token::TOKEN_BANDWIDTH:
        case TemplatedUri::Token::TOKEN_NUMBER:
        {
            std::string newstr = FormatValue(repl.value, token.width);
            str.replace(pos, token.fulllength, newstr);
            return newstr.length();
        }
        case TemplatedUri::Token::TOKEN_REPRESENTATION:
        {
            str.replace(pos, token.fulllength, repl.str);
            return repl.str.length();
        }
        case TemplatedUri::Token::TOKEN_ESCAPE:
        {
            str.erase(pos, 1);
            return 1;
        }
        default:
            return std::string::npos;
    }
}