📄 appendix.html
字号:
* This module implements a random number object. * * IMPLEMENTATION: * * The random numbers are implemented just like they are * in the Microsoft C8 RTL. * * NOTES: * *--------------------------------------------------------------*/#define USE_HRAND#include "app.h"USEWINASSERT/*--- The random number class object ---*/CLASS(hRand, HRAND) { long lRand; };/*--- Implement random numbers just like the MS C8 RTL ---*/#define NEXTRAND(l) (l*214013L+2531011L)#define FINALRAND(l) ((l>>16)&0x7FFF)/*pf-------------------------------------------------------------- * * DESCRIPTION: (Create Random Number Object) JLJ * * Given a seed, create a new random number generator object * * ARGUMENTS: * * nSeed - Seed of the new random number generator * * RETURNS: * * A new random number generator object * *--------------------------------------------------------------*/HRAND APIENTRY RandCreate( int nSeed ){ HRAND hRand; NEWOBJ(hRand); hRand->lRand = nSeed; return (hRand);} /* RandCreate *//*pf-------------------------------------------------------------- * * DESCRIPTION: (Destroy Random Number Object) JLJ * * Destroy the given random number generator object * * ARGUMENTS: * * hRand - Random number generator object or NULL * * RETURNS: * * NULL * *--------------------------------------------------------------*/HRAND APIENTRY RandDestroy( HRAND hRand ){ VERIFYZ(hRand) { FREE(hRand); } return (NULL);} /* RandDestroy *//*pf-------------------------------------------------------------- * * DESCRIPTION: (Generate Next Random Number) JLJ * * Generate the next random number for the given random * number generator object. * * ARGUMENTS: * * hRand - Random number generator object * * RETURNS: * * The next random number * *--------------------------------------------------------------*/int APIENTRY RandNext( HRAND hRand ){ int nRand=0; VERIFY(hRand) { hRand->lRand = NEXTRAND(hRand->lRand); nRand = (int)FINALRAND(hRand->lRand); } return(nRand);} /* RandNext */</pre></tr></td></table><br><a name="dosc"><br></a><table bgcolor="#F0F0F0"><tr><td><img src="images/windows.gif"> <big><b>A.5 DOS.C Module</b></big><br><br>This code implements the DOS module as described in <a href="chapter6.html">Chapter 6</a>.<br><br><table bgcolor="#CCCCEE" cellpadding=0 cellspacing=0><tr><td><pre><b>A DOS interface</b>/****************************************************************//* *//* (project name) *//* *//* Copyright (date) (Company Name). All rights reserved. *//* *//* This program contains the confidential trade secret *//* information of (Company Name). Use, disclosure, or *//* copying without written consent is strictly prohibited. *//* *//****************************************************************//*pm-------------------------------------------------------------- * * OUTLINE: * * This module provides access to the low-level file I/O * functions of the standard Microsoft C run-time library. * * IMPLEMENTATION: * * This module is simply a code wrapper module. * * NOTES: * *--------------------------------------------------------------*/#define USE_LOWIO#define USE_HDOSFH#include "app.h"USEWINASSERT/*--- The class object ---*/CLASS(hDosFh, HDOSFH) { int fh; };/*pf-------------------------------------------------------------- * * DESCRIPTION: (Open File) JLJ * * Attempt to open a file * * ARGUMENTS: * * lpFilename - The name of the file to open * * RETURNS: * * A file object handle or NULL if there was some error * in opening the specified file. * *--------------------------------------------------------------*/HDOSFH APIENTRY DosOpenFile( LPSTR lpFilename ){ HDOSFH hDosFh=NULL; int fh=open(lpFilename, _O_RDWR|_O_BINARY); if (fh!=-1) { NEWOBJ(hDosFh); hDosFh->fh = fh; } return (hDosFh);} /* DosOpenFile *//*pf-------------------------------------------------------------- * * DESCRIPTION: (Close File) JLJ * * Close a previously opened file * * ARGUMENTS: * * hDosFh - The file object or NULL * * RETURNS: * * NULL * *--------------------------------------------------------------*/HDOSFH APIENTRY DosCloseFile( HDOSFH hDosFh ){ VERIFYZ(hDosFh) { int nResult=close(hDosFh->fh); WinAssert(!nResult); FREE(hDosFh); } return (NULL);} /* DosCloseFile *//*pf-------------------------------------------------------------- * * DESCRIPTION: (Read File) JLJ * * Attempt to read a block of information from a file. * * ARGUMENTS: * * hDosFh - The file object * lpMem - Pointer to memory buffer * wCount - Number of bytes to read into the memory buffer * * RETURNS: * * The number of bytes that were actually read * *--------------------------------------------------------------*/WORD APIENTRY DosRead( HDOSFH hDosFh, LPVOID lpMem, WORD wCount ){ WORD wNumRead=0; VERIFY(hDosFh) { wNumRead = (WORD)read(hDosFh->fh, lpMem, wCount); } return (wNumRead);} /* DosRead *//*pf-------------------------------------------------------------- * * DESCRIPTION: (Write File) JLJ * * Attempt to write a block of information to a file. * * ARGUMENTS: * * hDosFh - The file object * lpMem - Pointer to memory buffer * wCount - Number of bytes to write to the file * * RETURNS: * * The number of bytes that were actually written * *--------------------------------------------------------------*/WORD APIENTRY DosWrite( HDOSFH hDosFh, LPVOID lpMem, WORD wCount ){ WORD wNumWritten=0; VERIFY(hDosFh) { wNumWritten = (WORD)write(hDosFh->fh, lpMem, wCount); } return (wNumWritten);} /* DosWrite */</pre></tr></td></table></td></tr></table><br><a name="outputdebugstring"><br></a><table bgcolor="#F0F0F0"><tr><td><img src="images/windows.gif"> <big><b>A.6 OutputDebugString() for MS-DOS Programmers</b></big><br><br>If you are programming for Windows, you already have access to the OutputDebugString() function for placing messages onto the monochrome screen. This is an option in the DBWIN.EXE program provided with Microsoft C8. <br><br>However, if you are programming under MS-DOS, an OutputDebugString() that you can use to place messages onto the monochrome screen is as follows. <br><br><table bgcolor="#CCCCEE" cellpadding=0 cellspacing=0><tr><td><pre><b>OutputDebugString() for MS-DOS programmers</b>/*pf-------------------------------------------------------------- * * DESCRIPTION: (Output String to Mono Screen) JLJ * * Scrolls the monochrome screen and places a new * string on the 25'th line. * * ARGUMENTS: * * lpS - String to place on monochrome screen * * RETURNS: * * (void) * *--------------------------------------------------------------*/void APIENTRY OutputDebugString( LPSTR lpS ){ LPSTR lpScreen=(LPSTR)0xB0000000; /* base of mono screen */ int nPos=0; /* for walking lpS string */ /*--- Scroll monochrome screen up one line ---*/ _fmemcpy( lpScreen, lpScreen+2*80, 2*80*24 ); /*--- Place new line down in 25'th line ---*/ for (int loop=0; loop<80; ++loop) { lpScreen[2*(80*24+loop)] = (lpS[nPos]?lpS[nPos++]:' '); }} /* OutputDebugString */</pre></tr></td></table>Refer to<a href="chapter7.html#monoscreen">§7.27</a> for a description of OutputDebugString().</td></tr></table><br><a name="reportwinassert"><br></a><big><b>A.7 ReportWinAssert()</b></big><br><br>The ReportWinAssert() functions presented here are bare-bones and should be tailored by you. ReportWinAssert() is called whenever there has been an assertion failure in your code. This can happen when there are run-time type checking failures or when the heap manager is reporting a problem.<br><br><table bgcolor="#F0F0F0"><tr><td><img src="images/windows.gif"> A ReportWinAssert() that can be used for Windows is as follows.<br><br><table bgcolor="#CCCCEE" cellpadding=0 cellspacing=0><tr><td><pre><b>ReportWinAssert() for Windows</b>void APIENTRY ReportWinAssert( LPSTR lpFilename, int nLine ){ char buffer[100]; wsprintf( buffer, "WinAssert: %s %d", lpFilename, nLine ); MessageBox( NULL, buffer, "WinAssert", MB_OK|MB_SYSTEMMODAL );} /* ReportWinAssert */</pre></tr></td></table></td></tr></table><br>A ReportWinAssert() that can be used for C console apps is as follows.<br><br><table bgcolor="#CCCCEE" cellpadding=0 cellspacing=0><tr><td><pre><b>ReportWinAssert() for C console apps</b>void APIENTRY ReportWinAssert( LPSTR lpFilename, int nLine ){ printf( "WinAssert: %s-%d (Press Enter) ", lpFilename, nLine ); while ('\n'!=getchar()) { }} /* ReportWinAssert */</pre></tr></td></table><br><br><hr><center><small>Copyright © 1993-1995, 2002-2003 Jerry Jongerius<br>This book was previously published by Person Education, Inc.,<br>formerly known as Prentice Hall. ISBN: 0-13-183898-9<br></small></center></html></body>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -