/* * [MASMTidy.java] * * Summary: Tidies MASM assembler source, aligning it in four columns. * * Copyright: (c) 2011-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 2011-01-01 initial version * 1.1 2011-01-05 remove excess blank lines */ package com.mindprod.masmtidy; import com.mindprod.commandline.CommandLine; import com.mindprod.common18.EIO; import com.mindprod.filter.AllButSVNDirectoriesFilter; import com.mindprod.filter.ExtensionListFilter; import com.mindprod.hunkio.HunkIO; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import static java.lang.System.*; /** * Tidies MASM assembler source, aligning it in four columns. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2011-01-05 remove excess blank lines * @since 2011-01-02 */ public final class MASMTidy { private static final int FIRST_COPYRIGHT_YEAR = 2011; /** * undisplayed copyright notice. * * @noinspection UnusedDeclaration */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; /** * date this version released. * * @noinspection UnusedDeclaration */ private static final String RELEASE_DATE = "2011-01-05"; /** * how to use the command line */ private static final String USAGE = "\nMASMTidy needs a filename.asm or a space-separated list of filenames, " + "with optional -s -q -v switches."; /** * embedded version string. * * @noinspection UnusedDeclaration */ private static final String VERSION_STRING = "1.1"; /** * constructor, not used. * * @noinspection WeakerAccess */ private MASMTidy() { } /** * fix amps in one file. * * @param fileBeingProcessed the file currently being processed. * @param detail 0=out output at all, 1=just files changed, 2=all files. * * @throws java.io.IOException if cannot read and write the file. * @noinspection SameParameterValue, WeakerAccess */ private static void tidyMASMFile( File fileBeingProcessed, int detail ) throws IOException { // don't need to test extension. Filter handles that. String big = HunkIO.readEntireFile( fileBeingProcessed ); String result = MASMState.align( big ); if ( result.equals( big ) ) { // nothing changed. No need to write results. if ( detail >= 2 ) { out.println( "- " + fileBeingProcessed.getName() ); } return; } // generate output into a temporary file until we are sure all is ok. // create a temp file in the same directory as filename if ( detail >= 1 ) { // it changed out.println( "* " + fileBeingProcessed.getName() ); } final File tempFile = HunkIO.createTempFile( "temp_", ".tmp", fileBeingProcessed ); FileWriter emit = new FileWriter( tempFile ); emit.write( result ); emit.close(); // successfully created output in same directory as input, // Now make it replace the input file. HunkIO.deleteAndRename( tempFile, fileBeingProcessed ); } /** * fixes ampersands in HTML files. * * @param args names of files to process, dirs, files, -q -s, ., no wildcards. */ public static void main( String[] args ) { // gather all the files mentioned on the command line. // either directories, files, with -s and subdirs option. // warning. Windows expands any wildcards in a nasty way. // do not use wildcards. // See http://mindprod.com/jgloss/wildcard.html out.println( "Gathering masm files to tidy..." ); CommandLine commandLine = new CommandLine( args, new AllButSVNDirectoriesFilter(), new ExtensionListFilter( "asm", "asmfrag" ) ); if ( commandLine.size() == 0 ) { throw new IllegalArgumentException( "No files found to process\n" + USAGE ); } final boolean quiet = commandLine.isQuiet(); for ( File file : commandLine ) { try { // -q gives no output at all, otherwise just files that changed. tidyMASMFile( file, quiet ? 0 : 1 ); } catch ( FileNotFoundException e ) { out.println( "Error: " + EIO.getCanOrAbsPath( file ) + " not found." ); } catch ( Exception e ) { out.println( e.getMessage() + " in file " + EIO.getCanOrAbsPath( file ) ); } } // end for } // end main }