Skip to content

Commit 3052a3b

Browse files
Implement an encoding switcher of DISPLAY/ACCEPT statements (#590)
1 parent b7cef72 commit 3052a3b

File tree

9 files changed

+119
-11
lines changed

9 files changed

+119
-11
lines changed

.github/workflows/test-other.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,17 @@ jobs:
5050
run: |
5151
dnf -y update
5252
dnf install -y gcc make diffutils glibc-gconv-extra unzip wget
53-
wget "http://sourceforge.jp/frs/redir.php?m=jaist&f=%2Fnkf%2F59912%2Fnkf-2.1.3.tar.gz" -O nkf-2.1.3.tar.gz --no-check-certificate
53+
wget "https://github.com/nurse/nkf/archive/refs/tags/v2_1_3.tar.gz" -O nkf-2.1.3.tar.gz
5454
tar zxf nkf-2.1.3.tar.gz
55-
cd nkf-2.1.3
55+
cd nkf-2_1_3
5656
make
5757
make install
5858
5959
- name: Install dependencies on Amazon Linux 2023
6060
if: inputs.os == 'amazonlinux:2023'
6161
run: |
6262
dnf -y update
63-
dnf install -y gcc make diffutils tar gzip unzip wget
64-
wget "http://sourceforge.jp/frs/redir.php?m=jaist&f=%2Fnkf%2F59912%2Fnkf-2.1.3.tar.gz" -O nkf-2.1.3.tar.gz --no-check-certificate
65-
tar zxf nkf-2.1.3.tar.gz
66-
cd nkf-2.1.3
67-
make
68-
make install
63+
dnf install -y gcc make diffutils tar gzip unzip wget nkf
6964
7065
- name: Install Java
7166
if: inputs.os == 'amazonlinux:2023'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package jp.osscons.opensourcecobol.libcobj.common;
2+
3+
/**
4+
* 文字コードを表す列挙型
5+
*/
6+
public enum CobolEncoding {
7+
/**
8+
* UTF-8を表す
9+
*/
10+
UTF8,
11+
/**
12+
* Shift_JISを表す
13+
*/
14+
SHIFT_JIS,
15+
};

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/common/CobolUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public class CobolUtil {
7272
/** TDOD: 準備中 */
7373
public static int fileSeqWriteBufferSize = 10;
7474

75+
/** DISPLAY/ACCEPT文によるデータ出力時のエンコーディング */
76+
public static CobolEncoding terminalEncoding = CobolEncoding.SHIFT_JIS;
77+
7578
private static boolean lineTrace = false;
7679

7780
/** TDOD: 準備中 */
@@ -349,6 +352,16 @@ public static void cob_init(String[] argv, boolean cobInitialized) {
349352
CobolUtil.fileSeqWriteBufferSize = size;
350353
}
351354
}
355+
356+
s = System.getenv("COB_TERMINAL_ENCODING");
357+
CobolUtil.terminalEncoding = CobolEncoding.SHIFT_JIS;
358+
if (s != null) {
359+
Pattern p = Pattern.compile("[uU][tT][fF][_-]?8");
360+
Matcher m = p.matcher(s);
361+
if (m.matches()) {
362+
CobolUtil.terminalEncoding = CobolEncoding.UTF8;
363+
}
364+
}
352365
}
353366

354367
// libcob/common.cとcob_localtime

libcobj/app/src/main/java/jp/osscons/opensourcecobol/libcobj/termio/CobolTerminal.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@
1818
*/
1919
package jp.osscons.opensourcecobol.libcobj.termio;
2020

21+
import java.io.InputStreamReader;
2122
import java.io.PrintStream;
2223
import java.nio.ByteBuffer;
24+
import java.nio.charset.Charset;
25+
import java.nio.charset.StandardCharsets;
26+
import java.nio.charset.UnsupportedCharsetException;
2327
import java.time.LocalDateTime;
2428
import java.time.format.DateTimeFormatter;
2529
import java.util.Scanner;
30+
import jp.osscons.opensourcecobol.libcobj.common.CobolEncoding;
2631
import jp.osscons.opensourcecobol.libcobj.common.CobolModule;
2732
import jp.osscons.opensourcecobol.libcobj.common.CobolUtil;
2833
import jp.osscons.opensourcecobol.libcobj.data.AbstractCobolField;
@@ -68,7 +73,16 @@ public static void display(boolean dispStdout, boolean newline, AbstractCobolFie
6873

6974
private static void displayAlnum(AbstractCobolField f, PrintStream stream) {
7075
CobolDataStorage storage = f.getDataStorage();
71-
stream.write(storage.getRefOfData(), storage.getIndex(), f.getSize());
76+
if (CobolUtil.terminalEncoding == CobolEncoding.UTF8) {
77+
byte[] utf8Bytes =
78+
new String(
79+
storage.getByteArrayRef(0, f.getSize()),
80+
AbstractCobolField.charSetSJIS)
81+
.getBytes(StandardCharsets.UTF_8);
82+
stream.write(utf8Bytes, 0, utf8Bytes.length);
83+
} else {
84+
stream.write(storage.getRefOfData(), storage.getIndex(), f.getSize());
85+
}
7286
}
7387

7488
/**
@@ -108,7 +122,18 @@ public static void display(
108122
public static void accept(AbstractCobolField f) {
109123
try {
110124
if (scan == null) {
111-
scan = new Scanner(System.in);
125+
if (CobolUtil.terminalEncoding == CobolEncoding.UTF8) {
126+
scan = new Scanner(new InputStreamReader(System.in, StandardCharsets.UTF_8));
127+
} else {
128+
try {
129+
scan =
130+
new Scanner(
131+
new InputStreamReader(
132+
System.in, Charset.forName("Shift_JIS")));
133+
} catch (UnsupportedCharsetException e) {
134+
scan = new Scanner(System.in);
135+
}
136+
}
112137
}
113138

114139
String input = scan.nextLine();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
�d�H��
2+
������
3+
��������H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
重工業
2+
給水所
3+
洗濯板滑走路
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
���{��
2+
�R����
3+
�㒆��
4+
���w�Z
5+
�d�H��
6+
������
7+
�����
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
日本語
2+
山梨県
3+
上中下
4+
小学校
5+
重工業
6+
給水所
7+
洗濯板

tests/i18n_sjis.src/pic-x.at

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,44 @@ AT_CHECK([grep '
541541
AT_CHECK([grep '�2' < prog3.java > /dev/null])
542542
AT_CHECK([grep '�3' < prog3.java > /dev/null])
543543

544-
AT_CLEANUP
544+
AT_CLEANUP
545+
546+
AT_SETUP([DISPLAY/ACCEPT encodings])
547+
548+
AT_DATA([prog.cbl], [
549+
IDENTIFICATION DIVISION.
550+
PROGRAM-ID. prog.
551+
ENVIRONMENT DIVISION.
552+
DATA DIVISION.
553+
WORKING-STORAGE SECTION.
554+
01 X1 PIC X(6) VALUE "�R����".
555+
01 N1 PIC N(3) VALUE "�㒆��".
556+
01 G1.
557+
03 X2 PIC X(6) VALUE "���w�Z".
558+
01 N2 PIC N(3) VALUE "���ؗ�".
559+
PROCEDURE DIVISION.
560+
DISPLAY "���{��".
561+
DISPLAY X1.
562+
DISPLAY N1.
563+
DISPLAY G1.
564+
565+
ACCEPT X1.
566+
DISPLAY X1.
567+
ACCEPT N1.
568+
DISPLAY N1.
569+
ACCEPT G1.
570+
DISPLAY G1.
571+
STOP RUN.
572+
])
573+
574+
AT_CHECK([cobj prog.cbl])
575+
576+
AT_CHECK([java prog < ../../i18n_sjis.src/data/in-sjis.txt > out-none.txt])
577+
AT_CHECK([COB_TERMINAL_ENCODING=SHIFT_JIS java prog < ../../i18n_sjis.src/data/in-sjis.txt > out-sjis.txt])
578+
AT_CHECK([COB_TERMINAL_ENCODING=UTF-8 java prog < ../../i18n_sjis.src/data/in-utf8.txt > out-utf8.txt])
579+
580+
AT_CHECK([diff out-none.txt ../../i18n_sjis.src/data/out-sjis.txt])
581+
AT_CHECK([diff out-sjis.txt ../../i18n_sjis.src/data/out-sjis.txt])
582+
AT_CHECK([diff out-utf8.txt ../../i18n_sjis.src/data/out-utf8.txt])
583+
584+
AT_CLEANUP

0 commit comments

Comments
 (0)