• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. How to parse the contents of a file?

Stats

  • Locked Locked
  • Replies 15
  • Subscribers 145
  • Views 24575
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to parse the contents of a file?

Messi
Messi over 14 years ago

Hi everyone,

    If for example i have some file say rules file with contents of file being : <ruleno.> <rule name> <value>.

   How to parse the contents of this file and store in  table format?

  Can i get code for this?

Thanks,

  Messi

  • Cancel
  • Messi
    Messi over 14 years ago

    Hi,

    Also i found my output to be in format like:

       "R2B:  ("          ACTIVE" "   ACTIVE SPACING  0.3\n")

    How can i make my ouput luk sumthng like:

       "R2B:   ACTIVE   ACTIVE SPACING   0.3\n"

    Is it done by getting length of parsed output and then removing brackets??This is just to make my output luk better and better formatted.

    Regards,

    Messi

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Messi
    Messi over 14 years ago

    Hi,

     in my input code i used comma as my delimiter.i used parseString( line "," ) and i found the output as i mentioned above...I am finding trouble in formatting it.

    Thanks,

    Messi

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • dmay
    dmay over 14 years ago

    Messi,
    Your requests are not consistent. This formum is not intended for coding solutions to meet everyone's needs, we are simply here to provide help and suggestions to get you started. Your original posts did not show you had commas in your input. One of your other posts indicated you just wanted to write the data back to a file. Yet another post said you wanted the data in a table. Now it becomes apparent that you want your data in a table, you want to write it to a file in nearly the same format as it came in with better tabulation and now your input has commas. You also mentioned your code was failing but never pasted any code.

    Some comments based on your last three posts:
    If you want to throw out your header line you can add a line before you add data to the table:
         when(tokens
            unless(car(tokens)=="RULE"
               table[car(tokens)] = cdr(tokens)
            )
          )


    Tables do not maintain order like lists do, but they are much more efficient. If your rules are in alphabetical order, then they can be sorted before you print them out. I simply used the printstruct command to show the contents of the table. If you want to write the data back out to a file in a "cleaner" format, you can write it out as you read it in AND store it in a table to be used later in your session. If you want to loop through the table and write the data out, the best you can do is sort the keys of the table.
    foreach(key sort(table~>? 'alphalessp)
        printf("%-10s:    %L\n" key table[key])
    )

    As far as the format of the output, when you started using a parseString with commas, you end up leaving all of the spaces in the data. Doing a parseString on the whitespace (no arguments after the string), will get rid of the extra space, but will split stuff like "POLY WIDTH" into two separate list items. If all you are doing is reading in a text file and writing it back out, Skill is NOT the ideal language for this. In this case, if you must use commas, you'll probably want to get familiar with commands like rexCompile and rexReplace so that you can remove leading spaces.

    a="    MY RULE"
    rexCompile("^ *")
    b=rexReplace(a "" 0)

    Another option is to use: buildString(parseString(a))
    This parses out the spaces and rebuilds the string with only one space between each word.

    Example:
    rexCompile("^ *")
    foreach(key sort(table~>? 'alphalessp)
        printf("%s:\t" key)
        foreach(col table[key]
            col = rexReplace(col "" 0)
            printf("%-15s \n" col)
        )
        printf("\n")
    )

    So a final solution could be:
    procedure(parseFile( @optional (ipFile "/tmp/rule.txt" ))
      let( ( tokens table fpFile value key line)
        table = makeTable("ruleTable" nil)
        fpFile = infile( ipFile )
        while( gets( line fpFile )
          tokens = parseString( line ",")
          when(tokens
            value = nil
            unless(rexMatchp("RULE NO" car(tokens))
              rexCompile("^ *")
              key = rexReplace(car(tokens) "" 0)
              foreach(col cdr(tokens)
                  col = buildString(parseString(col))
                  value = append1(value col)
              )
              when(key && value
                  table[key] = value
              )
            )
          )
        ) ;while file
        close( fpFile )
        foreach(key sort(table~>? 'alphalessp)
            printf("%s:  " key)
            foreach(col table[key]
                printf("%-15s " col)
            )
            printf("\n")
        )
        table
      ) ;let
    ) ;procedure

    Derek

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Messi
    Messi over 14 years ago

    Hi, thanks for the help

     My code was this: This had space as delimiter .

    procedure( parseFile( fileName )
    let( (ruleFile line token tmpList Ctr wordlist)
     
        unless( ruleFile = infile( fileName)
          error( "Cannot open file %L for reading" fileName)
        ) ; unless
        
        Table = makeTable( "MY_TABLE" nil)
        Ctr = 1
        token = car( line)
        tmpList = cons( token tmpList)
        Table[token] = list( "list(")
        gets( line ruleFile)
        Ctr = Ctr + length(
      setof( paren parseString( line "") paren == "("))
        Ctr = Ctr - length(
      setof( paren parseString( line "") paren == ")")) 
      
        wordList = parseString( line " \n\t")
         while( Ctr != 0 && line
             Table[token] = cons( line Table[token])
                    gets( line ruleFile)
        wordList = parseString( line||"" " \n\t")
            Ctr = Ctr + length(
          setof( paren parseString( line||"" "") paren == "("))
             Ctr = Ctr - length(
        setof( paren parseString( line||"" "") paren == ")"))
           ) ; while 
         if( Ctr == 0 then
             Table[token] = reverse( cons( line
               Table[token]))
           else
      
      Table[token] = "ERROR "
           ) ; if
          );list      
          
          
      
        close(ruleFile)
        list( reverse( tmpList) Table )
      
     
       );let
     );procedure
       
    anyway thanks, i was experimenting with my input file and trying to bring changes in my code to as i felt that is the best way to learn.

     

    Regards,

    Messi

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • ztzg
    ztzg over 12 years ago
    Hi Sanramz,

    What Andrew said.

    You are also missing a level of parens in the let( (cv) ... ) of
    create_boundary. As I was reindenting your code to get a better view, I
    couldn't help but split it into a couple of procedures as suggested by
    Andrew; the following seems to work for me:

    ;; File format:
    ;;
    ;; cell_name llx lly urx ury
    ;; BUF 0 0 4.448 4.424
    ;; AND 0 0 4.448 4.424

    procedure(MyCreateBoundary(libName cellName llx lly urx ury)
    let( (cv)
    cv = dbOpenCellViewByType(libName cellName "layout" "maskLayout" "w")
    dbCreateRect(cv list("prBoundary" "drawing") list(llx:lly urx:ury))
    dbSave(cv)
    )
    ) ; procedure

    procedure(MyCreateCells(libName listPath)
    let( (inPort line fields values)
    inPort = infile(listPath)
    if(inPort then
    while(gets(line inPort)
    ;; Split on *any* whitespace--gets rid of the trailing \n.
    fields = parseString(line)
    ;; Will contain nils if some fields cannot be converted.
    values = mapcar('atof (cdr fields))
    if(length(fields) == 5 && !memq(nil values) then
    ;; Looks good.
    apply('MyCreateBoundary libName car(fields) values)
    else
    warn("Skipping malformed line with fields %L" fields)
    )
    )
    close(inPort)
    else
    error("Unable to open %L for reading" listPath)
    ) ; if
    ) ; let
    ) ; procedure

    MyCreateCells("test" "/home/san/tiler/cell_list")

    Cheers, -D

    P.-S. — Pretty version at http://s.crosstwine.com/spb/487c57af. Feel
    free to use the reformatter to make your code easier to read!


    Andrew Beckett writes:
    > Two problems (at least, may be more because I didn't try the code)
    >
    > 1. The function create_boundary is not defined until after it's called, which is why you
    > have the problem you're seeing. If the main bit of the code had been contained within a
    > function definition, that would have been OK, because loading the file would have
    > defined the functions rather than executing them. In other words, if you'd put procedure
    > (createBoundaryFromFile(ListPath)... around your main code, the forward reference would
    > have been fine because you would not have been calling create_boundary before it had
    > been defined. Then after loading the code, you'd do createBoundaryFromFile("/path/to/the
    > /file")
    >
    > 2. The coordinates will all be strings, so you should do atof(nth(1 lineData)) etc.
    >
    > Note too that if this is for IC61, you'd have to use dbCreatePRBoundary rather than
    > creating a shape on the PRBoundary/drawing LPP.
    >
    > Regards,
    >
    > Andrew
    >
    > From: sanramz [mailto:bounce-sanramz@cadence.com]
    > Sent: Monday, December 17, 2012 07:00 AM
    > To: Andrew Beckett
    > Subject: Re: [Custom IC SKILL Forum] How to parse the contents of a file?
    >
    > Hi,
    >
    > I am trying to create multiple new layouts from a list unix file in to one library.
    > In each cell Iam also trying to create a PR boundary with the coordinates that are
    > present in the unix file.
    >
    > The list file contains the cell name and PR boundary coordinates as shown below.
    >
    > cell_name llx lly urx ury
    >
    > BUF 0 0 4.448 4.424
    >
    > AND 0 0 4.448 4.424
    >
    > My code for this is as shown below.
    >
    > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    >
    > ListPath = "/home/san/tiler/cell_list"
    >
    > ;lib_name="test"
    >
    > inPort = infile( ListPath )
    >
    > when( inPort
    >
    > while( gets( line inPort )
    >
    > ;println( nextLine )
    >
    > lineData = parseString(line " ")
    >
    > ;portTable[car(lineData)] = evalstring(cadr(lineData))
    >
    > ;cell_name=car(lineData)
    >
    > cell_name=nth(0 lineData)
    >
    > llx=nth(1 lineData)
    >
    > lly=nth(2 lineData)
    >
    > urx=nth(3 lineData)
    >
    > ury=nth(4 lineData)
    >
    > create_boundary(cell_name llx lly urx ury)
    >
    > )
    >
    > close( inPort )
    >
    > )
    >
    > procedure(create_boundary(cell_name llx lly urx ury)
    >
    > let (cv)
    >
    > cv=dbOpenCellViewByType("test" "cell_name" "layout" "maskLayout" "w")
    >
    > dbCreateRect(cv list("prBoundary" "drawing") list(llx:lly urx:ury))
    >
    > dbSave(cv)
    >
    > );procedure
    >
    > ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    >
    > For this code, I am getting the following error.
    >
    > *Error* eval: undefined function - create_boundary
    > *Error* load: error while loading file - "create_cell.il" at line 20
    >
    > Kindly help me to solve this.
    >
    > Is it because of the "parsing", as i expect \n is coming for the "urx" variable when I
    > try to access it??
    >
    > Regards,
    >
    > santhosh.
    >
    > You received this email because you subscribed to notifications for the Cadence Custom
    > IC SKILL Forum. To unsubscribe, log in, go to forums, and change your forum
    > subscriptions.
    >
    > http://www.cadence.com/community/forums/ForumSubscriptions.aspx
    >
    > --
    > View this message online at: http://www.cadence.com/Community/forums/p/17331/
    > 1317721.aspx#1317721
    >
    http://crosstwine.com
    tel: +49 89 2189 2939
    cell: +49 174 3489 428

    “Strong Opinions, Weakly Held”
    — Bob Johansen
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
<

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information