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

📄 misc.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * misc.c * Copyright (C) 1998-2005 A.J. van Os; Released under GNU GPL * * Description: * Miscellaneous functions */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <time.h>#if defined(__riscos)#include "DeskLib:SWI.h"#else#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#endif /* __riscos */#if !defined(S_ISREG)#define S_ISREG(x)	(((x) & S_IFMT) == S_IFREG)#endif /* !S_ISREG */#include "antiword.h"#if defined(__vms)#include <unixlib.h>#endif#if !defined(__riscos)/* * szGetHomeDirectory - get the name of the home directory */const char *szGetHomeDirectory(void){	const char	*szHome;#if defined(__vms)	szHome = decc$translate_vms(getenv("HOME"));#elif defined(__Plan9__)	szHome = getenv("home");#else	szHome = getenv("HOME");#endif /* __vms */	if (szHome == NULL || szHome[0] == '\0') {#if defined(N_PLAT_NLM)		szHome = "SYS:";#elif defined(__dos)		szHome = "C:";#else		werr(0, "I can't find the name of your HOME directory");		szHome = "";#endif /* __dos */	}	return szHome;} /* end of szGetHomeDirectory *//* * szGetAntiwordDirectory - get the name of the Antiword directory */const char *szGetAntiwordDirectory(void){#if defined(__vms)	return decc$translate_vms(getenv("ANTIWORDHOME"));#else	return getenv("ANTIWORDHOME");#endif /* __vms */} /* end of szGetAntiwordDirectory */#endif /* !__riscos *//* * Get the size of the specified file. * Returns -1 if the file does not exist or is not a proper file. */longlGetFilesize(const char *szFilename){#if defined(__riscos)	os_error	*e;	int	iType, iSize;	e = SWI(2, 5, SWI_OS_File | XOS_Bit,		17, szFilename,		&iType, NULL, NULL, NULL, &iSize);	if (e != NULL) {		werr(0, "Get Filesize error %d: %s",			e->errnum, e->errmess);		return -1;	}	if (iType != 1) {		/* It's not a proper file or the file does not exist */		return -1;	}	return (long)iSize;#else	struct stat	tBuffer;	errno = 0;	if (stat(szFilename, &tBuffer) != 0) {		werr(0, "Get Filesize error %d", errno);		return -1;	}	if (!S_ISREG(tBuffer.st_mode)) {		/* It's not a regular file */		return -1;	}	return (long)tBuffer.st_size;#endif /* __riscos */} /* end of lGetFilesize */#if defined(DEBUG)voidvPrintBlock(const char	*szFile, int iLine,		const UCHAR *aucBlock, size_t tLength){	int i, j;	fail(szFile == NULL || iLine < 0 || aucBlock == NULL);	fprintf(stderr, "%s[%3d]:\n", szFile, iLine);	for (i = 0; i < 32; i++) {		if (16 * i >= (int)tLength) {			return;		}		fprintf(stderr, "%03x: ", (unsigned int)(16 * i));		for (j = 0; j < 16; j++) {			if (16 * i + j < (int)tLength) {				fprintf(stderr, "%02x ",					(unsigned int)aucBlock[16 * i + j]);			}		}		fprintf(stderr, "\n");	}} /* end of vPrintBlock */voidvPrintUnicode(const char *szFile, int iLine, const UCHAR *aucUni, size_t tLen){	char	*szASCII;	fail(tLen % 2 != 0);	tLen /= 2;	/* Length in bytes to length in characters */	szASCII = xmalloc(tLen + 1);	(void)unincpy(szASCII, aucUni, tLen);	szASCII[tLen] = '\0';	(void)fprintf(stderr, "%s[%3d]: %.*s\n",				szFile, iLine, (int)tLen, szASCII);	szASCII = xfree(szASCII);} /* end of vPrintUnicode */BOOLbCheckDoubleLinkedList(output_type *pAnchor){	output_type	*pCurr, *pLast;	int		iInList;	pLast = pAnchor;	iInList = 0;	for (pCurr = pAnchor; pCurr != NULL; pCurr = pCurr->pNext) {		pLast = pCurr;		iInList++;	}	NO_DBG_DEC(iInList);	for (pCurr = pLast; pCurr != NULL; pCurr = pCurr->pPrev) {		pLast = pCurr;		iInList--;	}	DBG_DEC_C(iInList != 0, iInList);	return pAnchor == pLast && iInList == 0;} /* end of bCheckDoubleLinkedList */#endif /* DEBUG *//* * bReadBytes * This function reads the specified number of bytes from the specified file, * starting from the specified offset. * Returns TRUE when successfull, otherwise FALSE */BOOLbReadBytes(UCHAR *aucBytes, size_t tMemb, ULONG ulOffset, FILE *pFile){	fail(aucBytes == NULL || pFile == NULL || ulOffset > (ULONG)LONG_MAX);	if (ulOffset > (ULONG)LONG_MAX) {		return FALSE;	}	if (fseek(pFile, (long)ulOffset, SEEK_SET) != 0) {		return FALSE;	}	if (fread(aucBytes, sizeof(UCHAR), tMemb, pFile) != tMemb) {		return FALSE;	}	return TRUE;} /* end of bReadBytes *//* * bReadBuffer * This function fills the specified buffer with the specified number of bytes, * starting at the specified offset within the Big/Small Block Depot. * * Returns TRUE when successful, otherwise FALSE */BOOLbReadBuffer(FILE *pFile, ULONG ulStartBlock,	const ULONG *aulBlockDepot, size_t tBlockDepotLen, size_t tBlockSize,	UCHAR *aucBuffer, ULONG ulOffset, size_t tToRead){	ULONG	ulBegin, ulIndex;	size_t	tLen;	fail(pFile == NULL);	fail(ulStartBlock > MAX_BLOCKNUMBER && ulStartBlock != END_OF_CHAIN);	fail(aulBlockDepot == NULL);	fail(tBlockSize != BIG_BLOCK_SIZE && tBlockSize != SMALL_BLOCK_SIZE);	fail(aucBuffer == NULL);	fail(tToRead == 0);	for (ulIndex = ulStartBlock;	     ulIndex != END_OF_CHAIN && tToRead != 0;	     ulIndex = aulBlockDepot[ulIndex]) {		if (ulIndex >= (ULONG)tBlockDepotLen) {			DBG_DEC(ulIndex);			DBG_DEC(tBlockDepotLen);			if (tBlockSize >= BIG_BLOCK_SIZE) {				werr(1, "The Big Block Depot is damaged");			} else {				werr(1, "The Small Block Depot is damaged");			}		}		if (ulOffset >= (ULONG)tBlockSize) {			ulOffset -= tBlockSize;			continue;		}		ulBegin = ulDepotOffset(ulIndex, tBlockSize) + ulOffset;		tLen = min(tBlockSize - (size_t)ulOffset, tToRead);		ulOffset = 0;		if (!bReadBytes(aucBuffer, tLen, ulBegin, pFile)) {			werr(0, "Read big block 0x%lx not possible", ulBegin);			return FALSE;		}		aucBuffer += tLen;		tToRead -= tLen;	}	DBG_DEC_C(tToRead != 0, tToRead);	return tToRead == 0;} /* end of bReadBuffer *//* * Convert a Word colornumber into a true color for use in a drawfile * * Returns the true color */ULONGulColor2Color(UCHAR ucFontColor){	static const ULONG	aulColorTable[] = {		/*  0 */	0x00000000UL,	/* Automatic */		/*  1 */	0x00000000UL,	/* Black */		/*  2 */	0xff000000UL,	/* Blue */		/*  3 */	0xffff0000UL,	/* Turquoise */		/*  4 */	0x00ff0000UL,	/* Bright Green */		/*  5 */	0xff00ff00UL,	/* Pink */		/*  6 */	0x0000ff00UL,	/* Red */		/*  7 */	0x00ffff00UL,	/* Yellow */		/*  8 */	0xffffff00UL,	/* White */		/*  9 */	0x80000000UL,	/* Dark Blue */		/* 10 */	0x80800000UL,	/* Teal */		/* 11 */	0x00800000UL,	/* Green */		/* 12 */	0x80008000UL,	/* Violet */		/* 13 */	0x00008000UL,	/* Dark Red */		/* 14 */	0x00808000UL,	/* Dark Yellow */		/* 15 */	0x80808000UL,	/* Gray 50% */		/* 16 */	0xc0c0c000UL,	/* Gray 25% */	};	if ((size_t)ucFontColor >= elementsof(aulColorTable)) {		return aulColorTable[0];	}	return aulColorTable[(int)ucFontColor];} /* end of ulColor2Color *//* * iFindSplit - find a place to split the string * * returns the index of the split character or -1 if no split found. */static intiFindSplit(const char *szString, size_t tStringLen){	size_t	tSplit;	if (tStringLen == 0) {		return -1;	}	tSplit = tStringLen - 1;	while (tSplit >= 1) {		if (szString[tSplit] == ' ' ||		    (szString[tSplit] == '-' && szString[tSplit - 1] != ' ')) {			return (int)tSplit;		}		tSplit--;	}	return -1;} /* end of iFindSplit *//* * pSplitList - split the specified list in a printable part and a leftover part * * returns the pointer to the leftover part */output_type *pSplitList(output_type *pAnchor){	output_type	*pCurr, *pLeftOver;	int		iIndex; 	fail(pAnchor == NULL);	for (pCurr = pAnchor; pCurr->pNext != NULL; pCurr = pCurr->pNext)		;	/* EMPTY */	iIndex = -1;	for (; pCurr != NULL; pCurr = pCurr->pPrev) {		iIndex = iFindSplit(pCurr->szStorage, pCurr->tNextFree);		if (iIndex >= 0) {			break;		}	}	if (pCurr == NULL || iIndex < 0) {		/* No split, no leftover */		return NULL;	}	/* Split over the iIndex-th character */	NO_DBG_MSG("pLeftOver");	pLeftOver = xmalloc(sizeof(*pLeftOver));	fail(pCurr->tNextFree < (size_t)iIndex);	pLeftOver->tStorageSize = pCurr->tNextFree - (size_t)iIndex;	pLeftOver->szStorage = xmalloc(pLeftOver->tStorageSize);	pLeftOver->tNextFree = pCurr->tNextFree - (size_t)iIndex - 1;	(void)strncpy(pLeftOver->szStorage,		pCurr->szStorage + iIndex + 1, pLeftOver->tNextFree);	pLeftOver->szStorage[pLeftOver->tNextFree] = '\0';	NO_DBG_MSG(pLeftOver->szStorage);	pLeftOver->ucFontColor = pCurr->ucFontColor;	pLeftOver->usFontStyle = pCurr->usFontStyle;	pLeftOver->tFontRef = pCurr->tFontRef;	pLeftOver->usFontSize = pCurr->usFontSize;	pLeftOver->lStringWidth = lComputeStringWidth(					pLeftOver->szStorage,					pLeftOver->tNextFree,					pLeftOver->tFontRef,					pLeftOver->usFontSize);	pLeftOver->pPrev = NULL;	pLeftOver->pNext = pCurr->pNext;	if (pLeftOver->pNext != NULL) {		pLeftOver->pNext->pPrev = pLeftOver;	}	fail(!bCheckDoubleLinkedList(pLeftOver));	NO_DBG_MSG("pAnchor");	NO_DBG_HEX(pCurr->szStorage[iIndex]);	while (iIndex >= 0 && isspace((int)(UCHAR)pCurr->szStorage[iIndex])) {		iIndex--;	}	pCurr->tNextFree = (size_t)iIndex + 1;	pCurr->szStorage[pCurr->tNextFree] = '\0';	NO_DBG_MSG(pCurr->szStorage);	pCurr->lStringWidth = lComputeStringWidth(					pCurr->szStorage,					pCurr->tNextFree,					pCurr->tFontRef,					pCurr->usFontSize);	pCurr->pNext = NULL;	fail(!bCheckDoubleLinkedList(pAnchor));	return pLeftOver;} /* end of pSplitList *//* * tNumber2Roman - convert a number to Roman Numerals * * returns the number of characters written */size_ttNumber2Roman(UINT uiNumber, BOOL bUpperCase, char *szOutput){	char	*outp, *p, *q;	UINT	uiNextVal, uiValue;	fail(szOutput == NULL);	uiNumber %= 4000;	/* Very high numbers can't be represented */	if (uiNumber == 0) {		szOutput[0] = '\0';		return 0;	}	outp = szOutput;	p = bUpperCase ? "M\2D\5C\2L\5X\2V\5I" : "m\2d\5c\2l\5x\2v\5i";	uiValue = 1000;	for (;;) {		while (uiNumber >= uiValue) {			*outp++ = *p;			uiNumber -= uiValue;		}		if (uiNumber == 0) {			*outp = '\0';			fail(outp < szOutput);			return (size_t)(outp - szOutput);		}		q = p + 1;		uiNextVal = uiValue / (UINT)(UCHAR)*q;		if ((int)*q == 2) {		/* magic */			uiNextVal /= (UINT)(UCHAR)*(q += 2);		}		if (uiNumber + uiNextVal >= uiValue) {			*outp++ = *++q;			uiNumber += uiNextVal;		} else {			p++;			uiValue /= (UINT)(UCHAR)(*p++);		}	}} /* end of tNumber2Roman *//* * iNumber2Alpha - convert a number to alphabetic "numbers" * * returns the number of characters written */size_ttNumber2Alpha(UINT uiNumber, BOOL bUpperCase, char *szOutput){	char	*outp;	UINT	uiTmp;	fail(szOutput == NULL);	if (uiNumber == 0) {		szOutput[0] = '\0';		return 0;	}	outp = szOutput;	uiTmp = (UINT)(bUpperCase ? 'A': 'a');	if (uiNumber <= 26) {

⌨️ 快捷键说明

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