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.

Michael KRISPER
committed
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Public Module VECTO_Global

Michael KRISPER
committed
Public COREvers As String = "NOT FOUND"
Public Const LicSigAppCode As String = "VECTO-Release-0093C61E0A2E4BFA9A7ED7E729C56AE4"
Public MyAppPath As String
Public MyConfPath As String

Markus Quaritsch
committed

Michael KRISPER
committed
'to ensure correct format for backgroundworker thread

Michael KRISPER
committed
Public Cfg As Configuration
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

Michael KRISPER
committed
Dim logfDetail As FileInfo
'Start new log if file size limit reached

Michael KRISPER
committed
If File.Exists(MyAppPath & "LOG.txt") Then
'File size check
logfDetail = My.Computer.FileSystem.GetFileInfo(MyAppPath & "LOG.txt")

Michael KRISPER
committed
If logfDetail.Length / (2 ^ 20) > Cfg.LogSize Then
WriteToLog(MessageType.Normal, "Starting new logfile")

Michael KRISPER
committed
If File.Exists(MyAppPath & "LOG_backup.txt") Then File.Delete(MyAppPath & "LOG_backup.txt")
File.Move(MyAppPath & "LOG.txt", MyAppPath & "LOG_backup.txt")
WriteToLog(MessageType.Err, "Failed to backup logfile! (" & MyAppPath & "LOG_backup.txt)")
WriteToLog(MessageType.Normal, "Logfile restarted. Old log saved to LOG_backup.txt")
Public Function CloseLog() As Boolean
Try
WriteToLog(MessageType.Normal, "Closing Session " & Now)
Catch ex As Exception
Return False
End Try
Public Function WriteToLog(msgType As MessageType, msg As String) As Boolean
Dim msgTypeStr As String
Case MessageType.Err
Case MessageType.Warn
_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
'If empty file => Abort
If file = "" Then Return ""
file = Replace(file, DefVehPath & "\", MyAppPath & "Default Vehicles\", 1, -1,

Michael KRISPER
committed
CompareMethod.Text)
file = Replace(file, HomePath & "\", MyAppPath, 1, -1, CompareMethod.Text)
If mainDir = "" Then
replPath = MyAppPath
Do While replPath.Length > 0 AndAlso Left(file, 3) = "..\"
replPath = PathUp(replPath)
Else
Return file
End If
End Function
'Path one-level-up "C:\temp\ordner1\" >> "C:\temp\"
Private Function PathUp(pfad As String) As String
Dim x As Integer
pfad = pfad.Substring(0, pfad.Length - 1)
x = pfad.LastIndexOf("\", StringComparison.Ordinal)

Michael KRISPER
committed
If x = -1 Then Return ""
'File name without the path "C:\temp\TEST.txt" >> "TEST.txt" oder "TEST"

Markus Quaritsch
committed
Public Function GetFilenameWithoutPath(file As String, includeFileExtension As Boolean) As String _
'GetFilenameWithoutPath
Dim x As Integer
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)
'Filename without extension "C:\temp\TEST.txt" >> "C:\temp\TEST"
'Filename without path if Path = WorkDir or MainDir

Markus Quaritsch
committed
Public Function GetFilenameWithoutDirectory(file As String, Optional ByVal mainDir As String = "") As String _
'GetFilenameWithoutDirectory
If UCase(GetPath(file)) = UCase(path) Then file = GetFilenameWithoutPath(file, True)
'Path alone "C:\temp\TEST.txt" >> "C:\temp\"
' "TEST.txt" >> ""

Markus Quaritsch
committed
Public Function GetPath(file As String) As String 'GetPath
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)
'Extension alone "C:\temp\TEST.txt" >> ".txt"

Markus Quaritsch
committed
Public Function GetExtension(file As String) As String 'GetExtension
Dim x As Integer

Michael KRISPER
committed
If x = -1 Then
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>"
'Public Sub New()

Markus Quaritsch
committed
' AUX = New AuxiliaryKey
'End Sub
' ReSharper disable once ClassNeverInstantiated.Global
End Module