~ubuntu-branches/ubuntu/trusty/postgresql-8.4/trusty

« back to all changes in this revision

Viewing changes to src/backend/utils/mb/mbutils.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 *
5
5
 * Tatsuo Ishii
6
6
 *
7
 
 * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.86 2009/04/24 08:43:50 mha Exp $
 
7
 * $PostgreSQL: pgsql/src/backend/utils/mb/mbutils.c,v 1.87 2009/06/11 14:49:05 momjian Exp $
8
8
 */
9
9
#include "postgres.h"
10
10
 
30
30
/*
31
31
 * We maintain a simple linked list caching the fmgr lookup info for the
32
32
 * currently selected conversion functions, as well as any that have been
33
 
 * selected previously in the current session.  (We remember previous
 
33
 * selected previously in the current session.  (We remember previous
34
34
 * settings because we must be able to restore a previous setting during
35
35
 * transaction rollback, without doing any fresh catalog accesses.)
36
36
 *
40
40
{
41
41
        int                     s_encoding;             /* server and client encoding IDs */
42
42
        int                     c_encoding;
43
 
        FmgrInfo        to_server_info; /* lookup info for conversion procs */
 
43
        FmgrInfo        to_server_info; /* lookup info for conversion procs */
44
44
        FmgrInfo        to_client_info;
45
45
} ConvProcInfo;
46
46
 
119
119
        {
120
120
                /*
121
121
                 * If we're in a live transaction, it's safe to access the catalogs,
122
 
                 * so look up the functions.  We repeat the lookup even if the info
123
 
                 * is already cached, so that we can react to changes in the contents
124
 
                 * of pg_conversion.
 
122
                 * so look up the functions.  We repeat the lookup even if the info is
 
123
                 * already cached, so that we can react to changes in the contents of
 
124
                 * pg_conversion.
125
125
                 */
126
126
                Oid                     to_server_proc,
127
127
                                        to_client_proc;
168
168
                ToClientConvProc = &convinfo->to_client_info;
169
169
 
170
170
                /*
171
 
                 * Remove any older entry for the same encoding pair (this is just
172
 
                 * to avoid memory leakage).
 
171
                 * Remove any older entry for the same encoding pair (this is just to
 
172
                 * avoid memory leakage).
173
173
                 */
174
174
                foreach(lc, ConvProcList)
175
175
                {
191
191
        else
192
192
        {
193
193
                /*
194
 
                 * If we're not in a live transaction, the only thing we can do
195
 
                 * is restore a previous setting using the cache.  This covers all
196
 
                 * transaction-rollback cases.  The only case it might not work for
197
 
                 * is trying to change client_encoding on the fly by editing
 
194
                 * If we're not in a live transaction, the only thing we can do is
 
195
                 * restore a previous setting using the cache.  This covers all
 
196
                 * transaction-rollback cases.  The only case it might not work for is
 
197
                 * trying to change client_encoding on the fly by editing
198
198
                 * postgresql.conf and SIGHUP'ing.  Which would probably be a stupid
199
199
                 * thing to do anyway.
200
200
                 */
275
275
 *
276
276
 * CAUTION: although the presence of a length argument means that callers
277
277
 * can pass non-null-terminated strings, care is required because the same
278
 
 * string will be passed back if no conversion occurs.  Such callers *must*
 
278
 * string will be passed back if no conversion occurs.  Such callers *must*
279
279
 * check whether result == src and handle that case differently.
280
280
 *
281
281
 * Note: we try to avoid raising error, since that could get us into
622
622
size_t
623
623
wchar2char(char *to, const wchar_t *from, size_t tolen)
624
624
{
625
 
        size_t result;
626
 
        
 
625
        size_t          result;
 
626
 
627
627
        if (tolen == 0)
628
628
                return 0;
629
629
 
630
630
#ifdef WIN32
 
631
 
631
632
        /*
632
 
         * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding,
633
 
         * and for some reason mbstowcs and wcstombs won't do this for us,
634
 
         * so we use MultiByteToWideChar().
 
633
         * On Windows, the "Unicode" locales assume UTF16 not UTF8 encoding, and
 
634
         * for some reason mbstowcs and wcstombs won't do this for us, so we use
 
635
         * MultiByteToWideChar().
635
636
         */
636
637
        if (GetDatabaseEncoding() == PG_UTF8)
637
638
        {
638
639
                result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
639
 
                                                                NULL, NULL);
 
640
                                                                         NULL, NULL);
640
641
                /* A zero return is failure */
641
642
                if (result <= 0)
642
643
                        result = -1;
650
651
        else
651
652
#endif   /* WIN32 */
652
653
        {
653
 
                Assert( !lc_ctype_is_c() );
 
654
                Assert(!lc_ctype_is_c());
654
655
                result = wcstombs(to, from, tolen);
655
656
        }
656
657
        return result;
701
702
                /* mbstowcs requires ending '\0' */
702
703
                char       *str = pnstrdup(from, fromlen);
703
704
 
704
 
                Assert( !lc_ctype_is_c() );
 
705
                Assert(!lc_ctype_is_c());
705
706
                result = mbstowcs(to, str, tolen);
706
707
                pfree(str);
707
708
        }
722
723
                                (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
723
724
                                 errmsg("invalid multibyte character for locale"),
724
725
                                 errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
725
 
        }       
 
726
        }
726
727
 
727
728
        return result;
728
729
}
729
 
 
730
730
#endif
731
731
 
732
732
/* convert a multibyte string to a wchar */
907
907
pg_bind_textdomain_codeset(const char *domainname)
908
908
{
909
909
#if defined(ENABLE_NLS)
910
 
        int             encoding = GetDatabaseEncoding();
911
 
        int     i;
 
910
        int                     encoding = GetDatabaseEncoding();
 
911
        int                     i;
912
912
 
913
913
        /*
914
 
         * gettext() uses the codeset specified by LC_CTYPE by default,
915
 
         * so if that matches the database encoding we don't need to do
916
 
         * anything. In CREATE DATABASE, we enforce or trust that the
917
 
         * locale's codeset matches database encoding, except for the C
918
 
         * locale. In C locale, we bind gettext() explicitly to the right
919
 
         * codeset.
 
914
         * gettext() uses the codeset specified by LC_CTYPE by default, so if that
 
915
         * matches the database encoding we don't need to do anything. In CREATE
 
916
         * DATABASE, we enforce or trust that the locale's codeset matches
 
917
         * database encoding, except for the C locale. In C locale, we bind
 
918
         * gettext() explicitly to the right codeset.
920
919
         *
921
 
         * On Windows, though, gettext() tends to get confused so we always
922
 
         * bind it.
 
920
         * On Windows, though, gettext() tends to get confused so we always bind
 
921
         * it.
923
922
         */
924
923
#ifndef WIN32
925
924
        const char *ctype = setlocale(LC_CTYPE, NULL);