src/dbapi/driver/ftds64/freetds/tds/locale.c

Go to the documentation of this file.
00001 /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
00002  * Copyright (C) 1998-1999  Brian Bruns
00003  * Copyright (C) 2005  Frediano Ziglio
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #if HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include <ctype.h>
00026 #include <stdio.h>
00027 
00028 #if HAVE_STRING_H
00029 #include <string.h>
00030 #endif /* HAVE_STRING_H */
00031 
00032 #if HAVE_STDLIB_H
00033 #include <stdlib.h>
00034 #endif /* HAVE_STDLIB_H */
00035 
00036 #include "tds.h"
00037 #include "tds_configs.h"
00038 #include "replacements.h"
00039 #ifdef DMALLOC
00040 #include <dmalloc.h>
00041 #endif
00042 
00043 TDS_RCSID(var, "$Id: locale.c 86967 2006-07-31 15:44:10Z ssikorsk $");
00044 
00045 
00046 static void tds_parse_locale(const char *option, const char *value, void *param);
00047 
00048 /**
00049  * Get locale information. 
00050  * @return allocated structure with all information or NULL if error
00051  */
00052 TDSLOCALE *
00053 tds_get_locale(void)
00054 {
00055     TDSLOCALE *locale;
00056     char *s;
00057     FILE *in;
00058 
00059     /* allocate a new structure with hard coded and build-time defaults */
00060     locale = tds_alloc_locale();
00061     if (!locale)
00062         return NULL;
00063 
00064     tdsdump_log(TDS_DBG_INFO1, "Attempting to read locales.conf file\n");
00065 
00066     in = fopen(FREETDS_LOCALECONFFILE, "r");
00067     if (in) {
00068         tds_read_conf_section(in, "default", tds_parse_locale, locale);
00069 
00070         s = getenv("LANG");
00071         if (s && s[0]) {
00072             int found;
00073             char buf[128];
00074             const char *strip = "@._";
00075             const char *charset = NULL;
00076 
00077             /* do not change environment !!! */
00078             tds_strlcpy(buf, s, sizeof(buf));
00079 
00080             /* search full name */
00081             rewind(in);
00082             found = tds_read_conf_section(in, buf, tds_parse_locale, locale);
00083 
00084             /*
00085              * Here we try to strip some part of language in order to
00086              * catch similar language
00087              * LANG is composed by 
00088              *   language[_sublanguage][.charset][@modified]
00089              * ie it_IT@euro or it_IT.UTF-8 so we strip in order
00090              * modifier, charset and sublanguage
00091              * ie it_IT@euro -> it_IT -> it
00092              */
00093             for (;!found && *strip; ++strip) {
00094                 s = strrchr(buf, *strip);
00095                 if (!s)
00096                     continue;
00097                 *s = 0;
00098                 if (*strip == '.')
00099                     charset = s+1;
00100                 rewind(in);
00101                 found = tds_read_conf_section(in, buf, tds_parse_locale, locale);
00102             }
00103 
00104             /* charset specified in LANG ?? */
00105             if (charset) {
00106                 if (locale->server_charset)
00107                     free(locale->server_charset);
00108                 locale->server_charset = strdup(charset);
00109             }
00110         }
00111 
00112         fclose(in);
00113     }
00114     return locale;
00115 }
00116 
00117 static void
00118 tds_parse_locale(const char *option, const char *value, void *param)
00119 {
00120     TDSLOCALE *locale = (TDSLOCALE *) param;
00121 
00122     if (!strcmp(option, TDS_STR_CHARSET)) {
00123         if (locale->server_charset)
00124             free(locale->server_charset);
00125         locale->server_charset = strdup(value);
00126     } else if (!strcmp(option, TDS_STR_LANGUAGE)) {
00127         if (locale->language)
00128             free(locale->language);
00129         locale->language = strdup(value);
00130     } else if (!strcmp(option, TDS_STR_DATEFMT)) {
00131         if (locale->date_fmt)
00132             free(locale->date_fmt);
00133         locale->date_fmt = strdup(value);
00134     }
00135 }
00136 
00137 

Generated on Sun Dec 6 22:23:16 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:20:59 2009 by modify_doxy.py rev. 173732