/* * [Chop.java] * * Summary: Chops tail ends off files. * * Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2014-05-18 initial version * 1.1 2014-07-13 do search for marker starting at the end. Add Behead. */ package com.mindprod.chop; import com.mindprod.commandline.CommandLine; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import static java.lang.System.*; /** * Chops tail ends off files. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2014-07-13 do search for marker starting at the end. Add Behead. * @since 2014-05-18 */ public class Chop { // declarations /** * true if want extra output */ private static final boolean DEBUGGING = false; /** * when program first copyrighted */ private static final int FIRST_COPYRIGHT_YEAR = 2014; /** * undisplayed copyright notice */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; @SuppressWarnings( { "UnusedDeclaration" } ) private static final String RELEASE_DATE = "2014-07-13"; /** * how to use the command line * * @see Behead */ private static final String USAGE = "\nchop.jar [-q] [-charset=\"UTF-8\"] [-before=\"xxx\"] [-after=\"xxx\"] [-s] dirs/files..."; /** * embedded version string. */ @SuppressWarnings( { "UnusedDeclaration" } ) private static final String VERSION_STRING = "1.1"; // /declarations // methods /** * sort items in one file * * @param file file to process * @param marker chop at first occurence of this string. * @param chopAfter true chop just after marker. false chop just before marker. * @param charset Character set * @param quiet true, suppress progress messages. */ private static void processOneFile( final File file, final String marker, final boolean chopAfter, final Charset charset, final boolean quiet ) throws IOException { final String contents = HunkIO.readEntireFile( file, charset ); final int place = contents.lastIndexOf( marker ); if ( place < 0 ) { if ( !quiet ) { out.println( EIO.getCanOrAbsPath( file ) + " left as is." ); return; } } final int end = chopAfter ? place + marker.length() : place; HunkIO.writeEntireFile( file, contents.substring( 0, end ), charset ); if ( !quiet ) { out.println( "File " + EIO.getCanOrAbsPath( file ) + " chopped to " + end + " characters." ); } }// /method /** * @param args -before="xxx" -after="xxx" [-s] dirs/files... * * @throws java.io.IOException */ public static void main( final String[] args ) throws IOException { String before = null; String after = null; boolean quiet = false; Charset charset = null; for ( int i = 0; i < args.length; i++ ) { final String arg = args[ i ]; final int equalPlace = arg.indexOf( '=' ); if ( arg.startsWith( "-" ) && equalPlace >= 0 ) { final String key = arg.substring( 1, equalPlace ).trim(); final String value = arg.substring( equalPlace + 1 ).trim(); // mark this arg handled so it will not fool the command line processor args[ i ] = null; // command line processor has already stripped " " out of parm. switch ( key ) { case "before": before = value; break; case "charset": charset = Charset.forName( value ); break; case "after": after = value; break; case "q": case "v": case "s": /* ignore, part of files */ break; default: throw new IllegalArgumentException( "unrecognised option " + arg + "\n" + USAGE ); } } else { // filenames. CommandLine will deal with it later } } // end for if ( ST.isEmpty( before ) && ST.isEmpty( after ) ) { throw new IllegalArgumentException( "Must have either -before= or -after=.\n" + USAGE ); } if ( !ST.isEmpty( before ) && !ST.isEmpty( after ) ) { throw new IllegalArgumentException( "Cannot have both -before= and -after=.\n" + USAGE ); } final String marker; final boolean chopAfter; if ( ST.isEmpty( after ) ) { marker = before; chopAfter = false; } else { marker = after; chopAfter = true; } if ( charset == null ) { charset = Charset.defaultCharset(); } CommandLine commandLine = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( ExtensionListFilter.COMMON_TEXT_EXTENSIONS ) ); quiet = commandLine.isQuiet(); if ( commandLine.size() == 0 ) { throw new IllegalArgumentException( "No files found to process\n" + USAGE ); } for ( File file : commandLine ) { processOneFile( file, marker, chopAfter, charset, quiet ); } }// /method // /methods }