1 /*===- AutoConvert.h - Auto conversion between ASCII/EBCDIC -----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains functions used for auto conversion between
10 // ASCII/EBCDIC codepages specific to z/OS.
11 //
12 //===----------------------------------------------------------------------===*/
13
14 #ifndef LLVM_SUPPORT_AUTOCONVERT_H
15 #define LLVM_SUPPORT_AUTOCONVERT_H
16
17 #ifdef __MVS__
18 #include <_Ccsid.h>
19 #endif
20 #ifdef __cplusplus
21 #include "llvm/Support/Error.h"
22 #include <system_error>
23 #endif /* __cplusplus */
24
25 #define CCSID_IBM_1047 1047
26 #define CCSID_UTF_8 1208
27 #define CCSID_ISO8859_1 819
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif /* __cplusplus */
32
33 int enablezOSAutoConversion(int FD);
34 int disablezOSAutoConversion(int FD);
35 int restorezOSStdHandleAutoConversion(int FD);
36
37 #ifdef __cplusplus
38 }
39 #endif /* __cplusplus */
40
41 #ifdef __cplusplus
42 namespace llvm {
43
44 #ifdef __MVS__
45
46 /** \brief Set the tag information for a file descriptor. */
47 std::error_code setzOSFileTag(int FD, int CCSID, bool Text);
48
49 /** \brief Get the the tag ccsid for a file name or a file descriptor. */
50 ErrorOr<__ccsid_t> getzOSFileTag(const char *FileName, const int FD = -1);
51
52 /** \brief Query the file tag to determine if it needs conversion to UTF-8
53 * codepage.
54 */
55 ErrorOr<bool> needzOSConversion(const char *FileName, const int FD = -1);
56
57 #endif /* __MVS__*/
58
disableAutoConversion(int FD)59 inline std::error_code disableAutoConversion(int FD) {
60 #ifdef __MVS__
61 if (::disablezOSAutoConversion(FD) == -1)
62 return errnoAsErrorCode();
63 #endif
64 return std::error_code();
65 }
66
enableAutoConversion(int FD)67 inline std::error_code enableAutoConversion(int FD) {
68 #ifdef __MVS__
69 if (::enablezOSAutoConversion(FD) == -1)
70 return errnoAsErrorCode();
71 #endif
72 return std::error_code();
73 }
74
restoreStdHandleAutoConversion(int FD)75 inline std::error_code restoreStdHandleAutoConversion(int FD) {
76 #ifdef __MVS__
77 if (::restorezOSStdHandleAutoConversion(FD) == -1)
78 return errnoAsErrorCode();
79 #endif
80 return std::error_code();
81 }
82
setFileTag(int FD,int CCSID,bool Text)83 inline std::error_code setFileTag(int FD, int CCSID, bool Text) {
84 #ifdef __MVS__
85 return setzOSFileTag(FD, CCSID, Text);
86 #endif
87 return std::error_code();
88 }
89
90 inline ErrorOr<bool> needConversion(const char *FileName, const int FD = -1) {
91 #ifdef __MVS__
92 return needzOSConversion(FileName, FD);
93 #endif
94 return false;
95 }
96
97 } /* namespace llvm */
98 #endif /* __cplusplus */
99
100 #endif /* LLVM_SUPPORT_AUTOCONVERT_H */
101