Advertisement
Guest User

fildir program

a guest
Aug 29th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class fildir{
  5.  
  6.     int globalID;
  7.     fildir()
  8.     {
  9.         Scanner input = new Scanner(System.in);
  10.  
  11.         HashMap<String, String> map;
  12.         Directory head = null;
  13.         Directory curr = null;
  14.         int cse = 1;
  15.         while(true)
  16.         {
  17.             String command = input.next();
  18.  
  19.             if(command.equals("end")) break;
  20.  
  21.             if(command.equals("init"))
  22.             {
  23.                 System.out.println("File system #" + cse + ":\n");
  24.                 head = new Directory("home", 0, null);
  25.                 curr = head;
  26.                 map = new HashMap<String, String>();
  27.                 globalID++;
  28.             }
  29.             else if(command.equals("mkdir"))
  30.             {
  31.                 String name = input.next();
  32.                 head = AddDirectory(head, name, curr);
  33.             }
  34.             else if(command.equals("mkfile"))
  35.             {
  36.                 String name = input.next();
  37.                 head = AddFile(head, name, curr);
  38.             }
  39.             else if(command.equals("cd"))
  40.             {
  41.                 String[] path = input.next().split("/");
  42.  
  43.                 for(int i = 0; i < path.length; i++)
  44.                 {
  45.                     if(path[i].equals(".."))
  46.                     {
  47.                         //need to move up
  48.                         curr = curr.up;
  49.                     }
  50.                     else
  51.                     {
  52.                         //need to move forward.
  53.                         for(int j = 0; j < curr.sublist.size(); j++)
  54.                         {
  55.                             if(curr.sublist.get(j).name == path[i])
  56.                                 curr = curr.sublist.get(j);
  57.                         }
  58.  
  59.                     }
  60.                 }
  61.             }
  62.             else if(command.equals("ls"))
  63.             {
  64.                 Directory temp = curr;
  65.                 String path = getPath(temp);
  66.  
  67.                 System.out.println("Listing for " + path + ":");
  68.                 printDirectories(curr);
  69.                 printFiles(curr);
  70.                 System.out.println();
  71.             }
  72.  
  73.  
  74.  
  75.         }
  76.     }
  77.  
  78.     String getPath(Directory curr)
  79.     {
  80.         String path = "/" + curr.name;
  81.         curr = curr.up;
  82.         while(curr != null)
  83.         {
  84.             path = "/" + curr.name + path;
  85.         }
  86.  
  87.         return path;
  88.     }
  89.  
  90.     void printDirectories(Directory curr)
  91.     {
  92.         Collections.sort(curr.sublist);
  93.         for(int i = 0; i < curr.sublist.size(); i++)
  94.             System.out.println(curr.sublist.get(i).name);
  95.  
  96.     }
  97.  
  98.     void printFiles(Directory curr)
  99.     {
  100.         Collections.sort(curr.files);
  101.         for(int i = 0; i < curr.files.size(); i++)
  102.             System.out.println(curr.files.get(i));
  103.     }  
  104.  
  105.     Directory AddDirectory(Directory head, String name, Directory curr)
  106.     {
  107.         if(head.id == curr.id)
  108.         {
  109.             head.sublist.add(new Directory(name, globalID, head));
  110.             globalID++;
  111.             return head;
  112.         }
  113.  
  114.         for(int i = 0; i < head.sublist.size(); i++)
  115.         {
  116.             Directory t = AddDirectory(head.sublist.get(i), name, curr);
  117.  
  118.             if(t != null)
  119.                 return head;
  120.  
  121.         }
  122.         return null;
  123.     }
  124.  
  125.     Directory AddFile(Directory head, String name, Directory curr)
  126.     {
  127.         if(head.id == curr.id)
  128.         {
  129.             head.files.add(name);
  130.             return head;
  131.         }
  132.  
  133.         for(int i = 0; i < head.sublist.size(); i++)
  134.         {
  135.             Directory t = AddFile(head.sublist.get(i), name, curr);
  136.  
  137.             if(t != null)
  138.                 return head;
  139.  
  140.         }
  141.         return null;
  142.     }
  143.     public static void main(String[] args)
  144.     {
  145.         new fildir();
  146.     }
  147. }
  148.  
  149. class Directory implements Comparable<Directory>
  150. {
  151.     String name;
  152.     ArrayList<Directory> sublist;
  153.     ArrayList<String> files;
  154.     int id;
  155.     Directory up;
  156.     Directory(String name, int id, Directory up)
  157.     {
  158.         this.name = name;
  159.         this.id = id;
  160.         this.up = up;
  161.         sublist = new ArrayList<Directory>();
  162.         files = new ArrayList<String>();
  163.    
  164.     }
  165.  
  166.     @Override
  167.     public int compareTo(Directory other)
  168.     {
  169.         return this.name.compareTo(other.name);
  170.     }
  171. }
  172.  
  173. /*
  174. init
  175. mkfile temp
  176. mkdir COURSES
  177. mkfile junk
  178. mkdir CONTESTS
  179. ls
  180. cd COURSES
  181. mkdir COP4020
  182. cd COP4020
  183. mkfile exam1
  184. mkfile exam3
  185. mkfile exam2
  186. mkfile EXAM1
  187. ls
  188. cd ..
  189. mkdir COT4810
  190. cd COT4810
  191. cd ../../CONTESTS
  192. mkdir REAL
  193. mkfile scoring
  194. mkdir PRACTICE
  195. ls
  196. cd REAL
  197. mkfile temp
  198. mkdir junk
  199. cd ../../COURSES/COT4810
  200. ls
  201. cd ..
  202. ls
  203. init
  204. mkfile aaa
  205. mkfile bbb
  206. mkdir Level1
  207. cd Level1
  208. mkdir Level2a
  209. mkdir Level2b
  210. cd Level2a
  211. mkdir Level3
  212. cd Level3
  213. mkdir Level4
  214. cd ../../..
  215. ls
  216. end
  217.  
  218.  
  219. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement