home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / EXAMPLES / CH15EX03.PRO < prev    next >
Encoding:
Prolog Source  |  1990-03-26  |  2.4 KB  |  83 lines

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