读了前5章,感触最深的是作者在例子程序中对UNICODE的考虑,另外就是将windows的API和UNIX进行对比,结合前段时间在看的《UNIX网络编程 卷2》,感觉对比性很强。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
印象最深刻的就是第
5
章的
3
个排序示例,将外部文件排序用内存来实现,堆,内存映射文件以及基准指针的使用让人大开眼见,不过比之《
windows
核心编程》还是有差距
#include
"
EvryThng.h
"
![None.gif](/Images/OutliningIndicators/None.gif)
#define
KEY_SIZE8
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Structuredefinitionforatreenode.*/
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
typedef
struct
_TreeNode
{
struct_TreeNode*Left,*Right;
TCHARKey[KEY_SIZE];
LPTSTRpData;
}
TREENODE,
*
LPTNODE,
**
LPPTNODE;
![None.gif](/Images/OutliningIndicators/None.gif)
#define
NODE_SIZEsizeof(TREENODE)
#define
NODE_HEAP_ISIZE0x8000
#define
DATA_HEAP_ISIZE0x8000
#define
MAX_DATA_LEN0x1000
#define
TKEY_SIZEKEY_SIZE*sizeof(TCHAR)
![None.gif](/Images/OutliningIndicators/None.gif)
LPTNODEFillTree(HANDLE,HANDLE,HANDLE);
BOOLScan(LPTNODE);
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
KeyCompare(LPCTSTR,LPCTSTR);iFile;
/**/
/*foraccessinexceptionhandler*/
BOOLInsertTree(LPPTNODE,LPTNODE);
![None.gif](/Images/OutliningIndicators/None.gif)
int
_tmain(
int
argc,LPTSTRargv[])
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
HANDLEhIn,hNode=NULL,hData=NULL;
LPTNODEpRoot;
BOOLNoPrint;
CHARErrorMessage[256];
intiFirstFile=Options(argc,argv,_T("n"),&NoPrint,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(argc<=iFirstFile)
ReportError(_T("Usage:sortBT[options]files"),1,FALSE);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Processallfilesonthecommandline.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(iFile=iFirstFile;iFile<argc;iFile++)__try
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Opentheinputfile.*/
hIn=CreateFile(argv[iFile],GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
if(hIn==INVALID_HANDLE_VALUE)
RaiseException(0,0,0,NULL);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Allocatethetwoheaps.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__try
{
hNode=HeapCreate(
HEAP_GENERATE_EXCEPTIONS|HEAP_NO_SERIALIZE,NODE_HEAP_ISIZE,0);
hData=HeapCreate(
HEAP_GENERATE_EXCEPTIONS|HEAP_NO_SERIALIZE,DATA_HEAP_ISIZE,0);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Processtheinputfile,creatingthetree.*/
pRoot=FillTree(hIn,hNode,hData);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*DisplaythetreeinKeyorder.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(!NoPrint)
{
_tprintf(_T("Sortedfile:%s/n"),argv[iFile]);
Scan(pRoot);
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}__finally
{/**//*Heapsandfilehandlearealwaysclosed*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Destroythetwoheapsanddatastructures.*/
if(hNode!=NULL)HeapDestroy(hNode);
if(hNode!=NULL)HeapDestroy(hData);
hNode=NULL;hData=NULL;
if(hIn!=INVALID_HANDLE_VALUE)CloseHandle(hIn);
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}/**//*Endofmainfileprocessingloopandtryblock.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
__except(EXCEPTION_EXECUTE_HANDLER)
{
_stprintf(ErrorMessage,_T("/n%s%s"),_T("sortBTerroronfile:"),argv[iFile]);
ReportError(ErrorMessage,0,TRUE);
}
return0;
}
![None.gif](/Images/OutliningIndicators/None.gif)
LPTNODEFillTree(HANDLEhIn,HANDLEhNode,HANDLEhData)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Scantheinputfile,creatingabinarysearchtreeinthe
hNodeheapwithdatapointerstothehDataheap.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Usethecallingprogram'sexceptionhandler.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
LPTNODEpRoot=NULL,pNode;
DWORDnRead,i;
BOOLAtCR;
TCHARDataHold[MAX_DATA_LEN];
LPTSTRpString;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Opentheinputfile.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(TRUE)
{
pNode=HeapAlloc(hNode,HEAP_ZERO_MEMORY,NODE_SIZE);
pNode->pData=NULL;
(pNode->Left)=pNode->Right=NULL;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Readthekey.Returnifdone.*/
if(!ReadFile(hIn,pNode->Key,TKEY_SIZE,
&nRead,NULL)||nRead!=TKEY_SIZE)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Assumeendoffileonerror.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
returnpRoot;/**//*Readthedatauntiltheendofline.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
AtCR=FALSE;/**//*LastcharacterwasnotaCR.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(i=0;i<MAX_DATA_LEN;i++)
{
ReadFile(hIn,&DataHold[i],TSIZE,&nRead,NULL);
if(AtCR&&DataHold[i]==LF)break;
AtCR=(DataHold[i]==CR);
}
DataHold[i-1]='/0';
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*DataHoldcontainsthedatawithoutthekey.
CombinetheKeyandtheData.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
pString=HeapAlloc(hData,HEAP_ZERO_MEMORY,
(SIZE_T)(KEY_SIZE+_tcslen(DataHold)+1)*TSIZE);
memcpy(pString,pNode->Key,TKEY_SIZE);
pString[KEY_SIZE]='/0';
_tcscat(pString,DataHold);
pNode->pData=pString;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Insertthenewnodeintothesearchtree.*/
InsertTree(&pRoot,pNode);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}/**//*Endofwhile(TRUE)loop*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
returnNULL;/**//*Failure*/
}
![None.gif](/Images/OutliningIndicators/None.gif)
BOOLInsertTree(LPPTNODEppRoot,LPTNODEpNode)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Insertthenewnode,pNode,intothebinarysearchtree,pRoot.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(*ppRoot==NULL)
{
*ppRoot=pNode;
returnTRUE;
}
if(KeyCompare(pNode->Key,(*ppRoot)->Key)<0)
InsertTree(&((*ppRoot)->Left),pNode);
else
InsertTree(&((*ppRoot)->Right),pNode);
returnTRUE;
}
![None.gif](/Images/OutliningIndicators/None.gif)
int
KeyCompare(LPCTSTRpKey1,LPCTSTRpKey2)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Comparetworecordsofgenericcharacters.
Thekeypositionandlengthareglobalvariables.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
return_tcsncmp(pKey1,pKey2,KEY_SIZE);
}
![None.gif](/Images/OutliningIndicators/None.gif)
static
BOOLScan(LPTNODEpNode)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Scanandprintthecontentsofabinarytree.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(pNode==NULL)
returnTRUE;
Scan(pNode->Left);
_tprintf(_T("%s/n"),pNode->pData);
Scan(pNode->Right);
returnTRUE;
}
#include
"
EvryThng.h
"
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Definitionsoftherecordstructureinthesortfile.*/
![None.gif](/Images/OutliningIndicators/None.gif)
#define
DATALEN56
#define
KEY_SIZE8
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
typedef
struct
_RECORD
{
TCHARKey[KEY_SIZE];
TCHARData[DATALEN];
}
RECORD;
![None.gif](/Images/OutliningIndicators/None.gif)
#define
RECSIZEsizeof(RECORD)
typedefRECORD
*
LPRECORD;
int
KeyCompare(LPCTSTR,LPCTSTR);
![None.gif](/Images/OutliningIndicators/None.gif)
int
_tmain(
int
argc,LPTSTRargv[])
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Thefileisthefirstargument.Sortingisdoneinplace.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Sortingisdonebyfilememorymapping.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
HANDLEhFile=INVALID_HANDLE_VALUE,hMap=NULL;
HANDLEhStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
LPVOIDpFile=NULL;
DWORDFsLow,Result=2;
TCHARTempFile[MAX_PATH];
LPTSTRpTFile;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
BOOLNoPrint;
intiFirstFile;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
iFirstFile=Options(argc,argv,_T("n"),&NoPrint,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(argc<=iFirstFile)
ReportError(_T("Usage:sortFL[options]files"),1,FALSE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_try
{/**//*try-except*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Copytheinputfiletoatempoutputfilethatwillbesorted.
Donotaltertheinputfile.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
_stprintf(TempFile,_T("%s%s"),argv[iFirstFile],_T(".tmp"));
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
CopyFile(argv[iFirstFile],TempFile,TRUE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Result=1;/**//*tmpfileisnewandshouldbedeleted.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Openthefile(usethetemporarycopy).*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hFile=CreateFile(TempFile,GENERIC_READ
|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(hFile==INVALID_HANDLE_VALUE)
ReportException(_T("Fileopenfailed."),2);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Getfilesize.
Ifthefileistoolarge,catchthatwhenitismapped.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
FsLow=GetFileSize(hFile,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(FsLow==0)
{/**//*Thereisnothingtosort*/
CloseHandle(hFile);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return0;/**//*Youmightwanttoputoutaninformationalmessage*/
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Createafilemappingobject.
Usethefilesizebutaddspaceforthenullcharacter.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hMap=CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,FsLow+TSIZE,NULL);
if(hMap==NULL)
ReportException(_T("CreateFilemapfailed."),3);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
pFile=MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0/**//*FsLow+TSIZE*/,0);
if(pFile==NULL)
ReportException(_T("MapViewfailed"),4);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Nowsortthefile.
PerformthesortwiththeClibrary-inmappedmemory.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
qsort(pFile,FsLow/RECSIZE,RECSIZE,KeyCompare);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Printouttheentiresortedfile.Treatitasonesinglestring.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
pTFile=(LPTSTR)pFile;
pTFile[FsLow/TSIZE]='/0';
if(!NoPrint)
PrintMsg(hStdOut,pFile);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Indicatesuccessfulcompletion.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Result=0;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ReportException(EMPTY,5);/**//*Forceanexceptiontocleanup.*/
return0;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}/**//*Endofinnertry-except.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
_except(EXCEPTION_EXECUTE_HANDLER)
{
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Catchanyexceptionhere.Indicateanyerror.
Thisisthenormaltermination.Deletethetemporary
fileandFreealltheresources.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(pFile!=NULL)
UnmapViewOfFile(pFile);
if(hMap!=NULL)
CloseHandle(hMap);
if(hFile!=INVALID_HANDLE_VALUE)
CloseHandle(hFile);
if(Result!=2)
DeleteFile(TempFile);
returnResult;
}
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
}
/**/
/*Endof_tmain*/
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*CODEFROMHERETOENDISNOTINCLUDEDINTEXT.*/
![None.gif](/Images/OutliningIndicators/None.gif)
int
KeyCompare(LPCTSTRpKey1,LPCTSTRpKey2)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Comparetworecordsofgenericcharacters.
Thekeypositionandlengthareglobalvariables.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
return_tcsncmp(pKey1,pKey2,KEY_SIZE);
}
![None.gif](/Images/OutliningIndicators/None.gif)
#include
"
EvryThng.h
"
![None.gif](/Images/OutliningIndicators/None.gif)
int
KeyCompare(LPCTSTR,LPCTSTR);
DWORDCreateIndexFile(DWORD,LPCTSTR,LPTSTR);
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
DWORD_PTRKStart,KSize;
/**/
/*Keystartposition&size(TCHAR).*/
BOOLRevrs;
![None.gif](/Images/OutliningIndicators/None.gif)
int
_tmain(
int
argc,LPTSTRargv[])
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Thefileisthefirstargument.Sortingisdoneinplace.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Sortingisdonebyfilememorymapping.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
HANDLEhInFile,hInMap;/**//*Inputfilehandles.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
HANDLEhXFile,hXMap;/**//*Indexfilehandles.*/
HANDLEhStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
BOOLIdxExists,NoPrint;
DWORDFsIn,FsX,RSize,iKey,nWrite,*pSizes;
LPTSTRpInFile=NULL;
LPBYTEpXFile=NULL,pX;
TCHAR_based(pInFile)*pIn;
TCHARIdxFlNam[MAX_PATH],ChNewLine='/n';
intFlIdx;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Determinetheoptions.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
FlIdx=Options(argc,argv,_T("rIn"),&Revrs,&IdxExists,&NoPrint,NULL);
if(FlIdx>=argc)
ReportError(_T("Nofilenameoncommandline."),1,FALSE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step1:OpenandMaptheInputFile.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hInFile=CreateFile(argv[FlIdx],GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);
if(hInFile==INVALID_HANDLE_VALUE)
ReportError(_T("Failedtoopeninputfile."),2,TRUE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Createafilemappingobject.Usethefilesize.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hInMap=CreateFileMapping(hInFile,NULL,PAGE_READWRITE,0,0,NULL);
if(hInMap==NULL)
ReportError(_T("Failedtocreateinputfilemapping."),3,TRUE);
pInFile=MapViewOfFile(hInMap,FILE_MAP_ALL_ACCESS,0,0,0);
if(pInFile==NULL)
ReportError(_T("Failedtomapinputfile."),4,TRUE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Getthefilesize.
Asthemappingsucceeded,thefilesizeislessthan2GB.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
FsIn=GetFileSize(hInFile,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Createtheindexfilename.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
_stprintf(IdxFlNam,_T("%s%s"),argv[FlIdx],_T(".idx"));
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Steps2and3,ifnecessary.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(!IdxExists)
RSize=CreateIndexFile(FsIn,IdxFlNam,pInFile);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step4.Maptheindexfile.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hXFile=CreateFile(IdxFlNam,GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);
if(hXFile==INVALID_HANDLE_VALUE)
ReportError(_T("Failedtoopenexistingindexfile."),5,TRUE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Createafilemappingobject.Usethefilesize.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hXMap=CreateFileMapping(hXFile,NULL,PAGE_READWRITE,0,0,NULL);
if(hXMap==NULL)
ReportError(_T("Failedtocreateindexfilemapping."),6,TRUE);
pXFile=MapViewOfFile(hXMap,FILE_MAP_ALL_ACCESS,0,0,0);
if(pXFile==NULL)
ReportError(_T("Failedtomapindexfile."),7,TRUE);
FsX=GetFileSize(hXFile,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Getthekeysize/keystartandadjustthefilesizeforthe
KeySize/KeyStartfields.Computetherecordsizefromthekeysize.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
pSizes=(LPDWORD)pXFile;KSize=*pSizes;
KStart=*(pSizes+1);
FsX-=2*sizeof(DWORD);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step5.Sorttheindexfileusingqsort.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(!IdxExists)
qsort(pXFile+2*sizeof(DWORD),FsX/RSize,RSize,KeyCompare);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step6.Outputtheinputfileinsortedorder.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Pointtotheaddressoftheinputfilestring.
StartintheInputfile.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
pX=pXFile+2*sizeof(DWORD)+RSize-sizeof(LPTSTR);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if(!NoPrint)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(iKey=0;iKey<FsX/RSize;iKey++)
{
WriteFile(hStdOut,&ChNewLine,TSIZE,&nWrite,NULL);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*ThecastonpXisimportant,asitisapointertoa
byteandweneedthefourbytesofabasedpointer.*/
pIn=(TCHAR_based(pInFile)*)*(DWORD_PTR*)pX;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while((*pIn!=CR||*(pIn+1)!=LF)&&(SIZE_T)pIn<(SIZE_T)FsIn)
{
WriteFile(hStdOut,pIn,TSIZE,&nWrite,NULL);
pIn++;
}
pX+=RSize;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Done.Freeallthehandlesandmaps.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
UnmapViewOfFile(pInFile);
CloseHandle(hInMap);
CloseHandle(hInFile);
UnmapViewOfFile(pXFile);
CloseHandle(hXMap);
CloseHandle(hXFile);
return0;
}
![None.gif](/Images/OutliningIndicators/None.gif)
DWORDCreateIndexFile(DWORDFsIn,LPCTSTRIdxFlNam,LPTSTRpInFile)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*PerformSteps2-3asdefinedinprogramdescription.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Thisstepwillbeskippediftheoptionsspecifyuseofanexistingindexfile.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
HANDLEhXFile;
TCHAR_based(pInFile)*pInScan=0;
DWORDnWrite;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step2a:Createanindexfile.
Donotmapityetasitslengthisnotknown.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
hXFile=CreateFile(IdxFlNam,GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL);
if(hXFile==INVALID_HANDLE_VALUE)
ReportError(_T("Failuretocreateindexfile."),8,TRUE);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step2b:Getthefirstkeyanddeterminekeysizeandstart.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
KStart=(DWORD_PTR)pInScan;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Computedstartofkeyfield.*/
while(*pInScan!=''&&*pInScan!='/t')pInScan++;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Computedendofkeyfield*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
KSize=((DWORD_PTR)pInScan-KStart)/TSIZE;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Computedkeyfieldsizeincharacters.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Step2c.Step3.Scanthecompletefile,writingkeys
andrecordpointerstothekeyfile.*/
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*TheeightbytescontaintheKeySize/KeyStart.
Thisisnecessarysothatwecanre-usetheindexfile.*/
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
WriteFile(hXFile,&KSize,sizeof(DWORD),&nWrite,NULL);
WriteFile(hXFile,&KStart,sizeof(DWORD),&nWrite,NULL);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
pInScan=/**//*(TCHAR_based(pInFile)*)*/0;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while((DWORD_PTR)pInScan<FsIn)
{
WriteFile(hXFile,pInScan+KStart,KSize*TSIZE,&nWrite,NULL);
WriteFile(hXFile,&pInScan,sizeof(LPTSTR),&nWrite,NULL);
while((DWORD)(DWORD_PTR)pInScan<FsIn&&((*pInScan!=CR)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
||(*(pInScan+1)!=LF)))
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
pInScan++;/**//*Skiptoendofline.*/
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
pInScan+=2;/**//*SkippastCR,LF.*/
}
CloseHandle(hXFile);
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*Sizeofanindividualrecord.*/
returnKSize*TSIZE+sizeof(LPTSTR);
}
![None.gif](/Images/OutliningIndicators/None.gif)
int
KeyCompare(LPCTSTRpKey1,LPCTSTRpKey2)
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/*Comparetworecordsofgenericcharacters.
Thekeypositionandlengthareglobalvariables.*/
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DWORDi;
TCHARt1,t2;
intResult=0;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(i=0;i<KSize&&Result==0;i++)
{
t1=*pKey1;
t2=*pKey2;
if(t1<t2)
Result=-1;
if(t1>t2)
Result=+1;
pKey1++;
pKey2++;
}
returnRevrs?-Result:Result;
}