-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsong.cpp
More file actions
281 lines (276 loc) · 8.96 KB
/
song.cpp
File metadata and controls
281 lines (276 loc) · 8.96 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
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
/*******************************************************************************
Title : cp395_hwk2.cpp
Author : Cheng Pan
Created on : October 22, 2014
Description : Playlist manager. Create, modify, delete and view a playlist
saving the playlist to a text file
Purpose : get use to using class , linked list, processing string data and
organizing large programs
Usage : ./hwk2_cp395 file.csv
or ./hwk2_cp395 file.txt
Build with : g++ -o hwk2_cp395 hwk2_cp395.cpp song.cpp playlist.cpp
Modifications :
*******************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include "song.h"
using namespace std;
/******************************************************************************/
/* Function Definitions */
/******************************************************************************/
/**
Song() A default constructor for the Song class
*/
Song::Song()
{}
/**
Song() A default destructor for the Song class
*/
Song::~Song()
{}
/**
readSongList(argc, argv, ostream& error = cerr, ostream& out = cout) list all the playlist with the number of songs each playlist have
*/
void Song::readSongList(int argc, char* argv[], ostream& error, ostream& out)
{
ifstream input;
if (argc > 1) //if user entered a commandline argument
input.open(argv[1]); //open the file user entered
else
input.open("songs.csv"); //else, open the default file
if (input.fail()) //Check if it can open the file or not
{
error << "Unable to open file. " << endl;
error << "This program will now exit. " << endl;
exit(0);
}
else
error << "FILE OPENED SUCCESSFULLY" << endl;
bool checkedHeadings = false; //use to check the heading once during the loop
string word;
if (input.good())
{
while (getline(input, word)) //loop to get each line in the file
{
if (checkedHeadings == false) //only check the heading once
{
if (word == "\"Name\" \"Artist\" \"Album\" \"Genre\" \"Size\" \"Time\" \"Year\" \"Comments\"")
{
}
else //if heading is wrong, output error message and exit the program
{
error << "Headings are wrong or formatted incorrectly." << endl;
error << "The heading fields are tab-separated and enclosed in double-quotes." << endl;
error << "Example:"<<endl<<
"\"name\" \"Artist\" \"Album\" \"Genre\" \"Size\" \"Time\" \"Year\" \"Comments\" "<< endl;
exit(0);
}
checkedHeadings = true;
}
else
{
int qPos = 0; //store the quote position
int findQuotes = word.find('\"', qPos); //find the position of quotes
int numQuotes = 0; //count the # of quotes
int counter = 0; //to see which field to store into which vector
while (findQuotes != string::npos) //loop to find all the quotes
{
qPos = findQuotes + 1;
findQuotes = word.find('\"', qPos);
numQuotes++;
}
if (numQuotes != 16) //2 quotes for each field. 8 fields, so it's 16 quotes total.
{
error << "The name field cannot be a blank field.The field also needs to be in quotes." << endl;
error << "A blank field is indicated by an empty set of quotes (not even white spaces should be allowed)." << endl;
exit(0);
}
int numTab = 0; //store the # of tabs
int pos = 0; //store the position of the tabs
int found = word.find(" ", pos);
bool snameArtist = false; //use to check the first two field of each line, sname and Artist.
while (found != string::npos)
{
if (snameArtist == false) //first two fields cannot be empty
{
int length = 0;
string sname = word.substr(0, found);
string temp;
ss << sname;
while (ss >> temp) //stringstream to get the length of each substring
{
length += temp.length();
}
ss.clear();
if (2 >= length || sname == "" || sname[0] != '\"' || sname[found - 1] != '\"')
{
error << "The name field cannot be a blank field.The field also needs to be in quotes." << endl;
error << "A blank field is indicated by an empty set of quotes (not even white spaces should be allowed)." << endl;
exit(0);
}
name.push_back(sname);
songDatabase.push_back(sname);
}
//used to get all the other fields
pos=found+1; //position of the previous + 1
found = word.find(" ", pos); //new position of the next tab space
int length = 0;
string sname = word.substr(pos, found - pos); //get the string between the quotes
string temp;
ss << sname;
while (ss >> temp) //stringstream to get the length of each substring
{
length += temp.length();
}
ss.clear();
if (snameArtist == false)
{
if (2 >= length || sname == "" || sname[0] != '\"' || sname[found - pos - 1] != '\"')
{
error << "The name field cannot be a blank field.The field also needs to be in quotes." << endl;
error << "A blank field is indicated by an empty set of quotes (not even white spaces should be allowed)." << endl;
exit(0);
}
snameArtist = true;
}
else if (found != string::npos) //enter if not found
{
if (2 > length || sname[0] != '\"' || sname[found - pos - 1] != '\"')
{
error << "The name field cannot be a blank field.The field also needs to be in quotes." << endl;
error << "A blank field is indicated by an empty set of quotes (not even white spaces should be allowed)." << endl;
exit(0);
}
}
else
{
//there are only 7 tabs so it can only loop 7 times but there are 8 fields so comments field is left out
//this is used to check the comment field and stores it
int temp=0;
sname = word.substr(pos);
temp = word.find('\"', pos + 1);
length = sname.length();
if (temp == string::npos || length-1!=temp-pos)
{
error << "The name field cannot be a blank field.The field also needs to be in quotes." << endl;
error << "A blank field is indicated by an empty set of quotes (not even white spaces should be allowed)." << endl;
exit(0);
}
if (2 > length || sname[0] != '\"' || sname[temp-pos] != '\"')
{
error << "The name field cannot be a blank field.The field also needs to be in quotes." << endl;
error << "A blank field is indicated by an empty set of quotes (not even white spaces should be allowed)." << endl;
error << "No extra space should be allowed outside the field" << endl;
exit(0);
}
comments.push_back(sname);
}
//store the fields in their vector by using the counter
if (counter == 0)
{
artist.push_back(sname);
}
else if (counter == 1)
{
album.push_back(sname);
}
else if (counter == 2)
{
genre.push_back(sname);
}
else if (counter == 3)
{
size.push_back(sname);
}
else if (counter == 4)
{
time.push_back(sname);
}
else if (counter == 5)
{
year.push_back(sname);
}
songDatabase.push_back(sname); //store everything inside the vector
counter++;
numTab++;
}
if (numTab != 7) //if there're more than 7 tabs then it's wrong
{
error << "ERROR: Each line have to contains eight fields, corresponding to the headings." << endl;
error << "Only the first two fields: name and Artist are mandatory on the line." << endl;
exit(0);
}
}
}
input.clear(); //clear the inputfile
input.seekg(65, ios::beg); //goto beginning of the file
input >> noskipws >> word; //do not skip spaces
}
}
/**
getName() return vector of song names
*/
vector<string> &Song::getName() //return vector of all the song names
{
return name;
}
/**
getArtist() return vector of artist names
*/
vector<string> &Song::getArtist() //return vector of all the artist names
{
return artist;
}
/**
getAlbum() return vector of album names
*/
vector<string> &Song::getAlbum() //return vector of all the album names
{
return album;
}
/**
getGenre() return vector of genres
*/
vector<string> &Song::getGenre() //return vector of all the genres
{
return genre;
}
/**
getSize() return vector of song size
*/
vector<string> &Song::getSize() //return vector of all the size of the songs
{
return size;
}
/**
getTime() return vector of song duration
*/
vector<string> &Song::getTime() //return vector of all the time of the songs
{
return time;
}
/**
getYear() return vector of the year song was produced in
*/
vector<string> &Song::getYear() //return vector of all the year of the songs
{
return year;
}
/**
getComments() return vector of song comments
*/
vector<string> &Song::getComments() //return vector of all the comments of the songs
{
return comments;
}
/**
getSongDatabase() return vector of the entire database
*/
vector<string> &Song::getSongDatabase() //return vector of that contains all of the above
{
return songDatabase;
}