| 1 | % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | :- module(tools_files, | |
| 6 | [write_lines_to_file/2, | |
| 7 | write_to_file/2, | |
| 8 | write_to_utf8_file/2, write_to_utf8_file_or_user_output/2, | |
| 9 | put_codes/2, | |
| 10 | read_file_codes/2]). | |
| 11 | ||
| 12 | ||
| 13 | :- use_module(module_information). | |
| 14 | ||
| 15 | :- module_info(group,infrastructure). | |
| 16 | :- module_info(description,'A few utilities on files seperated out from tools.pl to avoid cyclic module dependencies.'). | |
| 17 | ||
| 18 | write_lines_to_file(FILE,Lines) :- | |
| 19 | lines_to_code_list(Lines,CodeList), | |
| 20 | write_to_file(FILE,CodeList). | |
| 21 | ||
| 22 | lines_to_code_list([],[]). | |
| 23 | lines_to_code_list([Line|Lines],CodeList) :- | |
| 24 | (number(Line) -> number_codes(Line,L) ; atom(Line) -> atom_codes(Line,L) | |
| 25 | ; format(user_error,'*** Illegal line for lines_to_code_list: ~w~n',[Line]),L="?"), | |
| 26 | append(L,[10],L1), | |
| 27 | append(L1,RL,CodeList), | |
| 28 | lines_to_code_list(Lines,RL). | |
| 29 | ||
| 30 | write_to_utf8_file(FILE,CodesList) :- | |
| 31 | open(FILE,write,S,[encoding(utf8)]), | |
| 32 | put_codes(CodesList,S), | |
| 33 | close(S). | |
| 34 | ||
| 35 | write_to_utf8_file_or_user_output(user_output,CodesList) :- !, | |
| 36 | put_codes(CodesList,user_output). | |
| 37 | write_to_utf8_file_or_user_output(FILE,CodesList) :- write_to_utf8_file(FILE,CodesList). | |
| 38 | ||
| 39 | write_to_file(FILE,CodesList) :- | |
| 40 | open(FILE,write,S), | |
| 41 | put_codes(CodesList,S), | |
| 42 | close(S). | |
| 43 | ||
| 44 | put_codes([],_). | |
| 45 | put_codes([H|T],Stream) :- put_code(Stream,H), put_codes(T,Stream). | |
| 46 | ||
| 47 | ||
| 48 | ||
| 49 | read_file_codes(Filename,Codes) :- | |
| 50 | open(Filename,read,S,[encoding(utf8)]), | |
| 51 | call_cleanup(read_codes(S,Codes), | |
| 52 | close(S)). | |
| 53 | read_codes(S,Codes) :- | |
| 54 | get_code(S,Code), | |
| 55 | ( Code < 0 -> | |
| 56 | Codes = [] | |
| 57 | ; | |
| 58 | Codes = [Code|Rest], | |
| 59 | read_codes(S,Rest)). |