C++-ify it some more, begin readfile

This commit is contained in:
2022-04-17 22:03:17 -05:00
parent f770d105b8
commit 476aec5734
7 changed files with 122 additions and 97 deletions

View File

@@ -1,22 +1,59 @@
#include <cstdlib>
#include "graph.hpp"
int graph_reduce(struct adjmatrix *graph) {
/* reducible:
Perform the graph reduction algorithm on the adjacency matrix to detect deadlocks
This algorithm is described in section 5.2 of the Zybook
(!THIS MODIFIES THE GRAPH!)
@params: none, uses class-internal data
@returns:
bool:
false if graph is not reducible (deadlock)
true if graph is reducible (no deadlock)
*/
bool graph::reducible() {
// TODO: Implement a function which checks if a process is blocked
// TODO: Use that function to implement the graph reduction algorithm
//? Make sure when reducing the graph, you don't try to delete the
return 0;
return false;
}
int knot_detect(struct adjmatrix *graph) {
return 0;
/* knotted:
Perform the knot detection algorithm on the adjacency matrix to detect deadlocks
(!THIS MODIFIES THE GRAPH!)
@params: none, uses class-internal data
@returns:
int:
true if graph is knotted (deadlock)
false if graph is not (no deadlock)
*/
bool graph::knotted() {
return false;
}
int destroy_adjmatrix(struct adjmatrix *matrix) {
for (int i = 0; i < matrix->num_processes + matrix->num_resources; i++) {
free(matrix->matrix[i]);
graph::graph (const int processes, const int resources) {
num_processes = processes;
num_resources = resources;
matrix.x = processes + resources;
matrix.y = matrix.x;
matrix.data = new int*[matrix.x];
for (int x = 0; x < matrix.x; x++) {
matrix.data[x] = new int[matrix.y];
}
free(matrix->matrix);
free(matrix->resource_counts);
return 0;
}
graph::~graph() {
/* clean up the 2D matrix */
if (matrix.data) {
if (matrix.x || matrix.y) {
// delete y axes
for (int x = 0; x < matrix.x; x++) {
delete matrix.data[x];
}
// delete x axis
delete matrix.data;
matrix.x = 0; matrix.y = 0;
}
}
free(resource_counts);
}

View File

@@ -1,9 +1,10 @@
/* +-------------+---------+-----------------------+
| John Breaux | jab0910 | JohnBreaux@my.unt.edu | //TODO put your name here
| John Breaux | jab0910 | JohnBreaux@my.unt.edu |
| | | | //TODO put your names here
+-------------+---------+-----------------------+
| Created 2022-04-16 |
+-----------------------------------------------+ */
#include <cstdio> // printf
#include <string>
#include "graph.hpp" // Graph reduction/Knot detection, struct adjmatrix
#include "read_input.hpp" //TODO: Read input in the associated C file, and provide an interface to that function here
@@ -12,16 +13,15 @@ int main(int argc, char** argv) {
// TODO: Grab file name from args
//? Command line argument structure?
//? Other flags? What other features should this have?
char* filename = NULL;
if (argc < 2) return 1;
std::string filename = argv[1];
//TODO: Implement reading from a file
struct adjmatrix *graph;
read_file(filename, graph);
read_file(filename);
// TODO: Implement graph reduction and/or knot detection
// TODO: Destroy the graph created by read_file
destroy_adjmatrix(graph);
return 0;
}

View File

@@ -1,39 +1,58 @@
//TODO: Include C file IO header (stdio.h? Whatever it is)
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <regex>
#include "graph.hpp" // struct adjmatrix
#include "read_input.hpp" // read_input
#define MAX_LINE_LEN 1024
// Vector of regices
std::vector<std::regex> patterns = {
//* A line starting with a % is a comment, and should be skipped
//* A blank line should be skipped
//* A line starting with `num_processes=` contains the number of processes
std::regex("^.*(num_processes)\\s*=\\s*(\\d+).*"),
//* A line starting with `num_resources=` contains the number of resources
std::regex("^.*(num_resources)\\s*=\\s*(\\d+)"),
//* A line containing comma-separated values should be returned as-is, for manual disassembly
std::regex("^([0-9, ]+)")
};
// TODO: Implement reading from a file
int read_file(char *filename, struct adjmatrix *graph) {
// TODO: Open file with name filename as read-only
FILE *f = fopen(filename, "r");
void read_file(std::string filename) {
// Open file with name filename as read-only
std::fstream f; f.open(filename, f.in);
// TODO: Check for file IO errors (I might have a solution for that in another project)
int num_processes = 0, num_resources = 0;
char line[MAX_LINE_LEN]; // Lines can be no more than 1KB in size, a sensible limitation
while (!feof(f)) {
//* A line starting with a % is a comment, and should be skipped
// if %:
continue;
//* A blank line should be skipped
// if blank:
continue;
//* A line starting with `num_processes=` contains the number of processes
//if "num_processes":
num_processes = 0; // TODO: Read file
//* A line starting with `num_resources=` contains the number of resources
//if "num_resources":
num_resources = 0;
// TODO: create graph 'object' here
//* A line containing num_resources numbers follows, indicating the number of each resource
//* Resources are comma-separated, so there can never be more than 1/2 as many as max line length
int resource_counts[MAX_LINE_LEN/2];
//* A comma-separated, newline-separated adjacency matrix with which to reduce
std::vector<int> rescount;
std::vector<std::vector<int>> matrix;
std::string line; // Lines can be no more than 1KB in size, a sensible limitation
while (!f.eof()) {
std::getline (f, line);
std::smatch res;
// Iterate over each pattern, and grab the associated data
for (auto pattern: patterns) {
if (std::regex_search (line, res, pattern)) {
// get the pattern type, value
std::string type = res.format("$1"), value = res.format("$2");
// Handle the pattern
if (type == "num_processes") { num_processes = std::stoi(value); } else
if (type == "num_resources") { num_resources = std::stoi(value); } else
if (type.length()/2 <= num_resources) {
// This is the resource count structure
}
break;
}
}
}
// TODO: Close file
// Close file
f.close();
// TODO: Check for file IO errors (Shouldn't be any)
return;
}