-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChordGraph.cpp
More file actions
104 lines (89 loc) · 2.85 KB
/
ChordGraph.cpp
File metadata and controls
104 lines (89 loc) · 2.85 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
#include "ChordGraph.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <algorithm>
#include <vector>
#include <limits>
#include <string>
#include <sstream>
using namespace std;
// Code that creates a vector of strings using delimiters. Each index of the vector will contain a chord.
void line_populate(vector<string> &record, const string& line, char delimiter) {
int linepos=0;
char c;
int i;
int linemax=line.length();
string curstring;
record.clear();
while(line[linepos]!=0 && linepos < linemax) {
c = line[linepos];
// Skip over text inbetween brackets
if (c=='['){
while (c!=']'){
linepos++;
c = line[linepos];
}
linepos++;
c = line[linepos];
}
// Skip over things that are not chords, and add the current string to the vector (unless empty due to previous delimiter)
if (c==delimiter || c=='|' || c=='.') {
//end of field
if (curstring != ""){
record.push_back( curstring );
curstring="";
}
}
else if ((c=='\r' || c=='\n')) { // End of line
if (curstring != ""){
record.push_back( curstring );
}
return;
}
else { // Is part of chord
curstring.push_back(c);
}
linepos++;
}
if (curstring != ""){
record.push_back( curstring );
}
return;
}
// Code that reads a file of chords, and makes a ChordGraph of it. The file must contain chords separated by spaces or newlines only!
// The very last line of the file will not be read, song information can be stored there.
bool parseChords(ChordGraph & cg, char * fileName){
ifstream inFile(fileName); // Input File Stream
// Error stuff
if (!inFile){
cerr << "Error with opening file" << endl;
return false;
}
if (inFile.fail()) {
cerr << "No file with that name" <<endl;
return false;
}
string line; // Holds the current line from getLine
vector<string> row; // Holds the parsed chords of the current line
string carry = ""; // Holds the chord in the last index of the last row
while (getline(inFile, line, '\n') && inFile.good() ){
line_populate(row, line, ' '); // Read the current line
// If this isn't the first line of the file
if (carry != ""){
// Make a progression from to last chord of the last line to the first chord of this line
//cout << "--------------------" << endl;
cg.addProgression(carry, row[0]);
}
// For every chord except the last in this line
for (int i = 0; i < row.size() - 1; i++){
// Make a progression from first chord to next chord
//cout << "--------------------" << endl;
cg.addProgression(row[i], row[i+1]);
}
// Set the carry chord to the last chord of this line that a progression can be made to the first chord of the next line
carry = row.back();
}
return true;
}