File Search & Select PGM

new topic     » topic index » view thread      » older message » newer message

Welcome Euphoria Users,

I have written an "include file" routine to make it easier to find
and select a file which then returns the file name and path to the
program.  One example where I find it very useful is the Euphoria
Editor, ed.ex, program,; no longer do you have to remember the file
name and type it in, but you can search on all drives and
subdirectories for the desired file and then click on the mouse left
button to select the file and return it to the editor.  It runs in
the graphics text Mode 3 with 25 lines of text.  To check out the
program to see how it works on your system you can type in this short
program:

        include filescrh.e  -- Name of the include file routine
        sequence fs
        fs = file_select()
        puts(1,fs)          -- prints to screen the path and file name
selected


If the above four line program works fine on your computer, you may
want to incorporate this feature in the Euphoria Editor or your own
program.

To modify the Euphoria Editor to use this feature only a few things
need to be changed.  First, it would be wise to make a backup copy of
the editor before you make any changes.  Start the Editor and load in
ed.ex.  At the beginning of the ed.ex program add the one new line as
shown below:

        without type_check -- makes it a bit faster
        include graphics.e
        include get.e
        include file.e
        include filescrh.e                                     --new line

To find the next section of the program to change use the find
feature of the editor.  Press the "Esc" key, then f, then type in the
words, file name:, and press the enter key twice. It will take you to
the section listed below.  Add the three (3) new lines.

        elsif command[1] = 'n' then
                if modified and last_use() then
                set_top_line("")
                printf(SCREEN, "save changes to %s? ", {file_name})
                if find('y', key_gets("yn")) then
                        save_file(file_name)
                end if
                end if
                save_state()
                temp_name = lower(file_select())                   -- new line
                if length(temp_name) = 0 then                      -- new line
                set_top_line("new file name: ")
                temp_name = delete_trailing_white(key_gets(""))
                end if                                             -- new line
                if length(temp_name) != 0 then
                file_name = temp_name
                stop = TRUE
                end if

The next section is also found with the find feature.  Press the
"Esc" key, then f, then press the Enter key. (find defaults to, file
name:) It will take you to the section listed below.  Add the three
(3) new lines.

        if length(file_name) = 0 then
                -- we still don't know the file name - so ask user
                file_name = lower(file_select())                   --new line
                if length(file_name) = 0 then                      --new line
                puts(SCREEN, "file name: ")
                cursor(ED_CURSOR)
                file_name = key_gets("")
                puts(SCREEN, '\n')
                end if                                             --new line
    end if

That is all the changes necessary for the Euphoria Editor.  If you
want to start a new file name; one that doesn't yet exist in the
directories, you can do so by clicking on EXIT and then typing in the
name, exactly like it is now done.

I am providing this include file program to the Euphoria Public
Domain.  This program is supplied "AS IS" and without warranty.  The
program works fine for me but it has been tested on only a few
computer systems, so if there are occasions that this program does
not work properly or when used in your application, feel free to
modify to suit your needs.

Enjoy!!

Fred Cole      (facole at prodigy.com)

The include file "filescrh.e" follows:


                  ------------------------------------------
                  ---        File Search & Select        ---
                  ---               Ver 1.0              ---
                  ---        Written by: Fred Cole       ---
                  ---         facole at prodigy.com         ---
                  ---   Free to Euphoria Public Domain   ---
                  ---        Provided "AS IS" and        ---
                  ---          Without Warranty          ---
                  ---     Permission given to modify     ---
                  ------------------------------------------

-- Search sub-directories and drives using a mouse to search for a
file.
-- When the desired file is found and the mouse pointer is moved to
that line,
-- the mouse left button is clicked to return the directory path and
file name
-- of the selected file. Use in Graphics Mode (3) with 25 lines text.


include file.e
include get.e
include graphics.e
include image.e
include machine.e
include mouse.e
include sort.e

function mouse()
    object y
    mouse_events(LEFT_DOWN)
    mouse_pointer(1)
    while 1 do
        y = get_mouse()
        if atom(y) then
            else
                exit
        end if
    end while
    mouse_pointer(0)
    return y
end function

procedure first_line(sequence b)
    text_color(6)
    position(1,10)
    puts(1,"Directory Path is:")
    position(1,30)
    text_color(9)
    puts(1,b)
    position(2,1)
end procedure

function file_path(sequence b)
    object x,y,p
    integer a,c
    sequence x1,x2
    x = dir(b)
    p = 0
    if not atom(x) and length(b)>3 then
        for t=1 to length(x) by 1 do
            if find("..",x[t]) = 0 then
                else
                    p = 1
                    exit
            end if
        end for
        if p=0 then
            p = {{46,46},{100},0,0,0,0,0,0,0}
            x = prepend(x,p)
        end if
    elsif atom(x) and length(b)>3 then
        x = {{{46,46},{100},0,0,0,0,0,0,0}}
    elsif atom(x) then
        clear_screen()
        while atom(x) do
            first_line(b)
            position(3,12)
            text_color(15)
            puts(1,"NO FILES FOUND")
            text_color(3)
            position(25,37)
            puts(1,"<CHANGE DRIVE>")
            text_color(4)
            puts(1,"         <EXIT>")
            y = mouse()
            if y[3]/8 = 24 and y[2] < 288 or y[2] > 392 and y[2]<472 or
                y[2]>512 then
            elsif y[3]/8 = 24 and y[2] <= 392 and y[2] >= 288 then
                return("CHANGE DRIVE")
            elsif y[3]/8 = 24 and y[2] <= 512 and y[2] >= 472 then
                return("EXIT")
            end if
        end while
    end if
    x = sort(x)
    a = 0
    x1 = {}
    x2 = {}
    while a < length(x) do
        a = a+1
        if compare(x[a][2],"d") = 0  then
            x1 = append(x1,x[a])
            else x2 = append(x2,x[a])
        end if
    end while
    clear_screen()
    first_line(b)
    a = 0
    c = 0
    x = x1 & x2
    while a < length(x) do
        a = a+1
        if compare(x[a][2],"d") = 0  then
            text_color(14)
            else text_color(15)
        end if
        printf(1,"%12s",{x[a][1]})
        printf(1,"  %4s",{x[a][2]})
        printf(1,"   %7d",x[a][3])
        printf(1,"  %02d",x[a][5])
        printf(1,"/%02d",x[a][6])
        printf(1,"/%04d",x[a][4])
        printf(1,"  %02d",x[a][7])
        printf(1,":%02d",x[a][8])
        printf(1,":%02d\n",x[a][9])
        c = c+1
        if a > 23 and a = length(x) then
            position(25,21)
            text_color(10)
            puts(1,"<RETURN>")
            text_color(3)
            position(25,37)
            puts(1,"<CHANGE DRIVE>")
            text_color(4)
            puts(1,"         <EXIT>")
            y = mouse()
            if y[3]/8 > c and y[3]/8 < 24 or y[3] = 0 or (y[3]/8 = 24 and
                y[2] < 160 or y[2] > 216 and y[2] < 288 or y[2] > 392 and
                y[2]<472 or y[2]>512) then
                first_line(b)
                a = a - c
                c = 0
            elsif y[3]/8 = 24 and y[2] <= 216 and y[2] >= 160 then
                clear_screen()
                first_line(b)
                a = 0
                c = 0
            elsif y[3]/8 = 24 and y[2] <= 392 and y[2] >= 288 then
                return("CHANGE DRIVE")
            elsif y[3]/8 = 24 and y[2] <= 512 and y[2] >= 472 then
                return("EXIT")
            elsif (y[3]/8)-c <= 0 and y[3] != 0 then
                return (x[y[3]/8+a-c])
            end if
        elsif a <= 23 and a = length(x) then
            text_color(3)
            position(25,37)
            puts(1,"<CHANGE DRIVE>")
            text_color(4)
            puts(1,"         <EXIT>")
            y = mouse()
            if (y[3]/8)-c <= 0 and y[3] != 0 then
                return (x[y[3]/8+a-c])
            elsif y[3]/8 > c and y[3]/8 < 24 or y[3] = 0 or (y[3]/8 = 24
and
                y[2] < 288 or y[2] > 392 and y[2]<472 or y[2]>512) then
                first_line(b)
                a = 0
                c = 0
            elsif y[3]/8 = 24 and y[2] <= 392 and y[2] >= 288 then
                return("CHANGE DRIVE")
            elsif y[3]/8 = 24 and y[2] <= 512 and y[2] >= 472 then
                return("EXIT")
            end if
        elsif c = 23 and a != length(x) then
            position(25,22)
            text_color(10)
            puts(1,"<MORE>")
            text_color(3)
            position(25,37)
            puts(1,"<CHANGE DRIVE>")
            text_color(4)
            puts(1,"         <EXIT>")
            y = mouse()
            if y[3]=8*24 and y[2]<=208 and y[2]>=168 then
                clear_screen()
                first_line(b)
                c = 0
            elsif y[3] != 0 and y[3] != 8*24 then
                return (x[y[3]/8+a-c])
            elsif y[3] = 0 or (y[3]=8*24 and y[2]<168 or y[2]>208 and
y[2]<288
                or y[2]>392 and y[2]<472 or y[2]>512) then
                first_line(b)
                c = 0
                a = a - 23
            elsif y[3]/8 = 24 and y[2] <= 392 and y[2] >= 288 then
                return("CHANGE DRIVE")
            elsif y[3]/8 = 24 and y[2] <= 512 and y[2] >= 472 then
                return("EXIT")
            end if
        end if
    end while
end function

function select_drive()
    sequence rv -- list of register values
    object b, bl, br, cntr, drives, xy
    bk_color(0)
    clear_screen()
    drives = {}
    for h=1 to 26 by 1 do
        rv = repeat(0, 10)  -- zero register
        rv[REG_AX] = #4409   -- Function:  Check if Block Device is Remote
        rv[REG_BX] = 0000 + h     -- Drive Number
        rv = dos_interrupt(#21, rv) -- Call DOS interrupt #21
        if rv[5] != 0 then
            drives = drives & 64+h & "  "
        end if
    end for
    position(2,34)
    text_color(10)
    puts(1,"Select Drive\n\n")
    text_color(15)
    cntr = 40 - floor((length(drives))/2)  -- Center available Drive
Choices
    puts(1,repeat(32,cntr) & drives)
    br = 24*((length(drives))/3-1)+8*cntr
    bl = 8*cntr
    text_color(4)
    position(25,60)
    puts(1,"<EXIT>")
    position(5,1)
    mouse_events(LEFT_DOWN)
    while 1 do
        xy = mouse()
        if xy[2]<bl or xy[2]>br or xy[3]<24 or xy[3]>24 then
            if xy[3]/8 = 24 and xy[2] <= 512 and xy[2] >= 472 then
                return("EXIT")
            end if
        elsif remainder((xy[2]-bl),24)=0 then
            b = drives[((xy[2]-bl)/8)+1]
            return (b)
        end if
    end while
end function

procedure closing()
    cursor(UNDERLINE_CURSOR)
    text_color(15)
    set_active_page(0)
    set_display_page(0)
end procedure

global function file_select()
    sequence t
    object b
    integer i
    set_active_page(1)
    set_display_page(1)
    text_color(4)
    cursor(NO_CURSOR)
    t = "CHANGE DRIVE"
    while compare(t,"CHANGE DRIVE") = 0 do
        b = select_drive()
        if compare(b,"EXIT") = 0 then
            closing()
            return({})
        end if
        b = b & ":\\"
        t = file_path(b)
        if compare(t,"EXIT") = 0 then
            closing()
            return({})
        end if
        while compare(t[2],"d") = 0  do
            if compare(t[1],"..") = 0 then
                i = length(b)-1
                while compare(b[i],92) != 0 do
                    i = i-1
                end while
                b = b[1..i]
            elsif compare(t[1],".") = 0 then
            else
                b = b & t[1] & "\\"
            end if
            if length(b) > 3 then
                t = file_path(b[1..length(b)-1])
                else
                    t = file_path(b)
            end if
            if compare(t,"EXIT") = 0 then
                closing()
                return({})
            end if
        end while
    end while
    closing()
    return (b & t[1])
end function

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu