/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4loc.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4loc.c The dk4loc module. */ #line 67 "dk4loc.ctr" #include "dk4loc.h" #ifndef STDLIB_H_INCLUDED #include #define STDLIB_H_INCLUDED 1 #endif #if DK4_HAVE_STRING_H #ifndef STRING_H_INCLUDED #include #define STRING_H_INCLUDED 1 #endif #endif #if DK4_HAVE_CTYPE_H #ifndef CTYPE_H_INCLUDED #include #define CTYPE_H_INCLUDED 1 #endif #endif #ifndef DK4ENC_H_INCLUDED #include "dk4enc.h" #endif #ifndef DK4STR8_H_INCLUDED #include "dk4str8.h" #endif #if DK4_ON_WINDOWS #ifndef DK4WREG8_H_INCLUDED #include "dk4wreg8.h" #endif #ifndef DK4WREGC_H_INCLUDED #include "dk4wregc.h" #endif #endif #line 110 "dk4loc.ctr" /** Keywords used by the module. */ static const char * const dk4loc_mod_kw_8[] = { /* 0 */ "LANG", /* 1 */ "utf8", /* 2 */ "UTF-8", /* 3 */ "Control Panel\\International", /* 4 */ "LocaleName", /* 5 */ "sLanguage", NULL #line 133 "dk4loc.ctr" }; static int dk4loc_use_default(dkChar *lptr, size_t szl, dk4_er_t *erp) { int back = 0; if ((NULL != lptr) && (2 < szl)) { lptr[0] = dkT('e'); lptr[1] = dkT('n'); lptr[2] = dkT('\0'); back = 1; } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } return back; } int dk4loc_get_settings( dkChar *lang, size_t szlang, dkChar *reg, size_t szreg, int *enc, dk4_er_t *erp ) { #if DK4_ON_WINDOWS char buf[64]; /* Buffer to process getenv result */ void *kptr; /* Registry key pointer */ dkChar *pd; /* Copy string */ char *p3; /* Region */ char *p4; /* Copy string */ size_t sz; /* String length */ int res; /* Result from RegQueryValueEx() */ int expand = 0; /* Flag: Expansion necessary. */ #else char buf[64]; /* Buffer to process getenv result */ dkChar *pd; /* Copy string */ char *p1; /* Result from getenv */ char *p2; /* Encoding */ char *p3; /* Region */ char *p4; /* Copy string */ size_t sz; /* String length */ #endif int back = 0; #line 184 "dk4loc.ctr" if ((NULL != lang) && (0 < szlang)) { *lang = dkT('\0'); } if ((NULL != reg ) && (0 < szreg )) { *reg = dkT('\0'); } if (NULL != enc) { *enc = DK4_ENCODING_ASCII; } if ((NULL != lang) && (NULL != reg) && (NULL != enc)) { if ((0 < szlang) && (0 < szreg)) { #if DK4_ON_WINDOWS #if DK4_CHAR_SIZE > 1 *enc = DK4_ENCODING_UTF16; #else *enc = DK4_ENCODING_ASCII; #endif kptr = dk4wreg_open_key_c8(DK4_WREGKEY_HKCU, dk4loc_mod_kw_8[3], 0, erp); if (NULL != kptr) { res = dk4wreg_get_string_c8( kptr, dk4loc_mod_kw_8[4], buf, sizeof(buf), &expand, erp ); if (0 != res) { p3 = dk4str8_chr(buf, '-'); if (NULL != p3) { *(p3++) = '\0'; } dk4str8_normalize(buf, NULL); if (NULL != p3) { dk4str8_normalize(p3, NULL); } sz = dk4str8_len(buf); if (0 < sz) { if (szlang > sz) { /* strcpy(lang, buf); */ pd = lang; p4 = buf; while('\0' != *p4) { *(pd++) = (dkChar)dk4str8_tolower(*(p4++)); } *pd = dkT('\0'); back = 1; sz = dk4str8_len(p3); if (0 < sz) { if (szreg > sz) { /* strcpy(reg, p3); */ pd = reg; p4 = p3; while('\0' != *p4) { #if 0 *(pd++) = (dkChar)(tolower((unsigned char)(*(p4++)))); #endif *(pd++) = (dkChar)dk4str8_tolower(*(p4++)); } *pd = dkT('\0'); } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); back = 0; } } } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } else { back = dk4loc_use_default(lang, szlang, erp); } } else { res = dk4wreg_get_string_c8( kptr, dk4loc_mod_kw_8[5], buf, sizeof(buf), &expand, erp ); if (0 != res) { dk4str8_normalize(buf, NULL); sz = dk4str8_len(buf); if (2 < sz) { buf[2] = '\0'; } sz = dk4str8_len(buf); if (0 < sz) { if (szlang > sz) { pd = lang; p4 = buf; while ('\0' != *p4) { *(pd++) = (dkChar)(tolower((unsigned char)(*(p4++)))); } *pd = dkT('\0'); back = 1; } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } else { back = dk4loc_use_default(lang, szlang, erp); } } else { back = dk4loc_use_default(lang, szlang, erp); } } dk4wreg_close_key(kptr); } else { back = dk4loc_use_default(lang, szlang, erp); } #else p1 = getenv(dk4loc_mod_kw_8[0]); if (NULL != p1) { #line 272 "dk4loc.ctr" if (sizeof(buf) > dk4str8_len(p1)) { #line 273 "dk4loc.ctr" dk4str8_cpy_s(buf, sizeof(buf), p1, NULL); p2 = dk4str8_chr(buf, '.'); if (NULL != p2) { #line 276 "dk4loc.ctr" *(p2++) = '\0'; #line 277 "dk4loc.ctr" } p3 = dk4str8_chr(buf, '_'); if (NULL != p3) { #line 280 "dk4loc.ctr" *(p3++) = '\0'; #line 281 "dk4loc.ctr" } #line 282 "dk4loc.ctr" dk4str8_normalize(buf, NULL); #line 283 "dk4loc.ctr" if (NULL != p2) { dk4str8_normalize(p2, NULL); } if (NULL != p3) { dk4str8_normalize(p3, NULL); } sz = dk4str8_len(buf); #line 286 "dk4loc.ctr" if (0 < sz) { if (szlang > sz) { /* strcpy(lang, buf); */ pd = lang; p4 = buf; while('\0' != *p4) { *(pd++) = (dkChar)(tolower((unsigned char)(*(p4++)))); } *pd = dkT('\0'); back = 1; if (NULL != p3) { sz = dk4str8_len(p3); if (0 < sz) { if (szreg > sz) { /* strcpy(reg, p3); */ pd = reg; p4 = p3; while('\0' != *p4) { *(pd++) = (dkChar)(tolower((unsigned char)(*(p4++)))); } *pd = dkT('\0'); } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); back = 0; } } } #if DK4_CHAR_SIZE > 1 #if DK4_CHAR_SIZE > 2 #line 314 "dk4loc.ctr" *enc = DK4_ENCODING_32; #else #line 317 "dk4loc.ctr" *enc = DK4_ENCODING_UTF16; #endif #else #line 321 "dk4loc.ctr" *enc = DK4_ENCODING_ASCII; if (NULL != p2) { if (0 == dk4str8_casecmp(p2, dk4loc_mod_kw_8[1])) { *enc = DK4_ENCODING_UTF8; } else { if (0 == dk4str8_casecmp(p2, dk4loc_mod_kw_8[2])) { *enc = DK4_ENCODING_UTF8; } } } #endif } else { #line 333 "dk4loc.ctr" dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } else { #line 336 "dk4loc.ctr" back = dk4loc_use_default(lang, szlang, erp); } } else { #line 339 "dk4loc.ctr" dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } else { #line 342 "dk4loc.ctr" back = dk4loc_use_default(lang, szlang, erp); } #endif } else { #line 346 "dk4loc.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { #line 349 "dk4loc.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 351 "dk4loc.ctr" return back; }