How to access .txt file using tcl PROC function

Viewed 58

I have two files and I am comparing specific lines between two files using the def function.

def readPinFile(filename): 

   result = None
   with open(filename, "r") as file:    

    result = {}
    lastPin = None        
    for line in file:
        lines = line.strip()
        
        if lines[:3] == "PIN": 
            lastPin = lines.split(" ")[1]               
            result[lastPin] = {"LAYER": None, "RECT": None}
        
        if lines[:5] == "LAYER":
            result[lastPin]["LAYER"] = lines.split(" ")[1]
           
        if lines[:4] == "RECT":
            result[lastPin]["RECT"] = lines.split(" ")

return result

pin_of_file1 = readPinFile("osi_hbmp_top_briscm_1.lef") #lef file1
pin_of_file2 = readPinFile("osi_hbmp_top_briscm_2.lef")#lef file2

comparing between pins

with open("file04.txt", "r+") as output_file4:  #compare same pins with layer and location
 for pin, pin_data in pin_of_file1.items():
      if pin in pin_of_file2:
         if pin_of_file2[pin]["LAYER"] == pin_data["LAYER"] and pin_of_file2[pin]["RECT"] == pin_data["RECT"]:
             output_file4.write(pin + "\n\n")

The TCL code I tried to get the same output

proc fileinput {filename} {
set filedata [open filename r]
set file1 [ read $filedata ]
foreach line [split $file1 \n] {
      set pindata { PIN { LAYER {} RECT {} }}
      if {[string match *PIN* $line]} {
          dict lappend pindata PIN $line         
         }
      if {[string match *LAYER* $line]} {
           dict lappend pindata PIN {LAYER{$line}} 
         } 
      if {[string match *RECT* $line]} {
           dict lappend pindata PIN {RECT{$line}}
          } 
      }
  return $pindata
}
set fileinput1 [fileinput osi_hbmp_top_briscm_1.txt]
set fileinput2 [fileinput osi_hbmp_top_briscm_2.txt]

In tcl I am trying to write comparing between the pins section, but I am stuck in the middle. i am fully confused to continue this code

    foreach $pin, $pin_data [gets $fileinput1]
        if{[string match $pin $fileinput2]} 

This is the code I tried

1 Answers

The error trace tells you the immediate problem:

couldn't open "filename": no such file or directory                     
    while executing                     
"open filename r"                      
    (procedure "fileinput" line 2)

You need to give the name of the file, not the name of the variable containing the file name. Tcl cares about whether things are uses or references/names a lot. You fix this by using:

    set filedata [open $filename r]

in the procedure; the added $ is vital as it says "read from the variable and use its value here".

Related