Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
VECTO_Global.vb 6.65 KiB
Newer Older
' Copyright 2017 European Union.
' Licensed under the EUPL (the 'Licence');
'
' * You may not use this work except in compliance with the Licence.
' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
' * Unless required by applicable law or agreed to in writing,
'   software distributed under the Licence is distributed on an "AS IS" basis,
'   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'
' See the LICENSE.txt for the specific language governing permissions and limitations.
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Markus Quaritsch's avatar
Markus Quaritsch committed
	Public Const VECTOvers As String = "3"
	Public Const LicSigAppCode As String = "VECTO-Release-0093C61E0A2E4BFA9A7ED7E729C56AE4"
	Public MyAppPath As String
	Public MyConfPath As String
	Public LogFile As FileLogger
	'to ensure correct format for backgroundworker thread
Markus Quaritsch's avatar
Markus Quaritsch committed
	Public VectoWorkerV3 As BackgroundWorker
	Public ReadOnly FileFormat As Encoding = Encoding.UTF8
	Public ProgBarCtrl As ProgressbarControl
	Public Class FileLogger
		Private _logStream As StreamWriter
		Public Function StartLog() As Boolean
			Try
				_logStream = My.Computer.FileSystem.OpenTextFileWriter(MyAppPath & "LOG.txt", True, FileFormat)
				_logStream.AutoFlush = True
				WriteToLog(MessageType.Normal, "Starting Session " & Now)
				WriteToLog(MessageType.Normal, "VECTO " & VECTOvers)
			Catch ex As Exception
				Return False
			End Try
			Return True
		End Function
		Public Function SizeCheck() As Boolean
			Dim backUpError As Boolean
			'Start new log if file size limit reached
				'File size check
				logfDetail = My.Computer.FileSystem.GetFileInfo(MyAppPath & "LOG.txt")
				'If Log too large: Delete
				If logfDetail.Length / (2 ^ 20) > Cfg.LogSize Then
					WriteToLog(MessageType.Normal, "Starting new logfile")
					_logStream.Close()
					backUpError = False
						If File.Exists(MyAppPath & "LOG_backup.txt") Then File.Delete(MyAppPath & "LOG_backup.txt")
						File.Move(MyAppPath & "LOG.txt", MyAppPath & "LOG_backup.txt")
					Catch ex As Exception
						backUpError = True
					If Not StartLog() Then Return False
					If backUpError Then
						WriteToLog(MessageType.Err, "Failed to backup logfile! (" & MyAppPath & "LOG_backup.txt)")
						WriteToLog(MessageType.Normal, "Logfile restarted. Old log saved to LOG_backup.txt")
			Return True
		End Function
		Public Function CloseLog() As Boolean
			Try
				WriteToLog(MessageType.Normal, "Closing Session " & Now)
				_logStream.Close()
			Catch ex As Exception
				Return False
			End Try
			Return True
		End Function
		Public Function WriteToLog(msgType As MessageType, msg As String) As Boolean
			Dim msgTypeStr As String
			Select Case msgType
					msgTypeStr = "Error"
					msgTypeStr = "Warning"
					msgTypeStr = "-"
			End Select
				_logStream.WriteLine(Now.ToString("yyyy/MM/dd-HH:mm:ss") & vbTab & msgTypeStr & vbTab & msg)
				Return True
			Catch ex As Exception
				Return False
			End Try
		End Function
	End Class

#Region "File path functions"

	'When no path is specified, then insert either HomeDir or MainDir   Special-folders
	Public Function FileRepl(file As String, Optional ByVal mainDir As String = "") As String
		Dim replPath As String

		'Trim Path
		file = Trim(file)
		'If empty file => Abort
		If file = "" Then Return ""
		'Replace sKeys
		file = Replace(file, DefVehPath & "\", MyAppPath & "Default Vehicles\", 1, -1,
		file = Replace(file, HomePath & "\", MyAppPath, 1, -1, CompareMethod.Text)
		'Replace - Determine folder
		If mainDir = "" Then
			replPath = MyAppPath
			replPath = mainDir
		' "..\" => One folder-level up
		Do While replPath.Length > 0 AndAlso Left(file, 3) = "..\"
			replPath = PathUp(replPath)
			file = file.Substring(3)
		Loop
		'Supplement Path, if not available
Markus Quaritsch's avatar
Markus Quaritsch committed
		If GetPath(file) = "" Then
			Return replPath & file
		Else
			Return file
		End If
	End Function
	'Path one-level-up      "C:\temp\ordner1\"  >>  "C:\temp\"
	Private Function PathUp(pfad As String) As String
		pfad = pfad.Substring(0, pfad.Length - 1)
		x = pfad.LastIndexOf("\", StringComparison.Ordinal)
		Return pfad.Substring(0, x + 1)
	End Function
	'File name without the path    "C:\temp\TEST.txt"  >>  "TEST.txt" oder "TEST"
	Public Function GetFilenameWithoutPath(file As String, includeFileExtension As Boolean) As String _
'GetFilenameWithoutPath
Markus Quaritsch's avatar
Markus Quaritsch committed
		x = file.LastIndexOf("\", StringComparison.Ordinal) + 1
		file = Right(file, Len(file) - x)
		If Not includeFileExtension Then
			x = file.LastIndexOf(".", StringComparison.Ordinal)
			If x > 0 Then file = Left(file, x)
Markus Quaritsch's avatar
Markus Quaritsch committed
		Return file
	End Function
	'Filename without extension   "C:\temp\TEST.txt" >> "C:\temp\TEST"
	'Filename without path if Path = WorkDir or MainDir
	Public Function GetFilenameWithoutDirectory(file As String, Optional ByVal mainDir As String = "") As String _
'GetFilenameWithoutDirectory
		Dim path As String
Markus Quaritsch's avatar
Markus Quaritsch committed
		If mainDir = "" Then
			path = MyAppPath
		Else
Markus Quaritsch's avatar
Markus Quaritsch committed
			path = mainDir
Markus Quaritsch's avatar
Markus Quaritsch committed
		If UCase(GetPath(file)) = UCase(path) Then file = GetFilenameWithoutPath(file, True)
		Return file
	End Function
	'Path alone        "C:\temp\TEST.txt"  >>  "C:\temp\"
	'                   "TEST.txt"          >>  ""
	Public Function GetPath(file As String) As String 'GetPath
Markus Quaritsch's avatar
Markus Quaritsch committed
		Dim x As Integer
		If file Is Nothing OrElse file.Length < 3 OrElse file.Substring(1, 2) <> ":\" Then Return ""
		x = file.LastIndexOf("\", StringComparison.Ordinal)
		Return Left(file, x + 1)
	End Function
	'Extension alone      "C:\temp\TEST.txt" >> ".txt"
	Public Function GetExtension(file As String) As String 'GetExtension
Markus Quaritsch's avatar
Markus Quaritsch committed
		x = file.LastIndexOf(".", StringComparison.Ordinal)
			Return ""
		Else
Markus Quaritsch's avatar
Markus Quaritsch committed
			Return Right(file, Len(file) - x)
		End If
	End Function
Module Constants
	'Public ReadOnly AUX As AuxiliaryKey
	Public Const HomePath As String = "<HOME>"
	Public Const DefVehPath As String = "<VEHDIR>"
	Public Const NoFile As String = "<NOFILE>"
	' ReSharper disable once ClassNeverInstantiated.Global
End Module