/* * [Propagate.java] * * Summary: Copies a piece of a file into another file. * * Copyright: (c) 2016-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 2016-06-09 initial version */ package com.mindprod.propagate; import com.mindprod.common18.EIO; import com.mindprod.common18.ST; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.IOException; import static java.lang.System.*; /** * Copies a piece of a file into another file. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2016-06-09 initial version * @since 2016-06-09 */ public final class Propagate { private static final int FIRST_COPYRIGHT_YEAR = 2016; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2016-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2016-06-09"; private static final String USAGE = "Usage: Propagate fromFile toFile startDelimiter endDelimiter"; /** * embedded version string. */ private static final String VERSION_STRING = "1.0"; /** * copy a chuck of delimited text from one file to another * * @param fromFile source * @param toFile targe * @param startDelimiter start delimiter in both files * @param endDelimiter end delimiter in both files * * @throws IOException */ private static void copyChunk( final File fromFile, final File toFile, final String startDelimiter, final String endDelimiter ) throws IOException { final String fromContents = HunkIO.readEntireFile( fromFile, HunkIO.UTF8 ); int fromStart = fromContents.indexOf( startDelimiter ); if ( fromStart < 0 ) { err.println( "Error: missing start delimiter in " + EIO.getCanOrAbsPath( fromFile ) ); System.exit( 1 ); } fromStart += startDelimiter.length(); final int fromEnd = fromContents.indexOf( endDelimiter, fromStart ); if ( fromEnd < 0 ) { err.println( "Error: missing end delimmiter in " + EIO.getCanOrAbsPath( fromFile ) ); System.exit( 1 ); } final String toContents = HunkIO.readEntireFile( toFile, HunkIO.UTF8 ); int toStart = toContents.indexOf( startDelimiter ); if ( toStart < 0 ) { err.println( "Error: missing start delimiter in " + EIO.getCanOrAbsPath( toFile ) ); System.exit( 1 ); } toStart += startDelimiter.length(); final int toEnd = toContents.indexOf( endDelimiter, toStart ); if ( toEnd < 0 ) { err.println( "Error: missing end delimmiter in " + EIO.getCanOrAbsPath( toFile ) ); System.exit( 1 ); } final String newToContents = toContents.substring( 0, toStart ) + fromContents.substring( fromStart, fromEnd ) + toContents.substring( toEnd ); HunkIO.writeEntireFile( toFile, newToContents, HunkIO.UTF8 ); out.println( "Propagate copied " + ( fromEnd - fromStart ) + " characters\nsurrounded by [" + startDelimiter + "] and [" + endDelimiter + "]\nfrom " + EIO.getCanOrAbsPath( fromFile ) + " into " + EIO.getCanOrAbsPath( toFile ) + "." ); } /** * Copy delimited chunk from one text file to another. * * @param args fromFile toFile startDelimiter endDelimiter */ public static void main( String[] args ) { if ( args.length != 4 ) { err.println( USAGE ); System.exit( 2 ); } final File fromFile = new File( args[ 0 ] ); if ( !fromFile.exists() ) { err.println( "Error: " + EIO.getCanOrAbsPath( fromFile ) + " does not exist." ); System.exit( 1 ); } if ( !fromFile.canRead() ) { err.println( "Error: " + EIO.getCanOrAbsPath( fromFile ) + " cannot be read." ); System.exit( 1 ); } final File toFile = new File( args[ 1 ] ); if ( !toFile.exists() ) { err.println( "Error: " + EIO.getCanOrAbsPath( toFile ) + " does not exist." ); System.exit( 1 ); } if ( !( toFile.canRead() && toFile.canWrite() ) ) { err.println( "Error: " + EIO.getCanOrAbsPath( toFile ) + " cannot be written." ); System.exit( 1 ); } final String startDelimiter = args[ 2 ]; if ( ST.isEmpty( startDelimiter ) ) { err.println( "Error: missing start delimiter." ); System.exit( 1 ); } final String endDelimiter = args[ 3 ]; if ( ST.isEmpty( endDelimiter ) ) { err.println( "Error: missing end delimiter." ); System.exit( 1 ); } try { copyChunk( fromFile, toFile, startDelimiter, endDelimiter ); } catch ( IOException e ) { err.println( "Error: " + e.getMessage() ); } } } // end Propagate