From b9ce5e434dc1e4fd8554cb6cff414658c03c8d71 Mon Sep 17 00:00:00 2001 From: Rob Greene Date: Tue, 19 Jun 2018 20:39:39 -0500 Subject: [PATCH] Allow generating to AppleSingle format. #16. --- tools/st/README.md | 24 ++++++++++- .../bastools/tools/st/GenerateCommand.java | 40 +++++++++++++++++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/tools/st/README.md b/tools/st/README.md index 2887d42..4278408 100644 --- a/tools/st/README.md +++ b/tools/st/README.md @@ -1,10 +1,10 @@ -## Overview +# Overview `st` is a command-line tool to investigate and (ultimately) author shape tables for inclusion in Applesoft programs. Samples are extracted from the [original Mouse Maze](https://github.com/a2geek/mouse-maze-2001/tree/master/doc/original), written in 1983. -## Usage +# Usage ```shell $ st --help @@ -54,6 +54,8 @@ Options: -w, --width= Set width (defaults: text=80, image=1024) ``` +# Features + ## Text extract ```shell @@ -122,3 +124,21 @@ $ st generate --stdout api/src/test/resources/box-longform.st | st extract --std |.XXX.| +-----+ ``` + +## Generate to AppleSingle + +```shell +$ cat api/src/test/resources/box-longform.st | st generate --stdin --stdout --single | asu info --stdin +Real Name: SHAPES.BIN +ProDOS info: + Access: 0xC3 + File Type: 0x06 + Auxtype: 0x6000 +File dates info: + Creation: 2018-06-20T01:36:59Z + Modification: 2018-06-20T01:36:59Z + Access: 2018-06-20T01:36:59Z + Backup: 2018-06-20T01:36:59Z +Data Fork: Present, 14 bytes +Resource Fork: Not present +``` \ No newline at end of file diff --git a/tools/st/src/main/java/io/github/applecommander/bastools/tools/st/GenerateCommand.java b/tools/st/src/main/java/io/github/applecommander/bastools/tools/st/GenerateCommand.java index 6b2967e..7f47c63 100644 --- a/tools/st/src/main/java/io/github/applecommander/bastools/tools/st/GenerateCommand.java +++ b/tools/st/src/main/java/io/github/applecommander/bastools/tools/st/GenerateCommand.java @@ -1,13 +1,18 @@ package io.github.applecommander.bastools.tools.st; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.concurrent.Callable; +import io.github.applecommander.applesingle.AppleSingle; import io.github.applecommander.bastools.api.shapes.ShapeGenerator; import io.github.applecommander.bastools.api.shapes.ShapeTable; import picocli.CommandLine.Command; +import picocli.CommandLine.Help.Visibility; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; @@ -16,6 +21,8 @@ import picocli.CommandLine.Parameters; descriptionHeading = "%n", optionListHeading = "%nOptions:%n") public class GenerateCommand implements Callable { + public static final int BIN = 0x06; + @Option(names = { "-h", "--help" }, description = "Show help for subcommand", usageHelp = true) private boolean helpFlag; @@ -24,23 +31,48 @@ public class GenerateCommand implements Callable { @Option(names = "--stdout", description = "Write to stdout") private boolean stdoutFlag; + + @Option(names = "--single", description = "Write to AppleSingle file (requires address, defaults to 0x6000)") + private boolean applesingleFlag; + + @Option(names = "--address", description = "Address for AppleSingle file", showDefaultValue = Visibility.ALWAYS) + private int address = 0x6000; + + @Option(names = "--name", description = "Filename assign in AppleSingle file", showDefaultValue = Visibility.ALWAYS) + private String realName = "SHAPES.BIN"; @Option(names = { "-o", "--output" }, description = "Write output to file") private Path outputFile; @Parameters(arity = "0..1", description = "File to process") private Path inputFile; - + @Override public Void call() throws IOException { validateArguments(); - ShapeTable st = stdinFlag ? ShapeGenerator.generate(System.in) : ShapeGenerator.generate(inputFile); + ShapeTable st = stdinFlag ? ShapeGenerator.generate(System.in) : ShapeGenerator.generate(inputFile); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + st.write(byteStream); + + if (applesingleFlag) { + AppleSingle applesingle = AppleSingle.builder() + .realName(realName) + .dataFork(byteStream.toByteArray()) + .auxType(address) + .fileType(BIN) + .build(); + byteStream.reset(); + applesingle.save(byteStream); + } + if (stdoutFlag) { - st.write(System.out); + System.out.write(byteStream.toByteArray()); } else { - st.write(outputFile); + try (OutputStream outputStream = Files.newOutputStream(outputFile)) { + outputStream.write(byteStream.toByteArray()); + } } return null;