| Index | Manual | Download | Source | Macros | Embedded word Case ins.
Sitemap | Links | Forum | Tests | Projects

# convwinc.htm
Data
Data
Procedures
ConversionPrologue
ConversionEpilogue
MainCon
ReadFile_ini
RecapEncoding
RecapFile
RecapHeaderAndFooter
RecapNumber
WriteFileProxy

This is a CON module of EuroTool program EuroConv for Windows.

             EUROASM CPU=X64, SIMD=yes, Unicode=no
    convwinc PROGRAM Format=COFF, Width=64
             %DROPMACRO *
             INCLUDE1 winabi.htm, wins.htm, winf64.htm, status32.htm
             INCLUDE1  stdcal64.htm, string64.htm, cpuext.htm, cpuext64.htm,
             INCLUDEHEAD argument.htm
             LINK ../objlib/winapi.lib

Data

    [.bss]
    File_ini     DS FILE64
    FileInput::  DS FILE64
    FileOutput:: DS FILE64
    NameUTF16    D MAX_PATH_SIZE * UNICHAR ; Temporary working space for file names in UTF-16.
    NameUTF8     D MAX_PATH_SIZE * UNICHAR ; Temporary working space for file names in UTF-8.
    Work16::     D MAX_PATH_SIZE * UNICHAR ; Temporary working space for reports in UTF-8.
    [.text]

MainCon
  The MainCon procedure is executed both in console and in graphic mode.

    MainCon:: PROC                         ; Entry of EuroText.
        LEA RSI,[CPid::]
        LEA RCX,[CPidEnd::]
        SUB RCX,RSI
        SHR ECX,1
        MOV [CodePagesLength::],ECX
        LEA RSI,[CodePoint::]
        LEA RCX,[CodePointEnd::]
        SUB RCX,RSI
        SHR ECX,1
        MOV [CodePointLength::],ECX
        MOVD [ArgOutputEncoding::],65001   ; Set the default UTF-8.
        MOVB [ArgHtmlEntities::],'I'       ; Set the default value.
        MOVB [ArgInvalidCharacter::],'T'   ; Set the default value.
        LEA RAX,[FileWriteProxy]
        MOV [WriteProxy::],RAX
        StdOutput EuroConv::, Version::, Eol=yes
        ; Try to load arguments from the configuration file "%AppData%\eurotool\euroconv.ini".
        WinABI GetEnvironmentVariableW, =U'AppData', NameUTF16, SIZE# NameUTF16
        TEST RAX
        JZ .30:                  ; Jump if %AppData% was not found.
        FileAssign File_ini, NameUTF16, =U'\eurotool\euroconv.ini', Unicode=yes
        FileExists? File_ini
        JNC .13:
        FileMkDir File_ini
        JC .10:
        LEA RDI,[Help::]             ; Online help and default configuration.
        GetLength$ RDI, Unicode=no
        FileStore File_ini, RDI, RCX
        JNC .12:
    .10:StdOutput =U'Configuration "',File_ini.Name, =U'" could not be saved.', Eol=yes, Console=yes, Unicode=yes
        JMP .30:
    .12:StdOutput =U'Configuration "',File_ini.Name, =U'" was saved.', Eol=yes, Console=yes, Unicode=yes
    .13:CALL ReadFile_ini
    .30:; And finally read arguments from the command-line.
        INCB [ArgNr::]            ; Prepare to load the next argument.
        GetArg [ArgNr],Unicode=yes ; RSI,RCX is the line with one argument in UTF-16, e. g. /InputFile="C:\TEMP\Sometext.txt"
        JC .70:                ; When they are no more arguments.
        SHR ECX,1
        LEA RDI,[NameUTF8]
        WinABI WideCharToMultiByte,65001,0,RSI,RCX,RDI,SIZE#NameUTF8/2,0,0 ; Convert to UTF-8.
        MOVB [RDI+RAX],0
        MOV RSI,RDI
        GetLengthUTF8 RSI
        CALL ArgParse::  ; Use ArgParse to translate UTF-8 string RSI,RCX to a public symbol Arg***.
        JNC .30:         ; Go to read the next argument.
        StdOutput ErrorMessage::,Unicode=no       ; On error write help and exit.
        JSt [Status::],ArgEnc?|ArgLoc?|ArgFF?,.50:
        StdOutput 3+Help::, Unicode=no  ; On error write help and exit.
    .50:TerminateProgram 8
    .70:                 ; Processing of command-line arguments is done. No errors detected.
        CMPB [ArgInputFile::],0      ; Was the file name specified by command-line?
        JZ .GUI:
        CMPB [ArgOutputFile::],0     ; Was the file name specified by command-line?
        JNZ .80:                      ; If both files are specified, perform the conversion in console mode. No GUI.
    .GUI:CALL MainGui::
        CALL Gui2Arg::
        JSt [Status::],ArgCancel,.85:
    .80:CALL ConversionPrologue
        CALL ConvFile::               ; Convert the input file .
    .85:CALL ConversionEpilogue
        TerminateProgram 0
      ENDP MainCon

AutoPrologue
  This procedure will open th input file and autodetect input encoding.
It does nothing if it cannot read the input file.

    AutoPrologue:: PROC
        LEA RSI,[ArgInputFile::]
        CMPB [RSI],0
        JZ .90:
        LEA RDI,[Work16]
        WinABI MultiByteToWideChar,65001,0,RSI,-1,RDI,SIZE# Work16
        FileAssign FileInput,RDI,Unicode=yes
        FileLoad FileInput
        JC .90:
        MOV [HeaderInMemPtr::],RSI
        MOV [HeaderInMemEnd::],RSI
        ADD RAX,RSI
        MOV [FooterInMemEnd::],RAX
        MOV [FooterInMemPtr::],RAX
        CALL AutodetectEncoding::
        MOV [ArgInputEncoding::],EAX
    .90:RET
       ENDP AutoPrologue::

ConversionPrologue
  This procedure will check the arguments, open the files, autodetect input encoding if not specified, apply restriction on header and footer, recapitulate arguments.

    ConversionPrologue:: PROC
        FileClose FileInput, FileOutput
        StdOutput Eol=yes
        LEA RSI,[ArgOutputFile::]
        LEA RDI,[Work16]
        WinABI MultiByteToWideChar,65001,0,RSI,-1,RDI,SIZE# Work16
        FileAssign FileOutput,Work16,Unicode=yes
        FileStreamCreate FileOutput
        JC .ErrorWrite:
        LEA RSI,[ArgInputFile::]
        LEA RDI,[Work16]
        WinABI MultiByteToWideChar,65001,0,RSI,-1,RDI,SIZE# Work16
        FileAssign FileInput,Work16,Unicode=yes
        FileLoad FileInput
        JC .ErrorRead:
        MOV [HeaderInMemPtr::],RSI
        MOV [HeaderInMemEnd::],RSI
        ADD RAX,RSI
        MOV [FooterInMemEnd::],RAX
        MOV [FooterInMemPtr::],RAX
        MOV EDX,[ArgInputEncoding::]
        TEST EDX
        JNZ .20:
        CALL AutodetectEncoding::
        MOV [ArgInputEncoding::],EAX
    .20:CALL HeaderAndFooter::   ; Crop the input.
        JC .ErrorRestrictions:
        ; Recapitulation of arguments:
        LEA RSI,[=B'Input']
        LEA RAX,[FileInput.Name]
        CALL RecapFile:
        MOV EAX,[ArgHeaderSize::]
        LEA RSI,[=B'Header size: ']
        CALL RecapHeaderAndFooter:
        MOV EAX,[ArgHeaderLength::]
        LEA RSI,[=B'Header length: ']
        CALL RecapHeaderAndFooter:
        MOV EAX,[ArgFooterSize::]
        LEA RSI,[=B'Footer size: ']
        CALL RecapHeaderAndFooter:
        MOV EAX,[ArgFooterLength::]
        LEA RSI,[=B'Footer length: ']
        CALL RecapHeaderAndFooter:
        MOV EAX,[ArgInputEncoding::]
        LEA RSI,[=B'Input']
        CALL RecapEncoding:
        StdOutput =B'HtmlEntities: ',ArgHtmlEntities::, Eol=yes, Unicode=no
        LEA RSI,[=B'Output']
        LEA RAX,[FileOutput.Name]
        CALL RecapFile:
        LEA RSI,[=B'Output']
        MOV EAX,[ArgOutputEncoding::]
        CALL RecapEncoding:
        MOV EAX,[ArgOutputEncoding::]
        Dispatch AX,65001,1200,1201,12000,12001
        JMP .40:
    .1200:
    .1201:
    .12000:
    .12001:
    .65001:
        StdOutput =B"Byte Order Mark: ",Unicode=no
        LEA RSI,[=B"on"]
        JSt [Status::],ArgBOM,.30:
        LEA RSI,[=B"off"]
    .30:StdOutput RSI,Eol=yes,Unicode=no
    .40:StdOutput =B'InvalidCharacters: ',ArgInvalidCharacter::, Eol=yes, Unicode=no
        JMP .90:
    .ErrorRestrictions:
        StdOutput =B"Wrong header and footer restrictions.", Eol=yes, Unicode=no
        JMP .90:
    .ErrorWrite:
        StdOutput =B'Cannot write the file "',Unicode=no
        StdOutput FileOutput.Name, Console=yes, Unicode=yes
    .80:StdOutput =B'"',Unicode=no, Eol=yes
        JMP .90:
    .ErrorRead:
        StdOutput =B'Cannot read the file "',Unicode=no
        StdOutput FileInput.Name, Console=yes, Unicode=yes,
        JMP .80:
    .90:RET
    ENDP ConversionPrologue

ConversionEpilogue
  This procedure will recapitulate number of converted characters, input and output errors.

    ConversionEpilogue:: PROC
        FileClose FileInput, FileOutput
        JNSt [Status::],ArgCancel,.50:
        StdOutput =B'Conversion aborted.',Eol=yes
        JMP .90:
    .50:MOV EAX,[Converted::]
        LEA RSI,[=B'Converted characters: ']
        CALL RecapNumber:
        MOV EAX,[InputErrors::]
        LEA RSI,[=B'Input errors: ']
        CALL RecapNumber:
        MOV EAX,[OutputErrors::]
        LEA RSI,[=B'Output errors: ']
        CALL RecapNumber:
    .90:RET
       ENDP ConversionEpilogue

RecapFile
  Recapitulate input|output file.
Input
  RAX= ^file name.
  RSI= prompt.
Called by
  ConversionPrologue.

    RecapFile: PROC
        StdOutput RSI,=B' file: "', Unicode=no
        StdOutput RAX, Console=yes, Unicode=yes
        StdOutput =B'"', Eol=yes, Unicode=no
        RET
      ENDP RecapFile:

RecapHeaderAndFooter
  Recapitulate header and footer.
Input
  RAX= value of the header or footer in bytes|lines.
  RSI= prompt.
Called by
  ConversionPrologue.

    RecapHeaderAndFooter: PROC
        TEST EAX
        JZ .90:
        LEA RDI,[Work16]      ; Recapitulate only when nonzero.
        StoD RDI
        XOR EAX,EAX
        STOSB
        StdOutput RSI, Work16, Eol=yes, Unicode=no
    .90:RET
       ENDP RecapHeaderAndFooter:

RecapEncoding
  Recapitulate encoding.
Input
  RAX= encoding in Microsoft nomenclature.
  RSI= prompt.
Called by
  ConversionPrologue.

    RecapEncoding: PROC
        LEA RDI,[CPid::]
        MOV ECX,[CodePagesLength::]
        MOV RDX,RDI
        REPNE SCASW
        SUB RDI,RDX
        MOV EDX,EDI
        SHR EDX,1
        LEA RDI,[Names::]
        LEA RCX,[Tables::]
        SUB RCX,RDI
        XOR EAX,EAX
    .R5:DEC EDX
        JZ .R7:
        REPNE SCASB
        REPNE SCASB
        JMP .R5:
    .R7:StdOutput RSI, =B' encoding: ', RDI, =B' ', Unicode=no
        XOR EAX,EAX
        REPNE SCASB
        StdOutput RDI, Eol=yes, Unicode=no
        RET
      ENDP RecapEncoding:

RecapNumber
  Recapitulate a number (of errors).
Input
  RAX= binary number.
  RSI= prompt.
Called by
  ConversionEpilogue.

    RecapNumber: PROC
        LEA RDI,[Work16]
        StoD RDI
        XOR EAX,EAX
        STOSB
        StdOutput RSI,Work16, Eol=yes, Unicode=no
        RET
      ENDP RecapNumber:

WriteFileProxy
  Procedure WriteFileProxy writes one byte of data in AL to previously created OutputFile in Windows.
Input
  AL= the data to write.
Called by
  ConvFile.
Clobbers
  nothing

    FileWriteProxy PROC
        CMPW [FileInput.Name+0],0
        JE .90:                              ; Do not write to std output
        FileStreamWriteByte FileOutput
        JNC .90:
        StdOutput =B'Error writing to the file "', Unicode=no
        StdOutput FileOutput.Name, Unicode=yes, Console=yes
        StdOutput =B'", Aborting.', Eol=yes, Unicode=no
        FileClose FileInput, FileOutput
        TerminateProgram 8
    .90:RET
       ENDP FileWriteProxy

ReadFile_ini
  Procedure ReadFile_ini reads lines from File_ini in UTF-8 and parses its each line into configuration variables Arg*** in UTF-8, too.
  The File_ini.Name is in UTF-16. Procedure writes information about the file name and whether it was found to Work16 in UTF-16.
Called by
  MainCon.
Input
  File_ini is assigned with the name.
Clobbers
  RAX,RCX,RDX,RSI,RDI

    ReadFile_ini:: PROC
        SetSt [Status::],ArgFromFile       ; Tell ArgParse that arguments need not begin with / or -.
        StdOutput =U'Configuration "', File_ini.Name, Console=yes, Unicode=yes
    .10:FileStreamOpen File_ini, BufSize=4K
        JNC .20:
        StdOutput =B'" was not found.', Eol=yes, Unicode=no
        JMP .90:
    .20:FileStreamReadLn File_ini
        JBE .80:
        MOV ECX,EAX              ; Line size. The first line may begin with UTF-8 BOM.
        MOV AX,[RSI]
        CMPW AX,0xBBEF           ; UTF-8 BOM?
        JNE .50:
        ADD RSI,3                ; Skip the BOM.
        SUB ECX,3
        JB .80:
        JMP .50:
    .30:FileStreamReadLn File_ini
        JBE .80:
        MOV ECX,EAX
    .50:CALL ArgParse::          ; RSI,RCX is the UTF-8 string to parse by ArgParse.
        JNC .30:
        LEA RSI,[ErrorMessage::]
        LEA RDI,[Work16]
        WinABI MultiByteToWideChar,65001,0,RSI,-1,RDI,SIZE# Work16
        StdOutput Work16,Console=yes,Unicode=yes
        StdOutput Help::, Unicode=no
        TerminateProgram 8
    .80:FileClose File_ini
        StdOutput =B'" was accepted.', Eol=yes, Unicode=no
    .90:RstSt [Status::],ArgFromFile
        RET
      ENDP ReadFile_ini

          ENDPROGRAM convwinc

▲Back to the top▲