/****************************** Module Header ******************************* * * Module Name: encoding.e * * Copyright (c) Netlabs EPM Distribution Project 2019 * * $Id$ * * =========================================================================== * * This file is part of the Netlabs EPM Distribution package and is free * software. You can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * Netlabs EPM Distribution. This library is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * ****************************************************************************/ ; Select encoding compile if not defined( SMALL) -- If compiled separately EA_comment 'Select encoding.' define INCLUDING_FILE = 'ENCODING.E' tryinclude 'mycnf.e' const include 'stdconst.e' const compile if not defined( NLS_LANGUAGE) NLS_LANGUAGE = 'ENGLISH' compile endif include NLS_LANGUAGE'.e' compile endif ; --------------------------------------------------------------------------- ; Syntax: EncodingNum ; Accepts also 'delete' as arg to delete all entries. ; Accepts also 'switch' as arg to switch to next entry. ; When called without arg, the SelectEncoding command is called to ; select/add/configure/delete an encoding. defc EncodingNum universal nepmd_hini parse arg args args = strip( args) opt = upcase( args) if args = '' then 'SelectEncoding' return endif KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Selected') if ActiveNum = '' then ActiveNum = 1 endif Order = QueryConfigKey( KeyPath'\Order') SearchOption = 'C' NextNum = '' NumFound = 0 Select = '' do while NepmdGetNextConfigKey( nepmd_hini, KeyPath, SearchOption, NextNum) if not IsNum( NextNum) then iterate endif NumFound = NumFound + 1 if Opt = 'DELETE' then DeleteConfigKey( KeyPath'\'NextNum'\UconvCode') DeleteConfigKey( KeyPath'\'NextNum'\RecodeCode') -- (required?) DeleteConfigKey( KeyPath'\'NextNum'\Name') Select = 'DELETE' else if NextNum = args then Select = NextNum leave endif endif enddo -- Handle RegDefault entries if Select = '' & IsNum( args) then Select = args endif if Opt = 'SWITCH' then if IsNum( ActiveNum) & Order <> '' then wpos = wordpos( ActiveNum, Order) if wpos = words( Order) then nextwpos = 1 else nextwpos = wpos + 1 endif Select = word( Order, nextwpos) endif endif if Opt = 'DELETE' then elseif Select = '' & NumFound = 0 then sayerror 'No encoding defined.' elseif Select = '' then sayerror 'Encoding number "'args'" not found.' else WriteConfigKey( KeyPath'\Selected', Select) SelectName = QueryConfigKey( KeyPath'\'Select'\Name') sayerror 'Switched to encoding "'SelectName'"' endif return ; --------------------------------------------------------------------------- ; Syntax: StandardEncodingNum ; Accepts also 'switch' as arg to switch to next entry. ; When called without arg, the SelectEncoding command is called to ; select/add/configure/delete an encoding. defc StandardEncodingNum universal nepmd_hini universal codepage parse arg args args = strip( args) opt = upcase( args) if args = '' then 'SelectEncoding' return endif KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Standard') if ActiveNum = '' then ActiveNum = 0 endif Order = QueryConfigKey( KeyPath'\Order') SearchOption = 'C' NextNum = '' NumFound = 0 Select = '' do while NepmdGetNextConfigKey( nepmd_hini, KeyPath, SearchOption, NextNum) if not IsNum( NextNum) then iterate endif NumFound = NumFound + 1 if NextNum = args then Select = NextNum leave endif enddo -- Handle RegDefault entries if Select = '' & IsNum( args) then Select = args endif if lowcase( GetEncodingTool()) <> 'recode' then SystemEncoding = 'ibm-'codepage else SystemEncoding = 'ibm'codepage endif SystemEncodingName = 'ibm-'codepage if Opt = 'SWITCH' then if IsNum( ActiveNum) & Order <> '' then wpos = wordpos( ActiveNum, Order) if wpos = words( Order) then nextwpos = 0 Select = 0 else nextwpos = wpos + 1 Select = word( Order, nextwpos) endif endif endif if Select = '' & NumFound = 0 then sayerror 'No encoding defined.' elseif Select = '' then sayerror 'Encoding number "'args'" not found.' else WriteConfigKey( KeyPath'\Standard', Select) if Select = 0 then SelectName = SystemEncodingName else SelectName = QueryConfigKey( KeyPath'\'Select'\Name') endif sayerror 'Switched default encoding to "'SelectName'"' endif return ; --------------------------------------------------------------------------- defproc EncodingNameToNum( SearchName) universal nepmd_hini -- Optinally return the number for the last found name fReverse = abbrev( upcase( arg( 2)), 'R') KeyPath = '\NEPMD\User\Encoding' SearchOption = 'C' NextNum = '' FoundNum = '' do while NepmdGetNextConfigKey( nepmd_hini, KeyPath, SearchOption, NextNum) if not IsNum( NextNum) then iterate endif NextName = QueryConfigKey( KeyPath'\'NextNum'\Name') if upcase( SearchName) = upcase( NextName) then FoundNum = NextNum if not fReverse then leave endif endif enddo -- Handle RegDefault entries if FoundNum = '' then n = 0 do forever n = n + 1 NextName = QueryConfigKey( KeyPath'\'n'\Name') if NextName = '' then leave endif if upcase( SearchName) = upcase( NextName) then FoundNum = n leave endif enddo endif return FoundNum ; --------------------------------------------------------------------------- ; Buttons = '/~Set/~Up.../~Down.../~Edit.../'CANCEL__MSG defc SelectEncoding universal nepmd_hini KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Selected') Order = QueryConfigKey( KeyPath'\Order') NoEntry = '-none-' Delim = \1 -- Build EncodingList and get data for active item EncodingList = '' ActiveSelectNum = '' ActiveEncoding = '' do w = 1 to words( Order) NextNum = word( Order, w) Name = QueryConfigKey( KeyPath'\'NextNum'\Name') EncodingList = EncodingList''Delim''Name if NextNum = ActiveNum then ActiveSelectNum = w ActiveEncoding = Name endif enddo if EncodingList = '' then EncodingList = Delim''NoEntry -- without an entry, listbox would return Button = 0 endif -- Determine pre-selected item PreSelectNum = arg( 1) if PreSelectNum = '' then if ActiveSelectNum = '' then PreSelectNum = 1 else PreSelectNum = ActiveSelectNum endif endif -- Open Listbox -- No Linebreak allowed in Text Title = 'Select an encoding' Text = 'Selected encoding: 'ActiveEncoding if ActiveNum = '' then DefButton = 4 -- Edit else DefButton = 1 -- Set endif HelpId = 0 refresh ret = ListBox( Title, EncodingList, '/~Set/~Up.../~Down.../~Edit.../'CANCEL__MSG, -- buttons 6, 10, -- top, left, Min( count( Delim, EncodingList) + 1, 12), 80, -- height, width GethWndC( APP_HANDLE) || atoi( PreSelectNum) || atoi( DefButton) || atoi( HelpId) || Text\0) refresh -- Parse button number and selected list item string Button = asc( leftstr( ret, 1)) EOS = pos( \0, ret, 2) -- CHR(0) signifies End Of String Select = substr( ret, 2, EOS - 2) SelectNum = '' if Select = NoEntry then Select = '' if Button < 4 then Button = 3 -- switch to Edit endif else SelectNum = EncodingNameToNum( Select) endif if Button = 1 then -- Set 'EncodingNum' SelectNum return elseif Button = 2 then -- Up wpos = wordpos( SelectNum, Order) NewOrder = Order widx = 0 if wpos <> 0 then NewOrder = SpaceStr( delword( Order, wpos, 1)) widx = wordindex( NewOrder, Max( 1, wpos - 1)) endif NewOrder = SpaceStr( insertstr( SelectNum' ', NewOrder, Max( 0, widx - 1))) WriteConfigKey( KeyPath'\Order', NewOrder) NewSelectNum = wordpos( SelectNum, NewOrder) 'SelectEncoding' NewSelectNum return elseif Button = 3 then -- Down wpos = wordpos( SelectNum, Order) NewOrder = Order widx = 0 if wpos <> 0 then NewOrder = SpaceStr( delword( Order, wpos, 1)) widx = wordindex( NewOrder, Max( 1, Min( words( NewOrder), wpos))) endif NewOrder = SpaceStr( insertstr( ' 'SelectNum, NewOrder, Min( length( NewOrder), widx))) WriteConfigKey( KeyPath'\Order', NewOrder) NewSelectNum = wordpos( SelectNum, NewOrder) 'SelectEncoding' NewSelectNum return elseif Button = 4 then -- Edit 'EditEncoding' SelectNum return else -- Cancel or Esc key return endif return ; --------------------------------------------------------------------------- ; Find NextFreeNum for 'Add' defproc GetNextFreeEncodingNum universal nepmd_hini -- Search first free number in RegKeys and RegDefaults KeyPath = '\NEPMD\User\Encoding' NextFreeNum = 1 n = 0 do forever n = n + 1 Next = QueryConfigKey( KeyPath'\'n'\Name') if Next <> '' then iterate endif Next = QueryConfigKey( KeyPath'\'n'\UconvCode') if Next <> '' then iterate endif Next = QueryConfigKey( KeyPath'\'n'\RecodeCode') if Next <> '' then iterate endif NextFreeNum = n leave enddo return NextFreeNum ; --------------------------------------------------------------------------- ; Buttons = '/~Add.../Show ~list.../~Configure.../~Delete/Return' defc EditEncoding KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Selected') Order = QueryConfigKey( KeyPath'\Order') NoEntry = '-none-' Delim = \1 -- Build EncodingList and get data for active item EncodingList = '' ActiveSelectNum = '' ActiveEncoding = '' do w = 1 to words( Order) NextNum = word( Order, w) Name = QueryConfigKey( KeyPath'\'NextNum'\Name') EncodingList = EncodingList''Delim''Name if NextNum = ActiveNum then ActiveSelectNum = w ActiveEncoding = Name endif enddo if EncodingList = '' then EncodingList = Delim''NoEntry -- without an entry, listbox would return Button = 0 endif -- Determine pre-selected item PreSelectNum = arg( 1) if not IsNum( PreSelectNum) then PreSelectNum = '' endif if PreSelectNum = '' then if ActiveSelectNum = '' then PreSelectNum = 1 else PreSelectNum = ActiveSelectNum endif endif -- Open Listbox -- No Linebreak allowed in Text Title = 'Edit encoding' Text = 'Selected encoding: 'ActiveEncoding if ActiveNum = '' then DefButton = 1 -- Add else DefButton = 3 -- Configure endif HelpId = 0 refresh ret = ListBox( Title, EncodingList, '/~Add.../Show ~list.../~Configure.../~Delete/Return', -- buttons 6, 10, -- top, left, Min( count( Delim, EncodingList) + 1, 12), 60, -- height, width GethWndC( APP_HANDLE) || atoi( PreSelectNum) || atoi( DefButton) || atoi( HelpId) || Text\0) refresh -- Parse button number and selected list item string Button = asc( leftstr( ret, 1)) EOS = pos( \0, ret, 2) -- chr( 0) signifies End Of String Select = substr( ret, 2, EOS - 2) SelectNum = '' if Select = NoEntry then Select = '' if Button = 3 | Button = 4 then Button = 1 -- switch to Add endif endif if Select = '' then SelectNum = PreSelectNum else SelectNum = EncodingNameToNum( Select, 'R') -- For multiple entries, take last one first endif if Button = 1 then -- Add Config = 'NEW' -- How handle codes that exist only in recode? -- 'Add' should allow for either specifying a code name or a codepage number. 'ConfigEncoding' Config SelectNum return elseif Button = 2 then -- Show list "open 'shell uconv -l'" "open 'shell recode -l'" 'EditEncoding' -- restart return elseif Button = 3 then -- Configure Config = 'CONFIG' 'ConfigEncoding' Config SelectNum return elseif Button = 4 then -- Delete if Select <> '' then DeleteConfigKey( KeyPath'\'SelectNum'\UconvCode') DeleteConfigKey( KeyPath'\'SelectNum'\RecodeCode') -- (required?) DeleteConfigKey( KeyPath'\'SelectNum'\Name') if SelectNum = ActiveNum then NewNum = 1 NewUconvCode = QueryConfigKey( KeyPath'\'NewNum'\UconvCode') NewRecodeCode = QueryConfigKey( KeyPath'\'NewNum'\RecodeCode') -- (required?) WriteConfigKey( KeyPath'\Selected', NewNum) endif wpos = wordpos( SelectNum, Order) if wpos then NewOrder = SpaceStr( delword( Order, wpos, 1)) WriteConfigKey( KeyPath'\Order', NewOrder) endif endif 'EditEncoding' -- restart return else -- Cancel or Esc key 'SelectEncoding' SelectNum -- return return endif return ; --------------------------------------------------------------------------- ; Buttons = '/'LeftArrow' ~Back/~Next 'RightArrow'/Return' defc ConfigEncoding RightArrow = \16 LeftArrow = \17 Empty = \255 -- This button text works to create an empty button i = 1 KeyPath = '\NEPMD\User\Encoding' NoName = '-untitled-' parse arg Config ConfigNum SavedConfigNum = ConfigNum Config = upcase( Config) Order = QueryConfigKey( KeyPath'\Order') ActiveNum = QueryConfigKey( KeyPath'\Selected') UconvCode = '' RecodeCode = '' Name = '' if Config = 'NEW' | ConfigNum = '' then UconvCode = QueryConfigKey( KeyPath'\'ActiveNum'\UconvCode') RecodeCode = QueryConfigKey( KeyPath'\'ActiveNum'\RecodeCode') ConfigNum = GetNextFreeEncodingNum() else DefaultUconvCode = QueryConfigKey( KeyPath'\'ConfigNum'\UconvCode') DefaultRecodeCode = QueryConfigKey( KeyPath'\'ConfigNum'\RecodeCode') DefaultName = QueryConfigKey( KeyPath'\'ConfigNum'\Name') UconvCode = DefaultUconvCode RecodeCode = DefaultRecodeCode Name = DefaultName endif Again = 1 do while Again Again = 0 -- Open EntryBox -- No line break allowed in Text maxi = 3 if Config = 'NEW' then Title = 'Add new encoding - page 'i'/'maxi else Title = 'Configure encoding "'Name'" - page 'i'/'maxi endif if i = 1 then --dprintf( 'i = 'i', UconvCode = 'UconvCode', RecodeCode = 'RecodeCode', Name = 'Name) Text = 'Enter an encoding for uconv:' Buttons = '/'Empty'/~Next 'RightArrow'/Return' Back = 0; Next = 2; OK = 0; Cancel = 3 OldEntry = UconvCode DefaultButton = Next elseif i = 2 then --dprintf( 'i = 'i', UconvCode = 'UconvCode', RecodeCode = 'RecodeCode', Name = 'Name) Text = 'Enter an encoding for recode:' OldEntry = RecodeCode Buttons = '/'LeftArrow' ~Back/~Next 'RightArrow'/Return' Back = 1; Next = 2; OK = 0; Cancel = 3 DefaultButton = Next else --dprintf( 'i = 'i', UconvCode = 'UconvCode', RecodeCode = 'RecodeCode', Name = 'Name) Text = 'Enter a name for the encoding:' Buttons = '/'LeftArrow' ~Back/'OK__MSG'/Return' Back = 1; Next = 0; OK = 2; Cancel = 3 if Name = '' then Name = UconvCode endif OldEntry = Name DefaultButton = OK endif Title = leftstr( Title, 100)'.' -- Add spaces to fit the text in titlebar HelpId = 0 ret = EntryBox( Title, Buttons, OldEntry, '', 255, -- Length atoi( DefaultButton) || atoi( HelpId) || GethWndC( APP_HANDLE) || Text\0) -- Parse button number and selected list item string EntryButton = asc( leftstr( ret, 1)) EOS = pos( \0, ret, 2) -- CHR(0) signifies End Of String NewEntry = substr( ret, 2, EOS - 2) --dprintf( 'EntryButton = 'EntryButton', Back = 'Back', Next = 'Next', OK = 'OK', Cancel = 'Cancel) -- Check if empty button was pressed if (Entrybutton = 1 & Back = 0) then -- Re-open this dialog Again = 1 -- Check if 0 for Esc key must come first elseif EntryButton = Cancel | EntryButton = 0 then 'EditEncoding' SavedConfigNum return elseif EntryButton = Next then if i = 1 then UconvCode = NewEntry elseif i = 2 then RecodeCode = NewEntry elseif i = 3 then Name = NewEntry endif --dprintf( 'Button = Next, i = 'i', UconvCode = 'UconvCode', RecodeCode = 'RecodeCode', Name = 'Name) -- Open this dialog with next page Again = 1 i = i + 1 elseif EntryButton = Back then if i = 1 then UconvCode = NewEntry elseif i = 2 then RecodeCode = NewEntry elseif i = 3 then Name = NewEntry endif --dprintf( 'Button = Back, i = 'i', UconvCode = 'UconvCode', RecodeCode = 'RecodeCode', Name = 'Name) -- Open this dialog with previous page Again = 1 i = i - 1 elseif EntryButton = OK then if i = 1 then UconvCode = NewEntry elseif i = 2 then Recode = NewEntry elseif i = 3 then Name = NewEntry endif --dprintf( 'Button = OK, i = 'i', UconvCode = 'UconvCode', RecodeCode = 'RecodeCode', Name = 'Name) if Name = '' then Name = NoName endif if IsNum( ConfigNum) then if Config = 'NEW' then -- Check if name already exists fNameExists = 0 do n = 1 to ConfigNum - 1 if Name = QueryConfigKey( KeyPath'\'n'\Name') then fNameExists = 1 leave endif enddo if fNameExists then Again = 1 Title = 'Add new encoding name' Text = '' Text = Text || Name\n\n Text = Text || 'This name is already defined. Specify another one.' Style = MB_OK + MB_WARNING + MB_MOVEABLE rcx = WinMessageBox( Title, Text, Style) iterate endif endif -- Write changed entries if UconvCode <> DefaultUconvCode | Config = 'NEW' then WriteConfigKey( KeyPath'\'ConfigNum'\UconvCode', UconvCode) endif if RecodeCode <> DefaultRecodeCode | Config = 'NEW' then WriteConfigKey( KeyPath'\'ConfigNum'\RecodeCode', RecodeCode) -- (required?) endif if Name <> DefaultName | Config = 'NEW' then WriteConfigKey( KeyPath'\'ConfigNum'\Name', Name) endif -- Append to Order, if not already present if not wordpos( ConfigNum, Order) then WriteConfigKey( KeyPath'\Order', strip( Order ConfigNum)) endif else if Config = 'NEW' then Title = 'Add new encoding' else Title = 'Configure encoding' endif Text = '' Text = Text || Name\n\n Text = Text || 'No entry written to NEPMD.INI, ConfigNum = "'ConfigNum'".' Style = MB_OK + MB_ERROR + MB_MOVEABLE rcx = WinMessageBox( Title, Text, Style) endif -- Automatically make the new encoding the selected one, if named and if none selected before if Config = 'NEW' & Name <> NoName then 'EncodingNum' ConfigNum endif 'EditEncoding' ConfigNum return endif enddo return ; --------------------------------------------------------------------------- defproc GetEncodingCode Tool = GetEncodingTool() Tool = upcase( leftstr( Tool, 1))''lowcase( substr( Tool, 2)) KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Selected') Code = QueryConfigKey( KeyPath'\'ActiveNum'\'Tool'Code') return Code ; --------------------------------------------------------------------------- defproc GetStandardEncodingCode universal codepage Tool = GetEncodingTool() Tool = upcase( leftstr( Tool, 1))''lowcase( substr( Tool, 2)) KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Standard') if ActiveNum = 0 then if lowcase( GetEncodingTool()) <> 'recode' then SystemEncoding = 'ibm-'codepage else SystemEncoding = 'ibm'codepage endif Code = SystemEncoding else Code = QueryConfigKey( KeyPath'\'ActiveNum'\'Tool'Code') endif return Code ; --------------------------------------------------------------------------- defproc GetEncodingName KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Selected') Name = QueryConfigKey( KeyPath'\'ActiveNum'\Name') return Name ; --------------------------------------------------------------------------- defproc GetStandardEncodingName universal codepage KeyPath = '\NEPMD\User\Encoding' ActiveNum = QueryConfigKey( KeyPath'\Standard') if ActiveNum = 0 then Name = 'ibm-'codepage else Name = QueryConfigKey( KeyPath'\'ActiveNum'\Name') endif return Name ; --------------------------------------------------------------------------- defc SetEncodingTool Tool = lowcase( strip( arg( 1))) KeyPath = '\NEPMD\User\Encoding' call WriteConfigKey( KeyPath'\Tool', Tool) ; --------------------------------------------------------------------------- ; This queries the config key and respects also previous CheckTool calls. ; It returns a tool that exists with default value = 'Uconv'. defproc GetEncodingTool KeyPath = '\NEPMD\User\Encoding' Tool = QueryConfigKey( KeyPath'\Tool') fUconvFound = (GetAVar( 'tool.uconv') <> '') fRecodeFound = (GetAVar( 'tool.recode') <> '') if not fRecodeFound then Tool = 'uconv' elseif not fUconvFound & fRecodeFound then Tool = 'recode' endif return Tool