Advertisement
deseven

PB ReadProgramString() bug

Apr 13th, 2017
3,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Procedure.s runToolReadProgramString(path.s,args.s = "",workdir.s = "")
  2.   Protected tool.i,out.s,error.s
  3.   Protected bytes,oldBytes
  4.   Protected *buf
  5.   tool = RunProgram(path,args,workdir,#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
  6.   If tool
  7.     While ProgramRunning(tool)
  8.       If AvailableProgramOutput(tool)
  9.         out + ReadProgramString(tool) + #LF$
  10.       EndIf
  11.       error + ReadProgramError(tool)
  12.       Delay(10)
  13.     Wend
  14.     error + ReadProgramError(tool)
  15.     CloseProgram(tool)
  16.   Else
  17.     Debug "failed to start"
  18.   EndIf
  19.   If Len(error)
  20.     Debug "error: " + error
  21.   EndIf
  22.   out = RTrim(RTrim(out,#LF$),#CR$)
  23.   ProcedureReturn out
  24. EndProcedure
  25.  
  26. Procedure.s runToolReadProgramData(path.s,args.s = "",workdir.s = "")
  27.   Protected tool.i,out.s,error.s
  28.   Protected bytes,oldBytes
  29.   Protected *buf
  30.   tool = RunProgram(path,args,workdir,#PB_Program_Open|#PB_Program_Read|#PB_Program_Error|#PB_Program_Hide)
  31.   If tool
  32.     While ProgramRunning(tool)
  33.       bytes = AvailableProgramOutput(tool)
  34.       If bytes
  35.         If Not *buf
  36.           *buf = AllocateMemory(bytes)
  37.         Else
  38.           *buf = ReAllocateMemory(*buf,oldBytes+bytes)
  39.         EndIf
  40.         ReadProgramData(tool,*buf+oldBytes,bytes)
  41.         oldBytes = MemorySize(*buf)
  42.       EndIf
  43.       error + ReadProgramError(tool)
  44.       Delay(10)
  45.     Wend
  46.     error + ReadProgramError(tool)
  47.     If *buf
  48.       out = PeekS(*buf,MemorySize(*buf),#PB_UTF8|#PB_ByteLength)
  49.       FreeMemory(*buf)
  50.     EndIf
  51.     CloseProgram(tool)
  52.   Else
  53.     Debug "failed to start"
  54.   EndIf
  55.   If Len(error)
  56.     Debug "error: " + error
  57.   EndIf
  58.   out = RTrim(RTrim(out,#LF$),#CR$)
  59.   ProcedureReturn out
  60. EndProcedure
  61.  
  62. string.s = "[⛄️✈️😀🚶✳️‼️🌐🏂👤🔍]"
  63.  
  64. CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  65.   ReadProgramDataResult.s = runToolReadProgramData("echo",string)
  66.   ReadProgramStringResult.s = runToolReadProgramString("echo",string)
  67. CompilerElse
  68.   ReadProgramDataResult.s = runToolReadProgramData("powershell",~"-Command \"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Write-Host " + string + ~"\"")
  69.   ReadProgramStringResult.s = runToolReadProgramString("powershell",~"-Command \"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Write-Host " + string + ~"\"")
  70.   ; fix for older PoSh versions by kenmo
  71.   If (Left(ReadProgramDataResult, 1) = Chr($FEFF))
  72.     ReadProgramDataResult = Mid(ReadProgramDataResult, 2)
  73.   EndIf
  74.   If (Left(ReadProgramStringResult, 1) = Chr($FEFF))
  75.     ReadProgramStringResult = Mid(ReadProgramStringResult, 2)
  76.   EndIf
  77. CompilerEndIf
  78.  
  79. Debug "Raw string:"
  80. Debug string
  81. Debug "ReadProgramData:"
  82. If ReadProgramDataResult = string
  83.   Debug ReadProgramDataResult + " - ok"
  84. Else
  85.   Debug ReadProgramDataResult + " - failed"
  86. EndIf
  87. Debug "ReadProgramString:"
  88. If ReadProgramStringResult = string
  89.   Debug ReadProgramStringResult + " - ok"
  90. Else
  91.   Debug ReadProgramStringResult + " - failed"
  92. EndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement