Skip to content

Commit

Permalink
Merge branch 'MrShoco-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kormushin committed Oct 14, 2014
2 parents dee67b3 + 0972994 commit 09c62ec
Show file tree
Hide file tree
Showing 22 changed files with 10,546 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jars

.classpath
.project
workbench.xmi
*.swp

.settings
.checkstyle
57 changes: 57 additions & 0 deletions src/ru/fizteh/fivt/students/mrshoco/filemap/FileMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import util.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

public final class FileMap {

public static void main(final String[] args) {
File fl = new File("db.dat"/*System.getProperty("db.file")*/);
HashMap<String, String> hm;
try {
hm = Data.load(fl);
} catch (FileNotFoundException e) {
System.out.println("File not found");
return;
} catch (IOException e) {
System.out.println("Error while reading file");
return;
}
Launcher lnch = new Launcher(hm);
if (args.length != 0) {
try {
lnch.run(args);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
Scanner sc = new Scanner(System.in);
while (true) {
try {
System.out.print("$ ");
String input = sc.nextLine().trim();
lnch.run(input.split(" "));
} catch (IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
sc.close();
System.out.println(e.getMessage());
break;
}
}
}

try {
Data.save(fl, hm);
} catch (FileNotFoundException e) {
System.out.println("File not found");
return;
} catch (IOException e) {
System.out.println("Error while reading file");
return;
}
}
}
Empty file.
47 changes: 47 additions & 0 deletions src/ru/fizteh/fivt/students/mrshoco/filemap/util/Data.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Data {
public static HashMap<String, String> load(File fl) throws
IOException, FileNotFoundException {
HashMap<String, String> hm = new HashMap<String, String>();
DataInputStream dis = new DataInputStream(new FileInputStream(fl));

while (dis.available() > 0) {
int keyLen = dis.readInt();
String key = "";
for (int i = 0; i < keyLen; i++) {
key += dis.readChar();
}
int valLen = dis.readInt();
String val = "";
for (int i = 0; i < valLen; i++) {
val += dis.readChar();
}
hm.put(key, val);
}
dis.close();
return hm;
}

public static void save(File fl, HashMap<String, String> hm) throws
IOException, FileNotFoundException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(fl));
for (Map.Entry<String, String> element : hm.entrySet()) {
dos.writeInt(element.getKey().length());
dos.writeChars(element.getKey());
dos.writeInt(element.getValue().length());
dos.writeChars(element.getValue());
}
dos.close();
}
}
79 changes: 79 additions & 0 deletions src/ru/fizteh/fivt/students/mrshoco/filemap/util/Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package util;

import java.util.HashMap;

public class Launcher {
HashMap<String, String> hashMap;

public Launcher(HashMap<String, String> hm) {
hashMap = hm;
}

public void run(String[] cmd) throws Exception {
switch (cmd[0]) {
case "put":
if (cmd.length != 3) {
throw new IndexOutOfBoundsException("Wrong number of arguments");
}
put(cmd[1], cmd[2]);
break;
case "get":
if (cmd.length != 2) {
throw new IndexOutOfBoundsException("Wrong number of arguments");
}
get(cmd[1]);
break;
case "remove":
if (cmd.length != 2) {
throw new IndexOutOfBoundsException("Wrong number of arguments");
}
remove(cmd[1]);
break;
case "list":
if (cmd.length != 1) {
throw new IndexOutOfBoundsException("Wrong number of arguments");
}
list();
break;
case "exit":
if (cmd.length != 1) {
throw new IndexOutOfBoundsException("Wrong number of arguments");
}
throw new Exception("Exit");
default:
System.out.println("Bad command");
}
}

private void put(String key, String value) {
if (hashMap.containsKey(key)) {
System.out.println("overwrite\nold value");
} else {
System.out.println("new");
}
hashMap.put(key, value);
}

private void get(String key) {
if (hashMap.containsKey(key)) {
System.out.println("found\n" + hashMap.get(key));
} else {
System.out.println("not found");
}
}

private void remove(String key) {
if (hashMap.containsKey(key)) {
System.out.println("removed");
hashMap.remove(key);
} else {
System.out.println("not found");
}
}

private void list() {
for (String key : hashMap.keySet()) {
System.out.println(key + " ");
}
}
}
Loading

0 comments on commit 09c62ec

Please sign in to comment.