Here is some demonstration code to test out the previous module include. I had some help from Amilcar Matos Pérez who helped layout the format of the code.
Code: Select all
;{- Program header
;==Code Header Comment==============================
; Name/title: EBODetector.pb
; Executable name: EBODetector.exe
; Version: 1.01
; Author: Demivec
; Collaborators: Amílcar Matos Pérez
; Translation by:
; Create date: 24/Dec/2015
; Previous releases:
;Most recent update:
; Release date/hour:
; Operating system: Windows [X]GUI
; Compiler version: PureBasic 5.41 (x64)
; Copyright: (C)2015 AMP All rights reserved.
; License: Free to use/abuse/modify.
; // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
; // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
; // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
; // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
; // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
; // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
; // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
; // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
; // SUCH DAMAGE.
; Libraries:
; Forum: http://www.purebasic.fr/english/viewtopic.php?f=13&t=64180&sid=ec0e244cfabf06876bfd82d3f709cc1c
; http://www.purebasic.fr/english/viewtopic.php?f=12&t=64385
; Tested platforms: Windows
; Explanation: Demonstration of how to use dte module to detect the text file encoding
; by examining the file content.
; ==================================================
; v1.1 Added detection of zero-length files and set them to generate an error result.
;.......10........20........30........40........50........60........70........80
;}
EnableExplicit
XIncludeFile "AutoDetectTextEncoding.pbi"
;{ Declare procedures
Declare.i SetCursorPosition(StringGadgetID, CursorPosition) ;credit to Shardik for this multi-platform code
Declare.i BrowseProcedure(Array filenames.s(1))
Declare.i ClearWindowDataEntryFields (Window_EBO)
Declare OpenWindow_EBO (x = 0, y = 0, width = 600, height = 440)
Declare.i Window_Events (Event_EBO)
;}
;{ Variable exposure stmts
Global Window_EBO
Global Frame_0, FilenameStr_EBO, BrowseButton_EBO
Global Frame_1, DetectButton_EBO, EnforceSurrogatesCheckBox_EBO
Global RestrictSurrogatesCheckBox_EBO, DisallowNullsCheckBox_EBO
Global Frame_2, ResultsEditor_EBO,ClipBoardButton_EBO, ClearButton_EBO
Global ExitButton_EBO
;}
Enumeration FormFont
#Font_Window_EBO_0
EndEnumeration
LoadFont(#Font_Window_EBO_0,"Consolas", 10)
OpenWindow_EBO()
ClearWindowDataEntryFields(Window_EBO)
Define Event_EBO, Quit_EBO
Repeat
Event_EBO = WaitWindowEvent()
Quit_EBO = Window_Events(Event_EBO)
Until Quit_EBO = 0
End
Procedure.i Window_Events(Event_EBO)
Static multipleFilesSelected = 0 ;contains number of selected files if >1 or zero
Static PreviousContentsOfFilenameStr$ = ""
Static Dim filenames.s(0)
Protected results, flags, i, j, count, files$
Dim encodingResults(0)
Select Event_EBO
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_GadgetDrop ;{- Accept file list via a GadgetDrop
If EventGadget() = FilenameStr_EBO
files$ = EventDropFiles()
count = CountString(files$, Chr(10)) + 1
Dim filenames.s(count - 1)
For i = 1 To count
filenames(i - 1) = StringField(files$, i, Chr(10))
Next
If count = 1
SetGadgetText(FilenameStr_EBO, filenames(0))
multipleFilesSelected = 0
ElseIf count > 1
multipleFilesSelected = count
SetGadgetText(FilenameStr_EBO, "***** " + count + " Files Selected *****")
EndIf
SetActiveGadget(DetectButton_EBO)
EndIf ;}
Case #PB_Event_Gadget
Select EventGadget()
Case ExitButton_EBO ;{- Exit"
ProcedureReturn #False ;}
Case ClearButton_EBO ;{- Clear Data Entry Fields
ClearWindowDataEntryFields(Window_EBO) ;}
Case FilenameStr_EBO ;{- Handle differences between multiple files and a single file
Select EventType()
Case #PB_EventType_Focus
PreviousContentsOfFilenameStr$ = GetGadgetText(FilenameStr_EBO)
Case #PB_EventType_Change
If multipleFilesSelected
files$ = GetGadgetText(FilenameStr_EBO)
i = 1
j = 1
While Left(files$, i) = Left(PreviousContentsOfFilenameStr$, i)
i + 1
Wend
While Right(files$, j) = Right(PreviousContentsOfFilenameStr$, j)
j + 1
Wend
SetGadgetText(FilenameStr_EBO, Mid(files$, i, Len(files$) - j - i + 2))
SetCursorPosition(FilenameStr_EBO, j - i + 2) ;set cursor after text
EndIf
PreviousContentsOfFilenameStr$ = GetGadgetText(FilenameStr_EBO)
multipleFilesSelected = 0
Dim filenames.s(0) ;zero contents
Dim encodingResults(0)
EndSelect ;}
Case BrowseButton_EBO ;{- Browse for file to test.
results = BrowseProcedure(filenames())
If results = 1
multipleFilesSelected = 0
SetGadgetText(FilenameStr_EBO, filenames(0))
ElseIf results > 1
multipleFilesSelected = results
SetGadgetText(FilenameStr_EBO, "***** " + results + " Files Selected *****")
EndIf ;}
Case ClipBoardButton_EBO ;{- Copy the results to the clipboard.
SetClipboardText(GetGadgetText(ResultsEditor_EBO)) ;}
Case DetectButton_EBO ;{- Detect Encoding and Byte Order
flags = Bool(GetGadgetState(DisallowNullsCheckBox_EBO) = #PB_Checkbox_Checked) * dte::#disallowNulls +
Bool(GetGadgetState(RestrictSurrogatesCheckBox_EBO) = #PB_Checkbox_Checked) * dte::#restrictSurrogateCodesToUTF16 +
Bool(GetGadgetState(EnforceSurrogatesCheckBox_EBO) = #PB_Checkbox_Checked) * dte::#enforceProperSurrogateCodes
If multipleFilesSelected
dte::detectTextEncodingOfFiles(filenames(), encodingResults(), -1, flags)
For i = 0 To multipleFilesSelected - 1
AddGadgetItem(ResultsEditor_EBO, -1, filenames(i))
AddGadgetItem(ResultsEditor_EBO, -1, "Encoding detection results are: '" + dte::textForDetectedStringFormat(encodingResults(i)) + "'.")
Next
Else
AddGadgetItem(ResultsEditor_EBO, -1, GetGadgetText(FilenameStr_EBO))
results = dte::detectTextEncodingInFile(GetGadgetText(FilenameStr_EBO), -1, flags)
AddGadgetItem(ResultsEditor_EBO, -1, "Encoding detection results are: '" + dte::textForDetectedStringFormat(results) + "'.")
EndIf ;}
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
Procedure.i ClearWindowDataEntryFields(Window_EBO)
;{- Procedure explanation
; To blank the screen data entry fields.
;}
SetGadgetText(FilenameStr_EBO , #Null$)
SetGadgetText(ResultsEditor_EBO, #Null$)
SetActiveGadget(FilenameStr_EBO)
ProcedureReturn #True
EndProcedure
Procedure.i BrowseProcedure(Array filenames.s(1))
;{- Procedure explanation
; To ease the file selection task.
;}
Protected StandardFile$, Filename$, Pattern$, Pattern, i
NewList file.s()
StandardFile$ = "C:\" ; set initial file+path to display
; With next string we will set the search patterns ("|" as separator) for file displaying:
; 1st: "Text (*.txt)" as name, ".txt" and ".bat" as allowed extension
; 2nd: "PureBasic (*.pb)" as name, ".pb" and ".pbi" and ".pbf" as allowed extension
; 3rd: "All files (*.*) as name, "*.*" as allowed extension, valid for all files
Pattern$ = "Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb;*.pbi;*.pbf|All files (*.*)|*.*"
Pattern = 0 ; use the first of the three possible patterns as standard
Filename$ = OpenFileRequester("Please choose file to test", StandardFile$, Pattern$, Pattern, #PB_Requester_MultiSelection)
If Filename$ <> ""
While Filename$
AddElement(file())
file() = Filename$
Filename$ = NextSelectedFileName()
Wend
Dim filenames(ListSize(file()))
ForEach file()
filenames(i) = file()
i + 1
Next
EndIf
ProcedureReturn i ;number of files selected
EndProcedure
Procedure SetCursorPosition(StringGadgetID, CursorPosition) ;sets position of cursor in a StringGadget
SetActiveGadget(StringGadgetID)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
SendMessage_(GadgetID(StringGadgetID), #EM_SETSEL, CursorPosition, CursorPosition)
CompilerCase #PB_OS_Linux
gtk_editable_set_position_(GadgetID(StringGadgetID), CursorPosition)
CompilerCase #PB_OS_MacOS
Protected Range.NSRange
Protected TextView.I
Range\location = CursorPosition
Range\length = 0
TextView = CocoaMessage(0, GadgetID(StringGadgetID), "currentEditor")
If TextView
CocoaMessage(0, TextView, "setSelectedRange:@", @Range)
EndIf
CompilerEndSelect
EndProcedure
Procedure OpenWindow_EBO(x = 0, y = 0, width = 600, height = 440)
Window_EBO = OpenWindow(#PB_Any, x, y, width, height, "Encoding/Byte Order Detector of text files",
#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
CreateStatusBar(0, WindowID(Window_EBO))
AddStatusBarField(150)
StatusBarText(0, 0, "(c) 2015 Demivec")
Frame_0 = FrameGadget(#PB_Any, 20, 20, 560, 50, "Filename")
FilenameStr_EBO = StringGadget(#PB_Any, 30, 40 , 470, 20, #Null$)
SetGadgetFont(FilenameStr_EBO , FontID(#Font_Window_EBO_0))
BrowseButton_EBO = ButtonGadget(#PB_Any, 510, 35, 60, 30, "Browse")
EnableGadgetDrop(FilenameStr_EBO, #PB_Drop_Files, #PB_Drag_Copy)
GadgetToolTip(FilenameStr_EBO, "Enter, Select, or Drop files here to attempt encoding detection.")
Frame_1 = FrameGadget(#PB_Any, 20, 80, 560, 90, "Detection Settings")
DisallowNullsCheckBox_EBO = CheckBoxGadget(#PB_Any, 100, 100, 200, 20, "Disallow Nulls")
RestrictSurrogatesCheckBox_EBO = CheckBoxGadget(#PB_Any, 100, 120, 200, 20, "Restrict Surrogate Codes to UTF-16")
EnforceSurrogatesCheckBox_EBO = CheckBoxGadget(#PB_Any, 100, 140, 200, 20, "Enforce Proper Surrogate Codes")
DetectButton_EBO = ButtonGadget(#PB_Any, 510, 110, 60, 30, "Detect")
Frame_2 = FrameGadget(#PB_Any, 20, 180, 560, 220, "Results" )
ResultsEditor_EBO = EditorGadget(#PB_Any, 30, 200, 470, 190)
SetGadgetFont(ResultsEditor_EBO, FontID(#Font_Window_EBO_0))
ClipBoardButton_EBO = ButtonGadget(#PB_Any, 510, 200, 60, 30, "Copy To Clipboard", #PB_Button_MultiLine)
ClearButton_EBO = ButtonGadget(#PB_Any, 510, 240, 60, 30, "Clear" )
ExitButton_EBO = ButtonGadget(#PB_Any, 510, 360, 60 , 30, "Exit" )
EndProcedure
I ran a hardcore test by scanning 15231 files totaling 231 Mbytes and it took about 4 minutes and used up 30% of my CPU the entire time. As a result I think the code could use some additional improvements such as perhaps creating threads if acting on a group of files (and maybe a progress bar
).