Class JRT
Variable access is achieved through the VariableManager interface. The constructor requires a VariableManager instance (which, in this case, is the compiled Jawk class itself).
Main services include:
- File and command output redirection via print(f).
- File and command input redirection via getline.
- Most built-in AWK functions, such as system(), sprintf(), etc.
- Automatic AWK type conversion routines.
- IO management for input rule processing.
- Random number engine management.
- Input field ($0, $1, ...) management.
All static and non-static service methods should be package-private
to the resultant AWK script class rather than public. However,
the resultant script class is not in the org.sentrysoftware.jawk.jrt
package
by default, and the user may reassign the resultant script class
to another package. Therefore, all accessed methods are public.
- Author:
- Danny Daglas
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
applyRS.static void
Called by AVM/compiled modules to assign local environment variables to an associative array (in this case, to ENVIRON).final void
assignInitialVariables
(Map<String, Object> initial_var_map) Assign all -v variables.static boolean
Compares two objects.static Object
Return an object which is numerically equivalent to one minus a given object.Getter for the fieldinputLine
.Getter for the fieldoutputFiles
.Getter for the fieldpartitioningReader
.static Object
Return an object which is numerically equivalent to one plus a given object.Attempt to close an open stream, whether it is an input file, output file, input process, or output process.void
jrtCloseAll.boolean
jrtConsumeCommandInput.jrtConsumeCommandInputForGetline
(String cmd_string) Retrieve the next line of output from a command, executing the command if necessary and store it to $0.boolean
jrtConsumeFileInput
(String filename) jrtConsumeFileInput.jrtConsumeFileInputForGetline
(String filename) jrtConsumeFileInputForGetline.boolean
jrtConsumeInput
(InputStream input, boolean for_getline, Locale locale) Attempt to consume one line of input, either from stdin or from filenames passed in to ARGC/ARGV via the command-line.jrtGetInputField
(int fieldnum) jrtGetInputField.jrtGetInputField
(Object fieldnum_obj) Retrieve the contents of a particular input field.Retrieve $0.final PrintStream
jrtGetPrintStream
(String filename, boolean append) Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.void
Splits $0 into $1, $2, etc.jrtSetInputField
(Object value_obj, int field_num) Stores value_obj into an input field.jrtSpawnForOutput
(String cmd) Retrieve the PrintStream which shuttles data to stdin for a process, executing the process if necessary.static Integer
Executes the command specified by cmd and waits for termination, returning an Integer object containing the return code.static Random
newRandom
(int seed) newRandom.static void
printfNoCatch
(PrintStream ps, Locale locale, String fmt_arg, Object... arr) printfFunctionNoCatch.static void
printfNoCatch
(Locale locale, String fmt_arg, Object... arr) printfFunctionNoCatch.static Integer
replaceAll
(Object orig_value_obj, Object repl_obj, Object ere_obj, StringBuffer sb, String convfmt, Locale locale) replaceAll.static Integer
replaceFirst
(Object orig_value_obj, Object repl_obj, Object ere_obj, StringBuffer sb, String convfmt, Locale locale) replaceFirst.void
setInputLine
(String inputLine) Setter for the fieldinputLine
.static int
Splits the string into parts separated the regular expression fs.static int
Splits the string into parts separated by one or more spaces; blank first and last fields are eliminated.static String
sprintfNoCatch
(Locale locale, String fmt_arg, Object... arr) sprintfFunctionNoCatch.static String
substr.static String
substr.static int
timeSeed()
timeSeed.static String
toAwkString
(Object o, String convfmt, Locale locale) Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents.static String
toAwkStringForOutput
(Object o, String ofmt, Locale locale) Convert a String, Integer, or Double to String based on the OFMT variable contents.final boolean
Converts an Integer, Double, String, Pattern, or ConditionPair to a boolean.static double
Convert a String, Integer, or Double to Double.static long
Convert a String, Long, or Double to Long.
-
Constructor Details
-
JRT
Create a JRT with a VariableManager- Parameters:
vm
- The VariableManager to use with this JRT.
-
-
Method Details
-
assignInitialVariables
Assign all -v variables.- Parameters:
initial_var_map
- A map containing all initial variable names and their values.
-
assignEnvironmentVariables
Called by AVM/compiled modules to assign local environment variables to an associative array (in this case, to ENVIRON).- Parameters:
aa
- The associative array to populate with environment variables. The module asserts that the associative array is empty prior to population.
-
toAwkString
Convert Strings, Integers, and Doubles to Strings based on the CONVFMT variable contents.- Parameters:
o
- Object to convert.convfmt
- The contents of the CONVFMT variable.locale
- aLocale
object- Returns:
- A String representation of o.
-
toAwkStringForOutput
Convert a String, Integer, or Double to String based on the OFMT variable contents. Jawk will subsequently use this String for output via print().- Parameters:
o
- Object to convert.ofmt
- The contents of the OFMT variable.locale
- aLocale
object- Returns:
- A String representation of o.
-
toDouble
Convert a String, Integer, or Double to Double.- Parameters:
o
- Object to convert.- Returns:
- the "double" value of o, or 0 if invalid
-
toLong
Convert a String, Long, or Double to Long.- Parameters:
o
- Object to convert.- Returns:
- the "long" value of o, or 0 if invalid
-
compare2
Compares two objects. Whether to employ less-than, equals, or greater-than checks depends on the mode chosen by the callee. It handles Awk variable rules and type conversion semantics.- Parameters:
o1
- The 1st object.o2
- the 2nd object.mode
-- < 0 - Return true if o1 < o2.
- 0 - Return true if o1 == o2.
- > 0 - Return true if o1 > o2.
- Returns:
- a boolean
-
inc
Return an object which is numerically equivalent to one plus a given object. For Integers and Doubles, this is similar to o+1. For Strings, attempts are made to convert it to a double first. If the String does not represent a valid Double, 1 is returned.- Parameters:
o
- The object to increase.- Returns:
- o+1 if o is an Integer or Double object, or if o is a String object and represents a double. Otherwise, 1 is returned. If the return value is an integer, an Integer object is returned. Otherwise, a Double object is returned.
-
dec
Return an object which is numerically equivalent to one minus a given object. For Integers and Doubles, this is similar to o-1. For Strings, attempts are made to convert it to a double first. If the String does not represent a valid Double, -1 is returned.- Parameters:
o
- The object to increase.- Returns:
- o-1 if o is an Integer or Double object, or if o is a String object and represents a double. Otherwise, -1 is returned. If the return value is an integer, an Integer object is returned. Otherwise, a Double object is returned.
-
toBoolean
Converts an Integer, Double, String, Pattern, or ConditionPair to a boolean.- Parameters:
o
- The object to convert to a boolean.- Returns:
- For the following class types for o:
- Integer - o.intValue() != 0
- Long - o.longValue() != 0
- Double - o.doubleValue() != 0
- String - o.length() > 0
- UninitializedObject - false
- Pattern - $0 ~ o
-
split
Splits the string into parts separated by one or more spaces; blank first and last fields are eliminated. This conforms to the 2-argument version of AWK's split function.- Parameters:
array
- The array to populate.string
- The string to split.convfmt
- Contents of the CONVFMT variable.locale
- aLocale
object- Returns:
- The number of parts resulting from this split operation.
-
split
Splits the string into parts separated the regular expression fs. This conforms to the 3-argument version of AWK's split function.If fs is blank, it behaves similar to the 2-arg version of AWK's split function.
- Parameters:
fs
- Field separator regular expression.array
- The array to populate.string
- The string to split.convfmt
- Contents of the CONVFMT variable.locale
- aLocale
object- Returns:
- The number of parts resulting from this split operation.
-
getPartitioningReader
Getter for the field
partitioningReader
.- Returns:
- a
PartitioningReader
object
-
getInputLine
Getter for the field
inputLine
.- Returns:
- a
String
object
-
setInputLine
Setter for the field
inputLine
.- Parameters:
inputLine
- aString
object
-
jrtConsumeInput
public boolean jrtConsumeInput(InputStream input, boolean for_getline, Locale locale) throws IOException Attempt to consume one line of input, either from stdin or from filenames passed in to ARGC/ARGV via the command-line.- Parameters:
for_getline
- true if call is for getline, false otherwise.input
- aInputStream
objectlocale
- aLocale
object- Returns:
- true if line is consumed, false otherwise.
- Throws:
IOException
- upon an IO error.
-
jrtParseFields
public void jrtParseFields()Splits $0 into $1, $2, etc. Called when an update to $0 has occurred. -
jrtGetInputField
Retrieve the contents of a particular input field.- Parameters:
fieldnum_obj
- Object referring to the field number.- Returns:
- Contents of the field.
-
jrtGetInputField
jrtGetInputField.
- Parameters:
fieldnum
- a int- Returns:
- a
Object
object
-
jrtSetInputField
Stores value_obj into an input field.- Parameters:
value_obj
- The RHS of the assignment.field_num
- Object referring to the field number.- Returns:
- A string representation of value_obj.
-
jrtConsumeFileInputForGetline
jrtConsumeFileInputForGetline.
-
jrtConsumeCommandInputForGetline
Retrieve the next line of output from a command, executing the command if necessary and store it to $0.- Parameters:
cmd_string
- The command to execute.- Returns:
- Integer(1) if successful, Integer(0) if no more input is available, Integer(-1) upon an IO error.
-
jrtGetInputString
Retrieve $0.- Returns:
- The contents of the $0 input field.
-
getOutputFiles
Getter for the field
outputFiles
.- Returns:
- a
Map
object
-
jrtGetPrintStream
Retrieve the PrintStream which writes to a particular file, creating the PrintStream if necessary.- Parameters:
filename
- The file which to write the contents of the PrintStream.append
- true to append to the file, false to overwrite the file.- Returns:
- a
PrintStream
object
-
jrtConsumeFileInput
jrtConsumeFileInput.
- Parameters:
filename
- aString
object- Returns:
- a boolean
- Throws:
IOException
- if any.
-
jrtConsumeCommandInput
jrtConsumeCommandInput.
- Parameters:
cmd
- aString
object- Returns:
- a boolean
- Throws:
IOException
- if any.
-
jrtSpawnForOutput
Retrieve the PrintStream which shuttles data to stdin for a process, executing the process if necessary. Threads are created to shuttle the data to/from the process.- Parameters:
cmd
- The command to execute.- Returns:
- The PrintStream which to write to provide input data to the process.
-
jrtClose
Attempt to close an open stream, whether it is an input file, output file, input process, or output process.The specification did not describe AWK behavior when attempting to close streams/processes with the same file/command name. In this case, all open streams with this name are closed.
- Parameters:
filename
- The filename/command process to close.- Returns:
- Integer(0) upon a successful close, Integer(-1) otherwise.
-
jrtCloseAll
public void jrtCloseAll()jrtCloseAll.
-
jrtSystem
Executes the command specified by cmd and waits for termination, returning an Integer object containing the return code. stdin to this process is closed while threads are created to shuttle stdout and stderr of the command to stdout/stderr of the calling process.- Parameters:
cmd
- The command to execute.- Returns:
- Integer(return_code) of the created process. Integer(-1) is returned on an IO error.
-
sprintfNoCatch
public static String sprintfNoCatch(Locale locale, String fmt_arg, Object... arr) throws IllegalFormatException sprintfFunctionNoCatch.
- Parameters:
locale
- aLocale
objectfmt_arg
- aString
objectarr
- an array ofObject
objects- Returns:
- a
String
object - Throws:
IllegalFormatException
- if any.
-
printfNoCatch
printfFunctionNoCatch.
-
printfNoCatch
printfFunctionNoCatch.
- Parameters:
ps
- aPrintStream
objectlocale
- aLocale
objectfmt_arg
- aString
objectarr
- an array ofObject
objects
-
replaceFirst
public static Integer replaceFirst(Object orig_value_obj, Object repl_obj, Object ere_obj, StringBuffer sb, String convfmt, Locale locale) replaceFirst.
-
replaceAll
public static Integer replaceAll(Object orig_value_obj, Object repl_obj, Object ere_obj, StringBuffer sb, String convfmt, Locale locale) replaceAll.
-
substr
substr.
-
substr
substr.
-
timeSeed
public static int timeSeed()timeSeed.
- Returns:
- a int
-
newRandom
newRandom.
- Parameters:
seed
- a int- Returns:
- a
Random
object
-
applyRS
applyRS.
- Parameters:
rs_obj
- aObject
object
-