1 | % (c) 2009-2023 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 | ||
6 | :- module(tools_files, | |
7 | [write_lines_to_file/2, | |
8 | write_to_file/2, | |
9 | write_to_utf8_file/2, write_to_utf8_file_or_user_output/2, | |
10 | put_codes/2, | |
11 | read_file_codes/2]). | |
12 | ||
13 | ||
14 | :- use_module(module_information). | |
15 | ||
16 | :- module_info(group,infrastructure). | |
17 | :- module_info(description,'A few utilities on files seperated out from tools.pl to avoid cyclic module dependencies.'). | |
18 | ||
19 | write_lines_to_file(FILE,Lines) :- | |
20 | lines_to_code_list(Lines,CodeList), | |
21 | write_to_file(FILE,CodeList). | |
22 | ||
23 | lines_to_code_list([],[]). | |
24 | lines_to_code_list([Line|Lines],CodeList) :- | |
25 | (number(Line) -> number_codes(Line,L); atom_codes(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)). |