

# proc for transforming a text dictionary into an "old" tksesh dictionary.

proc convertCSV {f g} {
    set id 0
    while {! [eof $f]} {
	gets $f l
	if {! [string match {"L",*} $l]} continue
	regexp -- {^"[^"]*","[^"]*","[^"]*","([^"]*)","([^"]*)","([^"]*)","([^"]*)"} $l dummy wri trl meaning comment
        set wri [string map {J Aa " " {}} $wri]
	
	if {[string match  "(*)" $comment]} {
        	set grammar [string trim $comment {()}]
		set comment ""
	} else {
		set grammar ""
	}
	incr id
	if {$grammar eq ""} {
		puts $g [list $id \
			[list GRP [list [list WRI $wri] [list MTRI $trl] [list TRAD $meaning] [list COM $comment]]]]
	} else {
		puts $g [list $id \
			[list GRP [list [list WRI $wri] [list MTRI $trl] [list TRAD $meaning] [list GRA $grammar]]]]
	}

    }
}

set message \
{Hieroword to tksesh converter
Choose a csv file to convert to tksesh dictionary.
This program converts files for hieroword into 
files tksesh can read as "old dictionary" sources.
It only works on dictionary-like entries.
S. Rosmorduc

Hieroword is a software by Luca Brigatti and can be downloaded, along
with a number of lexicons, at :
http://home.rochester.rr.com/lucabri/
} 

text .t -height 10 -borderwidth 0
.t insert insert $message
.t configure -state disabled
button .b -text "choose and convert" -command chooseAndConvert
pack .t
pack .b

proc chooseAndConvert {} {
    set fname [tk_getOpenFile -defaultextension .csv -filetypes {{{hieroword source} .csv}}\
		   -title "Hieroword to tksesh converter"]

    if {! [file exists $fname]} {
	tk_messageBox -type ok -icon error \
	    -message "file $fname does not exists"
	return
    }
    regsub -- {.csv$} $fname ".dic" gname
    
    set gname [tk_getSaveFile \
		   -defaultextension .dic\
		   -filetypes {{{tksesh old dictionary format} .dic}}\
		   -initialdir [file dirname $gname]\
		   -initialfile [file tail $gname]\
		   -parent .\
		   -title "Choose file for tksesh source"]
	       
    set f [open $fname]
    set g [open $gname "w"]
    set oldcursor [. cget -cursor]
    . configure -cursor clock
    convertCSV $f $g
    close $f
    close $g
    . configure -cursor $oldcursor
    tk_messageBox -type ok -icon info \
	-message "file $fname processed. $gname created."
    
}
