LLVM 24.0.0git
AutoConvert.h
Go to the documentation of this file.
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/ADT/Twine.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/File.h"
24#include <system_error>
25#endif /* __cplusplus */
26
27#define CCSID_IBM_1047 1047
28#define CCSID_UTF_8 1208
29#define CCSID_ISO8859_1 819
30
31#ifdef __cplusplus
32extern "C" {
33#endif /* __cplusplus */
34
36int enablezOSAutoConversionCcsid(int FD, int ccsid);
39
40#ifdef __cplusplus
41}
42#endif /* __cplusplus */
43
44#ifdef __cplusplus
45namespace llvm {
46
47#ifdef __MVS__
48
49/** \brief Set the tag information for a file descriptor. */
50std::error_code setzOSFileTag(sys::fs::file_t FD, int CCSID, bool IsText);
51
52/** \brief Get the the tag ccsid for a file name or a file descriptor. */
53ErrorOr<__ccsid_t> getzOSFileTag(const Twine &FileName,
54 const sys::fs::file_t FD = {});
55
56/** \brief Query the file tag to determine if it needs conversion to UTF-8
57 * codepage.
58 */
59ErrorOr<bool> needzOSConversion(const Twine &FileName,
60 const sys::fs::file_t FD = {});
61
62/** Copy the tag attributes from \a source to \a destination.
63 *
64 * @param Source The name of the source file.
65 * @param Destination The file descriptor of the destination file.
66 * @returns errc::success if the tag attributes were copied successfully,
67 * otherwise returns a specific error_code.
68 */
69std::error_code copyFileTagAttributes(const std::string &Source,
70 const sys::fs::file_t DestinationFD);
71
72#endif /* __MVS__*/
73
74inline std::error_code disableAutoConversion(sys::fs::file_t FD) {
75#ifdef __MVS__
76 if (::disablezOSAutoConversion(FD.get()) == -1)
77 return errnoAsErrorCode();
78#endif
79 return std::error_code();
80}
81
82inline std::error_code enableAutoConversion(int FD) {
83#ifdef __MVS__
84 if (::enablezOSAutoConversion(FD) == -1)
85 return errnoAsErrorCode();
86#endif
87 return std::error_code();
88}
89
90inline std::error_code enableAutoConversion(sys::fs::file_t FD) {
91#ifdef __MVS__
92 return enableAutoConversion(FD.get());
93#endif
94 return std::error_code();
95}
96
97inline std::error_code enableAutoConversion(sys::fs::file_t FD, int ccsid) {
98#ifdef __MVS__
99 if (::enablezOSAutoConversionCcsid(FD.get(), ccsid) == -1)
100 return errnoAsErrorCode();
101#endif
102 return std::error_code();
103}
104
105inline std::error_code restoreStdHandleAutoConversion(int FD) {
106#ifdef __MVS__
108 return errnoAsErrorCode();
109#endif
110 return std::error_code();
111}
112
113inline std::error_code restoreStdHandleAutoConversion(sys::fs::file_t FD) {
114#ifdef __MVS__
115 return restoreStdHandleAutoConversion(FD.get());
116#endif
117 return std::error_code();
118}
119
120inline std::error_code setFileTag(sys::fs::file_t FD, int CCSID, bool IsText) {
121#ifdef __MVS__
122 return setzOSFileTag(FD.get(), CCSID, IsText);
123#endif
124 return std::error_code();
125}
126
127inline ErrorOr<bool> needConversion(const Twine &FileName,
128 const sys::fs::file_t FD = {}) {
129#ifdef __MVS__
130 return needzOSConversion(FileName, FD.get());
131#endif
132 return false;
133}
134
135} /* namespace llvm */
136#endif /* __cplusplus */
137
138#endif /* LLVM_SUPPORT_AUTOCONVERT_H */
int restorezOSStdHandleAutoConversion(int FD)
int enablezOSAutoConversionCcsid(int FD, int ccsid)
int enablezOSAutoConversion(int FD)
int disablezOSAutoConversion(int FD)
This file declares llvm::sys::fs::file_t type.
Represents either an error or a value T.
Definition ErrorOr.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This is an optimization pass for GlobalISel generic memory operations.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition Error.h:1256
This class wraps the platform specific file handle/descriptor type to provide an unified representati...
Definition File.h:21