-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.h
144 lines (129 loc) · 4.09 KB
/
files.h
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
#ifndef FILES_LIBRARY
#define FILES_LIBRARY
#include "define.h"
//////////////////////////////////////////////////////////////////////////////////////////////////////
map<int,string> songList;
map<int,string> songArtist;
map<int,string> songTitle;
map<int,string> songDuration;
void tokenize(std::string const &str, const char delim,std::vector<std::string> &out)
{
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos)
{
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
}
string getList()
{
DIR *dir;
struct dirent *diread;
if ((dir = opendir("./songs/")) != nullptr)
{
string list,filename,extension;
int fl;
int index = 1;
while (((diread = readdir(dir)) != nullptr)&&(index<=9))
{
filename = diread->d_name;
fl = filename.length();
if (fl >= 3)
extension = filename.substr(fl - 3);
if (extension == "mp3")
list = list + to_string(index++) + " " + filename.substr(0,fl) + "\n";
}
return list;
closedir(dir);
}
else
{
perror("opendir");
}
return "";
}
void toMap(string list)
{
ifstream fin;
string line;
int index;
string songName;
fin.open("./songs/list.txt"); // by default open mode = ios::in mode
while (fin) // Execute a loop until EOF (End of File)
{
getline(fin, line);
if(line.length()>=8)
{
index=line[0]-48;
songName=line.substr(2,line.length()-6);
songList.insert(pair<int, string>(index, songName));
}
}
map<int, string>::iterator itr;
for (itr=songList.begin(); itr != songList.end(); itr++)
{
vector<string> songDetails;
tokenize(itr->second, '-', songDetails);
songArtist.insert(pair<int, string>(itr->first, songDetails[0] ));
songTitle.insert(pair<int, string>(itr->first, songDetails[1]));
vector<string> ms;
tokenize(songDetails[2], '.', ms);
songDuration.insert(pair<int, string>(itr->first, ms[0]+":"+ms[1]));
}
}
void updateList()
{
ofstream file;
file.open ("./songs/list.txt");
file << getList();
file.close();
toMap(getList());
}
string getSongFileName(int songIndex)
{
return songList[songIndex]+".mp3";
}
int getNumberOfSongs()
{
return songList.size();
}
string getSongArtist(int songIndex)
{
return songArtist[songIndex];
}
string getSongTitle(int songIndex)
{
return songTitle[songIndex];
}
string getSongDuration(int songIndex)
{
return songDuration[songIndex];
}
void errorSequence(int currentSongIndex)
{
clearLineY(yZero+3) horizontalBlank
clearLineY(yZero+5 ) xMargin cout<<"* Error In File *";
clearLineY(yZero+6) track1 cout<<currentSongIndex; track2
clearLineY(yZero+7) xMargin cout<<"* Playing Next Song *";
clearLineY(yZero+13) horizontalBlank
clearLineY(yZero+11) xMargin cout<<"* . *"; Sleep(1000) ;
clearLineY(yZero+11) xMargin cout<<"* . . *"; Sleep(1000) ;
clearLineY(yZero+11) xMargin cout<<"* . . . *"; Sleep(1000) ;
clearLineY(yZero+11) xMargin cout<<"* . . . . *"; Sleep(1000) ;
}
void displaySongListShowList()
{
for(int x=1;x<=getNumberOfSongs();x++)
{
string st = getSongTitle(x);
if(st.length()>18) {st=st.substr(0,16)+"..";}
else { int d=18-st.length(); for(int y=d;y>=1;y--){st=st+" ";}}
string sa = getSongArtist(x);
if(sa.length()>11) {sa=sa.substr(0,9)+"..";}
else { int d=11-sa.length(); for(int y=d;y>=1;y--){sa=sa+" ";}}
clearLineY(yZero+(4+x)) xMargin cout<<"* "<<x<<" "<<st<<" "<<sa<<" "<<getSongDuration(x)<<" *\n";
}
}
////////////////////////////////////////////////////////////////////////////////////////////
#endif