oop-labs-collection/labs/2/Main.java

161 lines
5.6 KiB
Java
Raw Normal View History

2023-03-11 11:51:09 +02:00
import java.util.Objects;
2023-03-11 13:14:02 +02:00
import java.lang.Exception;
2023-03-11 11:51:09 +02:00
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.net.URL;
import java.io.BufferedInputStream;
2023-03-11 13:14:02 +02:00
import java.io.FileOutputStream;
2023-03-11 11:51:09 +02:00
import java.util.ArrayList;
2023-03-11 11:51:09 +02:00
public class Main {
2023-03-11 13:14:02 +02:00
public static void main(String[] args) {
2023-03-11 11:51:09 +02:00
boolean help_enqueued = false;
2023-03-11 13:14:02 +02:00
boolean allow_networking = true;
String matrix_file_name = "";
2023-03-18 11:42:01 +02:00
2023-03-11 11:51:09 +02:00
int p = 0;
while (p < args.length) {
try {
if (Objects.equals(args[p], "-h")) {
help_enqueued = true;
2023-03-11 13:14:02 +02:00
p += 1;
2023-03-11 11:51:09 +02:00
}
else if (Objects.equals(args[p], "--offline")) {
2023-03-11 13:14:02 +02:00
allow_networking = false;
p += 1;
2023-03-11 11:51:09 +02:00
}
else {
matrix_file_name = args[p++];
2023-03-11 11:51:09 +02:00
}
}
catch (Exception e) {
System.err.println("[ERROR] Exception while parsing CLI arguments: " + e);
System.err.println("[ERROR] Aborting further execution.");
System.exit(1);
}
}
if (help_enqueued) {
2023-03-11 13:14:02 +02:00
printHelp(allow_networking);
2023-03-11 11:51:09 +02:00
System.exit(0);
}
if (Objects.equals("", matrix_file_name)) {
System.err.println("[WARN] No filename specified to read from, using default one");
matrix_file_name = "random_matrix.txt";
}
File matrix_file = new File(matrix_file_name);
if (!matrix_file.exists()) {
if (allow_networking) {
System.err.println("[WARN] File is missing, downloading random matrix from the server");
fetchResource("http://lab2.kpi.dev:16554/matrix.py", matrix_file_name);
} else {
System.err.println("[WARN] File is missing; networking is disabled, not recovering");
System.exit(1);
}
}
// loading matrix data from file
int[][] raw_m = loadMatrixFromFile(matrix_file);
// creating original matrix object
System.out.println("Original matrix:");
Matrix m = new Matrix();
m.init(raw_m);
m.print();
// transposing
m.transpose();
System.out.println("\nTransposed matrix:");
m.print();
// avg
System.out.println("\nAverage value is " + m.getAvg());
2023-03-11 11:51:09 +02:00
}
2023-03-11 13:14:02 +02:00
private static void fetchResource(String remote_url, String output_filename) {
try {
BufferedInputStream in = new BufferedInputStream(new URL(remote_url).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(output_filename);
2023-03-11 13:14:02 +02:00
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (Exception e) {
System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e);
2023-03-11 11:51:09 +02:00
}
}
2023-03-11 13:14:02 +02:00
private static void printHelp(boolean allow_net) {
2023-03-11 11:51:09 +02:00
try {
File help_file = new File("src/help.txt");
2023-03-11 13:14:02 +02:00
if (!help_file.exists()) {
System.err.println("[WARN] Help file is missing.");
if (allow_net) {
System.err.println("[INFO] Trying to recover it from the CDN server");
fetchResource("http://lab2.kpi.dev:16554/help.txt", "src/help.txt");
2023-03-11 13:14:02 +02:00
} else {
System.err.println("[INFO] Networking is disabled, not recovering");
System.exit(1);
}
2023-03-11 11:51:09 +02:00
}
2023-03-11 13:14:02 +02:00
Scanner help_file_scanner = new Scanner(help_file);
2023-03-11 13:14:02 +02:00
while (help_file_scanner.hasNextLine()) {
2023-03-18 11:42:01 +02:00
System.out.println(help_file_scanner.nextLine());
2023-03-11 13:14:02 +02:00
}
help_file_scanner.close();
} catch (Exception e) {
System.out.println("[ERROR] Failed to read help due to the following exception: " + e);
System.exit(1);
2023-03-11 11:51:09 +02:00
}
}
private static int[][] loadMatrixFromFile(File matrix_file) {
try {
Scanner matrix_file_reader = new Scanner(matrix_file);
ArrayList<String> raw_matrix_lines = new ArrayList<String>();
while (matrix_file_reader.hasNextLine()) {
raw_matrix_lines.add(matrix_file_reader.nextLine());
}
ArrayList<String[]> baked_matrix_lines = new ArrayList<String[]>();
for (String s : raw_matrix_lines) {
baked_matrix_lines.add(s.split(" "));
}
int ref_length = baked_matrix_lines.get(0).length;
int[][] baked_matrix = new int[baked_matrix_lines.size()][ref_length];
for (int i = 0; i < baked_matrix_lines.size(); i++) {
if (baked_matrix_lines.get(i).length != ref_length) {
System.err.println("[ERROR] Matrix lines have different length! Assuming the matrix was corrupted");
System.exit(2);
}
for (int j = 0; j < baked_matrix_lines.get(i).length; j++) {
baked_matrix[i][j] = Integer.parseInt(baked_matrix_lines.get(i)[j]);
}
}
return baked_matrix;
} catch (Exception e) {
System.err.println("[ERROR] Failed to read matrix from file " + matrix_file.getName());
System.exit(1);
return new int[1][1];
}
}
2023-03-11 11:51:09 +02:00
}