-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicClipPtr.cpp
More file actions
178 lines (147 loc) · 4.88 KB
/
Copy pathMusicClipPtr.cpp
File metadata and controls
178 lines (147 loc) · 4.88 KB
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
#include "MusicClipPtr.h"
MusicClipPtr::MusicClipPtr(int i) {
id = i;
filestr = filename();
mclip = NULL;
};
MusicClipPtr::MusicClipPtr() {
}
MusicClipPtr::~MusicClipPtr() {
if(mclip != NULL) {
saveToFile();
}
delete mclip;
}
// MusicClipPtr::MusicClipPtr(const MusicClipPtr& objref) {
// id = objref.id;
// filestr = objref.filestr;
// mclip = objref.mclip;
// }
// MusicClipPtr&::MusicClipPtr& operator=(const MusicClipPtr& obj) {
// id = obj->id;
// filestr = obj->filestr;
// mclip = obj->mclip;
// }
char* MusicClipPtr::filename() {
char filepart1[] = "mclip_";
char filepart2[] = ".clip";
std::ostringstream oss;
oss<<id; //convert int to stringstream
char * filenumber = const_cast<char*>( oss.str().c_str()); //convert stringstream to char array
char *file_name = new char(std::strlen(filepart1) + std::strlen(filenumber) + std::strlen(filepart2));
std::strcpy(file_name, filepart1);
std::strcat(file_name, filenumber);
std::strcat(file_name, filepart2);
return file_name;
}
void MusicClipPtr::loadFromFile() {
std::ifstream clipfile(filestr);
std::string line;
int c_index, c_id;
std::string c_artist, c_title, c_people, c_clip;
int c_genre;
// // public** clip;
double c_price;
if (clipfile.is_open())
{
std::cout << "loadfrom file" << std::endl;
getline(clipfile, line);
std::stringstream s(line);
s >> c_id; //clip id
getline(clipfile, line);
std::stringstream t(line);
t >> c_index; //clip index
getline(clipfile, c_title); //clip title
getline(clipfile, line);
std::stringstream u(line);
u >> c_genre; //clip genre
getline(clipfile, c_artist); //clip artist
getline(clipfile, c_people); //clip people - to be handled and created as a vector
getline(clipfile, line);
std::stringstream v(line);
v >> c_price; //clip price
getline(clipfile, c_clip); //clip
mclip = new MusicClip();
mclip->setId(c_id);
mclip->setTitle(c_title);
mclip->setArtist(c_artist);
mclip->setGenre(c_genre);
mclip->setPrice(c_price);
mclip->setPeople(c_people);
mclip->setClip(c_clip);
clipfile.close();
} else {
std::cout <<"File not opened" << std::endl;
}
}
void MusicClipPtr::saveToFile() {
std::ofstream clipfile;
clipfile.open(filestr);
clipfile << mclip->getId() << std::endl << mclip->getId() << std::endl << mclip->getTitle() << std::endl << mclip->getArtist()<< std::endl << mclip->getGenre() << std::endl;
std::vector<std::string> peeps = mclip->getPeople();
for(int i = 0; i < peeps.size(); i++) {
clipfile << peeps[i] << " ";
}
clipfile << std::endl << mclip->getPrice() << std::endl << mclip->getClip() << std::endl;
clipfile.close();
}
void MusicClipPtr::dcopy() {
if(mclip == NULL) {
loadFromFile();
}
std::ofstream clipfile;
clipfile.open("temp.clip");
clipfile << mclip->getId() << std::endl << mclip->getId() << std::endl << mclip->getTitle() << std::endl << mclip->getArtist()<< std::endl << mclip->getGenre() << std::endl;
std::vector<std::string> peeps = mclip->getPeople();
for(int i = 0; i < peeps.size(); i++) {
clipfile << peeps[i] << " ";
}
clipfile << std::endl << mclip->getPrice() << std::endl << mclip->getClip() << std::endl;
clipfile.close();
}
void MusicClipPtr::createClip() {
std::string mc_title, mc_artist, mc_people, mc_clip;
int mc_genre, mc_id;
double mc_price;
mclip = NULL;
mc_id = id;
std::cout << std::endl << "Enter the clip title : ";
std::getline(std::cin, mc_title);
std::cout << std::endl << "Enter the Name of the artist : ";
std::getline(std::cin, mc_artist);
std::cout << std::endl << "Enter the name of the people associated as comma seperated values : ";
std::getline(std::cin, mc_people);
std::cout << std::endl << "Enter the genre of the song : ";
std::cin >> mc_genre;
std::cout << std::endl << "Enter the price of the song : ";
std::cin >> mc_price;
std::cout << std::endl <<"Enter or paste the contents for clip field : ";
std::cin.ignore();
std::getline(std::cin, mc_clip);
std::cout << std::endl << "New clip is getting created" << std::endl;
mclip = new MusicClip();
mclip->setId(mc_id);
mclip->setTitle(mc_title);
mclip->setArtist(mc_artist);
mclip->setGenre(mc_genre);
mclip->setPrice(mc_price);
mclip->setPeople(mc_people);
mclip->setClip(mc_clip);
saveToFile();
}
void MusicClipPtr::dispdetails() {
std::cout << "File Name is : " << filestr << std::endl << "Id is : " << id << std::endl;
}
MusicClip* MusicClipPtr::operator-> () {
std::cout << "Inside overload method" << std::endl;
if(mclip == NULL) {
loadFromFile();
}
return mclip;
}
MusicClip& MusicClipPtr::operator* () {
if(mclip == NULL) {
loadFromFile();
}
return *mclip;
}