commit e4080fe882a9cfc78099598fab4dc520e7bccf20
parent 580b8f717113e45c47d4788a5a29b574b2724426
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 3 Jun 2021 00:18:47 +0300
very important commit message
Diffstat:
10 files changed, 149 insertions(+), 62 deletions(-)
diff --git a/c_os2/ex1/ex3.c b/c_os2/ex1/ex3.c
@@ -98,6 +98,7 @@ main(int argc, char *argv[])
struct foo *f; /* Each callback will receive this */
pthread_t *tds; /* Threads */
int totalsum; /* What its name says */
+ int rc; /* scanf(3) return value. */
int i; /* Counter */
f = emalloc(sizeof(struct foo));
@@ -109,16 +110,23 @@ main(int argc, char *argv[])
* we can just give it a new number until it's correct.
*/
do {
- printf("p: ");
- scanf("%d", &f->ntd);
+ printf("\rp: ");
+ /*
+ * Keep scanf(3)'s return value to make sure we
+ * did read valid input.
+ */
+ rc = scanf("%d", &f->ntd);
+ /* Flush input buffer. */
+ (void)getchar();
/* Cannot have < 0 threads. */
- } while (f->ntd < 0);
+ } while (f->ntd < 0 || rc != 1);
do {
- printf("n: ");
+ printf("\rn: ");
scanf("%d", &f->n);
+ (void)getchar();
/* Make sure `n` is positive and also a multiple of `ntd`. */
- } while (f->n < 0 || f->n % f->ntd != 0);
+ } while (f->n < 0 || f->n % f->ntd != 0 || rc != 1);
tds = emalloc(f->ntd * sizeof(pthread_t));
f->arr = emalloc(f->n * sizeof(int));
diff --git a/c_os2/ex2/ex2.c b/c_os2/ex2/ex2.c
@@ -159,9 +159,10 @@ main(int argc, char *argv[])
printf("\rp: ");
/*
* Save the return value of scanf(3) to make sure
- * that we did read valid input.
+ * we did read valid input.
*/
rc = scanf("%d", &f->ntd);
+ /* Flush input buffer. */
(void)getchar();
/* Cannot have less than 1 threads. */
} while (f->ntd < 1 || rc != 1);
@@ -172,7 +173,7 @@ main(int argc, char *argv[])
(void)getchar();
/*
* The number of elements must be greater than 0 (obviously) and
- * a multiple of the number of threads.
+ * a multiple of the total number of threads.
*/
} while (f->n < 0 || f->n % f->ntd != 0 || rc != 1);
diff --git a/c_os2/ex2/ex3_client.c b/c_os2/ex2/ex3_client.c
@@ -127,7 +127,6 @@ main(int argc, char *argv[])
do {
printf("\r%s> n: ", argv0);
rc = scanf("%d", &n);
- /* Flush input buffer */
(void)getchar();
} while (rc != 1);
arr = emalloc(n * sizeof(int));
diff --git a/c_os2/ex2/ex3_server.c b/c_os2/ex2/ex3_server.c
@@ -98,7 +98,6 @@ srv(struct foo *foo)
break;
}
rc = 0;
-
fail:
printf("[%s] connection with client %d closed\n", argv0, f->cfd);
(void)close(f->cfd);
@@ -220,10 +219,11 @@ main(int argc, char *argv[])
/*
* We'll try and see if the input is an IPv4 address. If
- * inet_aton(3) does not fail, the user passed an IPv4 address.
- * However if inet_addr(3) fail, we'll assume the user passed
- * an hostname. That lets us use both hostnames and IPv4
- * addresses as arguments.
+ * inet_aton(3) does not fail, the user did pass an IPv4 address.
+ * However if inet_addr(3) fails, we'll assume the user passed
+ * a hostname. If the hostname was wrong, gethostbyname(3) will
+ * fail. That lets us use both hostnames and IPv4 addresses
+ * as arguments.
*/
if (!inet_aton(*argv, &sin.sin_addr)) {
/*
diff --git a/java_development/population/bin/population/Country.class b/java_development/population/bin/population/Country.class
Binary files differ.
diff --git a/java_development/population/bin/population/ExcelParser.class b/java_development/population/bin/population/ExcelParser.class
Binary files differ.
diff --git a/java_development/population/bin/population/Main.class b/java_development/population/bin/population/Main.class
Binary files differ.
diff --git a/java_development/population/src/population/Country.java b/java_development/population/src/population/Country.java
@@ -0,0 +1,46 @@
+package population;
+
+import java.util.HashMap;
+
+public class Country {
+ private String index = "";
+ private String variant = "";
+ private String name = "";
+ private String notes = "";
+ private String ctrycode = "";
+ private String type = "";
+ private String pntcode = "";
+ private HashMap<String, String> population;
+
+ public void add_population(String k, String v) {
+ population.put(k, v);
+ }
+
+ public String get_index() {
+ return index;
+ }
+
+ public String get_variant() {
+ return variant;
+ }
+
+ public String get_name() {
+ return name;
+ }
+
+ public String get_notes() {
+ return notes;
+ }
+
+ public String get_ctrycode() {
+ return ctrycode;
+ }
+
+ public String get_type() {
+ return type;
+ }
+
+ public String get_pntcode() {
+ return pntcode;
+ }
+}
diff --git a/java_development/population/src/population/ExcelParser.java b/java_development/population/src/population/ExcelParser.java
@@ -1,63 +1,84 @@
package population;
-import java.io.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class ExcelParser {
- private XSSFWorkbook wb;
- private XSSFSheet ss;
+ private final String EST_SHEET = "ESTIMATES";
+ private final String TARGET_TYPE = "Country/Area";
+ private final int TARGET_TYPE_CELL = 5;
+ private final int HEADER_ROW = 16;
+ private final int COUNTRIES_ROW = 44;
+ private ArrayList<Country> countries;
private XSSFRow row;
ExcelParser(String path) throws Exception {
+ XSSFWorkbook wbook;
+ XSSFSheet sheet;
FileInputStream fis;
Iterator<Row> rit;
- Iterator<Cell> cit;
+ String cv = "";
+ int rownum;
+
+ countries = new ArrayList<Country>();
+ fis = new FileInputStream(new File(path));
+ wbook = new XSSFWorkbook(fis);
+ sheet = wbook.getSheet(EST_SHEET);
+ rit = sheet.iterator();
+
+ while (rit.hasNext()) {
+ row = (XSSFRow)rit.next();
+ rownum = row.getRowNum();
+ cv = row.getCell(TARGET_TYPE_CELL).getStringCellValue();
+ /* TODO: Separate header */
+ if (rownum == HEADER_ROW
+ || (rownum>= COUNTRIES_ROW && cv.equals(TARGET_TYPE)))
+ countries.add(read_country(row));
+ }
+ wbook.close();
+ fis.close();
+ }
+
+ private Country read_country(XSSFRow row) throws Exception {
+ Country ctry = new Country();
+ Iterator<Cell> cit = row.cellIterator();
Cell cell;
+ String str = "";
- try {
- fis = new FileInputStream(new File(path));
- wb = new XSSFWorkbook(fis);
- ss = wb.getSheetAt(0);
- rit = ss.iterator();
-
- while (rit.hasNext()) {
- row = (XSSFRow)rit.next();
- cit = row.cellIterator();
- while (cit.hasNext()) {
- cell = cit.next();
- switch (cell.getCellType()) {
- case NUMERIC:
- System.out.print(
- cell.getNumericCellValue() +
- "\t\t");
- break;
- case STRING:
- System.out.print(
- cell.getStringCellValue() +
- "\t\t");
- break;
- case BOOLEAN:
- System.out.print(
- cell.getBooleanCellValue() +
- "\t\t");
- break;
- case BLANK: /* FALLTHROUGH */
- case ERROR:
- case FORMULA:
- case _NONE:
- default:
- break;
- }
- }
- System.out.println();
+ while (cit.hasNext()) {
+ cell = cit.next();
+ switch (cell.getCellType()) {
+ case STRING:
+ str = cell.getStringCellValue();
+ break;
+ case NUMERIC:
+ str = String.valueOf(
+ cell.getNumericCellValue());
+ break;
+ case BOOLEAN:
+ str = String.valueOf(
+ cell.getBooleanCellValue());
+ break;
+ case BLANK:
+ str = "0";
+ case ERROR: /* FALLTHROUGH */
+ case FORMULA:
+ case _NONE:
+ default:
+ break;
}
- fis.close();
- } catch (FileNotFoundException e) {
- throw new Exception(path + ": no such file");
- } catch (IOException e) {
- throw new Exception("io error");
+ System.out.print(str + "\t\t");
}
+ System.out.println();
+
+ return ctry;
+ }
+
+ public ArrayList<Country> get_countries() {
+ return countries;
}
}
diff --git a/java_development/population/src/population/Main.java b/java_development/population/src/population/Main.java
@@ -1,15 +1,27 @@
package population;
+import java.io.*;
+import java.util.ArrayList;
+
public class Main {
- public static void main(String[] args) {
+ public static void main(String[] args) throws Exception {
ExcelParser ep;
+ ArrayList<Country> countries;
+ String path = "";
try {
- ep = new ExcelParser(args[0]);
+ path = args[0];
+ ep = new ExcelParser(path);
+ countries = ep.get_countries();
+ } catch (FileNotFoundException e) {
+ System.err.println(path + ": no such file");
+ return;
} catch (IndexOutOfBoundsException e) {
System.err.println("usage: population xlsx_file");
- } catch (Exception e) {
- System.err.println(e);
+ return;
+ } catch (IOException e) {
+ System.err.println(path + ": io error");
+ return;
}
}
}
\ No newline at end of file