There are three types of output routines, out(), outf() and the do_print() method. The out() routine prints expressions to the output, with a new line at the end. The syntax of the out() routine is as follows:
out() out(item: exp, ...);
The outf() routine prints formatted expressions to output, with no new line at the end. The syntax of the outf() routine is as follows:
outf(format: string, item: exp, ...);
The format string is very similar to C style format syntax with minor variations. The format string for the outf() routine uses the following syntax:
"%[0|-][#][min_width][.max-chars] (s|d|x|b|o|u)"
Table 6-3 describes the characters used in the formatting of the outf() string.
0 | Pads with 0 instead of blanks. |
- | Aligns left. The default is to align right. |
min_width | Specifies the minimum number of characters and digits. If there are not enough characters and digits, the expression is padded. |
max_chars | Specifies the maximum number of characters and digits. Extra characters and digits are truncated. |
s | Converts the expression to a string. |
d | Prints a numeric expression in decimal format. |
# | Adds 0x before the number. Can be used only with the x (hexadecimal) format specifier. Examples: %#x, %#010x |
x | Prints a numeric expression in hex format. With the optional # character, adds 0x before the number. |
b | Prints a numeric expression in binary format. |
o | Prints a numeric expression in octal format. |
u | Prints integers (int and uint) in uint format. |
Example 6-12 shows the usage of out() and outf() routines.
Examples of out() and outf() routines <' struct pkt { protocol: [ethernet, atm, other]; legal : bool; data[2]: list of byte; }; extend sys { pkts[5]: list of pkt; //List of 5 packets m1() is { //Method definition out(); //Empty new line printed out("ID of first packet is ", pkts[0]); //Print first packet //Print formatted data using outf() outf("%s %#x","pkts[1].data[0] is ", pkts[1].data[0]); out(); //Empty new line printed }; run()is also {m1()}; //Call m1() from the run method }; '>
The do_print() method is a predefined method for any struct or unit and is used to print struct or unit information. The syntax for the do_print() method is as follows:
[exp.]do_print() ; //exp is either a struct or unit instance.
This method is called by the print action whenever you print the struct. It is common to invoke the print action which implicitly calls the do_print() method for that struct. Example 6-13 shows the invocation of the print action.
Example shows the usage of a print action. This print action implicitly calls the do_print() method for that struct instance. The print action prints the fields of the struct in a nicely formatted manner. <' struct a { i: int; s: string; // do_print() is a built in method for every struct or unit }; extend sys { a_struct: a; //Instantiate a struct of type a m() is { print a_struct; //Print the a_struct fields in a nicely //formatted manner. The print action //implicitly calls the a_struct.do_print() //method }; }; '>