PostgreSQL Source Code  git master
win32setlocale.c File Reference
#include "c.h"
Include dependency graph for win32setlocale.c:

Go to the source code of this file.

Data Structures

struct  locale_map
 

Macros

#define MAX_LOCALE_NAME_LEN   100
 

Functions

static const char * map_locale (const struct locale_map *map, const char *locale)
 
char * pgwin32_setlocale (int category, const char *locale)
 

Variables

static const struct locale_map locale_map_argument []
 
static const struct locale_map locale_map_result []
 

Macro Definition Documentation

◆ MAX_LOCALE_NAME_LEN

#define MAX_LOCALE_NAME_LEN   100

Definition at line 108 of file win32setlocale.c.

Function Documentation

◆ map_locale()

static const char* map_locale ( const struct locale_map map,
const char *  locale 
)
static

Definition at line 111 of file win32setlocale.c.

112 {
113  static char aliasbuf[MAX_LOCALE_NAME_LEN];
114  int i;
115 
116  /* Check if the locale name matches any of the problematic ones. */
117  for (i = 0; map[i].locale_name_start != NULL; i++)
118  {
119  const char *needle_start = map[i].locale_name_start;
120  const char *needle_end = map[i].locale_name_end;
121  const char *replacement = map[i].replacement;
122  char *match;
123  char *match_start = NULL;
124  char *match_end = NULL;
125 
126  match = strstr(locale, needle_start);
127  if (match)
128  {
129  /*
130  * Found a match for the first part. If this was a two-part
131  * replacement, find the second part.
132  */
133  match_start = match;
134  if (needle_end)
135  {
136  match = strstr(match_start + strlen(needle_start), needle_end);
137  if (match)
138  match_end = match + strlen(needle_end);
139  else
140  match_start = NULL;
141  }
142  else
143  match_end = match_start + strlen(needle_start);
144  }
145 
146  if (match_start)
147  {
148  /* Found a match. Replace the matched string. */
149  int matchpos = match_start - locale;
150  int replacementlen = strlen(replacement);
151  char *rest = match_end;
152  int restlen = strlen(rest);
153 
154  /* check that the result fits in the static buffer */
155  if (matchpos + replacementlen + restlen + 1 > MAX_LOCALE_NAME_LEN)
156  return NULL;
157 
158  memcpy(&aliasbuf[0], &locale[0], matchpos);
159  memcpy(&aliasbuf[matchpos], replacement, replacementlen);
160  /* includes null terminator */
161  memcpy(&aliasbuf[matchpos + replacementlen], rest, restlen + 1);
162 
163  return aliasbuf;
164  }
165  }
166 
167  /* no match, just return the original string */
168  return locale;
169 }
static char * locale
Definition: initdb.c:140
int i
Definition: isn.c:72
const char * locale_name_end
const char * locale_name_start
const char * replacement
#define MAX_LOCALE_NAME_LEN

References i, locale, locale_map::locale_name_end, locale_map::locale_name_start, MAX_LOCALE_NAME_LEN, and locale_map::replacement.

Referenced by pgwin32_setlocale().

◆ pgwin32_setlocale()

char* pgwin32_setlocale ( int  category,
const char *  locale 
)

Definition at line 172 of file win32setlocale.c.

173 {
174  const char *argument;
175  char *result;
176 
177  if (locale == NULL)
178  argument = NULL;
179  else
181 
182  /* Call the real setlocale() function */
183  result = setlocale(category, argument);
184 
185  /*
186  * setlocale() is specified to return a "char *" that the caller is
187  * forbidden to modify, so casting away the "const" is innocuous.
188  */
189  if (result)
190  result = unconstify(char *, map_locale(locale_map_result, result));
191 
192  return result;
193 }
#define unconstify(underlying_type, expr)
Definition: c.h:1224
#define setlocale(a, b)
Definition: win32_port.h:485
static const char * map_locale(const struct locale_map *map, const char *locale)
static const struct locale_map locale_map_argument[]
static const struct locale_map locale_map_result[]

References locale, locale_map_argument, locale_map_result, map_locale(), setlocale, and unconstify.

Variable Documentation

◆ locale_map_argument

const struct locale_map locale_map_argument[]
static
Initial value:
= {
{"Hong Kong S.A.R.", NULL, "HKG"},
{"U.A.E.", NULL, "ARE"},
{"Chinese (Traditional)_Macau S.A.R..950", NULL, "ZHM"},
{"Chinese_Macau S.A.R..950", NULL, "ZHM"},
{"Chinese (Traditional)_Macao S.A.R..950", NULL, "ZHM"},
{"Chinese_Macao S.A.R..950", NULL, "ZHM"},
{NULL, NULL, NULL}
}

Definition at line 1 of file win32setlocale.c.

Referenced by pgwin32_setlocale().

◆ locale_map_result

const struct locale_map locale_map_result[]
static
Initial value:
= {
{"Norwegian (Bokm", "l)_Norway", "Norwegian_Norway"},
{"Norwegian Bokm", "l_Norway", "Norwegian_Norway"},
{NULL, NULL, NULL}
}

Definition at line 1 of file win32setlocale.c.

Referenced by pgwin32_setlocale().