% From the book % PROLOG PROGRAMMING IN DEPTH % by Michael A. Covington, Donald Nute, and Andre Vellino % (Prentice Hall, 1997). % Copyright 1997 Prentice-Hall, Inc. % For educational use only % File READCDF.PL % Reading comma-delimited fields from a file % Insert suitable definition of get_byte here get_byte(C) :- get0(C). % read_until(+Target,-String) % Reads characters until a specific character is found, % or end of line, or end of file. read_until(Target,String) :- get_byte(C), read_until_aux(C,Target,String). read_until_aux(-1,_,[]) :- !. % end of file, so stop read_until_aux(13,_,[]) :- !. % end of line (DOS) read_until_aux(10,_,[]) :- !. % end of line (UNIX) read_until_aux(T,T,[]) :- !. % found the target, so stop read_until_aux(C,T,[C|Rest]) :- % keep going read_until(T,Rest). % read_cdf(-String) % Reads a comma-delimited field. read_cdf(String) :- get_byte(FirstChar), % look at first character read_cdf_aux(FirstChar,String). read_cdf_aux(34,String) :- % field begins with " !, read_until(34,String), % read until next " read_until(44,_). % consume the following comma read_cdf_aux(C,String) :- % field does not begin with " read_until_aux(C,44,String). % (just in case C is -1 or 10...)