You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Sam Carleton <sc...@miltonstreet.com> on 2011/03/03 17:12:20 UTC

Converting a 16-bit string to 8-bit?

I am looking at using a 3rd party library that only operates on 16-bit
strings.  Is there a built in functions to convert the strings back to
8-bit?  I am currenly on Windows and Windows has built in functions I could
use, I would prefer to use Apache functions if they exist.

Re: Converting a 16-bit string to 8-bit?

Posted by Ben Noordhuis <in...@bnoordhuis.nl>.
On Sat, Mar 5, 2011 at 02:11, Adelle Hartley <ad...@akemi.com.au> wrote:
> Thanks for the feedback.  Is there any documentation for the apr_xlate
> functions?

My pleasure, Adelle. Documentation: I don't think so save for the
source itself[1].

[1] https://github.com/apache/apr/blob/trunk/xlate/xlate.c

Re: Converting a 16-bit string to 8-bit?

Posted by Adelle Hartley <ad...@akemi.com.au>.
On 4/03/2011 22:01, Ben Noordhuis wrote:
> Adelle, your code doesn't appear to be handling errors. A number of
> things can go wrong here:
>
> 1. The conversion may not be supported.
>
> 2. Partial character sequences (not an issue here since the input is
> UTF-32 but I mention it for posterity's sake), reported as
> APR_EINCOMPLETE.
>
> 3. Illegal character sequences, reported as APR_EINVAL.
>
> 4. Output buffer too short. Reported as APR_SUCCESS but with inbytes_left>  0.

Thanks for the feedback.  Is there any documentation for the apr_xlate 
functions?

Adelle.


Re: Converting a 16-bit string to 8-bit?

Posted by Ben Noordhuis <in...@bnoordhuis.nl>.
On Fri, Mar 4, 2011 at 02:24, Adelle Hartley <ad...@akemi.com.au> wrote:
> This is a helper class I wrote for the module I'm working on.  It assumes
> the native wide encoding is UTF-32.  To make it cross platform, you'd have
> to check what the correct wide encoding is.
>
> This is my first apache module, so any corrections welcome.
>
> class our_response_t
> {
> protected:
>        request_rec* m;
>        apr_xlate_t* m_convset;
>        char m_bufferBytes[CHARSET_CONVERSION_BUFFER_SIZE];
> public:
>        our_response_t(request_rec* request) : m(request), m_convset(NULL),
> m_html(NULL), m_json(NULL)
>        {
>                apr_pool_t* pool = m->pool;
>                apr_status_t status = apr_xlate_open(&m_convset, "UTF-8",
> "UTF-32", pool);
>
>                if (m_convset)
>                {
>                        ap_set_content_type(m, "text/html;charset=UTF-8");
>                }
>        }
>
>        ~our_response_t()
>        {
>                if (m_convset)
>                {
>                        apr_xlate_close(m_convset);
>                }
>        }
>
>        void append_chars(const wchar_t* str, size_t num_chars)
>        {
>                apr_size_t inbytes_left = num_chars*sizeof(wchar_t);
>                apr_size_t outbytes_left = CHARSET_CONVERSION_BUFFER_SIZE-1;
>                apr_status_t status = apr_xlate_conv_buffer(m_convset, (const
> char*)str, &inbytes_left, m_bufferBytes, &outbytes_left);
>                m_bufferBytes[CHARSET_CONVERSION_BUFFER_SIZE-outbytes_left-1]
> = 0;
>                ap_rputs(m_bufferBytes, m);
>        }
>
> };

Adelle, your code doesn't appear to be handling errors. A number of
things can go wrong here:

1. The conversion may not be supported.

2. Partial character sequences (not an issue here since the input is
UTF-32 but I mention it for posterity's sake), reported as
APR_EINCOMPLETE.

3. Illegal character sequences, reported as APR_EINVAL.

4. Output buffer too short. Reported as APR_SUCCESS but with inbytes_left > 0.

Re: Converting a 16-bit string to 8-bit?

Posted by Adelle Hartley <ad...@akemi.com.au>.
On 4/03/2011 03:12, Sam Carleton wrote:
> I am looking at using a 3rd party library that only operates on 16-bit
> strings.  Is there a built in functions to convert the strings back to
> 8-bit?  I am currenly on Windows and Windows has built in functions I could
> use, I would prefer to use Apache functions if they exist.

This is a helper class I wrote for the module I'm working on.  It 
assumes the native wide encoding is UTF-32.  To make it cross platform, 
you'd have to check what the correct wide encoding is.

This is my first apache module, so any corrections welcome.

class our_response_t
{
protected:
	request_rec* m;
	apr_xlate_t* m_convset;
	char m_bufferBytes[CHARSET_CONVERSION_BUFFER_SIZE];
public:
	our_response_t(request_rec* request) : m(request), m_convset(NULL), 
m_html(NULL), m_json(NULL)
	{
		apr_pool_t* pool = m->pool;
		apr_status_t status = apr_xlate_open(&m_convset, "UTF-8", "UTF-32", pool);

		if (m_convset)
		{
			ap_set_content_type(m, "text/html;charset=UTF-8");
		}
	}

	~our_response_t()
	{
		if (m_convset)
		{
			apr_xlate_close(m_convset);
		}
	}

	void append_chars(const wchar_t* str, size_t num_chars)
	{
		apr_size_t inbytes_left = num_chars*sizeof(wchar_t);
		apr_size_t outbytes_left = CHARSET_CONVERSION_BUFFER_SIZE-1;
		apr_status_t status = apr_xlate_conv_buffer(m_convset, (const 
char*)str, &inbytes_left, m_bufferBytes, &outbytes_left);
		m_bufferBytes[CHARSET_CONVERSION_BUFFER_SIZE-outbytes_left-1] = 0;
		ap_rputs(m_bufferBytes, m);
	}

};


Re:Re: not initial const aplog_module_index

Posted by Whut Jia <wh...@163.com>.
Error is come from http_log.h header file .
When I don't inclue this file and don't use ap_log_rerror function,everything is OK! 




At 2011-03-08 16:23:34,"Andrew Godziuk" <an...@cloudaccess.net> wrote:

>You could try enclosing the header file in
>
>extern "C" {
>.......
>}
>
>but it's not an ellegant solution.
>
>-- 
>Andrzej Godziuk
>http://CloudAccess.net/

Re: not initial const aplog_module_index

Posted by Andrew Godziuk <an...@cloudaccess.net>.
You could try enclosing the header file in

extern "C" {
.......
}

but it's not an ellegant solution.

-- 
Andrzej Godziuk
http://CloudAccess.net/

not initial const aplog_module_index

Posted by Whut Jia <wh...@163.com>.
Hi,all

when I compile myself module source file (in c++ language)with g++,it accures error below:

http_log.h:121:not initial const aplog_module_index.

But,it is all right when I compile a module in c language with apxs tool.Why??Please help me!

My apache version is httpd-2.3.8,

Thanks!

 

 

Re: Converting a 16-bit string to 8-bit?

Posted by Nick Kew <ni...@apache.org>.
On 3 Mar 2011, at 16:52, Ben Noordhuis wrote:

> On Thu, Mar 3, 2011 at 17:12, Sam Carleton <sc...@miltonstreet.com> wrote:
>> I am looking at using a 3rd party library that only operates on 16-bit
>> strings.  Is there a built in functions to convert the strings back to
>> 8-bit?  I am currenly on Windows and Windows has built in functions I could
>> use, I would prefer to use Apache functions if they exist.
> 
> Sam, you want either apr_xlate.h or apr-iconv / native iconv.

apr_xlate would be the appropriate abstraction for cross-platform support,
if it meets your needs.  If not, you'd need an external library.

You might not need to do anything at all:

(1) You could send data out in 16-bit encoding if you send it with
an appropriate charset parameter in the Content-type header.
(2) If (1) is not an option, an existing module like mod_charset
might be suitable to do the conversion for you.

-- 
Nick Kew

Available for work, contract or permanent
http://www.webthing.com/~nick/cv.html


Re: Converting a 16-bit string to 8-bit?

Posted by Ben Noordhuis <in...@bnoordhuis.nl>.
On Thu, Mar 3, 2011 at 17:12, Sam Carleton <sc...@miltonstreet.com> wrote:
> I am looking at using a 3rd party library that only operates on 16-bit
> strings.  Is there a built in functions to convert the strings back to
> 8-bit?  I am currenly on Windows and Windows has built in functions I could
> use, I would prefer to use Apache functions if they exist.

Sam, you want either apr_xlate.h or apr-iconv / native iconv.