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.
*/
long
lGetFilesize(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)
void
vPrintBlock(const char *szFile, int iLine,
const UCHAR *aucBlock, size_t tLength)
{
int i, j;
/*
* bReadBytes
* This function reads the specified number of bytes from the specified file,
* starting from the specified offset.
* Returns TRUE when successfull, otherwise FALSE
*/
BOOL
bReadBytes(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
*/
BOOL
bReadBuffer(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;
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
*/
ULONG
ulColor2Color(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 int
iFindSplit(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;
/*
* tNumber2Roman - convert a number to Roman Numerals
*
* returns the number of characters written
*/
size_t
tNumber2Roman(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;
}
/*
* iNumber2Alpha - convert a number to alphabetic "numbers"
*
* returns the number of characters written
*/
size_t
tNumber2Alpha(UINT uiNumber, BOOL bUpperCase, char *szOutput)
{
char *outp;
UINT uiTmp;
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
*/
long
lComputeLeading(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_t
tUcs2Utf8(ULONG ulChar, char *szResult, size_t tMaxResultLen)
{
if (szResult == NULL || tMaxResultLen == 0) {
return 0;
}
/*
* vGetBulletValue - get the bullet value for the conversing type and encoding
*/
void
vGetBulletValue(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?
*/
BOOL
bAllZero(const UCHAR *aucBytes, size_t tLength)
{
size_t tIndex;
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_t
tConvertDTTM(ULONG ulDTTM)
{
struct tm tTime;
time_t tResult;