/* A program to find non-ASCII characters in a file
 *
 * Copyright (C) 2015 Sidney Marshall (swm@cs.rit.edu)
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * This class looks for "funny" characters in a file. Funny
 * characters are bytes that are not between 0x20 and 0x7e or 0x0a (a
 * new-line).
 */
class Clean {
  /**
   * Search files for "funny" characters.
   *
   * @param args file names of files to search
   * @throws Exception This is just a catch-all
   */
  public static void main(String...args) throws Exception {
    for(String filename : args) {
      System.out.println("=====================================");
      System.out.println(filename);
      System.out.println("=====================================");
      InputStream strm = new FileInputStream(filename);
      int lineno = 0;
      ArrayList<Character> chararray = new ArrayList<Character>();
      int c;
      int line = 1;
      while((c = strm.read()) != -1) {
        if(c != '\n') {
          chararray.add((char)c);
        } else {
          int index = 0;
          for(char ch : chararray) {
            if(ch < 32 || ch > 126) {
              StringBuilder sb = new StringBuilder();
              for(char ch1 : chararray) {
                if(ch1 != '\t') {
                  sb.append(ch1);
                } else {
                  sb.append(' ');
                }
              }
              System.out.println("line: " + line);
              System.out.println(sb);
              for(int i = 0; i < index; i++) {
                System.out.print(' ');
              }
              System.out.println('^');
              System.out.println((int)chararray.get(index));
              if(index+1 < chararray.size()) System.out.println((int)chararray.get(index+1));
              if(index+2 < chararray.size()) System.out.println((int)chararray.get(index+2));
              break;
            }
            ++index;
          }
          chararray = new ArrayList<Character>();
          line++;
        }
      }
    }
  }
}  // class Clean
