Hi Carl and others, below the asterisks line follows the script
*********************************************************************************************
rebol [
        Title:    "Flag Viewer"
        Author:    "Arie van Wingerden"
    ]
   
{write/lines %log mold country-flag}

global: make object! [
    ;**********************************************************************
    ; The URL of the webpage holding the names of the different countries
    ; for which a flag is present.
    ;**********************************************************************
    allflags: 
http://www.cia.gov/cia/publications/factbook/docs/flagsoftheworld.html   
    ;**********************************************************************
    ; The URL of the webpage holding an enlarged flag of a certain country.
    ; Each country has a specific prefix code, like xx.
    ; The image of the flag is URL/xx-lgflag.gif.
    ; The flad description can be found in URL/xx-flag.html
    
;**********************************************************************   
    flagpage: http://www.cia.gov/cia/publications/factbook/flags/
    
;**********************************************************************   
    ; This field will contain (after get-flags has been called) :
    ; - the countryname
    ; - the 2 character countrycode
    ;**********************************************************************
    flags: copy ""
    
;**********************************************************************   
    ; This field will be used as a pointer to the current flag
    ; and will be initialized to 1
    ;**********************************************************************
    curflag: 1
    
;**********************************************************************   
    ; This field remembers the last find string in order to be able to
    ; carry on searching with the same string
    ;**********************************************************************
    findstr: copy ""
    
;**********************************************************************   
    ; This field remembers the pointer to the country last found
    ;**********************************************************************
    findptr: 1
]

get-flags: func [
    {Fetch countrynames and codes of all flags needed}
    /local countrycode countryname flaglist flags found
][
    flags: copy []
    parse read global/allflags [
        thru "<option selected>Select a Country</option>"
        copy flaglist
        to "</select>"
        to end
    ]
    forever [
        found:
            parse flaglist [
                thru "geos/"
                copy countrycode
                to "."
                thru ">"
                copy countryname
                to "<"
                copy flaglist
                to end
            ]
        if not found [ break ]
        either find [ "ay" "gz" "oo" "pf" "pg" "we" "xo" "xq" "xx" "zh" 
"zn" ] countrycode [
            {Due to error on website this code points to nowhere
            so we ignore this entry for the time being}
        ][
            append flags reduce [ countryname countrycode ]
        ]
    ]
    ;**********************************************************************
    ; Sort the list by countryname
    ;**********************************************************************
    sort/skip flags 2
    ;**********************************************************************
    ; Return the list of countrycodes and names implicitly
    ;**********************************************************************
    flags
]

show-flags: func [
    {Main program: show and search for flags}
][
    view layout [
        country-name: label current-country/name 600 black bold 
font-name font-sans-serif font-size 14
        country-flag: image fetch-url
        across
        country-find: field 400
        find-button: button "Find (next)" [
print "------------------------------------"
print "Right after push Find (next) button:"
print rejoin [ "   global/findstr=(" global/findstr ")" ]
print rejoin [ "   country-find/text=(" country-find/text ")" ]
            either find-country country-find/text [
                country-name/text: current-country/name
                country-flag/image: load fetch-url
                show country-name
                show country-flag               
                msg-info/text: copy ""
            ][
                msg-info/text: "Country could not be found!"
            ]
            show msg-info
            show country-find
        ]
        next-button: button "Next Country" [
            next-country
            country-name/text: current-country/name
            country-flag/image: load fetch-url
            country-find/text: copy ""
            msg-info/text: copy ""
            show country-name
            show country-flag
            show country-find
            show msg-info
        ]
        return
        msg-info: info copy "" 600 yellow
    ]
]

fetch-url: func [
    {Construct the URL of the image}
][
    return rejoin [ global/flagpage current-country/code "-lgflag.gif" ]   
]

current-country: func [
    {returns either the countryname or the countrycode}
    /code /name
][
    if code [
        return pick global/flags (global/curflag + 1)
    ]
    if name [
        return pick global/flags global/curflag
    ]
]

find-country: func [
    {Tries to find the country that the user suggests - a part of the 
name is OK}
    search-text /count /ptr /tmpflags
][
    if search-text = "" [
        return false
    ]
    either global/findstr <> search-text [
        ptr: 1
    ][
        ptr: global/findptr + 2
    ]
    if ptr > length? global/flags [
        return false
    ]
    tmpflags: skip global/flags (ptr - 1)
    count: ptr
    foreach [ country code ] tmpflags [
        if find country search-text [
            global/curflag: count
            global/findptr: count
            global/findstr: search-text
print "Right after a hit in find-country:"
print rejoin [ "   global/findstr=(" global/findstr ")" ]
print rejoin [ "   country-find/text=(" country-find/text ")" ]
            return true
        ]
        count: count + 2
    ]
    return false
]

next-country: func [
    {adds 1 to country counter; if at tail, than revert to the first 
country}
][
    global/curflag: global/curflag + 2
    if tail? skip global/flags global/curflag [
        global/curflag: 1
    ]
]

;***********************************************************************;;;
; Main line starts here
;***********************************************************************;;;
global/flags: get-flags
show-flags

-- 
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.

Reply via email to