package io.dindinw.cmdline.tool;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.dindinw.cmdline.CmdLine;
import io.dindinw.cmdline.Option;
import io.dindinw.cmdline.Parser;

/**
 * A Java-version of unix tool 'grep'
 * TODO:
 *   1. code need to refactor
 *   2. only support the -n option
 *   3. my option parser don't handle GNU getOpt() standard, so that I can't support the option like '-aHn'
 * Created by alex on 3/13/15.
 */
public final class JGrep {
    public static boolean show_line_number = false;
    public static void main(String[] args) throws Exception{
        Parser parser = new Parser();
        parser.addOption(new Option("-n","--line-number","print the matched line number"));
        CmdLine cmd = parser.parse(args);

        if (cmd.getArgs().length < 1) {
            System.err.println("Usage: JGrep pattern [file]|System.in "); System.exit(1);
            System.exit(1);
        }
        Pattern p= Pattern.compile(cmd.getArgs()[0]);
        if (cmd.hasOption("-n")){
            //System.out.printf("show line number \n");
           show_line_number = true;
        }
        if (cmd.getArgs().length == 1) {
            process(p,System.in);
        }else{
            for (int i=1; i<cmd.getArgs().length; i++)
                process(p, cmd.getArgs()[i]);
        }

    }
    static void process(Pattern pattern, InputStream input) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(input));

        Matcher m = pattern.matcher("");
        String line = null;
        int lineNum = 0;
        while ((line = br.readLine()) !=null){
            lineNum++;
            m.reset(line);
            if (m.find()){
                String line_number = "";
                if (show_line_number){
                    line_number = ""+lineNum+":";
                }
                System.out.printf("%s%s\n",line_number,line);
            }
        }
    }

    static void process(Pattern pattern, String fileName) throws IOException {
        // Get a FileChannel from the given file.
        process(pattern, new FileInputStream(fileName)); // Map the file's content

    }
}

@@ -673,116 +673,8 @@
 ion%0A
- *   3. my option parser don't handle GNU getOpt() standard, so that I can't support the option like '-aHn'%0A
  * C
@@ -731,26 +731,28 @@
 JGrep %7B%0A
+%0A
     p
-ublic
+rivate
  static 
@@ -785,16 +785,68 @@
  false;%0A
+    private static boolean show_file_name = false;%0A%0A
     publ
@@ -1028,16 +1028,111 @@
 ber%22));%0A
+        parser.addOption(new Option(%22-H%22,%22Always print filename headers with output lines.%22));%0A
         
@@ -1437,53 +1437,78 @@
     
- //System.out.printf(%22
 show
- 
+_
 line
- 
+_
 number 
-%5Cn%22);%0A
+= true;%0A        %7D%0A        if (cmd.hasOption(%22-H%22))%7B%0A 
     
@@ -1515,35 +1515,33 @@
        show_
-line_number
+file_name
  = true;%0A   
@@ -1610,25 +1610,27 @@
 ocess(p,
-System.in
+%22SYSTEM.IN%22
 );%0A     
@@ -1750,23 +1750,23 @@
       %7D%0A
-%0A
     %7D%0A
+%0A
     stat
@@ -1802,25 +1802,23 @@
 rn, 
-InputStream input
+String fileName
 ) th
@@ -1825,32 +1825,33 @@
 rows IOException
+ 
 %7B%0A        Buffer
@@ -1869,151 +1869,465 @@
  = n
-ew BufferedReader(new InputStreamReader(input));%0A%0A        Matcher m = pattern.matcher(%22%22);%0A        String line = null;%0A        int lineNum = 0;
+ull;%0A        if (%22SYSTEM.IN%22.equals(fileName))%7B%0A            br = new BufferedReader(new InputStreamReader(System.in));%0A        %7Delse%7B%0A            br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));%0A        %7D%0A%0A        Matcher m = pattern.matcher(%22%22);%0A        String line = null;%0A        int lineNum = 0;%0A        String line_number = %22%22;%0A        String file =%22%22;%0A        if (show_file_name)%7B%0A            file=fileName+%22:%22;%0A        %7D
 %0A   
@@ -2451,49 +2451,8 @@
 ))%7B%0A
-                String line_number = %22%22;%0A
     
@@ -2597,12 +2597,19 @@
 %25s%25s
+%25s
 %5Cn%22,
+file,
 line
@@ -2656,227 +2656,7 @@
    %7D
-%0A%0A    static void process(Pattern pattern, String fileName) throws IOException %7B%0A        // Get a FileChannel from the given file.%0A        process(pattern, new FileInputStream(fileName)); // Map the file's content%0A%0A    %7D
 %0A%7D%0A

support for option -H, show file name