/* ========================================================================== */
/*                              FILE HANDLING                                 */
/* ========================================================================== */

:- module(file,
          [
		  fwrite_list_atoms/2,

            ffwrite_term/2,
		  ffwrite_term_numbervars/2,

            ffwrite_term_ln/2,
		  ffwrite_term_ln_numbervars/2,

            fwrite/2,
		  fwrite_numbervars/2,

            fwrite_list/2,
		  fwrite_list_numbervars/2,

            fwrite_ln/2,
		  fwrite_ln_numbervars/2,

            fwrite_point/2,
		  fwrite_point_numbervars/2,

            fwrite_point_ln/2,
		  fwrite_point_ln_numbervars/2
          ] 
         ). 
 
/* ========================================================================== */
/*                        							                  */
/* ========================================================================== */  

	/* write a list of atoms */

fwrite_list_atoms(_Stream, []) :-
   !.

fwrite_list_atoms(Stream, [Atom|LAtoms]) :-
   fwrite_point_ln(Stream, Atom),
   fwrite_list_atoms(Stream, LAtoms).


	/* write an uplet */
ffwrite_term_numbervars(Stream, Term) :-
   Term =..[','|LArgs],
   !,
   write(Stream, '('),
   fwrite_terms_commas_numbervars(Stream, LArgs),
   write(Stream, ')').

ffwrite_term_numbervars(Stream, Term) :-
   numbervars(Term, 23, _),
   write(Stream, Term).


	/* write an uplet */
ffwrite_term_ln_numbervars(Stream, Term) :-
   ffwrite_term_numbervars(Stream, Term),
   nl(Stream).


	/* write an uplet */
ffwrite_term(Stream, Term) :-
   Term =..[','|LArgs],
   !,
   write(Stream, '('),
   fwrite_terms_commas(Stream, LArgs),
   write(Stream, ')').

ffwrite_term(Stream, Term) :-
   write(Stream, Term).


	/* write an uplet */
ffwrite_term_ln(Stream, Term) :-
   ffwrite_term(Stream, Term),
   nl(Stream).


 /* write terms separated with commas */
fwrite_terms_commas_number_vars(Stream, [X]) :-
   numbervars(X, 23, _),
   write(Stream, X),
   !.

fwrite_terms_commas_number_vars(Stream, [X|L]) :-
   numbervars(X,23,_),
   write(Stream, X),
   write(Stream, ','),
   fwrite_terms_commas_number_vars(Stream, L).


   /* write terms separated with commas */
fwrite_terms_commas(Stream, [X]) :-
   write(Stream, X),
   !.

fwrite_terms_commas(Stream, [X|L]) :-
   write(Stream, X),
   write(Stream, ','),
   fwrite_terms_commas(Stream, L).
   

	/* write the term on Stream */
fwrite(Stream, Term) :-
   write(Stream, Term).	

fwrite_numbervars(Stream, Term) :-
   numbervars(Term, 23, _),
   write(Stream, Term).


	/* write Term on Stream and add a new line */
fwrite_ln(Stream, Term) :-
   write(Stream, Term),
   nl(Stream).

	/* write Term on Stream and add a new line */
fwrite_ln_numbervars(Stream, Term) :-
   fwrite_numbervars(Stream, Term),
   nl(Stream).
   

fwrite_point(Stream, Term) :-
   write(Stream, Term),
   write(Stream, '.').


fwrite_point_numbervars(Stream, Term) :-
   fwrite_numbervars(Stream, Term),
   write(Stream, '.').
   

fwrite_point_ln(Stream, Term) :-
   fwrite_point(Stream, Term),
   nl(Stream).


fwrite_point_ln_numbervars(Stream, Term) :-
   fwrite_point_numbervars(Stream, Term),
   nl(Stream).


   
   /* write a list on the current Stream */
fwrite_list_numbervars(Stream, L) :-
   fwrite_ln(Stream, '['),
   fwrite_a_list_numbervars(Stream, L),
   fwrite_ln(Stream,']').

	/* write a list */   
fwrite_a_list_numbervars(_Stream, []) :-
   !.


fwrite_a_list_numbervars(Stream, [X]) :-
   write(Stream, '   '),
   ffwrite_term_numbervars(Stream, X),
   nl(Stream),
   !.   
   
fwrite_a_list_numbervars(Stream, [X|L]) :-
   write(Stream, '   '),
   ffwrite_term_numbervars(Stream, X),
   fwrite_ln(Stream, ','),
   fwrite_a_list_numbervars(Stream, L).

   
   /* write a list on the current Stream */
fwrite_list(Stream, L) :-
   fwrite_ln(Stream, '['),
   fwrite_a_list(Stream, L),
   fwrite_ln(Stream,']').


	/* write a list */   
fwrite_a_list(_Stream, []) :-
   !.


fwrite_a_list(Stream, [X]) :-
   write(Stream, '   '),
   ffwrite_term(Stream, X),
   nl(Stream),
   !.   
   
fwrite_a_list(Stream, [X|L]) :-
   write(Stream, '   '),
   ffwrite_term(Stream, X),
   fwrite_ln(Stream, ','),
   fwrite_a_list(Stream, L).
      
/* ============================= END OF LISTING ============================= */                       
