Skip to content

Commit ac61242

Browse files
committed
Add //IGNORE support for iconv compatibility.
1 parent 2df8507 commit ac61242

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

include/cppp/reiconv.h.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ extern _CPPP_API int reiconv_convert_static_size(reiconv_t cd, const char* input
177177
*/
178178
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);
179179

180+
/**
181+
* @brief Open a conversion descriptor. For iconv compatibility.
182+
* @param tocode The output buffer encoding. Supports "//IGNORE".
183+
* @param fromcode The input buffer encoding.
184+
* @return The conversion descriptor. (reiconv_t)(-1) is returned if error occured with errno set.
185+
*/
186+
extern _CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode);
187+
180188
/**
181189
* @brief Do conversion. For iconv compatibility.
182190
* @note At most `*inbytesleft` bytes starting at `*inbuf`.

include/iconv.h.in

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@
4646
*/
4747
#define iconv_t reiconv_t
4848

49-
/**
50-
* @brief Open a conversion descriptor.
51-
* @param tocode The target encoding.
49+
/**
50+
* @brief Open a conversion descriptor. For iconv compatibility.
51+
* @param tocode The target encoding. Supports "//IGNORE".
5252
* @param fromcode The source encoding.
5353
* @return The conversion descriptor. (iconv_t)(-1) on error with errno set.
54-
* @note This function is equivalent to `reiconv_open_from_name(fromcode, tocode)`.
55-
* @see reiconv_open_from_name
5654
*/
57-
#define iconv_open(tocode, fromcode) reiconv_open_from_name(fromcode, tocode, false)
55+
#define iconv_open reiconv_open
5856

5957
/**
6058
* @brief Convert at most `*inbytesleft` bytes from `*inbuf` according to the code conversion

lib/encoding_indexes.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* @copyright Copyright (C) 2024 The C++ Plus Project.
66
*/
77
/*
8-
* Copyright (C) 2024 The C++ Plus Project.
98
* This file is part of the cppp-reiconv library.
109
*
1110
* The cppp-reiconv library is free software; you can redistribute it

lib/iconv.c

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ _CPPP_API int reiconv_lookup_from_codepage(int codepage)
8888

8989
_CPPP_API reiconv_t reiconv_open_from_index(int fromcode, int tocode, bool discard_ilseq)
9090
{
91-
struct conv_struct *cd;
92-
cd = (struct conv_struct *)malloc(sizeof(struct conv_struct));
91+
struct conv_struct *cd = (struct conv_struct *)malloc(sizeof(struct conv_struct));
9392
if (cd == NULL)
9493
{
9594
errno = ENOMEM;
@@ -142,6 +141,51 @@ _CPPP_API reiconv_t reiconv_open_from_name(const char *fromcode, const char *toc
142141
return reiconv_open_from_index(from_index, to_index, discard_ilseq);
143142
}
144143

144+
_CPPP_API reiconv_t reiconv_open(const char *tocode, const char *fromcode)
145+
{
146+
char fromcode_buf[MAX_WORD_LENGTH + 2];
147+
char tocode_buf[MAX_WORD_LENGTH + 2];
148+
149+
size_t fromcode_len = reiconv_name_canonicalize(fromcode, fromcode_buf);
150+
size_t tocode_len = reiconv_name_canonicalize(tocode, tocode_buf);
151+
152+
bool discard_ilseq = false;
153+
154+
for (size_t i = 0; i < fromcode_len; i++)
155+
{
156+
if (i < fromcode_len && fromcode_buf[i] == '/')
157+
{
158+
fromcode_buf[i] = '\0';
159+
if (i + 7 < fromcode_len && memcmp(fromcode_buf + i + 1, "/IGNORE", 8) == 0)
160+
{
161+
discard_ilseq = true;
162+
}
163+
}
164+
}
165+
166+
for (size_t i = 0; i < tocode_len; i++)
167+
{
168+
if (tocode_buf[i] == '/')
169+
{
170+
tocode_buf[i] = '\0';
171+
if (i + 7 < fromcode_len && memcmp(tocode_buf + i + 1, "/IGNORE", 8) == 0)
172+
{
173+
discard_ilseq = true;
174+
}
175+
}
176+
}
177+
178+
int from_index = reiconv_lookup_from_name(fromcode_buf);
179+
int to_index = reiconv_lookup_from_name(tocode_buf);
180+
if (from_index == -1 || to_index == -1)
181+
{
182+
errno = EINVAL;
183+
return (reiconv_t)(-1);
184+
}
185+
186+
return reiconv_open_from_index(from_index, to_index, discard_ilseq);
187+
}
188+
145189
_CPPP_API size_t reiconv_iconv(reiconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
146190
{
147191
if (inbuf == NULL || *inbuf == NULL)

0 commit comments

Comments
 (0)