PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

#define MAX_LOCALE_NAME_LEN   100

Definition at line 104 of file win32setlocale.c.

Referenced by map_locale().

Function Documentation

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

Definition at line 107 of file win32setlocale.c.

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

Referenced by pgwin32_setlocale().

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

Definition at line 168 of file win32setlocale.c.

References map_locale(), NULL, and result.

169 {
170  const char *argument;
171  char *result;
172 
173  if (locale == NULL)
174  argument = NULL;
175  else
177 
178  /* Call the real setlocale() function */
179  result = setlocale(category, argument);
180 
181  /*
182  * setlocale() is specified to return a "char *" that the caller is
183  * forbidden to modify, so casting away the "const" is innocuous.
184  */
185  if (result)
186  result = (char *) map_locale(locale_map_result, result);
187 
188  return result;
189 }
static const char * map_locale(const struct locale_map *map, const char *locale)
static const struct locale_map locale_map_argument[]
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
static const struct locale_map locale_map_result[]
static char * locale
Definition: initdb.c:123

Variable Documentation

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}
}
#define NULL
Definition: c.h:229

Definition at line 57 of file win32setlocale.c.

const struct locale_map locale_map_result[]
static
Initial value:
= {
{"Norwegian (Bokm", "l)_Norway", "Norwegian_Norway"},
{NULL, NULL, NULL}
}
#define NULL
Definition: c.h:229

Definition at line 92 of file win32setlocale.c.