-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlbumFile.h
116 lines (104 loc) · 2.48 KB
/
AlbumFile.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
#pragma once
#include "Album.h"
#include "indexFile.h"
#include <cstdio>
#include <string>
#include<iostream>
#include "DeletedAlbums.h"
using namespace std;
class AlbumFile
{
private:
const char albumFilePath[13] = "AlbumFile.fl";
public:
void createFile()// ñòâîðåííÿ ôàéëó
{
FILE* albumFile;
fopen_s(&albumFile, albumFilePath, "w+b");
fclose(albumFile);
return;
}
bool checkFileExists(string& error) // ïåðåâ³ðêà ôàéëó
{
FILE* albumFile;
fopen_s(&albumFile, albumFilePath, "r+b");
if (albumFile == NULL) {
error = "Album file does not exist";
createFile();
return 0;
}
fclose(albumFile);
return 1;
}
int getSeekEnd()
{
int seekEnd;
FILE* albumFile;
fopen_s(&albumFile, albumFilePath, "r+b");
fseek(albumFile, 0, SEEK_END);
seekEnd = ftell(albumFile);
fclose(albumFile);
return seekEnd;
}
Album readAlbum(int adress)
{
Album album;
FILE* albumFile;
fopen_s(&albumFile, albumFilePath, "r+b");
fseek(albumFile, adress, SEEK_SET);
fread(&album, sizeof(Album), 1, albumFile);
fclose(albumFile);
return album;
}
void changeAlbum(Album album, int adress)
{
FILE* albumFile;
fopen_s(&albumFile, albumFilePath, "r+b");
fseek(albumFile, adress, SEEK_SET);
fwrite(&album, sizeof(Album), 1, albumFile);
fclose(albumFile);
return;
}
void writeAlbum(Album album)
{
FILE* albumFile;
fopen_s(&albumFile, albumFilePath, "a+b");
fseek(albumFile, 0, SEEK_END);
fwrite(&album, sizeof(Album), 1, albumFile);
fclose(albumFile);
return;
}
Album getAlbum(int key, string& error) // îòðèìóº àëüáîì ïî àéä³ ÿêùî â³í ³ñíóº
{
IndexFile fl = IndexFile();
if (!fl.checkKeyIsReal(key, error))return Album();
int adress = fl.getAdressByKey(key, error);
if (error != "")return Album();
return readAlbum(adress);
}
int addAlbum(Album album, string& error) // äîäຠàëüáîì äî ôàéëó ó çâ³ëüíåíå ì³ñöå àáî ó ê³íüöå ÿêùî çâ³ëüíåíèõ íåìàº
{
DeletedAlbums dAl = DeletedAlbums();
int adress = dAl.getFirstDeletedAdress();
IndexFile iFl = IndexFile();
album.id = iFl.getLastKey() + 1;
if (adress == -1) {
iFl.addIndex(getSeekEnd(), error);
writeAlbum(album);
}
else {
iFl.addIndex(adress, error);
if (error != "")return 0;
changeAlbum(album, adress);
}
return album.id;
}
void updateAlbum(int key, Album album, string& error) // îíîâëþº ïîëÿ àëüáîìà ç çàçíà÷åíèì àéä³
{
IndexFile iFl = IndexFile();
int adress = iFl.getAdressByKey(key, error);
if (error != "")return;
changeAlbum(album, adress);
return;
}
};