filter.java

来自「一个小型网络仿真器的实现」· Java 代码 · 共 172 行

JAVA
172
字号
/*
   JaNetSim  ---  Java Network Simulator
   -------------------------------------

   This software was developed at the Network Research Lab, Faculty of
   Computer Science and Information Technology (FCSIT), University of Malaya.
   This software may be used and distributed freely. FCSIT assumes no responsibility
   whatsoever for its use by other parties, and makes no guarantees, expressed or
   implied, about its quality, reliability, or any other characteristic.

   We would appreciate acknowledgement if the software is used.

   FCSIT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
   DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
   FROM THE USE OF THIS SOFTWARE.
*/

import java.io.*;

//simple utility to filter simulation log file to produce
//separate log file for each parameter (each log ID)

//(Right now it's not very efficient because it's doing one parse
// over the whole file per ID)
//(Going to write a better one if got time)
//(Or perhaps someone can do it for me?)

class filter {
  public static void main(String []args) {
    if(args.length<1) {
      System.out.println("Usage:");
      System.out.println("       java filter <filename> [every <line>] [indexonly] [<id> ...]");
      System.out.println("\nOptions:");
      System.out.println("       <filename> - the log file name");
      System.out.println("       every <line> - this is to reduce the output");
      System.out.println("                      file size by throwing away some");
      System.out.println("                      lines (put 1 for <line> to get");
      System.out.println("                      everything, which is the default)");
      System.out.println("       indexonly - specify this to generate ID index file only");
      System.out.println("       <id> - the parameter ID (can supply many)");
      System.out.println("              (if not supplied, will log all IDs");
      System.out.println("               and generate an ID index file)");
      return;
    }

    try {
      int i,j;
      boolean indexonly=false;
      int every=1;
      int [] ids=null;
  
      if(args.length>1) {
        for(i=1;i<args.length;i++) {
          if(args[i].equals("every")) {
            i++;
            if(i==args.length) {
              System.out.println("Error: 'every' without a <line>");
              System.exit(1);
            }
            try {
              every=Integer.parseInt(args[i]);
            } catch(NumberFormatException ex) {
              System.out.println("Error: invalid <line> after 'every'");
              System.exit(1);
            }
            if(every<1) {
              System.out.println("Error: invalid <line> after 'every',");
              System.out.println("       must be a positive integer!");
              System.exit(1);
            }
          }
          else if(args[i].equals("indexonly")) {
            indexonly=true;
          }
          else { //assume all the rest are IDs
            ids=new int[args.length-i];
            for(j=0;j<ids.length;j++) {
              try {
                ids[j]=Integer.parseInt(args[i+j]);
              } catch(NumberFormatException ex) {
                System.out.println("Error: bad ID '"+args[i+j]+"'");
                System.exit(1);
              }
            }
            break; //IDs should be the last arguments
          }
        }
      }

      if(ids==null) { //need one pass to get all the IDs
        java.util.List idlist=new java.util.ArrayList();
        BufferedReader infile=new BufferedReader(new FileReader(args[0]));
        PrintWriter outfile=new PrintWriter(new BufferedWriter(new FileWriter(args[0]+".index")));
        for(;;) {
          String aline=infile.readLine();
          if(aline==null) break; //end of file

          //get first token and look for "ID" mark
          int sp1=aline.indexOf(' ');
          String tok1=aline.substring(0,sp1);
          if(tok1.equals("ID")) {
            int sp2=aline.indexOf(' ',sp1+1);
            idlist.add(aline.substring(sp1+1,sp2)); //get next token (should be the ID)
            outfile.println(aline); //the index file entry
          }
        }
        outfile.close();
        infile.close();

        if(indexonly) System.exit(0); //stop here if only index file is needed

        ids=new int[idlist.size()];
        for(i=0;i<idlist.size();i++) {
          try {
            ids[i]=Integer.parseInt((String)idlist.get(i));
          } catch (NumberFormatException ex) {
            System.out.println("Error: can't read parameter index (may be corrupted log file)");
            System.exit(1);
          }
        }
      }

      for(i=0;i<ids.length;i++) {
        int counter=every;
        BufferedReader infile=new BufferedReader(new FileReader(args[0]));
        PrintWriter outfile=new PrintWriter(new BufferedWriter(new FileWriter(args[0]+".id"+ids[i])));

        for(;;) {
          String aline=infile.readLine();
          if(aline==null) break; //end of file
  
          //get first token
          int sp1=aline.indexOf(' ');
          String tok1=aline.substring(0,sp1);
          if(tok1.charAt(0)=='#') continue; //skip specials
          if(tok1.equals("ID")) continue; //skip ID definition line
          try {
            if(Integer.parseInt(tok1)!=ids[i]) continue;
          } catch(NumberFormatException ex) {
            System.out.println("Error: can't parse ID (may be corrupted log file)");
            infile.close();
            outfile.close();
            System.exit(1);
          }
  
          //get next token (should be the tick)
          int sp2=aline.indexOf(' ',sp1+1);
          String tok2=aline.substring(sp1+1,sp2);
  
          //get the last token (the value)
          String tok3=aline.substring(sp2+1);

          if(counter==every) {
            outfile.println(tok2+" "+tok3); //only output tick and value
            counter=0;
          }
          counter++;
        }

        outfile.close();
        infile.close();
      }

      System.out.println("Done.");
    } catch(IOException exio) {
      System.out.println("Error: you've got this IO exception - ");
      System.out.println(exio.toString());
      System.exit(1);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?