user9900698
user9900698

Reputation:

TCL file parsing (Loop to read subsequent lines once pattern found)?

Below is a sequence that is repeated multiple times in the file that I will be reading using TCL.

Startpoint: FF1 (rising edge-triggered flip-flop clocked by Clk)
Endpoint: FF2 (rising edge-triggered flip-flop clocked by Clk)
Path Group: Clk
Path Type: max
Point                                           Incr   Path 
----------------------------------------------------------- 
clock Clk (rise edge)                           0.00   0.00
clock network delay (propagated)                1.10 * 1.10
FF1/CLK (fdef1a15)                              0.00   1.10 r
FF1/Q (fdef1a15)                                0.50 * 1.60 r
U2/Y (buf1a27)                                  0.11 * 1.71 r
U3/Y (buf1a27)                                  0.11 * 1.82 r
FF2/D (fdef1a15)                                0.05 * 1.87 r
data arrival time                                      1.87
clock Clk (rise edge)                           4.00   4.00
clock network delay (propagated)                1.00 * 5.00
FF2/CLK (fdef1a15)                                     5.00 r
library setup time                             -0.21 * 4.79
data required time                                     4.79                                           
------------------------------------------------------------ 
data required time                                     4.79
data arrival time                                     -1.87                                           
------------------------------------------------------------                                    
slack (MET) 2.92

The start is indicated by Startpoint and end is indicated by slack.

I am finding a way to write a loop that will read the first line Startpoint.

Then file pointer goes to subsequent lines and I read and do postprocessing on them.

Once Slack line encountered, loop goes to the next Startpoint in the file and same thing repeats.

Only if startpoint line found, I go further and do all the processing.

Just having trouble finding way for the file pointer to shift to next line once Startpoint line is encountered. Similarly, file pointer should shift to next Startpoint line when slack line is encountered.

I am new to TCL and trying to imagine how this would work.

Python equivalent of what I want to acheive is:

open file for read with pointer fp

for line in fp:
   if startpoint found
   for next_line_in_file in fp:
      do post processing until slack line found and then continue from outer for loop

Upvotes: 0

Views: 622

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137567

The general pattern is this:

while {[gets $channel line] >= 0} {
    # Skip lines until the start point
    if {![string match "Startpoint: *" $line]} continue
    set lines [list $line]
    # Accumulate lines until we have a whole section
    while {[gets $channel line] >= 0} {
        lappend lines $line
        if {[string match "slack *" $line]} break
    }
    # You'll need to provide a command — most likely a procedure — for this...
    postProcess $lines
}

The main change from your pseudocode is that this feeds a whole list of lines (representing everything from the Startpoint: to the slack, inclusive) into the post-processing command at once. With plenty of experience, that's the sort of thing that makes complicated handling of a chunked data stream much easier rather than trying to process each line on its own (with a complicated state machine and so on; that's possible, but rather difficult and very error prone).

Upvotes: 2

Related Questions