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

  1. /*
  2.    Turbo Prolog 2.0 Chapter 15, Example Program 3
  3.    
  4.    Copyright (c) 1986, 88 by Borland International, Inc
  5.    
  6. */
  7.    
  8. domains
  9.    db_selector = dba
  10.  
  11. predicates
  12.    % List all keys in an index
  13.    list_keys(db_selector, bt_selector)
  14.  
  15. clauses
  16.    list_keys(dba, Bt_selector) :-
  17.       key_current(dba, Bt_selector, Key, _),
  18.       write(Key, ' '),
  19.       fail.
  20.    list_keys(dba, Bt_selector) :-
  21.       key_next(dba, Bt_selector, _), !,
  22.       list_keys(dba, Bt_selector).
  23.    list_keys(_, _).
  24.  
  25. predicates
  26.    open_dbase(bt_selector)
  27.    main(db_selector, bt_selector)
  28.    ed(db_selector, bt_selector, string)
  29.    ed1(db_selector, bt_selector, string)
  30.  
  31. clauses
  32.    % Loop until escape is pressed
  33.    main(dba, Bt_select) :-
  34.       write("File Name: "),
  35.       readln(Name),
  36.       ed(dba, Bt_select, Name), !,
  37.       main(dba, Bt_select).
  38.    main(_, _).
  39.  
  40.    % The ed predicates ensure that the edition will never fail. 
  41.       ed(dba, Bt_select, Name) :-
  42.          ed1(dba, Bt_select, Name), !.
  43.       ed(_, _, _).
  44.  
  45.    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  46.     * There are three choices:                                  *
  47.     *                                                           *
  48.     * a) The name is an empty string - list all the names       *
  49.     * b) The name already exists - modify the contents of the          file                                                   
  50.     * c) The name is a new name - create a new file             *
  51.     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  52.  
  53.    ed1(dba, Bt_select, "") :- !,
  54.       key_first(dba, Bt_select, _),
  55.       list_keys(dba, Bt_select),
  56.       nl.
  57.    ed1(dba, Bt_select, Name) :-
  58.       key_search(dba, Bt_select, Name, Ref), !,
  59.       ref_term(dba, string, Ref, Str),
  60.       editmsg(Str, Str1, "Edit old", NAME, "", 0, "PROLOG.HLP", RET),
  61.       clearwindow,
  62.       Str><Str1, RET=0,
  63.       term_replace(dba, string, Ref, Str1).
  64.    ed1(dba, Bt_select, Name) :-
  65.       editmsg("", STR1, "Create New", NAME, "", 0, "PROLOG.HLP", RET),
  66.       clearwindow,
  67.       ""><Str1, RET=0,
  68.       chain_insertz(dba, file_chain, string, Str1, Ref),
  69.       key_insert(dba, Bt_select, Name, Ref).
  70.   
  71.   open_dbase(INDEX):-
  72.      existfile("dd1.dat"),!,
  73.      db_open(dba, "dd1.dat", in_file),
  74.      bt_open(dba, "ndx", INDEX).
  75.  
  76.   open_dbase(INDEX):-
  77.      db_create(dba, "dd1.dat", in_file),
  78.      bt_create(dba, "ndx", INDEX, 20, 4).
  79.      
  80.  
  81. goal
  82.    open_dbase(INDEX),
  83.    main(dba, INDEX),
  84.    bt_close(dba, INDEX),
  85.    db_close(dba).
  86.