home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / ANSWERS.ARC / ANS_159B.PRO < prev    next >
Encoding:
Prolog Source  |  1988-06-21  |  2.8 KB  |  113 lines

  1. /*
  2.    Turbo Prolog 2.0, Answer to second Exercise on page 159.
  3.    
  4.    Copyright (c) 1986, 88 by Borland International, Inc
  5. */
  6.  
  7. Domains
  8.   file = infile; outfile
  9.   
  10. Predicates
  11.   nondeterm repeat
  12.   run
  13.   copy_file
  14.   exist_original ( string )
  15.   exist_copy ( string )
  16.   process_ans ( char, string )
  17.   get_filename ( string, string )
  18.   
  19. Clauses
  20.   repeat.  repeat :- repeat.      
  21.   
  22.   run :-
  23.         makewindow(1,2,3," Copy File ",0,0,25,80) ,
  24.         repeat ,
  25.           cursor(2,2) ,
  26.           write("Enter the name of the file you wish to copy: ") ,
  27.           readln(File_in) ,
  28.         exist_original(File_in) ,
  29.         repeat ,
  30.           cursor(4,2) ,
  31.           write("Enter the name of the file you wish to copy to: ") ,
  32.           readln(File_out) ,
  33.          exist_copy(File_out) ,
  34.         openread(infile, File_in) ,
  35.         openwrite(outfile, File_out) ,
  36.         readdevice(infile) ,
  37.         write("\n  File being copied...\n") ,
  38.         writedevice(outfile) ,
  39.         copy_file ,
  40.         closefile(infile) ,
  41.         closefile(outfile) ,
  42.         readdevice(keyboard) ,
  43.         writedevice(screen) ,
  44.         write("\n  The eagle has landed...\n" ,
  45.               "  File successfully copied (press a key).\n\n") ,
  46.         readchar(_).
  47.  
  48. /*
  49.  * Be sure the file to copy exists.
  50.  */        
  51.   exist_original(File) :-
  52.       existfile(File), !.
  53.   exist_original(_) :-
  54.       beep ,
  55.       write("\n  The file you wish to copy does not exist!\n" ,
  56.             "  Do you wish to continue? (Y/N): ") ,
  57.       readchar(Ans) ,
  58.       write(Ans), nl ,
  59.       clearwindow,
  60.       upper_lower(Ans,'n') ,
  61.       exit.
  62.       
  63. /*
  64.  * Be sure the file to copy to does not exist.
  65.  */
  66.   exist_copy(File) :-
  67.       not( existfile(File) ), !.
  68.   exist_copy(File) :-
  69.       beep ,
  70.       write("  The file you wish to copy to already exists.\n") ,
  71.       repeat ,
  72.       write("  Do you wish to Erase it or Copy it to a .BAK file?\n" ,
  73.             "   (E\\C) : ") ,
  74.       readchar(Ans) , 
  75.       upper_lower(Ans1, Ans) ,
  76.       write(Ans1), nl ,
  77.       process_ans(Ans1, File).
  78.       
  79.   process_ans('E', File) :-
  80.       deletefile(File),!.
  81.   process_ans('C', File) :-
  82.       get_filename(File, Filename) ,
  83.       concat(Filename, ".BAK", Filename1) ,
  84.       renamefile(File, Filename1), !.
  85.   process_ans(_, _) :- 
  86.       write("  Not a valid response!\n\n"), fail.
  87.  
  88. /*
  89.  * Parse filename from path and file extension.
  90.  */             
  91.   get_filename(File, File) :-
  92.       fronttoken(File, _, ""), !.
  93.   get_filename(File, Filename) :-
  94.       fronttoken(File, FileName, Rest) ,
  95.       fronttoken(Rest, ".", _), !.
  96.   get_filename(File, Filename) :-
  97.       fronttoken(File, _, F1) ,
  98.       get_filename(F1, Filename).
  99.  
  100. /*
  101.  * Once the proper files are opened for reading 
  102.  *  and writing, copying the files is made easy.
  103.  */      
  104.   copy_file :-
  105.       repeat ,
  106.       readchar(C) ,
  107.       write(C) ,
  108.       eof(infile).    % This is a failing condition that
  109.               %  succeeds when the task is done.
  110.  
  111. GOAL
  112.   run.      
  113.