⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 misc.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
		uiNumber -= 1;		*outp++ = (char)(uiTmp + uiNumber);	} else if (uiNumber <= 26U + 26U*26U) {		uiNumber -= 26 + 1;		*outp++ = (char)(uiTmp + uiNumber / 26);		*outp++ = (char)(uiTmp + uiNumber % 26);	} else if (uiNumber <= 26U + 26U*26U + 26U*26U*26U) {		uiNumber -= 26 + 26*26 + 1;		*outp++ = (char)(uiTmp + uiNumber / (26*26));		*outp++ = (char)(uiTmp + uiNumber / 26 % 26);		*outp++ = (char)(uiTmp + uiNumber % 26);	}	*outp = '\0';	fail(outp < szOutput);	return (size_t)(outp - szOutput);} /* end of tNumber2Alpha *//* * unincpy - copy a counted Unicode string to an single-byte string */char *unincpy(char *s1, const UCHAR *s2, size_t n){	char	*pcDest;	ULONG	ulChar;	size_t	tLen;	USHORT	usUni;	for (pcDest = s1, tLen = 0; tLen < n; pcDest++, tLen++) {		usUni = usGetWord(tLen * 2, s2);		if (usUni == 0) {			break;		}		ulChar = ulTranslateCharacters(usUni, 0, 8,				conversion_unknown, encoding_neutral, FALSE);		if (ulChar == IGNORE_CHARACTER) {			ulChar = (ULONG)'?';		}		*pcDest = (char)ulChar;	}	for (; tLen < n; tLen++) {		*pcDest++ = '\0';	}	return s1;} /* end of unincpy *//* * unilen - calculate the length of a Unicode string * * returns the length in bytes */size_tunilen(const UCHAR *s){	size_t	tLen;	USHORT	usUni;	tLen = 0;	for (;;) {		usUni = usGetWord(tLen, s);		if (usUni == 0) {			return tLen;		}		tLen += 2;	}} /* end of unilen *//* * szBaseName - get the basename of the specified filename */const char *szBasename(const char *szFilename){	const char	*szTmp;	fail(szFilename == NULL);	if (szFilename == NULL || szFilename[0] == '\0') {		return "null";	}	szTmp = strrchr(szFilename, FILE_SEPARATOR[0]);	if (szTmp == NULL) {		return szFilename;	}	return ++szTmp;} /* end of szBasename *//* * lComputeLeading - compute the leading * * NOTE: the fontsize is specified in half points * * Returns the leading in drawunits */longlComputeLeading(USHORT usFontSize){	long	lLeading;	lLeading = (long)usFontSize * 500L;	if (usFontSize < 18) {		/* Small text: 112% */		lLeading *= 112;	} else if (usFontSize < 28) {	/* Normal text: 124% */		lLeading *= 124;	} else if (usFontSize < 48) {	/* Small headlines: 104% */		lLeading *= 104;	} else {			/* Large headlines: 100% */		lLeading *= 100;	}	lLeading = lMilliPoints2DrawUnits(lLeading);	lLeading += 50;	lLeading /= 100;	return lLeading;} /* end of lComputeLeading *//* * Convert a UCS character to an UTF-8 string * * Returns the string length of the result */size_ttUcs2Utf8(ULONG ulChar, char *szResult, size_t tMaxResultLen){	if (szResult == NULL || tMaxResultLen == 0) {		return 0;	}	if (ulChar < 0x80 && tMaxResultLen >= 2) {		szResult[0] = (char)ulChar;		szResult[1] = '\0';		return 1;	}	if (ulChar < 0x800 && tMaxResultLen >= 3) {		szResult[0] = (char)(0xc0 | ulChar >> 6);		szResult[1] = (char)(0x80 | (ulChar & 0x3f));		szResult[2] = '\0';		return 2;	}	if (ulChar < 0x10000 && tMaxResultLen >= 4) {		szResult[0] = (char)(0xe0 | ulChar >> 12);		szResult[1] = (char)(0x80 | (ulChar >> 6 & 0x3f));		szResult[2] = (char)(0x80 | (ulChar & 0x3f));		szResult[3] = '\0';		return 3;	}	if (ulChar < 0x200000 && tMaxResultLen >= 5) {		szResult[0] = (char)(0xf0 | ulChar >> 18);		szResult[1] = (char)(0x80 | (ulChar >> 12 & 0x3f));		szResult[2] = (char)(0x80 | (ulChar >> 6 & 0x3f));		szResult[3] = (char)(0x80 | (ulChar & 0x3f));		szResult[4] = '\0';		return 4;	}	szResult[0] = '\0';	return 0;} /* end of tUcs2Utf8 *//* * vGetBulletValue - get the bullet value for the conversing type and encoding */voidvGetBulletValue(conversion_type eConversionType, encoding_type eEncoding,	char *szResult, size_t tMaxResultLen){	fail(szResult == NULL);	fail(tMaxResultLen < 2);	if (eEncoding == encoding_utf_8) {		(void)tUcs2Utf8(UNICODE_BULLET, szResult, tMaxResultLen);	} else {		szResult[0] = (char)ucGetBulletCharacter(eConversionType,							eEncoding);		szResult[1] = '\0';	}} /* end of vGetBulletValue *//* * bAllZero - are all bytes zero? */BOOLbAllZero(const UCHAR *aucBytes, size_t tLength){	size_t	tIndex;	if (aucBytes == NULL || tLength == 0) {		return TRUE;	}	for (tIndex = 0; tIndex < tLength; tIndex++) {		if (aucBytes[tIndex] != 0) {			return FALSE;		}	}	return TRUE;} /* end of bAllZero */#if !defined(__riscos)/* * GetCodesetFromLocale - get the codeset from the current locale * * Original version: Copyright (C) 1999  Bruno Haible * Syntax: * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]] * * Returns TRUE when sucessful, otherwise FALSE */static BOOLbGetCodesetFromLocale(char *szCodeset, size_t tMaxCodesetLength, BOOL *pbEuro){#if !defined(__dos)	const char	*szLocale;	const char	*pcTmp;	size_t		tIndex;	char		szModifier[6];#endif /* __dos */	if (pbEuro != NULL) {		*pbEuro = FALSE;	/* Until proven otherwise */	}	if (szCodeset == NULL || tMaxCodesetLength == 0) {		return FALSE;	}#if defined(__dos)	if (tMaxCodesetLength < 2 + sizeof(int) * 3 + 1) {		DBG_DEC(tMaxCodesetLength);		DBG_DEC(2 + sizeof(int) * 3 + 1);		return FALSE;	}	/* Get the active codepage from DOS */	sprintf(szCodeset, "cp%d", iGetCodepage());	DBG_MSG(szCodeset);#else	/* Get the locale from the environment */	szLocale = getenv("LC_ALL");	if (szLocale == NULL || szLocale[0] == '\0') {		szLocale = getenv("LC_CTYPE");		if (szLocale == NULL || szLocale[0] == '\0') {			szLocale = getenv("LANG");		}	}	if (szLocale == NULL || szLocale[0] == '\0') {		/* No locale, so no codeset name and no modifier */		return FALSE;	}	DBG_MSG(szLocale);	pcTmp = strchr(szLocale, '.');	if (pcTmp == NULL) {		/* No codeset name */		szCodeset[0] = '\0';	} else {		/* Copy the codeset name */		pcTmp++;		for (tIndex = 0; tIndex < tMaxCodesetLength; tIndex++) {			if (*pcTmp == '@' || *pcTmp == '+' ||			    *pcTmp == ',' || *pcTmp == '_' ||			    *pcTmp == '\0') {				szCodeset[tIndex] = '\0';				break;			}			szCodeset[tIndex] = *pcTmp;			pcTmp++;		}		szCodeset[tMaxCodesetLength - 1] = '\0';	}	if (pbEuro == NULL) {		/* No need to get the modifier */		return TRUE;	}	pcTmp = strchr(szLocale, '@');	if (pcTmp != NULL) {		/* Copy the modifier */		pcTmp++;		for (tIndex = 0; tIndex < sizeof(szModifier); tIndex++) {			if (*pcTmp == '+' || *pcTmp == ',' ||			    *pcTmp == '_' || *pcTmp == '\0') {				szModifier[tIndex] = '\0';				break;			}			szModifier[tIndex] = *pcTmp;			pcTmp++;		}		szModifier[sizeof(szModifier) - 1] = '\0';		*pbEuro = STRCEQ(szModifier, "Euro");	}#endif /* __dos */	return TRUE;} /* end of bGetCodesetFromLocale *//* * GetNormalizedCodeset - get the normalized codeset from the current locale * * Returns TRUE when sucessful, otherwise FALSE */BOOLbGetNormalizedCodeset(char *szCodeset, size_t tMaxCodesetLength, BOOL *pbEuro){	BOOL	bOnlyDigits;	const char	*pcSrc;	char	*pcDest;	char	*szTmp, *szCodesetNorm;	if (pbEuro != NULL) {		*pbEuro = FALSE;	/* Until proven otherwise */	}	if (szCodeset == NULL || tMaxCodesetLength < 4) {		return FALSE;	}	/* Get the codeset name */	szTmp = xmalloc(tMaxCodesetLength - 3);	if (!bGetCodesetFromLocale(szTmp, tMaxCodesetLength - 3, pbEuro)) {		szTmp = xfree(szTmp);		return FALSE;	}	/* Normalize the codeset name */	szCodesetNorm = xmalloc(tMaxCodesetLength - 3);	bOnlyDigits = TRUE;	pcDest = szCodesetNorm;	for (pcSrc = szTmp; *pcSrc != '\0'; pcSrc++) {		if (isalnum(*pcSrc)) {			*pcDest = tolower(*pcSrc);			if (!isdigit(*pcDest)) {				bOnlyDigits = FALSE;			}			pcDest++;		}	}	*pcDest = '\0';	DBG_MSG(szCodesetNorm);	/* Add "iso" when szCodesetNorm contains all digits */	if (bOnlyDigits && szCodesetNorm[0] != '\0') {		fail(strlen(szCodesetNorm) + 3 >= tMaxCodesetLength);		sprintf(szCodeset, "iso%s", szCodesetNorm);	} else {		fail(strlen(szCodesetNorm) >= tMaxCodesetLength);		strncpy(szCodeset, szCodesetNorm, pcDest - szCodesetNorm + 1);		szCodeset[tMaxCodesetLength - 1] = '\0';	}	DBG_MSG(szCodeset);	/* Clean up and leave */	szCodesetNorm = xfree(szCodesetNorm);	szTmp = xfree(szTmp);	return TRUE;} /* end of bGetNormalizedCodeset *//* * szGetDefaultMappingFile - get the default mapping file * * Returns the basename of the default mapping file */const char *szGetDefaultMappingFile(void){	static const struct {		const char	*szCodeset;		const char	*szMappingFile;	} atMappingFile[] = {		{ "iso88591",	MAPPING_FILE_8859_1 },		{ "iso88592",	MAPPING_FILE_8859_2 },		{ "iso88593",	"8859-3.txt" },		{ "iso88594",	"8859-4.txt" },		{ "iso88595",	"8859-5.txt" },		{ "iso88596",	MAPPING_FILE_8859_5 },		{ "iso88597",	"8859-7.txt" },		{ "iso88598",	"8859-8.txt" },		{ "iso88599",	"8859-9.txt" },		{ "iso885910",	"8859-10.txt" },		{ "iso885913",	"8859-13.txt" },		{ "iso885914",	"8859-14.txt" },		{ "iso885915",	MAPPING_FILE_8859_15 },		{ "iso885916",	"8859-16.txt" },		{ "koi8r",	MAPPING_FILE_KOI8_R },		{ "koi8u",	MAPPING_FILE_KOI8_U },		{ "utf8",	MAPPING_FILE_UTF_8 },		{ "cp437",	MAPPING_FILE_CP437 },		{ "cp850",	"cp850.txt" },		{ "cp852",	MAPPING_FILE_CP852 },		{ "cp862",	"cp862.txt" },		{ "cp864",	"cp864.txt" },		{ "cp866",	MAPPING_FILE_CP866 },		{ "cp1250",	MAPPING_FILE_CP1250 },		{ "cp1251",	MAPPING_FILE_CP1251 },		{ "cp1252",	"cp1252.txt" },	};	size_t	tIndex;	BOOL	bEuro;	char	szCodeset[20];	szCodeset[0] = '\0';	bEuro = FALSE;	/* Get the normalized codeset name */	if (!bGetNormalizedCodeset(szCodeset, sizeof(szCodeset), &bEuro)) {		return MAPPING_FILE_8859_1;	}	if (szCodeset[0] == '\0') {		if (bEuro) {			/* Default mapping file (with Euro sign) */			return MAPPING_FILE_8859_15;		} else {			/* Default mapping file (without Euro sign) */			return MAPPING_FILE_8859_1;		}	}	/* Find the name in the table */	for (tIndex = 0; tIndex < elementsof(atMappingFile); tIndex++) {		if (STREQ(atMappingFile[tIndex].szCodeset, szCodeset)) {			return atMappingFile[tIndex].szMappingFile;		}	}	/* Default default mapping file */#if defined(__dos)	return MAPPING_FILE_CP437;#else	return MAPPING_FILE_8859_1;#endif /* __dos */} /* end of szGetDefaultMappingFile */#endif /* !__riscos *//* * tConvertDTTM - convert Windows Date and Time format * * returns Unix time_t or -1 */time_ttConvertDTTM(ULONG ulDTTM){	struct tm	tTime;	time_t		tResult;	if (ulDTTM == 0) {		return (time_t)-1;	}	memset(&tTime, 0, sizeof(tTime));	tTime.tm_min = (int)(ulDTTM & 0x0000003f);	tTime.tm_hour = (int)((ulDTTM & 0x000007c0) >> 6);	tTime.tm_mday = (int)((ulDTTM & 0x0000f800) >> 11);	tTime.tm_mon = (int)((ulDTTM & 0x000f0000) >> 16);	tTime.tm_year = (int)((ulDTTM & 0x1ff00000) >> 20);	tTime.tm_isdst = -1;	tTime.tm_mon--;         /* From 01-12 to 00-11 */	tResult = mktime(&tTime);	NO_DBG_MSG(ctime(&tResult));	return tResult;} /* end of tConvertDTTM */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -