📄 setlocal.c
字号:
}
#endif /* _MT */
return retval;
} /* setlocale */
static char * __cdecl _setlocale_set_cat (
int category,
const char * locale
)
{
char * oldlocale;
LCID oldhandle;
UINT oldcodepage;
LC_ID oldid;
LC_ID idtemp;
UINT cptemp;
char lctemp[MAX_LC_LEN];
char * pch;
if (!_expandlocale((char *)locale, lctemp, &idtemp, &cptemp, category))
{
return NULL; /* unrecognized locale */
}
if (!(pch = (char *)_malloc_crt(strlen(lctemp)+1)))
{
return NULL; /* error if malloc fails */
}
oldlocale = __lc_category[category].locale; /* save for possible restore*/
oldhandle = __lc_handle[category];
memcpy((void *)&oldid, (void *)&__lc_id[category], sizeof(oldid));
oldcodepage = __lc_codepage;
/* update locale string */
__lc_category[category].locale = strcpy(pch,lctemp);
__lc_handle[category] = MAKELCID(idtemp.wLanguage, SORT_DEFAULT);
memcpy((void *)&__lc_id[category], (void *)&idtemp, sizeof(idtemp));
if (category==LC_CTYPE)
__lc_codepage = cptemp;
if ( category == LC_COLLATE )
__lc_collate_cp = cptemp;
if (__lc_category[category].init())
{
/* restore previous state! */
__lc_category[category].locale = oldlocale;
_free_crt(pch);
__lc_handle[category] = oldhandle;
__lc_codepage = oldcodepage;
return NULL; /* error if non-zero return */
}
/* locale set up successfully */
/* Cleanup */
if ((oldlocale != _clocalestr)
)
_free_crt(oldlocale);
return __lc_category[category].locale;
} /* _setlocale_set_cat */
static char * __cdecl _setlocale_get_all (
void
)
{
int i;
int same = 1;
/* allocate memory if necessary */
if (!__lc_category[LC_ALL].locale)
__lc_category[LC_ALL].locale =
_malloc_crt((MAX_LC_LEN+1) * (LC_MAX-LC_MIN+1) + CATNAMES_LEN);
__lc_category[LC_ALL].locale[0] = '\0';
for (i=LC_MIN+1; ; i++)
{
_strcats(__lc_category[LC_ALL].locale, 3, __lc_category[i].catname,"=",__lc_category[i].locale);
if (i<LC_MAX)
{
strcat(__lc_category[LC_ALL].locale,";");
if (strcmp(__lc_category[i].locale, __lc_category[i+1].locale))
same=0;
}
else
{
if (!same)
return __lc_category[LC_ALL].locale;
else
{
_free_crt(__lc_category[LC_ALL].locale);
__lc_category[LC_ALL].locale = (char *)NULL;
return __lc_category[LC_CTYPE].locale;
}
}
}
} /* _setlocale_get_all */
char * _expandlocale (
char *expr,
char * output,
LC_ID * id,
UINT * cp,
int category
)
{
static LC_ID cacheid = {0, 0, 0};
static UINT cachecp = 0;
static char cachein[MAX_LC_LEN] = "C";
static char cacheout[MAX_LC_LEN] = "C";
if (!expr)
return NULL; /* error if no input */
if (((*expr=='C') && (!expr[1]))
) /* for "C" locale, just return */
{
*output = 'C';
output[1] = '\0';
if (id)
{
id->wLanguage = 0;
id->wCountry = 0;
id->wCodePage = 0;
}
if (cp)
{
*cp = CP_ACP; /* return to ANSI code page */
}
return output; /* "C" */
}
/* first, make sure we didn't just do this one */
if (strcmp(cacheout,expr) && strcmp(cachein,expr))
{
/* do some real work */
LC_STRINGS names;
if (__lc_strtolc((LC_STRINGS *)&names, (const char *)expr))
return NULL; /* syntax error */
if (!__get_qualified_locale((LPLC_STRINGS)&names,
(LPLC_ID)&cacheid, (LPLC_STRINGS)&names))
return NULL; /* locale not recognized/supported */
/* begin: cache atomic section */
cachecp = cacheid.wCodePage;
__lc_lctostr((char *)cacheout, &names);
/* Don't cache "" empty string */
if (*expr)
strcpy(cachein, expr);
else
strcpy(cachein, cacheout);
/* end: cache atomic section */
}
if (id)
memcpy((void *)id, (void *)&cacheid, sizeof(cacheid)); /* possibly return LC_ID */
if (cp)
memcpy((void *)cp, (void *)&cachecp, sizeof(cachecp)); /* possibly return cp */
strcpy(output,cacheout);
return cacheout; /* return fully expanded locale string */
}
/* helpers */
int __cdecl __init_dummy(void) /* default routine for locale initializer */
{
return 0;
}
void _strcats
(
char *outstr,
int n,
...
)
{
int i;
va_list substr;
va_start (substr, n);
for (i =0; i<n; i++)
{
strcat(outstr, va_arg(substr, char *));
}
va_end(substr);
}
int __lc_strtolc
(
LC_STRINGS *names,
const char *locale
)
{
int i, len;
char ch;
memset((void *)names, '\0', sizeof(LC_STRINGS)); /* clear out result */
if (*locale=='\0')
return 0; /* trivial case */
/* only code page is given */
if (locale[0] == '.' && locale[1] != '\0')
{
strcpy((char *)names->szCodePage, &locale[1]);
return 0;
}
for (i=0; ; i++)
{
if (!(len=strcspn(locale,"_.,")))
return -1; /* syntax error */
ch = locale[len];
if ((i==0) && (len<MAX_LANG_LEN) && (ch!='.'))
strncpy((char *)names->szLanguage, locale, len);
else if ((i==1) && (len<MAX_CTRY_LEN) && (ch!='_'))
strncpy((char *)names->szCountry, locale, len);
else if ((i==2) && (ch=='\0' || ch==','))
strncpy((char *)names->szCodePage, locale, len);
else
return -1; /* error parsing locale string */
if (ch==',')
{
/* modifier not used in current implementation, but it
must be parsed to for POSIX/XOpen conformance */
/* strncpy(names->szModifier, locale, MAX_MODIFIER_LEN-1); */
break;
}
if (!ch)
break;
locale+=(len+1);
}
return 0;
}
void __lc_lctostr
(
char *locale,
const LC_STRINGS *names
)
{
strcpy(locale, (char *)names->szLanguage);
if (*(names->szCountry))
_strcats(locale, 2, "_", names->szCountry);
if (*(names->szCodePage))
_strcats(locale, 2, ".", names->szCodePage);
/* if (names->szModifier)
_strcats(locale, 2, ",", names->szModifier); */
}
#endif /* !defined (_WIN32) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -