NCBI C++ ToolKit
locale.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2  * Copyright (C) 1998-1999 Brian Bruns
3  * Copyright (C) 2005-2010 Frediano Ziglio
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #include <config.h>
22 
23 #if HAVE_LOCALE_H
24 #include <locale.h>
25 #endif
26 
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 
31 #if HAVE_STRING_H
32 #include <string.h>
33 #endif /* HAVE_STRING_H */
34 
35 #if HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif /* HAVE_STDLIB_H */
38 
39 #include <freetds/tds.h>
40 #include <freetds/configs.h>
41 #include "replacements.h"
42 
43 static void tds_parse_locale(const char *option, const char *value, void *param);
44 
45 /**
46  * Get locale information.
47  * @return allocated structure with all information or NULL if error
48  */
49 TDSLOCALE *
51 {
53  char *s;
54  FILE *in;
55 
56  /* allocate a new structure with hard coded and build-time defaults */
58  if (!locale)
59  return NULL;
60 
61  tdsdump_log(TDS_DBG_INFO1, "Attempting to read locales.conf file\n");
62 
63  in = fopen(FREETDS_LOCALECONFFILE, "r");
64  if (in) {
66 
67 #if HAVE_LOCALE_H
68  s = setlocale(LC_ALL, NULL);
69 #else
70  s = getenv("LANG");
71 #endif
72  if (s && s[0]) {
73  int found;
74  char buf[128];
75  const char *strip = "@._";
76 
77  /* do not change environment !!! */
78  strlcpy(buf, s, sizeof(buf));
79 
80  /* search full name */
81  rewind(in);
83 
84  /*
85  * Here we try to strip some part of language in order to
86  * catch similar language
87  * LANG is composed by
88  * language[_sublanguage][.charset][@modified]
89  * ie it_IT@euro or it_IT.UTF-8 so we strip in order
90  * modifier, charset and sublanguage
91  * ie it_IT@euro -> it_IT -> it
92  */
93  for (;!found && *strip; ++strip) {
94  s = strrchr(buf, *strip);
95  if (!s)
96  continue;
97  *s = 0;
98  rewind(in);
100  }
101 
102  }
103 
104 
105  fclose(in);
106  }
107  return locale;
108 }
109 
110 static void
111 tds_parse_locale(const char *option, const char *value, void *param)
112 {
113  TDSLOCALE *locale = (TDSLOCALE *) param;
114 
115  if (!strcmp(option, TDS_STR_CHARSET)) {
116  free(locale->server_charset);
117  locale->server_charset = strdup(value);
118  } else if (!strcmp(option, TDS_STR_LANGUAGE)) {
119  free(locale->language);
120  locale->language = strdup(value);
121  } else if (!strcmp(option, TDS_STR_DATEFMT)) {
122  free(locale->date_fmt);
123  locale->date_fmt = strdup(value);
124  }
125 }
#define FREETDS_LOCALECONFFILE
Definition: configs.h:41
#define tdsdump_log
Definition: tds.h:1561
#define TDS_DBG_INFO1
Definition: tds.h:900
#define TDS_STR_DATEFMT
Definition: tds.h:529
#define TDS_STR_LANGUAGE
Definition: tds.h:527
#define TDS_STR_CHARSET
Definition: tds.h:524
#define strlcpy(d, s, l)
Definition: replacements.h:80
#define option
static void tds_parse_locale(const char *option, const char *value, void *param)
Definition: locale.c:111
TDSLOCALE * tds_get_locale(void)
Get locale information.
Definition: locale.c:50
#define tds_read_conf_section
#define NULL
Definition: ncbistd.hpp:225
#define tds_alloc_locale
char * buf
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
int strcmp(const char *str1, const char *str2)
Definition: odbc_utils.hpp:160
#define strdup
Definition: ncbi_ansi_ext.h:70
std::istream & in(std::istream &in_, double &x_)
static char * locale
Definition: pcregrep.c:149
void free(voidpf ptr)
Modified on Wed Jun 19 17:00:56 2024 by modify_doxy.py rev. 669887