00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00031
00032 #if HAVE_STDLIB_H
00033 #include <stdlib.h>
00034 #endif
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
00050
00051
00052 TDSLOCALE *
00053 tds_get_locale(void)
00054 {
00055 TDSLOCALE *locale;
00056 char *s;
00057 FILE *in;
00058
00059
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
00078 tds_strlcpy(buf, s, sizeof(buf));
00079
00080
00081 rewind(in);
00082 found = tds_read_conf_section(in, buf, tds_parse_locale, locale);
00083
00084
00085
00086
00087
00088
00089
00090
00091
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
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