-
Notifications
You must be signed in to change notification settings - Fork 1
/
m3ufile.cpp
77 lines (61 loc) · 1.49 KB
/
m3ufile.cpp
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
#include "m3ufile.h"
#include <fstream>
using std::ifstream;
using std::string;
using std::map;
using std::vector;
m3uFile::m3uFile(std::string file)
{
ifstream iss(file);
string temp;
iss >> temp;
if(temp != "#EXTM3U")
throw "m3u8文件头部出错";
vector<double> body;
string key,value;
while (iss >> temp) {
if(temp == "#EXT-X-ENDLIST")//结束
break;
string::size_type pos = temp.find(':');
if(pos == string::npos){
throw "未能找到分割符':',文件分析失败";
}
key = temp.substr(0, pos);
if(key != "#EXTINF"){
value = temp.substr(pos+1, temp.length()-1);
_head[key] = atoi(value.c_str());
}
else {
value = temp.substr(pos+1, temp.length()-2);
body.push_back(atof(value.c_str()));
iss >> temp;//跳过一行
}
}
_body = new BTTree<double>(body);
}
int m3uFile::length()
{
return _body->getLen();
}
int m3uFile::getHeadValue(std::string str)
{
auto iter = _head.find(str);
if(iter == _head.end())
throw string("不存在该键值" + str);
return iter->second;
}
double m3uFile::getExtinfSum(int n)
{
//n [0, len)
if(n >= _body->getLen() || n < 0){
throw "索引出错";
}
return _body->getSum(n+1);//[1,len+1]
}
double m3uFile::getExtinf(int n)
{
if(n <= 0){
throw "索引出错";
}
return getExtinfSum(n) - getExtinfSum(n-1);
}