How to Eliminate whitespaces while Reading a file in VHDL

后端 未结 1 1559
谎友^
谎友^ 2021-01-23 10:57

I have below data in an input file to be read:

10101100 11010100 10101100 11010100

11111110 10111001 11111110 10111001

I need to read each nib

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 11:05

    I spent some time testing the boundary conditions for trailing whilepace which necessitated a change to your nested loops:

            while not endfile(f) loop
                readline(f, L);
                -- len := L'length;
                -- while len >= b'length loop
                while L.all'length >= b'length loop
                    read(L, b);
                    -- len := L'length;
                    mem(i) <= to_stdlogicvector(b);
                    i := i + 1;
                    -- len := len - b'length;
                end loop;
            end loop;
    

    The len variable can be eliminated. After a read L will contain the remainder and can be examined directly.

    The change is to support trailing whitespace characters that don't show up by copying and pasting your input file contents from your question to a file.

    I imagine you're seeing trailing spaces on the line, artifacts of how the input file is written.

    The above change is bullet proof for 1, 2 or three trailing spaces.

    For the case of four or more trailing spaces or a one or more of a space, a non-blocking space or a horizontal tab trailing the last valid b value a fix requires scanning for any of these whitepace characters, you can count on just trivial rejection.

    If you assume any non-whitespace character represents some portion of a b value you can simply blame failure of malformed b values on how the file was written.

    That allows you to simply test for characters remaining that are only whitespace.

    With an added function:

    package body io is
        function iswhitespace (inpstr:  in  string) return boolean is
            constant NBSP: character  := character'val(128); 
        begin
            for i in inpstr'range loop
                if inpstr(i) /= ' ' and inpstr(i) /= NBSP and inpstr(i) /= HT then
                    exit;
                elsif i = inpstr'RIGHT then
                    return TRUE;
                end if;
            end loop;
            return FALSE;
        end function;
    

    (keeping in mind the declaration in the package)

    The textio nested loop condition look like:

            -- while L.all'length >= b'length loop
            while L.all'length >= b'length and not iswhitespace(L.all) loop
    

    which makes file read immune to any length of trailing whitespace on a line.

    0 讨论(0)
提交回复
热议问题