Skip to content

Commit

Permalink
pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
gigatexal committed Aug 8, 2016
1 parent 57c4b86 commit 28722df
Show file tree
Hide file tree
Showing 24 changed files with 896 additions and 36 deletions.
118 changes: 118 additions & 0 deletions cs162/project5/DB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <fstream>
#include <iostream>
#include "DB.h"

DB::DB(){
this->currSize = 0;
}

DB::~DB(){
delete [] songs;
};

const unsigned int DB::getCurrSize() const {
return this->currSize;
}
/*
void DB::growDB(){
if (currSize > 2 * this->jumpAmount - 1){
Song tmp = new Song[this->currSize + this->jumpAmount];
for (int i = 0; i < currSize; i++){
Song s = this->get(i);
tmp[i] = s;
delete s;
}
delete [] this->songs;
this->songs = new Song[this->currSize + this->jumpAmount];
}
}
*/
void DB::add(Song s){
if (currSize < this->maxSize){
this->songs[currSize] = s;
this->currSize++;
}
}

void DB::add(Song s, unsigned int index){
if ((currSize < this->maxSize) && (index < this->maxSize)){
this->songs[index] = s;
this->currSize++;
}
}

Song DB::get(unsigned int index){
return songs[index];
//return this->songs[index];
}

void DB::remove(unsigned int index){
if ((this->currSize > 0) && (this->currSize < maxSize) && (index <= this->currSize) && (index > 0)){
for (int i = 0; i < currSize; i++){
this->songs[i] = this->songs[i+1];
}
this->currSize--;
}
}

bool DB::save(char filename[1024]){
std::ofstream file(filename);
bool success = false;
if (file){
for (int i = 0; i < currSize; i++){
file << songs[i].getTitle() << ";"
<< songs[i].getArtist() << ";"
<< songs[i].getLengthMinutes() << ";"
<< songs[i].getLengthSeconds() << ";"
<< songs[i].getAlbum() << std::endl;
}
success = true;
}
file.close();
return success;
}

//count the number of lines in teh file
//set the size of the db to this + 1
bool DB::loadData(const char filename[]){
bool success = false;
struct Loader {
char title[128];
char artist[128];
char album[128];
char length_minutes[128];
char length_seconds[128];
};
Loader l;
std::ifstream in(filename);

int i = 0;
char dummyLine[1024];
while (in.getline(dummyLine,1024)){
++i;
}
delete [] this->songs;
this->songs = new Song[i + (this->jumpAmount) ];

in.close();
in.open(filename);
int count = 0;
while ( in.getline(l.title,Song::MAX_CHAR,';')
&& in.getline(l.artist,Song::MAX_CHAR,';')
&& in.getline(l.length_minutes,Song::MAX_CHAR,';')
&& in.getline(l.length_seconds,Song::MAX_CHAR,';')
&& in.getline(l.album,Song::MAX_CHAR,'\n')
){

Song s(l.title,l.artist,l.album,atoi(l.length_minutes),atoi(l.length_seconds));
this->add(s);
/*
Song *s = new Song(l.title,l.artist,l.album,atoi(l.length_minutes),atoi(l.length_seconds));
this->songs[count] = s;
*/
success = true;
count++;
}
in.close();
return success;
}
36 changes: 36 additions & 0 deletions cs162/project5/DB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "Song.h"
#include <cstdlib>

class DB {
public:
static const unsigned int maxSize = 1024;
DB();
~DB();
//getters
void add(Song s);
void add(Song s, unsigned int index);
Song get(unsigned int index);
const unsigned int getCurrSize() const;
const void showAll() const;
//setters
void remove(unsigned int index);
bool save(char filename[1024]);//made large to accomodate a long path
bool loadData(const char filename[]);

private:
//Song songs[DB::maxSize];
Song *songs = new Song[maxSize];
int currSize;
const unsigned int jumpAmount = 100;
//void growDB();//if currSize is 90% of jump amount, grow
//int computeInitialSize
};

/*
ifstream f("songs.txt");
int i = 0;
char dummyLine[5];
while (f.getline(dummyLine,5)){
++i;
}
*/
102 changes: 102 additions & 0 deletions cs162/project5/Song.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "Song.h"
#include <iostream>//can remove when not debugging
#include <cstring>

//set some initial dummy variables
Song::Song(){
strncpy(_title,"title",Song::MAX_CHAR);
strncpy(_artist,"artist",Song::MAX_CHAR);
strncpy(_album,"album",Song::MAX_CHAR);
_length_minutes = 0;
_length_seconds = 0;
}
/*
Song::Song(char* title, char* artist, char* album, int minutes, int seconds){
strncpy(_title,title,Song::MAX_CHAR);
strncpy(_artist,artist,Song::MAX_CHAR);
strncpy(_album,album,Song::MAX_CHAR);
_length_minutes = minutes;
_length_seconds = seconds;
}
*/

Song::Song(char title[], char* artist, char* album, int minutes, int seconds){
strncpy(_title,title,Song::MAX_CHAR);
strncpy(_title,title,strlen(title));
strncpy(_artist,artist,Song::MAX_CHAR);
strncpy(_album,album,Song::MAX_CHAR);
_length_minutes = minutes;
_length_seconds = seconds;
}

Song::~Song(){
//delete _title;
}
//setters
void Song::setTitle(char* title){
strncpy(_title,title,Song::MAX_CHAR);
}

void Song::setArtist(char* artist){
strncpy(_artist,artist,Song::MAX_CHAR);
}

void Song::setAlbum(char* album){
strncpy(_album,album,Song::MAX_CHAR);
}

void Song::setLengthMinutes(int length_minutes){
_length_minutes = length_minutes;
}

void Song::setLengthSeconds(int length_seconds){
_length_seconds = length_seconds;
}

//getters
const char* Song::getTitle() const {
return _title;
}

const char* Song::getArtist() const {
return _artist;
}

const char* Song::getAlbum() const {
return _album;
}

int Song::getLengthMinutes() const {
return _length_minutes;
}

int Song::getLengthSeconds() const {
return _length_seconds;
}

void Song::print() const {
std::cout << this->getTitle()
<< " : "
<< this->getArtist()
<< " : "
<< this->getAlbum()
<< " : "
<< this->getLengthMinutes()
<< " : "
<< this->getLengthSeconds()
<< std::endl;
}














32 changes: 32 additions & 0 deletions cs162/project5/Song.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Song {
public:
Song();
~Song();
Song(char* title, char* artist, char* album, int length_minutes, int length_seconds);
static const int MAX_CHAR = 1024;

//setters
void setTitle(char* title);
void setArtist(char* artist);
void setAlbum(char* album);
void setLengthMinutes(int length_minutes);
void setLengthSeconds(int length_seconds);

//getters
const char* getTitle() const;
const char* getArtist() const;
const char* getAlbum() const;
int getLengthMinutes() const;
int getLengthSeconds() const;

//For Debug only
void print() const;

private:
char _title[Song::MAX_CHAR];
char _artist[Song::MAX_CHAR];
char _album[Song::MAX_CHAR];
int _length_minutes;
int _length_seconds;
};

48 changes: 48 additions & 0 deletions cs162/project5/UI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "UI.h"
#include <iomanip>
#include <iostream>
using namespace std;

void UI::displayChoices(){
cout << setw(80) << left << "Enter in a new song's info " << right << "(n)" << endl;
cout << setw(80) << left << "Show or display songs current loaded " << right << "(d)" << endl;
cout << setw(80) << left << "Delete or remove a song by a given index " << right << "(r)" << endl;
cout << setw(80) << left << "Search for songs by artist " << right << "(a)" << endl;
cout << setw(80) << left << "Search for songs by album " << right << "(b)" << endl;
cout << setw(80) << left << "Terminate or quit the program " << right << "(q)" << endl;
}

void UI::displaySongHeader(){
cout << setw(10) << left << "Index"
<< setw(30) << left << "Title"
<< setw(45) << left << "Artist"
<< setw(10) << left << "Duration"
<< setw(20) << left << "Album"
<< endl;
}


template <class T> void UI::get(T &var){
T val;
cin >> val;
while (!cin){
cout << "Data entered was invalid" << endl;
cin.clear();
cin.ignore(UI::MAX_CHAR,'\n');
cin >> val;
}
cin.ignore(UI::MAX_CHAR,'\n');
var = val;
}

void UI::get(char str[], unsigned int size){
cin.get(str, size, '\n');
while(!cin){
cout << "Field cannnot be empty. Please enter in something." << endl;
cin.clear();
cin.ignore(UI::MAX_CHAR,'\n');
cin.get(str, size, '\n');
}
cin.ignore(UI::MAX_CHAR,'\n');
}

9 changes: 9 additions & 0 deletions cs162/project5/UI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class UI {
public:
static const unsigned int MAX_CHAR = 1024;
void displaySongHeader();
void displayChoices();
template<class T> void showAll();
template <class T> void get(T &var);
void get(char str[], unsigned int size);
};
Binary file added cs162/project5/a.out
Binary file not shown.
Binary file added cs162/project5/app
Binary file not shown.
13 changes: 13 additions & 0 deletions cs162/project5/app.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <fstream>
#include "Song.h"
using namespace std;

int main(){
return 0;





}
3 changes: 3 additions & 0 deletions cs162/project5/design.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
db class :
- loadfromfile
-
Loading

0 comments on commit 28722df

Please sign in to comment.