如何在 Cobol 中打印变量名
How to print variable name in Cobol
我正在尝试将来自 Cobol 程序的变量名传递给 C 函数。
01 Message.
03 varA PIC X(32).
03 varB PIC X(32).
考虑到这个函数将在许多程序中使用,并且变量 Message
的结构每次都会不同,我如何将变量的名称传递给 C 函数?
我已经考虑制作另一个组数据项来包含变量名,但这对我来说不是一个好的解决方案。
我在 AIX 上使用 Microfocus Server Express v5.1。
how can i pass to the C function the names of the variables?
无法直接使用 CALL USING
传递变量名;至少,没有比组数据项更好的了。
我在这里所做的是创建一个类似 JSON 的对象,使用 STRING
语句和 REPLACE
语句来传递 name/value 对。我选择这种方法是因为有很多开源库可以从C
调用来解码JSON
对象。它在文本长度和要传递的变量数量方面也非常灵活。
[编辑。更改代码以将所有 STRING
语句数据项放在 REPLACE
语句中。更改了一些名称以反映 "name/value" 配对。删除了嵌套程序的字符串长度代码。]
program-id. call-c.
data division.
working-storage section.
1 msg.
2 varA pic x(32) value "Message 1".
2 varB pic x(32) value "Message 2".
1 lengths binary.
2 len-1 pic 9(4).
2 len-2 pic 9(4).
replace
==name-1== by =="varA"==
==value-1== by ==varA(1:len-1)==
==name-2== by =="varB"==
==value-2== by ==varB(1:len-2)==
==newline== by ==x"0a"==
.
1 json-string pic x(256).
procedure division.
begin.
compute len-1 = function length (varA)
call "rtrim-len" using varA len-1
compute len-2 = function length (varB)
call "rtrim-len" using varB len-2
string
"{" newline
quote name-1 quote ": "
quote value-1 quote "," newline
quote name-2 quote ": "
quote value-2 quote newline
"}" x"00"
delimited size into json-string
call "c_prog" using
by reference json-string
stop run
.
program-id. rtrim-len.
data division.
linkage section.
1 str pic x(256).
1 str-len binary pic 9(4).
procedure division using str str-len.
begin.
perform varying str-len from str-len by -1
until str-len < 1 or str (str-len:1) not = space
continue
end-perform
exit program
.
end program rtrim-len.
end program call-c.
一个 COBOL 程序来替代被调用的 C
程序。
program-id. "c_prog".
data division.
working-storage section.
1 json-string-count binary pic 9(4).
1 json-string-pos binary pic 9(4).
1 json-text-count binary pic 9(4).
1 json-text pic x(64).
linkage section.
1 json-string pic x(256).
procedure division using json-string.
begin.
move 0 to json-string-count
inspect json-string tallying
json-string-count for characters before x"00"
move 1 to json-string-pos
perform until json-string-pos > json-string-count
unstring json-string delimited x"0a" or x"00"
into json-text count json-text-count
pointer json-string-pos
display json-text (1:json-text-count)
end-perform
exit program
.
end program "c_prog".
输出:
{
"varA": "Message 1",
"varB": "Message 2"
}
如果TRIM
函数可用,则不需要长度计算、数据项和嵌套程序,REPLACE
语句变为,
replace
==name-1== by =="varA"==
==value-1== by ==trim(varA)==
==name-2== by =="varB"==
==value-2== by ==trim(varB)==
==newline== by ==x"0a"==
.
我正在尝试将来自 Cobol 程序的变量名传递给 C 函数。
01 Message.
03 varA PIC X(32).
03 varB PIC X(32).
考虑到这个函数将在许多程序中使用,并且变量 Message
的结构每次都会不同,我如何将变量的名称传递给 C 函数?
我已经考虑制作另一个组数据项来包含变量名,但这对我来说不是一个好的解决方案。
我在 AIX 上使用 Microfocus Server Express v5.1。
how can i pass to the C function the names of the variables?
无法直接使用 CALL USING
传递变量名;至少,没有比组数据项更好的了。
我在这里所做的是创建一个类似 JSON 的对象,使用 STRING
语句和 REPLACE
语句来传递 name/value 对。我选择这种方法是因为有很多开源库可以从C
调用来解码JSON
对象。它在文本长度和要传递的变量数量方面也非常灵活。
[编辑。更改代码以将所有 STRING
语句数据项放在 REPLACE
语句中。更改了一些名称以反映 "name/value" 配对。删除了嵌套程序的字符串长度代码。]
program-id. call-c.
data division.
working-storage section.
1 msg.
2 varA pic x(32) value "Message 1".
2 varB pic x(32) value "Message 2".
1 lengths binary.
2 len-1 pic 9(4).
2 len-2 pic 9(4).
replace
==name-1== by =="varA"==
==value-1== by ==varA(1:len-1)==
==name-2== by =="varB"==
==value-2== by ==varB(1:len-2)==
==newline== by ==x"0a"==
.
1 json-string pic x(256).
procedure division.
begin.
compute len-1 = function length (varA)
call "rtrim-len" using varA len-1
compute len-2 = function length (varB)
call "rtrim-len" using varB len-2
string
"{" newline
quote name-1 quote ": "
quote value-1 quote "," newline
quote name-2 quote ": "
quote value-2 quote newline
"}" x"00"
delimited size into json-string
call "c_prog" using
by reference json-string
stop run
.
program-id. rtrim-len.
data division.
linkage section.
1 str pic x(256).
1 str-len binary pic 9(4).
procedure division using str str-len.
begin.
perform varying str-len from str-len by -1
until str-len < 1 or str (str-len:1) not = space
continue
end-perform
exit program
.
end program rtrim-len.
end program call-c.
一个 COBOL 程序来替代被调用的 C
程序。
program-id. "c_prog".
data division.
working-storage section.
1 json-string-count binary pic 9(4).
1 json-string-pos binary pic 9(4).
1 json-text-count binary pic 9(4).
1 json-text pic x(64).
linkage section.
1 json-string pic x(256).
procedure division using json-string.
begin.
move 0 to json-string-count
inspect json-string tallying
json-string-count for characters before x"00"
move 1 to json-string-pos
perform until json-string-pos > json-string-count
unstring json-string delimited x"0a" or x"00"
into json-text count json-text-count
pointer json-string-pos
display json-text (1:json-text-count)
end-perform
exit program
.
end program "c_prog".
输出:
{
"varA": "Message 1",
"varB": "Message 2"
}
如果TRIM
函数可用,则不需要长度计算、数据项和嵌套程序,REPLACE
语句变为,
replace
==name-1== by =="varA"==
==value-1== by ==trim(varA)==
==name-2== by =="varB"==
==value-2== by ==trim(varB)==
==newline== by ==x"0a"==
.