通过流输入输出-Longchar输入

时间:2019-01-21 13:59:04

标签: openedge progress-4gl

在以下方面提供帮助。

我正在尝试使用输入输出通过来运行正在进行的OS命令。

案例:

Run command: "echo 'this is a long string'"
Capture output: "'this is a long string'"

问题是:

  • 我的输入字符串可能大于字符数限制
  • 直接使用through "program"through value("expression")运行时,输入被截断为约2500-3000个字符。
  • 使用文件,整个操作运行良好,但要避免IO和管理。
  • 将数据附加到操作系统命令时遇到麻烦(使用putexport

有人可以建议一种方法来执行命令行,附件数据超过35,000个字符吗?

这是我尝试过的:(伴随许多排列)

define variable cLongMsg as character  no-undo.
define variable iLoop as integer no-undo.
define variable lcResp as longchar no-undo.
define variable cFileLine as character no-undo.    
define stream logStream.

input-output stream logStream through "curl".
  /* execute curl -- attach data using put */
  put stream logStream "http://stackoverflow.com".
  put stream logStream "-d 'key=long_data"
  put stream logStream "&value=more_long_data'"
  put stream logStream CONTROL NULL(0).
output close.     

read_loop:
repeat:

    import stream logStream unformatted cFileLine.
    assign lcResp = lcResp + cFileLine.
end.

input close.
input-output close.

message "[OUT] " + string(lcResp) view-as alert-box.

当前使用的解决方案:

进度文件

function CURL_long returns longchar private (
         input cInput as longchar).

  def var cUnixCMD                      as longchar                     no-undo.
  def var cDataPart                     as char                         no-undo.
  def var cData                         as longchar                     no-undo.    
  def var cResultChunk                  as char                         no-undo.
  def var iResultChunkMax               as inte      init 30000         no-undo.
  def var iUsedChunkMax                 as inte                         no-undo.
  def var iResultPos                    as inte                         no-undo.

  fix-codepage(cData) = "UTF-8".

  assign cUnixCMD = "curl " + cInput + " | fold -c30000".

  input-output stream CURL_CMD through "input_buffer.sh".

  /* set the result position */
  assign iResultPos = 0.

  ResultLoop:
  repeat:
    if iResultPos + iUsedChunkMax > length(cUnixCMD) then
    do:
      assign cResultChunk = substring(cUnixCMD, iResultPos + 1).
      put stream CURL_CMD unformatted cResultChunk skip.
      leave ResultLoop.
    end.

    /* set the current chunk of the result */
    assign cResultChunk = substring(cUnixCMD, iResultPos + 1, iResultChunkMax)
           iUsedChunkMax = iResultChunkMax.

    put stream CURL_CMD unformatted cResultChunk skip.

    assign iResultPos = iResultPos + iUsedChunkMax.

  end. /* ResultLoop: repeat: */
  output stream CURL_CMD close.

  read_loop:
  repeat:
    import stream CURL_CMD unformatted cDataPart.
    if cDataPart <> "" then assign cData = cData + cDataPart.
    else leave.
  end.

  return cData.

end function.

Bash文件

#!/bin/bash
myCMD=""
while IFS= read line
do
  myCMD+=$line
done    
eval $myCMD
exit

2 个答案:

答案 0 :(得分:2)

您在正确的轨道上遇到了一些小的语法问题。

这对我有用:

define variable lcResp as longchar no-undo.
define variable cFileLine as character no-undo.

define stream logStream.

input stream logStream through "curl https://stackoverflow.com".

read_loop:
repeat:

  import stream logStream unformatted cFileLine.

  assign lcResp = lcResp + cFileLine + "~n".

end.

input stream logStream close.

message "[OUT] " length( lcResp ) view-as alert-box.

COPY-LOB lcResp to file "zzz".

当MESSAGE语句超过大约30,000个字符时,将无法输出lcResp。我使用length(lcResp)来显示数据。

MESSAGE,PUT或EXPORT等输出语句不适用于longchar数据。您需要使用COPY-LOB直接读取或写入longchar。

也-在处理换行符或缺少换行符时要小心。未用换行符终止的输入将在REPEAT循环中被IMPORT丢失。我通常使用类似这样的方法来避免这种情况:

define variable lcResp as longchar no-undo.
define variable cFileLine as character no-undo.

define stream logStream.

input stream logStream through "curl https://stackoverflow.com".

read_loop: do while true:

  cFileLine = ?.

  do on endkey undo, leave
     on error undo, leave:

    import stream logStream unformatted cFileLine.

  end.

  if cFileLine = ? then
    leave read_loop.
   else
    lcResp = lcResp + cFileLine + chr(13) + chr(10).

end.

input stream logStream close.

message "[OUT] " length( lcResp ) view-as alert-box.

COPY-LOB lcResp to file "zzz".

如果您的换行符需要为特定格式,那么“ chr(13)+ chr(10)”业务可能很重要。

答案 1 :(得分:0)

您可以使用-inp启动参数来解决此问题。更多信息在这里: https://documentation.progress.com/output/ua/OpenEdge_latest/index.html#page/dpspr/input-characters-(-inp).html