0

I'm trying to read/write a CSV file row by row using Lauterbach CMM script, but it's not working.

I thought I can read each row of CSV file as each line if I read csv as normal file, but it's not true, if you use LF characters in the cell data: Reading CSV file line by line will be terminate. I mean the script reads the row as partially.

Can you please let me know to read CSV file row by row?

My code to read CSV row by row :

OPEN #1 &csv_name /Read
WHILE !FILE.EOF(1)
(
    READ #1 %LINE &csv_row
    PRINT "&csv_row"
)

Can you please provide information on how to read/write CSV file row by row by using a Lauterbach CMM script?

Holger
  • 3,920
  • 1
  • 13
  • 35

1 Answers1

0

The way you are using OPEN and READ is intended to read one line of a text file. They are not made especially for CSV files. And while CSV files may have a line-feed-character inside a line (when encapsulated in double-quotes), a line of a normal text file simply ends with a line-feed.

So I think you have two options here: Read the file byte by byte as a binary file or load the whole file to the debuggers virtual memory (VM) and read it from there byte by byte.

Option 1 : Read the file byte by byte

OPEN #1 "&csv_name" /Read /Binary
WHILE !FILE.EOF(1)
(
    PRIVATE &line
    GOSUB getline "1"
    RETURNVALUES &line
    IF "&line"!=""
        ECHO "&line"
)
CLOSE #1
ENDDO

getline:
    PARAMETERS &fh
    PRIVATE &c &last &line &inquotes

    &last=0
    &inquotes=FALSE()

    READ #&fh &c
    WHILE !FILE.EOF(&fh)
    (
        IF (&last=='"')&&(&c!='"')
            &inquotes=!&inquotes
        IF (!&inquotes)&&(&c==CONvert.CHAR(0x0A))
        (
            IF &last==CONvert.CHAR(0x0D)
                &line=STRing.CUT("&line",-1)
            RETURN "&line"
        )
        &line="&line"+CONvert.CHAR(&c)
        &last=&c
        READ #&fh &c
    )
    RETURN "&line"

Option 2 : Load the whole file to the debuggers virtual memory (VM) and read it from there byte by byte.

PRIVATEn &line &size &pos

&pos=0    
&size=OS.FILE.SIZE("&csv_name")
SILENT.Data.LOAD.Binary "&csv_name" VM:&pos
WHILE &pos<&size
(
    GOSUB getline "1" "&pos" "&size"
    RETURNVALUES &line &pos
    IF "&line"!=""
        ECHO "&line"
)
CLOSE #1
ENDDO

getline:
    PARAMETERS &fh &pos &size
    PRIVATE &c &last &line &inquotes

    &last=0
    &inquotes=FALSE()

    WHILE &pos<&size
    (
        &c=Data.Byte(VM:&pos)
        &pos=&pos+1
        IF (&last=='"')&&(&c!='"')
            &inquotes=!&inquotes
        IF (!&inquotes)&&(&c==CONvert.CHAR(0x0A))
        (
            IF &last==CONvert.CHAR(0x0D)
                &line=STRing.CUT("&line",-1)
            RETURN "&line" "&pos"
        )
        &line="&line"+CONvert.CHAR(&c)
        &last=&c
    )
    RETURN "&line" "&pos"
Holger
  • 3,920
  • 1
  • 13
  • 35
  • Hi Holger, Thanks for answer option number 1 is worked for me. – Penchalaiah Jammula Nov 04 '19 at 09:02
  • Hi Holger, When I extract a number from string by default it goes to hex value but the value is in decimal.Do you know is there any cmm script function to convert string to decmal value. Ex:- from array [0,127,255] if I extract scond element from above array &temp_testval=0,127,255 &Test_Value_1=STRing.TRIM(STRing.SPLIT("&temp_testval",",",1)) by default Test_Value1 value is 0x127 Can you please let me know how i can read &Test_Value_1 as 127(decimal number) Thanks – Penchalaiah Jammula Nov 04 '19 at 13:25
  • In general please open a new question on StackOverflow for every new question. Anyhow: Decimal literals are suffixed with a dot in TRACE32. So `0x127` is hex, `127.` is decimal, and `127` is whatever configured with SETUP.RADIX. In you case I suggest to use `&Test_Value_1=STRing.TRIM(STRing.SPLIT("&temp_testval",",",1))+"."` – Holger Nov 04 '19 at 13:53
  • It works for me thanks a lot You're awesome :) Sure next time I create new question. – Penchalaiah Jammula Nov 04 '19 at 15:28