-
Notifications
You must be signed in to change notification settings - Fork 0
/
SongFIle.h
287 lines (265 loc) · 7.71 KB
/
SongFIle.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#pragma once
#include "Song.h"
#include <cstdio>
#include <string>
#include<iostream>
#include "AlbumFile.h"
#include "DeletedSongs.h"
#include <vector>
using namespace std;
class SongFile
{
private:
const char songFilePath[12] = "SongFile.fl";
int count = 0;
public:
void createFile()// ñòâîðåííÿ ôàéëó
{
FILE* songFile;
fopen_s(&songFile, songFilePath, "w+b");
fclose(songFile);
return;
}
bool checkFileExists(string& error) // ïåðåâ³ðêà ôàéëó
{
FILE* songFile;
fopen_s(&songFile, songFilePath, "r+b");
if (songFile == NULL) {
error = "Album file does not exist";
createFile();
return 0;
}
fclose(songFile);
return 1;
}
Song readSong(int adress)
{
Song song;
FILE* songFile;
fopen_s(&songFile, songFilePath, "r+b");
fseek(songFile, adress, SEEK_SET);
fread(&song, sizeof(Song), 1, songFile);
fclose(songFile);
return song;
}
void changeSong(Song song, int adress)
{
FILE* songFile;
fopen_s(&songFile, songFilePath, "r+b");
fseek(songFile, adress, SEEK_SET);
fwrite(&song, sizeof(Song), 1, songFile);
fclose(songFile);
return;
}
void writeSong(Song song)
{
FILE* songFile;
fopen_s(&songFile, songFilePath, "a+b");
fseek(songFile, 0, SEEK_END);
fwrite(&song, sizeof(Song), 1, songFile);
fclose(songFile);
return;
}
int getSeekEnd()
{
int seekEnd;
FILE* songFile;
fopen_s(&songFile, songFilePath, "r+b");
fseek(songFile, 0, SEEK_END);
seekEnd = ftell(songFile);
fclose(songFile);
return seekEnd;
}
bool checkSongExists(int albumKey, int songKey, string& error)// ïîâåðòຠ0 ÿêùî ï³ñí³ íå ³ñíóº àáî áóëà âèäàëåíà ³ 1 â ³íøîìó âèïàäêó
{
AlbumFile aFl = AlbumFile();
Album album = aFl.getAlbum(albumKey, error);
if (error != "")return 0;
int adress = album.firstSongAdress;
while (adress != -1) {
Song song = readSong(adress);
if (song.songId == songKey) {
if (song.isDeleted == 1) {
error = "song with key = " + to_string(songKey) + " was deleted";
}
return !song.isDeleted;
}
adress = song.nextAdress;
}
error = "song with key = " + to_string(songKey) + " does not exist";
return 0;
}
int getAdressOfSong(int albumKey, int songKey, string& error) // ïîâåðòຠàäðåñó ï³ñí³ àáî -1 ÿêùî ¿¿ íå ³ñíóº
{
if (!checkSongExists(albumKey, songKey, error))return -1;
AlbumFile aFl = AlbumFile();
Album album = aFl.getAlbum(albumKey, error);
if (error != "")return -1;
int adress = album.firstSongAdress;
while (adress != -1) {
Song song = readSong(adress);
if (song.songId == songKey) {
return adress;
}
adress = song.nextAdress;
}
return -1;
}
Song getSong(int albumKey, int songKey, string& error) // îòðèìóº ï³ñíþ ïî àéä³
{
int adress = getAdressOfSong(albumKey, songKey, error);
if (error != "")return Song();
Song song = readSong(adress);
return song;
}
void updateSong(int albumKey, int songKey, Song song, string& error) // îíîâëåííÿ ïîë³â ï³ñí³
{
int adress = getAdressOfSong(albumKey, songKey, error);
if (error != "")return;
changeSong(song, adress);
return;
}
void addNewSongAdress(int albumKey, int addingAdress, string& error) // îíîâëåííÿ ïîñèëàííÿ íà íîâó ï³ñíþ
{
AlbumFile aFl = AlbumFile();
Album album = aFl.getAlbum(albumKey, error);
if (error != "")return;
if (album.firstSongAdress == -1) {
album.firstSongAdress = addingAdress;
aFl.updateAlbum(albumKey, album, error);
return;
}
else {
int adress = album.firstSongAdress;
Song song = readSong(adress);
while (song.nextAdress != -1) {
adress = song.nextAdress;
song = readSong(adress);
}
song.nextAdress = addingAdress;
changeSong(song, adress);
return;
}
}
int addSong(int albumKey, Song song, string& error) // äîäàâàííÿ ï³ñíåé
{
DeletedSongs dSongs = DeletedSongs();
int adress = dSongs.getFirstDeletedAdress();
song.songId = ++count;
if (adress == -1) {
addNewSongAdress(albumKey, getSeekEnd(), error); // çì³íþºìî ïîñèëàííÿ ï³ñåíü
if (error != "")return 0;
writeSong(song);
}
else {
addNewSongAdress(albumKey, adress, error); // çì³íþºìî ïîñèëàííÿ ï³ñåíü
if (error != "")return 0;
changeSong(song, adress);
}
return song.songId;
}
void deleteSong(int albumKey, int songKey, string& error) // âèäàëåííÿ ï³ñí³
{
if (!checkSongExists(albumKey, songKey, error))return;
AlbumFile aFl = AlbumFile();
Album album = aFl.getAlbum(albumKey, error);
DeletedSongs dSongs = DeletedSongs();
if (error != "")return;
int adress = album.firstSongAdress;
vector< pair<int, Song> > songs;
while (adress != -1) {
Song song = readSong(adress);
songs.push_back({ adress,song });
adress = song.nextAdress;
}
if (songs.size() == 1) {
dSongs.addDeletedAdress(songs[0].first);
songs[0].second.isDeleted = 1;
changeSong(songs[0].second, songs[0].first);
album.firstSongAdress = -1;
aFl.updateAlbum(albumKey, album, error);
return;
}
else {
if (songs[0].second.songId == songKey) {
dSongs.addDeletedAdress(songs[0].first);
songs[0].second.isDeleted = 1;
changeSong(songs[0].second, songs[0].first);
album.firstSongAdress = songs[1].first;
aFl.updateAlbum(albumKey, album, error);
return;
}
else {
for (int i = 1; i < songs.size(); ++i) {
if (songs[i].second.songId == songKey) {
dSongs.addDeletedAdress(songs[i].first);
if (i == songs.size() - 1) {
songs[i - 1].second.nextAdress = -1;
}
else {
songs[i - 1].second.nextAdress = songs[i + 1].first;
}
changeSong(songs[i - 1].second, songs[i - 1].first);
songs[i].second.isDeleted = 1;
changeSong(songs[i].second, songs[i].first);
return;
}
}
}
}
}
void deleteAlbum(int albumKey, string& error) // âèäàëåííÿ àëüáîìó ç çàçíà÷åíèì àéä³
{
AlbumFile aFl = AlbumFile();
IndexFile fl = IndexFile();
DeletedSongs dSongs = DeletedSongs();
DeletedAlbums dAl = DeletedAlbums();
Album album = aFl.getAlbum(albumKey, error);
if (error != "")return;
int deletedAlbumAdress = fl.getAdressByKey(albumKey, error);
album.isDeleted = 1;
dAl.addDeletedAdress(deletedAlbumAdress);
aFl.updateAlbum(albumKey, album, error);
fl.deleteIndex(albumKey, error);
int adress = album.firstSongAdress;
while (adress != -1) {
Song song = readSong(adress);
song.isDeleted = 1;
changeSong(song, adress);
dSongs.addDeletedAdress(adress);
adress = song.nextAdress;
}
}
void writeAlbumAndSongsInfo(string& error)
{
IndexFile fl = IndexFile();
vector<int>albumKeys = fl.getRealKeys();
cout << "There are " + to_string(albumKeys.size()) + " albums in database" << endl;
AlbumFile aFl = AlbumFile();
for (int i = 0; i < albumKeys.size(); ++i) {
int albumKey = albumKeys[i];
Album album = aFl.getAlbum(albumKey, error);
cout << '\t' << "The album number " << i + 1 << " with key = " << album.id << ":" << endl;
cout << '\t' << "Name: " << album.name << endl;
cout << '\t' << "Release date: " << album.date << endl;
cout << '\t' << "Recordind studio: " << album.recordStudio << endl;
int adress = album.firstSongAdress;
vector<Song>songs;
while (adress != -1) {
Song song = readSong(adress);
songs.push_back(song);
adress = song.nextAdress;
}
cout << '\t' << '\t' << "Number of songs: " << songs.size() << endl;
for (int j = 0; j < songs.size(); ++j) {
Song song = songs[j];
cout << '\t' << '\t' << "The song number " << j + 1 << " with key = " << song.songId << endl;
cout << '\t' << '\t' << "Name: " << song.name << endl;
cout << '\t' << '\t' << "Duration: " << song.duration << endl;
cout << '\t' << '\t' << "Genre: " << song.genre << endl;
//cout << "----------------------------------------------------------" << endl;
}
//cout<<"<<<<<------------------------------------------------>>>>>" << endl;
}
}
};