diff options
Diffstat (limited to 'base/src/main/java/bjc/utils')
5 files changed, 177 insertions, 47 deletions
diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java index 5b14a58..6ab4cec 100644 --- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java @@ -26,36 +26,6 @@ public class BlockReaderCLI { /* Logger. */ private final Logger LOGGER = Logger.getLogger(BlockReaderCLI.class.getName()); - /** - * The state of the block reader. - * - * @author Ben Culkin - */ - public static class BlockReaderState { - /** - * All of the configured block readers. - */ - public final Map<String, BlockReader> readers; - /** - * All of the configured I/O sources. - */ - public final Map<String, Reader> sources; - - /** - * Create a new set of state for the block reader. - * - * @param readers - * The set of configured block readers. - * - * @param sources - * The set of configured I/O sources. - */ - public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) { - this.readers = readers; - this.sources = sources; - } - } - /* Our state. */ private BlockReaderState stat; diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java new file mode 100644 index 0000000..c528e6a --- /dev/null +++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java @@ -0,0 +1,36 @@ +package bjc.utils.cli.objects; + +import bjc.utils.ioutils.blocks.BlockReader; + +import java.io.Reader; +import java.util.Map; + +/** + * The state of the block reader. + * + * @author Ben Culkin + */ +public class BlockReaderState { + /** + * All of the configured block readers. + */ + public final Map<String, BlockReader> readers; + /** + * All of the configured I/O sources. + */ + public final Map<String, Reader> sources; + + /** + * Create a new set of state for the block reader. + * + * @param readers + * The set of configured block readers. + * + * @param sources + * The set of configured I/O sources. + */ + public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) { + this.readers = readers; + this.sources = sources; + } +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java index 45d27ce..787b91a 100644 --- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java @@ -1,6 +1,12 @@ package bjc.utils.cli.objects; +import bjc.utils.funcutils.StringUtils; +import bjc.utils.ioutils.format.CLFormatter; + +import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.function.UnaryOperator; @@ -104,26 +110,40 @@ public class DefineCLI { * The command to handle * @param interactive * Whether or not our I/O stream is interactive + * @return The status of the executed command. */ - public void handleCommand(Command com, boolean interactive) { + public CommandStatus handleCommand(Command com, boolean interactive) { switch(com.nameCommand) { case "def-string": + return defString(com); + case "def-format": + return defFormat(com); + case "bind-format": + return bindFormat(com); default: LOGGER.severe(com.error("Unknown command %s\n", com.nameCommand)); - break; + return FAIL; } } private CommandStatus defString(Command com) { - String remn = com.remnCommand; + List<String> arguments = StringUtils.processArguments(com.remnCommand); + + if(arguments.size() < 1) { + LOGGER.severe(com.error( + "def-string expects at least one argument: the name of the string to bind")); + return FAIL; + } - int idx = remn.indexOf(' '); - if(idx == -1) { - LOGGER.warning(com.warn("Binding empty string to name '%s'\n", remn)); - idx = remn.length(); + String name = arguments.get(0); + String strang; + + if(arguments.size() < 2) { + LOGGER.warning(com.warn("Binding empty string to name '%s'\n", name)); + strang = ""; + } else { + strang = arguments.get(1); } - String name = remn.substring(0, idx); - String strang = remn.substring(idx); if(stat.strings.containsKey(name)) { LOGGER.warning(com.warn("Shadowing string '%s'\n", name)); @@ -135,15 +155,23 @@ public class DefineCLI { } private CommandStatus defFormat(Command com) { - String remn = com.remnCommand; + List<String> arguments = StringUtils.processArguments(com.remnCommand); - int idx = remn.indexOf(' '); - if(idx == -1) { - LOGGER.warning(com.warn("Binding empty format to name '%s'\n", remn)); - idx = remn.length(); + if(arguments.size() < 1) { + LOGGER.severe(com.error( + "def-format expects at least one argument: the name of the format to bind")); + return FAIL; + } + + String name = arguments.get(0); + String fmt; + + if(arguments.size() < 2) { + LOGGER.warning(com.warn("Binding empty format to name '%s'\n", name)); + fmt = ""; + } else { + fmt = arguments.get(1); } - String name = remn.substring(0, idx); - String fmt = remn.substring(idx); if(stat.formats.containsKey(name)) { LOGGER.warning(com.warn("Shadowing format '%s'\n", name)); @@ -155,7 +183,54 @@ public class DefineCLI { } private CommandStatus bindFormat(Command com) { - String[] parts = com.remnCommand.split(" "); + List<String> strings = StringUtils.processArguments(com.remnCommand); + + if(strings.size() < 2) { + LOGGER.severe(com.error( + "Binding a format requires at least two arguments: the format to bind, and the name to bind it to.")); + return FAIL; + } + + String formatName = strings.get(0); + if(!stat.formats.containsKey(formatName)) { + LOGGER.severe(com.error("Unknown format %s", formatName)); + return FAIL; + } + + String bindName = strings.get(1); + if(stat.strings.containsKey(bindName)) { + LOGGER.warning(com.warn("Shadowing string '%s'", bindName)); + } + + List<String> fillIns = new ArrayList<>(strings.size() - 1); + + Iterator<String> itr = fillIns.iterator(); + /* Skip the format name and bind var. */ + itr.next(); + itr.next(); + + while(itr.hasNext()) { + String name = itr.next(); + + if(name.startsWith("$")) { + String varName = name.substring(1); + + if(stat.strings.containsKey(varName)) { + fillIns.add(stat.strings.get(varName)); + } else { + LOGGER.severe(com.error("Unknown string '%s'", varName)); + return FAIL; + } + } else { + fillIns.add(name); + } + } + + CLFormatter fmt = new CLFormatter(); + + String formatted = fmt.formatString(stat.formats.get(formatName), fillIns); + + stat.strings.put(bindName, formatted); return SUCCESS; } diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java index 0080dd1..1d0b060 100644 --- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -1,9 +1,14 @@ package bjc.utils.funcutils; +import java.util.ArrayList; import java.util.Deque; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import bjc.utils.data.BooleanToggle; +import bjc.utils.parserutils.TokenUtils; + import com.ibm.icu.text.BreakIterator; /** @@ -224,4 +229,29 @@ public class StringUtils { return strang.substring(0, strang.indexOf(vx)); } + + /** + * Split a line into a series of space-separated arguments, including + * string literals. + * + * @param com + * The command to split from + * @return The split arguments. + */ + public static List<String> processArguments(String com) { + List<String> strings = new ArrayList<>(); + + BooleanToggle togg = new BooleanToggle(); + + for(String strang : TokenUtils.removeDQuotedStrings(com)) { + if(togg.get()) { + strings.add(TokenUtils.descapeString(strang)); + } else { + for(String strung : strang.split("\\s+")) { + strings.add(strung); + } + } + } + return strings; + } } diff --git a/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java b/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java index 8172011..1ac97a6 100644 --- a/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java +++ b/base/src/main/java/bjc/utils/ioutils/format/CLFormatter.java @@ -111,6 +111,25 @@ public class CLFormatter { return sb.toString(); } + + /** + * Format a string in the style of CL's FORMAT. + * + * @param format + * The format string to use. + * @param params + * The parameters for the string. + * @return The formatted string. + */ + public String formatString(String format, Iterable<Object> params) { + StringBuffer sb = new StringBuffer(); + /* Put the parameters where we can easily handle them. */ + Tape<Object> tParams = new SingleTape<>(params); + + doFormatString(format, sb, tParams); + + return sb.toString(); + } /** * Fill in a partially started format string. |
