سؤال

I am a newbie to programming in postscript directly.

I am reading from a file and I am using the read command to parse symbols.

Most of the symbols I am checking for are 2 characters in length and one is 3 characters in length. I would change the one that is 3 characters in length into two characters, but that won't help for the following reasons:

  1. The symbols are a standard so I can't say.. well, thanks for making just one of them 3 characters unlike all the rest!
  2. The symbol that has 3 characters has the first 2 characters the same as another symbol.

I need to be able to "peek" at the next character in the file if the first two symbols match and not modify the the read order if the character peeked at doesn't match what I want to make the 3 character symbol.

Example: File text as a string: "AB3;ABB4;"

In this case I will read A, then B and because the two characters together are "AB", I need to see if reading the next character produces a B.. if not, I don't want to have modified my reading order so I can proceed in the code normally to extracting the value. If it does, I now have my 3 character symbol and can proceed to extract the value normally.

Thank you.

هل كانت مفيدة؟

المحلول

Postscript doesn't have an unget function, so you can't push a byte back onto the stream to read it again later. But, depending upon OS-support, Level 2 Postscript lets you reposition a file. So you could implement your peek function like this:

% file  peek  int true
%             false
/peek {
    dup fileposition exch dup  % pos file file
    read { %true:  pos file int
        3 1 roll exch setfileposition true
    }{ %false: pos file
        pop pop false
    } ifelse
} def
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top