From 60c578b8bf6a4e3b2cc4cd048202c4eb1ab76b0e Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sat, 11 Mar 2023 11:51:09 +0200 Subject: [PATCH] add development files for lab2 --- labs/2/Main.java | 89 +++++++++++++++++++++++++++++++++++++++++++++ labs/2/src/help.txt | 1 + 2 files changed, 90 insertions(+) create mode 100644 labs/2/Main.java create mode 100644 labs/2/src/help.txt diff --git a/labs/2/Main.java b/labs/2/Main.java new file mode 100644 index 0000000..97a3ca3 --- /dev/null +++ b/labs/2/Main.java @@ -0,0 +1,89 @@ +import java.util.Objects; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; +import java.io.FileWriter; + +import java.net.URL; +import java.io.BufferedInputStream; + +public class Main { + private boolean disable_networking = false; + + public void main(String[] args) { + boolean help_enqueued = false; + int verbosity = 1; + + int p = 0; + while (p < args.length) { + try { + if (Objects.equals(args[p], "-h")) { + help_enqueued = true; + } + else if (Objects.equals(args[p], "-v")) { + verbosity = Integer.parseInt(args[p+1]); + p += 2; + } + else if (Objects.equals(args[p], "--offline")) { + disable_networking = true; + p += 2; + } + else { + System.err.println("[ERROR] Unknown argument found: " + args[p] + "\n[ERROR] Please, use 'java Main -h' for help"); + System.exit(1); + } + } + 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) { + printHelp(); + System.exit(0); + } + } + + public fetchResource(String remote_url, String output_filename) { + BufferedInputStream in = new BufferedInputStream(new URL(remote_url).openStream()); + FileOutputStream fileOutputStream = new FileOutputStream() + + byte dataBuffer[] = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { + fileOutputStream.write(dataBuffer, 0, bytesRead); + } + } + + public printHelp() { + String help_text = ""; + + try { + File help_file = new File("src/help.txt"); + Scanner help_file_scanner = new Scanner(help_file); + + } catch (FileNotFoundException e) { + System.err.println("[WARN] Help file missing."); + if (!disable_networking) { + System.err.println("[INFO] Trying to recover it from the git server"); + fetchResource("http://139.162.162.130:3000/", "src/help.txt"); + File help_file = new File("src/help.txt"); + Scanner help_file_scanner = new Scanner(help_file); + } else { + System.err.println("[INFO] --offline flag is set, not recovering"); + System.exit(1); + } + } + + while (help_file_scanner.hasNextLine()) { + help_text += help_file_scanner.nextLine(); + } + + help_file_scanner.close(); + + System.out.println(help_text); + } +} diff --git a/labs/2/src/help.txt b/labs/2/src/help.txt new file mode 100644 index 0000000..09ee4c3 --- /dev/null +++ b/labs/2/src/help.txt @@ -0,0 +1 @@ +Dummy help file