Skip to content

Commit

Permalink
Alternate method of reading text files, to better handle locked files
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaniserse committed Sep 5, 2016
1 parent 8bc1997 commit adf3354
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 228 deletions.
2 changes: 1 addition & 1 deletion CopyToVA.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ echo Copying plugin...
xcopy "VAExtensions\bin\Release" "%PROGRAMFILES(x86)%\VoiceAttack\Apps\VAExtensions" /C /F /Y /S /I
echo -=-=-=-=-=-=-=-=-=-=-=-
echo Copying sample profiles...
xcopy "VAExtensions\Profiles\*.*" "%PROGRAMFILES(x86)%\VoiceAttack\Apps\VAExtensions\Profiles" /C /F /Y
REM xcopy "VAExtensions\Profiles\*.*" "%PROGRAMFILES(x86)%\VoiceAttack\Apps\VAExtensions\Profiles" /C /F /Y
echo -=-=-=-=-=-=-=-=-=-=-=-
echo Launching VoiceAttack...
pause
Expand Down
14 changes: 13 additions & 1 deletion VAExtensions/App.vb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
End If
End Function

Public Shared Function DownloadTextFile(ByVal url As String) As DownloadedFile
Public Shared Function DownloadFile(ByVal url As String) As DownloadedFile
Dim result As New DownloadedFile

If Not url.ToLower.StartsWith("http") Then
Expand Down Expand Up @@ -133,6 +133,18 @@
Return result
End Function

Public Shared Function ReadTextFile(ByVal filename As String) As String
Dim content As String = Nothing

Using fileStream = New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
Using textReader = New IO.StreamReader(fileStream)
content = textReader.ReadToEnd()
End Using
End Using

Return content
End Function

Public Shared Sub ClearCachedFiles()
Dim f As DownloadedFile

Expand Down
2 changes: 1 addition & 1 deletion VAExtensions/ContextHandlers/ContextHandlerReadCSV.vb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Public Class ContextHandlerReadCSV
Select Case m_Context
Case ContextFactory.Contexts.LoadCSV
Try
newFile = App.DownloadTextFile(m_TextValues(App.KEY_FILE))
newFile = App.DownloadFile(m_TextValues(App.KEY_FILE))
If newFile.LocalPath.Length = 0 Then
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error retrieving File '{0}'.", m_TextValues(App.KEY_FILE))
Expand Down
93 changes: 47 additions & 46 deletions VAExtensions/ContextHandlers/ContextHandlerReadFile.vb
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
Public Class ContextHandlerReadFile
Inherits ContextHandlerBase

Public Sub New(ByVal context As ContextFactory.Contexts _
, ByRef state As Dictionary(Of String, Object) _
, ByRef smallIntValues As Dictionary(Of String, Nullable(Of Short)) _
, ByRef textValues As Dictionary(Of String, String) _
, ByRef intValues As Dictionary(Of String, Nullable(Of Integer)) _
, ByRef decimalValues As Dictionary(Of String, Nullable(Of Decimal)) _
, ByRef booleanValues As Dictionary(Of String, Nullable(Of Boolean)) _
, ByRef extendedValues As Dictionary(Of String, Object))

MyBase.New(context, state, smallIntValues, textValues, intValues, decimalValues, booleanValues, extendedValues)
End Sub


Public Overrides Function Execute() As Boolean
Dim newFile As DownloadedFile = Nothing
Dim regexPattern As String

If Not m_TextValues.ContainsKey(App.KEY_FILE) Then
m_smallIntValues(App.KEY_ERROR) = ERR_ARGUMENTS
m_TextValues(App.KEY_RESULT) = String.Format("Unknown file name. Text variable '{0}' not set.", App.KEY_FILE)
Return False
End If

If m_TextValues.ContainsKey(App.KEY_REGEX) Then
regexPattern = m_TextValues(App.KEY_REGEX)
Else
regexPattern = String.Empty
End If
Try
newFile = App.DownloadTextFile(m_TextValues(App.KEY_FILE))
If newFile.LocalPath.Length = 0 Then
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error retrieving File '{0}'.", m_TextValues(App.KEY_FILE))
Return False
End If
Inherits ContextHandlerBase

Public Sub New(ByVal context As ContextFactory.Contexts _
, ByRef state As Dictionary(Of String, Object) _
, ByRef smallIntValues As Dictionary(Of String, Nullable(Of Short)) _
, ByRef textValues As Dictionary(Of String, String) _
, ByRef intValues As Dictionary(Of String, Nullable(Of Integer)) _
, ByRef decimalValues As Dictionary(Of String, Nullable(Of Decimal)) _
, ByRef booleanValues As Dictionary(Of String, Nullable(Of Boolean)) _
, ByRef extendedValues As Dictionary(Of String, Object))

MyBase.New(context, state, smallIntValues, textValues, intValues, decimalValues, booleanValues, extendedValues)
End Sub

Dim newFileContent As String = IO.File.ReadAllText(newFile.LocalPath)
m_TextValues(App.KEY_RESULT) = App.LimitResponse(newFileContent, regexPattern)

Catch ex As Exception
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error loading file content '{0}' ({1}).", m_TextValues(App.KEY_FILE), ex.Message)
End Try
If newFile.IsTemporary Then App.Settings.AddDownloadedFile(newFile)
Public Overrides Function Execute() As Boolean
Dim newFile As DownloadedFile = Nothing
Dim regexPattern As String

If Not m_TextValues.ContainsKey(App.KEY_FILE) Then
m_smallIntValues(App.KEY_ERROR) = ERR_ARGUMENTS
m_TextValues(App.KEY_RESULT) = String.Format("Unknown file name. Text variable '{0}' not set.", App.KEY_FILE)
Return False
End If

If m_TextValues.ContainsKey(App.KEY_REGEX) Then
regexPattern = m_TextValues(App.KEY_REGEX)
Else
regexPattern = String.Empty
End If
Try
newFile = App.DownloadFile(m_TextValues(App.KEY_FILE))
If newFile.LocalPath.Length = 0 Then
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error retrieving File '{0}'.", m_TextValues(App.KEY_FILE))
Return False
End If

'Dim newFileContent As String = IO.File.ReadAllText(newFile.LocalPath)
Dim newFileContent As String = App.ReadTextFile(newFile.LocalPath)
m_TextValues(App.KEY_RESULT) = App.LimitResponse(newFileContent, regexPattern)

Catch ex As Exception
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error loading file content '{0}' ({1}).", m_TextValues(App.KEY_FILE), ex.Message)
End Try
If newFile.IsTemporary Then App.Settings.AddDownloadedFile(newFile)

Return True
End Function
Return True
End Function

End Class
6 changes: 3 additions & 3 deletions VAExtensions/ContextHandlers/ContextHandlerReadJSON.vb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Public Class ContextHandlerReadJSON
Dim newFile As DownloadedFile = Nothing
Dim regexPattern As String
Dim elementPath As String
Dim i As Integer, elementCount As Short
Dim elementCount As Short

If Not m_TextValues.ContainsKey(App.KEY_FILE) Then
m_smallIntValues(App.KEY_ERROR) = ERR_CONTEXT
Expand All @@ -40,14 +40,14 @@ Public Class ContextHandlerReadJSON
End If

Try
newFile = App.DownloadTextFile(m_TextValues(App.KEY_FILE))
newFile = App.DownloadFile(m_TextValues(App.KEY_FILE))
If newFile.LocalPath.Length = 0 Then
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error retrieving File '{0}'.", m_TextValues(App.KEY_FILE))
Return False
End If

Dim jobj As Json.Linq.JObject = Json.Linq.JObject.Parse(IO.File.ReadAllText(newFile.LocalPath))
Dim jobj As Json.Linq.JObject = Json.Linq.JObject.Parse(App.ReadTextFile(newFile.LocalPath))

Dim token As Json.Linq.JToken = jobj.SelectToken(elementPath)
If token IsNot Nothing Then
Expand Down
2 changes: 1 addition & 1 deletion VAExtensions/ContextHandlers/ContextHandlerReadRSS.vb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
End If

Try
newFile = App.DownloadTextFile(m_TextValues(App.KEY_FILE))
newFile = App.DownloadFile(m_TextValues(App.KEY_FILE))
If newFile.LocalPath.Length = 0 Then
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error retrieving File '{0}'.", m_TextValues(App.KEY_FILE))
Expand Down
103 changes: 51 additions & 52 deletions VAExtensions/ContextHandlers/ContextHandlerReadStdOut.vb
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
Public Class ContextHandlerReadStdOut
Inherits ContextHandlerBase

Public Sub New(ByVal context As ContextFactory.Contexts _
, ByRef state As Dictionary(Of String, Object) _
, ByRef smallIntValues As Dictionary(Of String, Nullable(Of Short)) _
, ByRef textValues As Dictionary(Of String, String) _
, ByRef intValues As Dictionary(Of String, Nullable(Of Integer)) _
, ByRef decimalValues As Dictionary(Of String, Nullable(Of Decimal)) _
, ByRef booleanValues As Dictionary(Of String, Nullable(Of Boolean)) _
, ByRef extendedValues As Dictionary(Of String, Object))

MyBase.New(context, state, smallIntValues, textValues, intValues, decimalValues, booleanValues, extendedValues)
End Sub


Public Overrides Function Execute() As Boolean
Dim newFile As DownloadedFile = Nothing
Dim regexPattern As String

If Not m_TextValues.ContainsKey(App.KEY_FILE) Then
m_smallIntValues(App.KEY_ERROR) = ERR_CONTEXT
m_TextValues(App.KEY_RESULT) = String.Format("Unknown file name. Text variable '{0}' not set.", App.KEY_FILE)
Return False
End If

Try
Dim pName As String = m_TextValues(App.KEY_FILE)
Dim pInfo As ProcessStartInfo
Dim pOutput As String
Dim p As Process

If Not IO.Path.IsPathRooted(pName) Then
pName = IO.Path.Combine(IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location), pName)
End If
pInfo = New ProcessStartInfo(pName) With {.UseShellExecute = False, .CreateNoWindow = True, .RedirectStandardOutput = True}
If Not String.IsNullOrEmpty(m_TextValues(App.KEY_ARGUMENTS)) Then
pInfo.Arguments = m_TextValues(App.KEY_ARGUMENTS)
End If

p = Process.Start(pInfo)
pOutput = p.StandardOutput.ReadToEnd
p.WaitForExit()

m_TextValues(App.KEY_RESULT) = App.LimitResponse(pOutput)
Catch ex As Exception
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error running process '{0}' - {1}.", m_TextValues(App.KEY_FILE), ex.Message)
Return False
End Try

Return True
End Function
Inherits ContextHandlerBase

Public Sub New(ByVal context As ContextFactory.Contexts _
, ByRef state As Dictionary(Of String, Object) _
, ByRef smallIntValues As Dictionary(Of String, Nullable(Of Short)) _
, ByRef textValues As Dictionary(Of String, String) _
, ByRef intValues As Dictionary(Of String, Nullable(Of Integer)) _
, ByRef decimalValues As Dictionary(Of String, Nullable(Of Decimal)) _
, ByRef booleanValues As Dictionary(Of String, Nullable(Of Boolean)) _
, ByRef extendedValues As Dictionary(Of String, Object))

MyBase.New(context, state, smallIntValues, textValues, intValues, decimalValues, booleanValues, extendedValues)
End Sub


Public Overrides Function Execute() As Boolean
Dim newFile As DownloadedFile = Nothing

If Not m_TextValues.ContainsKey(App.KEY_FILE) Then
m_smallIntValues(App.KEY_ERROR) = ERR_CONTEXT
m_TextValues(App.KEY_RESULT) = String.Format("Unknown file name. Text variable '{0}' not set.", App.KEY_FILE)
Return False
End If

Try
Dim pName As String = m_TextValues(App.KEY_FILE)
Dim pInfo As ProcessStartInfo
Dim pOutput As String
Dim p As Process

If Not IO.Path.IsPathRooted(pName) Then
pName = IO.Path.Combine(IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location), pName)
End If
pInfo = New ProcessStartInfo(pName) With {.UseShellExecute = False, .CreateNoWindow = True, .RedirectStandardOutput = True}
If Not String.IsNullOrEmpty(m_TextValues(App.KEY_ARGUMENTS)) Then
pInfo.Arguments = m_TextValues(App.KEY_ARGUMENTS)
End If

p = Process.Start(pInfo)
pOutput = p.StandardOutput.ReadToEnd
p.WaitForExit()

m_TextValues(App.KEY_RESULT) = App.LimitResponse(pOutput)
Catch ex As Exception
m_smallIntValues(App.KEY_ERROR) = ERR_IO
m_TextValues(App.KEY_RESULT) = String.Format("Error running process '{0}' - {1}.", m_TextValues(App.KEY_FILE), ex.Message)
Return False
End Try

Return True
End Function
End Class
Loading

0 comments on commit adf3354

Please sign in to comment.