home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / oss / cvs-2004 / OpenHamLog / src / OpenHamLog.java,v < prev    next >
Text File  |  2003-07-14  |  4KB  |  154 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @# @;
  6.  
  7.  
  8. 1.1
  9. date    2003.07.14.19.37.57;    author htodd;    state Exp;
  10. branches;
  11. next    ;
  12.  
  13.  
  14. desc
  15. @@
  16.  
  17.  
  18. 1.1
  19. log
  20. @Moving files and starting ant.
  21. @
  22. text
  23. @//: OpenHamLog.java
  24. // $Log$
  25. // Counts words in a file, outputs
  26. // results in sorted form.
  27. import java.io.*;
  28. import java.util.*;
  29.  
  30. public class OpenHamLog {
  31.   private FileInputStream file;
  32.   private StreamTokenizer st;
  33.   private Hashtable counts = new Hashtable();
  34.   OpenHamLog(String filename)
  35.     throws FileNotFoundException {
  36.     try {
  37.       file = new FileInputStream(filename);
  38.             Reader r = new BufferedReader(new InputStreamReader(file));
  39.  
  40.       st = new StreamTokenizer(r);
  41.       st.ordinaryChar('.');
  42.       st.ordinaryChar('-');
  43.             st.wordChars('_','_');
  44.     } catch(FileNotFoundException e) {
  45.       System.out.println(
  46.         "Could not open " + filename);
  47.       throw e;
  48.     }
  49.   }
  50.  
  51.   void cleanup() {
  52.     try {
  53.       file.close();
  54.     } catch(IOException e) {
  55.       System.out.println(
  56.         "file.close() unsuccessful");
  57.     }
  58.   }
  59.   
  60.     void countRecords() {
  61.     try {
  62.       while(st.nextToken() != StreamTokenizer.TT_EOF) {
  63.         String s;
  64.         switch(st.ttype) {
  65.           case StreamTokenizer.TT_EOL:
  66.             s = new String("EOL");
  67.             break;
  68.           case StreamTokenizer.TT_NUMBER:
  69.             s = Double.toString(st.nval);
  70.             break;
  71.           case StreamTokenizer.TT_WORD:
  72.             s = st.sval; // Already a String
  73.             break;
  74.           default: // single character in ttype
  75.             s = String.valueOf((char)st.ttype);
  76.         }
  77.         if(counts.containsKey(s))
  78.           ((Counter)counts.get(s)).increment();
  79.         else
  80.           counts.put(s, new Counter());
  81.       }
  82.       Enumeration keys = this.sortedKeys();
  83.       while(keys.hasMoreElements()) {
  84.         String key = (String)keys.nextElement();
  85.         System.out.println(key + ": "
  86.                  + this.getCounter(key).read());
  87.       }
  88.     } catch(IOException e) {
  89.       System.out.println(
  90.         "st.nextToken() unsuccessful");
  91.     }
  92.   }
  93.  
  94.   void printToken() {
  95.     try {
  96.       while(st.nextToken() !=
  97.         StreamTokenizer.TT_EOF) {
  98.         String s;
  99.         switch(st.ttype) {
  100.           case StreamTokenizer.TT_EOL:
  101.             s = new String("EOL");
  102.             break;
  103.           case StreamTokenizer.TT_NUMBER:
  104.             s = Double.toString(st.nval);
  105.             break;
  106.           case StreamTokenizer.TT_WORD:
  107.             s = st.sval; // Already a String
  108.             break;
  109.           default: // single character in ttype
  110.             s = String.valueOf((char)st.ttype);
  111.         }
  112.                 System.out.println(s);
  113.       }
  114.     } catch(IOException e) {
  115.       System.out.println(
  116.         "st.nextToken() unsuccessful");
  117.     }
  118.   }
  119.     
  120.   Enumeration values() {
  121.     return counts.elements();
  122.   }
  123.   Enumeration keys() { return counts.keys(); }
  124.   Counter getCounter(String s) {
  125.     return (Counter)counts.get(s);
  126.   }
  127.   Enumeration sortedKeys() {
  128.     Enumeration e = counts.keys();
  129.     StrSortVector sv = new StrSortVector();
  130.     while(e.hasMoreElements())
  131.       sv.addElement((String)e.nextElement());
  132.     // This call forces a sort:
  133.     return sv.elements();
  134.  
  135.   }
  136.   public static void main(String[] args) {
  137.     try {
  138.       OpenHamLog wc = new OpenHamLog(args[0]);
  139.       wc.countRecords();
  140. //            wc.printToken();
  141. //      Enumeration keys = wc.sortedKeys();
  142. //      while(keys.hasMoreElements()) {
  143. //        String key = (String)keys.nextElement();
  144. //        System.out.println(key + ": "
  145. //                 + wc.getCounter(key).read());
  146. //      }
  147.       wc.cleanup();
  148.     } catch(Exception e) {
  149.       e.printStackTrace();
  150.     }
  151.   }
  152. } ///:~ 
  153. @
  154.