Skip to content

Commit 2e6a8f0

Browse files
committed
Use ConvertFlag to open convertion descriptor.
1 parent 68b758d commit 2e6a8f0

File tree

11 files changed

+87
-60
lines changed

11 files changed

+87
-60
lines changed

.markdownlint.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"MD024": {
3+
"siblings_only": true
4+
},
5+
"MD033": {
6+
"allowed_elements": ["img"]
7+
}
8+
}

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/build-aux")
3333
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../build-aux")
3434
set(BUILD_AUX "${CMAKE_CURRENT_SOURCE_DIR}/../build-aux")
3535
else()
36-
message(FATAL_ERROR "CMake build-aux directory is not exists, try to execute 'setup.cmd' or './setup.sh' to get it.")
36+
message(FATAL_ERROR "C+++ build-aux directory is not exists.")
3737
endif()
3838
get_filename_component(BUILD_AUX "${BUILD_AUX}" ABSOLUTE)
3939
include("${BUILD_AUX}/cmake/cppp.cmake")
@@ -71,7 +71,6 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/include/cppp/reiconv.hpp.in" "${outp
7171
set(CPPP_API "${CPPP_IMPORT_API}")
7272
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/include/cppp/reiconv.h.in" "${output_includedir}/cppp/reiconv.h.inst")
7373

74-
7574
# Header for build.
7675
# cppp/reiconv.hpp
7776
set(CPPP_API "${CPPP_EXPORT_API}")

include/cppp/encodings/reiconv.h.in

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
* If not, see <https://www.gnu.org/licenses/>.
2424
*/
2525

26-
/* When installed, this file is called "cppp/reiconv.h". */
27-
26+
#pragma once
2827
#ifndef _CPPP_ENCODINGS_REICONV_H_
2928
#define _CPPP_ENCODINGS_REICONV_H_
30-
#pragma once
3129

3230
#if _MSC_VER >= 1600
3331
#pragma execution_character_set("utf-8")
34-
#endif
32+
#endif /* _MSC_VER >= 1600 */
3533

3634
@INDEXES_CODE@
37-
#endif // _CPPP_ENCODINGS_REICONV_H_
35+
#endif /* _CPPP_ENCODINGS_REICONV_H_ */

include/cppp/encodings/reiconv.hpp.in

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
* If not, see <https://www.gnu.org/licenses/>.
2424
*/
2525

26-
/* When installed, this file is called "cppp/reiconv.hpp". */
27-
26+
#pragma once
2827
#ifndef _CPPP_ENCODINGS_REICONV_HPP_
2928
#define _CPPP_ENCODINGS_REICONV_HPP_
30-
#pragma once
3129

3230
#if _MSC_VER >= 1600
3331
#pragma execution_character_set("utf-8")
34-
#endif
32+
#endif // _MSC_VER >= 1600
3533

3634
@INDEXES_CODE@
3735
#endif // _CPPP_ENCODINGS_REICONV_HPP_

include/cppp/reiconv.h.in

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,26 @@
2323
* If not, see <https://www.gnu.org/licenses/>.
2424
*/
2525

26-
/* When installed, this file is called "cppp/reiconv.h". */
27-
2826
#pragma once
29-
3027
#ifndef _CPPP_REICONV_H_
3128
#define _CPPP_REICONV_H_
3229

33-
#include <cppp/encodings/reiconv.h>
30+
#include "encodings/reiconv.h"
3431

35-
#include <stdbool.h>
3632
#include <stddef.h>
3733

38-
#define _CPPP_API
39-
4034
#ifndef _CPPP_API
4135
#define _CPPP_API @CPPP_API@
42-
#endif
36+
#endif /* _CPPP_API */
4337

4438
#if _MSC_VER >= 1600
4539
#pragma execution_character_set("utf-8")
46-
#endif
40+
#endif /* _MSC_VER >= 1600 */
4741

4842
#ifdef __cplusplus
49-
extern "C" {
50-
#endif
43+
extern "C"
44+
{
45+
#endif /* __cplusplus */
5146

5247
/**
5348
* @brief Version information.
@@ -86,7 +81,7 @@ extern _CPPP_API size_t reiconv_name_canonicalize(const char *name, char *outbuf
8681

8782
/**
8883
* @brief Lookup an encoding by it's name.
89-
* @note The return value is an encoding index that can be used with `reiconv_open_from_codepage`.
84+
* @note The return value is an encoding index that can be used with `reiconv_open_from_index`.
9085
* @note We will ignore '-' and '_', and uppercase all characters.
9186
* @param encoding The encoding name.
9287
* @return The encoding index. If the it is not found, -1 is returned.
@@ -95,7 +90,7 @@ extern _CPPP_API int reiconv_lookup_from_name(const char *encoding);
9590

9691
/**
9792
* @brief Lookup an encoding by it's codepage.
98-
* @note The return value is an encoding index that can be used with `reiconv_open_from_codepage`.
93+
* @note The return value is an encoding index that can be used with `reiconv_open_from_index`.
9994
* @param codepage The codepage.
10095
* @return The encoding index. If the it is not found, -1 is returned.
10196
*/
@@ -106,36 +101,52 @@ extern _CPPP_API int reiconv_lookup_from_codepage(int codepage);
106101
*/
107102
typedef void *reiconv_t;
108103

104+
/**
105+
* @brief Convert flags. Used for open conversion descriptor.
106+
*/
107+
enum ConvertFlag
108+
{
109+
/**
110+
* @brief No flags.
111+
*/
112+
REICONV_NO_FLAGS = 0,
113+
114+
/**
115+
* @brief Discard illegal sequences. Same as iconv(3) '//IGNORE'.
116+
*/
117+
REICONV_DISCARD_ILSEQ = 1,
118+
};
119+
109120
/**
110121
* @brief Open a conversion descriptor from encoding index.
111122
* @note The return value is a conversion descriptor.
112123
* @param fromcode The input buffer encoding's index.
113124
* @param tocode The output buffer encoding's index.
114-
* @param discard_ilseq If true, we will ignore conversion errors. Same as iconv(3) '//IGNORE'.
125+
* @param flags The convert flags.
115126
* @return The conversion descriptor. Indexes MUST BE VALID or the behavior is undefined.
116127
* @note If the conversion descriptor cannot be created, returns (reiconv_t)(-1) and errno is set to ENOMEM.
117128
*/
118-
extern _CPPP_API reiconv_t reiconv_open_from_index(int fromcode, int tocode, bool discard_ilseq);
129+
extern _CPPP_API reiconv_t reiconv_open_from_index(int fromcode, int tocode, enum ConvertFlag flags);
119130

120131
/**
121132
* @brief Open a conversion descriptor from codepage.
122133
* @note The return value is a conversion descriptor.
123134
* @param fromcode The input buffer encoding's codepage.
124135
* @param tocode The output buffer encoding's codepage.
125-
* @param discard_ilseq If true, we will ignore conversion errors. Same as iconv(3) '//IGNORE'.
136+
* @param flags The convert flags.
126137
* @return The conversion descriptor. (reiconv_t)(-1) is returned if error occured with errno set.
127138
*/
128-
extern _CPPP_API reiconv_t reiconv_open_from_codepage(int fromcode, int tocode, bool discard_ilseq);
139+
extern _CPPP_API reiconv_t reiconv_open_from_codepage(int fromcode, int tocode, enum ConvertFlag flags);
129140

130141
/**
131142
* @brief Open a conversion descriptor from encoding name.
132143
* @note The return value is a conversion descriptor.
133144
* @param fromcode The input buffer encoding's name.
134145
* @param tocode The output buffer encoding's name.
135-
* @param discard_ilseq If true, we will ignore conversion errors. Same as iconv(3) '//IGNORE'.
146+
* @param flags The convert flags.
136147
* @return The conversion descriptor. (reiconv_t)(-1) is returned if error occured with errno set.
137148
*/
138-
extern _CPPP_API reiconv_t reiconv_open_from_name(const char* fromcode, const char* tocode, bool discard_ilseq);
149+
extern _CPPP_API reiconv_t reiconv_open_from_name(const char *fromcode, const char *tocode, enum ConvertFlag flags);
139150

140151
/**
141152
* @brief Get the size of the result. -1 on error with errno set.
@@ -159,7 +170,8 @@ extern _CPPP_API size_t reiconv_result_size(reiconv_t cd, const char *start, siz
159170
* @note If the output buffer is too big, the rest of the buffer is filled with zero.
160171
* @return 0 on success, -1 on error with errno set.
161172
*/
162-
extern _CPPP_API int reiconv_convert_static_size(reiconv_t cd, const char* input_data, size_t input_length, char* output_data, size_t output_length);
173+
extern _CPPP_API int reiconv_convert_static_size(reiconv_t cd, const char *input_data, size_t input_length,
174+
char *output_data, size_t output_length);
163175

164176
/**
165177
* @brief Converts an string from one encoding to another.
@@ -175,7 +187,8 @@ extern _CPPP_API int reiconv_convert_static_size(reiconv_t cd, const char* input
175187
* @note The length of the result is stored in `*output_length_ptr`.
176188
* @return 0 on success, -1 on error with errno set.
177189
*/
178-
extern _CPPP_API int reiconv_convert(reiconv_t cd, const char *input_data, size_t input_length, char **output_data_ptr, size_t *output_length_ptr);
190+
extern _CPPP_API int reiconv_convert(reiconv_t cd, const char *input_data, size_t input_length, char **output_data_ptr,
191+
size_t *output_length_ptr);
179192

180193
/**
181194
* @brief Open a conversion descriptor. For iconv compatibility.
@@ -192,7 +205,8 @@ extern _CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode
192205
* @note Decrements `*inbytesleft` and increments `*inbuf` by the same amount.
193206
* @note Decrements `*outbytesleft` and increments `*outbuf` by the same amount.
194207
*/
195-
extern _CPPP_API size_t reiconv_iconv(reiconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
208+
extern _CPPP_API size_t reiconv_iconv(reiconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
209+
size_t *outbytesleft);
196210

197211
/**
198212
* @brief Close a conversion descriptor. For iconv compatibility.
@@ -210,6 +224,6 @@ extern _CPPP_API const char *locale_charset();
210224

211225
#ifdef __cplusplus
212226
}
213-
#endif
227+
#endif /* __cplusplus */
214228

215229
#endif /* _CPPP_REICONV_H_ */

include/cppp/reiconv.hpp.in

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,16 @@
2323
* If not, see <https://www.gnu.org/licenses/>.
2424
*/
2525

26-
/* When installed, this file is called "cppp/reiconv.hpp". */
27-
2826
#pragma once
2927
#ifndef _CPPP_REICONV_HPP_
3028
#define _CPPP_REICONV_HPP_
3129

32-
#include <cppp/encodings/reiconv.hpp>
30+
#include "encodings/reiconv.hpp"
3331

34-
#include <cstddef>
3532
#include <cstring>
3633
#include <string>
3734
#include <string_view>
3835

39-
#define _CPPP_API // TODO: Remove this.
40-
4136
#ifndef _CPPP_API
4237
#define _CPPP_API @CPPP_API@
4338
#endif
@@ -123,6 +118,22 @@ namespace reiconv
123118
*/
124119
extern _CPPP_API VersionInfo version;
125120

121+
/**
122+
* @brief Convert flags. Used for open conversion descriptor.
123+
*/
124+
enum class ConvertFlag
125+
{
126+
/**
127+
* @brief No flags.
128+
*/
129+
NO_FLAGS = 0,
130+
131+
/**
132+
* @brief Discard illegal sequences. Same as iconv(3) '//IGNORE'.
133+
*/
134+
DISCARD_ILSEQ = 1,
135+
};
136+
126137
/**
127138
* @brief Converts an string from one encoding to another, supports encodings autodetecting.
128139
*
@@ -139,7 +150,7 @@ namespace reiconv
139150
* @return Return the output buffer.
140151
* @throw std::system_error if an error occurs.
141152
*/
142-
extern _CPPP_API std::string convert(Encoding from, Encoding to, const std::string_view input, bool strict = true);
153+
extern _CPPP_API std::string convert(Encoding from, Encoding to, const std::string_view input, enum ConvertFlag flag = ConvertFlag::NO_FLAGS);
143154

144155
/**
145156
* @brief Determine the current locale's character encoding.

include/iconv.h.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
* If not, see <https://www.gnu.org/licenses/>.
2424
*/
2525

26-
/* When installed, this file is called "iconv.h". */
27-
2826
#ifndef _ICONV_H_
2927
#define _ICONV_H_
3028

lib/iconv.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ _CPPP_API int reiconv_lookup_from_codepage(int codepage)
8686
return codepage_to_eindex[codepage] - 1;
8787
}
8888

89-
_CPPP_API reiconv_t reiconv_open_from_index(int fromcode, int tocode, bool discard_ilseq)
89+
_CPPP_API reiconv_t reiconv_open_from_index(int fromcode, int tocode, enum ConvertFlag flags)
9090
{
9191
struct conv_struct *cd = (struct conv_struct *)malloc(sizeof(struct conv_struct));
9292
if (cd == NULL)
@@ -111,12 +111,12 @@ _CPPP_API reiconv_t reiconv_open_from_index(int fromcode, int tocode, bool disca
111111
cd->ibyteorder = 0;
112112

113113
// Initialize the operation flags.
114-
cd->discard_ilseq = discard_ilseq;
114+
cd->discard_ilseq = flags & REICONV_DISCARD_ILSEQ;
115115

116116
return cd;
117117
}
118118

119-
_CPPP_API reiconv_t reiconv_open_from_codepage(int fromcode, int tocode, bool discard_ilseq)
119+
_CPPP_API reiconv_t reiconv_open_from_codepage(int fromcode, int tocode, enum ConvertFlag flags)
120120
{
121121
int from_index = reiconv_lookup_from_codepage(fromcode);
122122
int to_index = reiconv_lookup_from_codepage(tocode);
@@ -126,10 +126,10 @@ _CPPP_API reiconv_t reiconv_open_from_codepage(int fromcode, int tocode, bool di
126126
errno = EINVAL;
127127
return (reiconv_t)(-1);
128128
}
129-
return reiconv_open_from_index(from_index, to_index, discard_ilseq);
129+
return reiconv_open_from_index(from_index, to_index, flags);
130130
}
131131

132-
_CPPP_API reiconv_t reiconv_open_from_name(const char *fromcode, const char *tocode, bool discard_ilseq)
132+
_CPPP_API reiconv_t reiconv_open_from_name(const char *fromcode, const char *tocode, enum ConvertFlag flags)
133133
{
134134
int from_index = reiconv_lookup_from_name(fromcode);
135135
int to_index = reiconv_lookup_from_name(tocode);
@@ -139,7 +139,7 @@ _CPPP_API reiconv_t reiconv_open_from_name(const char *fromcode, const char *toc
139139
errno = EINVAL;
140140
return (reiconv_t)(-1);
141141
}
142-
return reiconv_open_from_index(from_index, to_index, discard_ilseq);
142+
return reiconv_open_from_index(from_index, to_index, flags);
143143
}
144144

145145
_CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode)
@@ -150,7 +150,7 @@ _CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode)
150150
size_t fromcode_len = reiconv_name_canonicalize(fromcode, fromcode_buf);
151151
size_t tocode_len = reiconv_name_canonicalize(tocode, tocode_buf);
152152

153-
bool discard_ilseq = false;
153+
enum ConvertFlag flag = REICONV_NO_FLAGS;
154154

155155
for (size_t i = 0; i < fromcode_len; i++)
156156
{
@@ -159,7 +159,7 @@ _CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode)
159159
fromcode_buf[i] = '\0';
160160
if (i + 7 < fromcode_len && memcmp(fromcode_buf + i + 1, "/IGNORE", 8) == 0)
161161
{
162-
discard_ilseq = true;
162+
flag |= REICONV_DISCARD_ILSEQ;
163163
}
164164
}
165165
}
@@ -171,7 +171,7 @@ _CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode)
171171
tocode_buf[i] = '\0';
172172
if (i + 7 < tocode_len && memcmp(tocode_buf + i + 1, "/IGNORE", 7) == 0)
173173
{
174-
discard_ilseq = true;
174+
flag |= REICONV_DISCARD_ILSEQ;
175175
}
176176
}
177177
}
@@ -184,7 +184,7 @@ _CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode)
184184
return (reiconv_t)(-1);
185185
}
186186

187-
return reiconv_open_from_index(from_index, to_index, discard_ilseq);
187+
return reiconv_open_from_index(from_index, to_index, flag);
188188
}
189189

190190
_CPPP_API size_t reiconv_iconv(reiconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)

lib/reiconv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ _CPPP_API reiconv::Encoding::Encoding(const int codepage)
5252
_CPPP_API reiconv::VersionInfo reiconv::version{VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH};
5353

5454
_CPPP_API std::string reiconv::convert(reiconv::Encoding from, reiconv::Encoding to, const std::string_view input,
55-
bool strict)
55+
enum ConvertFlag flag)
5656
{
57-
::reiconv_t cd = ::reiconv_open_from_index(from, to, !strict);
57+
::reiconv_t cd = ::reiconv_open_from_index(from, to, (enum ::ConvertFlag)flag);
5858
if (cd == (::reiconv_t)(-1))
5959
{
6060
throw std::system_error(errno, std::system_category(), "reiconv_open_from_index");

tests/reiconv-test.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ inline Buffer reiconv_test(reiconv::Encoding from, reiconv::Encoding to, const s
3434
{
3535
using namespace reiconv;
3636
Buffer input = Buffer::read_from_file(input_file_path);
37-
std::string res = convert(from, to, {input.data(), input.size}, true);
37+
std::string res = convert(from, to, {input.data(), input.size}, ConvertFlag::DISCARD_ILSEQ);
3838
return Buffer(res, "converted");
3939
}

0 commit comments

Comments
 (0)