-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.h
156 lines (119 loc) · 4.07 KB
/
resources.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
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
#include <iostream>
#include <iomanip>
#include <string>
#include <list>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
//static, external variables containing defaults settings
static string DEFAULT_FILE = "resources.txt"; //default file to be read when the program starts. this file cannot be overwritten by the program.
static string FILE_EXT = ".txt"; //the extension for file types to be read (set to "" if no file type should be specified)
static string DIR = "RESOURCE-FILES/"; //default directory where resource files are stored
//==================
// Global Functions
//==================
//checks that the file is the appropriate type (has FILE_EXT)
static bool checkFile(string file){
int location = (int)file.find(FILE_EXT);
if (FILE_EXT != ""){ //if a file type is specified (i.e. if file input should be checked)
if (location == -1){ //if extension not in filename
return false;
}
if ((location != (file.size() - FILE_EXT.size()))){ //if extension not at end of filename
return false;
}
}
return true;
}
//creates a copy of the filename
static string createCopy(string file){
int size = (int) file.size();
string s;
int i = 0;
while (file[i] != '.' && i < size){
s += file[i];
i++;
}
s+="-copy";
for (int j = i; j < size; j++){
s+=file[j];
}
return s;
}
//splits string on delimiter into vector
static vector<string> split(string s, char delim =' '){
vector<string> spl;
string temp;
stringstream ss (s);
while (getline(ss, temp, delim)){
spl.push_back(temp);
}
return spl;
}
//returns a slice of a vector of strings from begin to end
static vector<string> slice(vector<string> list, int begin, int end){
vector<string> sl;
for (int i = begin; i < end; i++){
if (i > list.size()){
break;
} else {
sl.push_back(list[i]);
}
}
return sl;
}
//============
// NODE CLASS
//============
class Node{
public:
string name; //name of node
vector<Node*> uses; //list of dependencies that the node uses
vector<Node*> usedby; //list of nodes that use this node
bool usable;
Node(string n);
//print out Node in format "<Name> || <Usable?> || <Dep1>, <Dep2>,...<DepN>"
void toString();
void toString(int width); //(width is formatting variable)
//update the nodes usability to u, and update all nodes in UsedBy
void updateNode(bool u);
//checks if any dependencies are unusable, and updates node's usability
void checkDeps();
//add n as a dependency
void addUses(Node *n);
//add n to "usedby" list
void addUsedby(Node *n);
};
//============
// GRAPH CLASS
//============
class Graph{
public: vector<Node*> nodes;
int longestItem;
//Adds a new node to the graph with a list of dependencies
void addNewNode (string name, vector<string> dependsOn);
//adds a new node to the graph w/ no dependencies
void addNewNode (string name);
//loads graph from file fileName
bool readFile (string fileName);
//saves graph in readable format to file fileName
string saveFile (string fileName);
//returns the node if it is in the graph, or adds and returns a new node if not
Node* contains(string name);
//prints out the current graph
void printGraph();
//delete node called by command "delete <itemname>" updates node as unusable
bool deleteNode(string name);
//adds node called by command "add <itemname> udpates node as usable
string addNode(string name);
void removeGraph();
//updates longestName for formatting purposes
void checkLongest(string name);
//sorts graph by usability
void sortGraphUSE();
//sorts graph by node name
void sortGraphNAME();
//returns index of node "name" if in nodes list, or -1 if not found
int inGraph(string name);
};