《Windows系统编程》读后感(一)

读了前5章,感触最深的是作者在例子程序中对UNICODE的考虑,另外就是将windowsAPIUNIX进行对比,结合前段时间在看的《UNIX网络编程 2》,感觉对比性很强。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

印象最深刻的就是第 5 章的 3 个排序示例,将外部文件排序用内存来实现,堆,内存映射文件以及基准指针的使用让人大开眼见,不过比之《 windows 核心编程》还是有差距
None.gif #include " EvryThng.h "
None.gif
None.gif
#define KEY_SIZE8
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Structuredefinitionforatreenode.*/
None.gif
ExpandedBlockStart.gifContractedBlock.giftypedef
struct _TreeNode dot.gif {
InBlock.gif
struct_TreeNode*Left,*Right;
InBlock.gifTCHARKey[KEY_SIZE];
InBlock.gifLPTSTRpData;
ExpandedBlockEnd.gif}
TREENODE, * LPTNODE, ** LPPTNODE;
None.gif
None.gif
#define NODE_SIZEsizeof(TREENODE)
None.gif
#define NODE_HEAP_ISIZE0x8000
None.gif
#define DATA_HEAP_ISIZE0x8000
None.gif
#define MAX_DATA_LEN0x1000
None.gif
#define TKEY_SIZEKEY_SIZE*sizeof(TCHAR)
None.gif
None.gifLPTNODEFillTree(HANDLE,HANDLE,HANDLE);
None.gifBOOLScan(LPTNODE);
ExpandedBlockStart.gifContractedBlock.gif
int KeyCompare(LPCTSTR,LPCTSTR);iFile; /**/ /*foraccessinexceptionhandler*/
None.gifBOOLInsertTree(LPPTNODE,LPTNODE);
None.gif
None.gif
int _tmain( int argc,LPTSTRargv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifHANDLEhIn,hNode
=NULL,hData=NULL;
InBlock.gifLPTNODEpRoot;
InBlock.gifBOOLNoPrint;
InBlock.gifCHARErrorMessage[
256];
InBlock.gif
intiFirstFile=Options(argc,argv,_T("n"),&NoPrint,NULL);
InBlock.gif
InBlock.gif
if(argc<=iFirstFile)
InBlock.gifReportError(_T(
"Usage:sortBT[options]files"),1,FALSE);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Processallfilesonthecommandline.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(iFile=iFirstFile;iFile<argc;iFile++)__trydot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Opentheinputfile.*/
InBlock.gifhIn
=CreateFile(argv[iFile],GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
InBlock.gif
if(hIn==INVALID_HANDLE_VALUE)
InBlock.gifRaiseException(
0,0,0,NULL);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Allocatethetwoheaps.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif__try
dot.gif{
InBlock.gifhNode
=HeapCreate(
InBlock.gifHEAP_GENERATE_EXCEPTIONS
|HEAP_NO_SERIALIZE,NODE_HEAP_ISIZE,0);
InBlock.gifhData
=HeapCreate(
InBlock.gifHEAP_GENERATE_EXCEPTIONS
|HEAP_NO_SERIALIZE,DATA_HEAP_ISIZE,0);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Processtheinputfile,creatingthetree.*/
InBlock.gifpRoot
=FillTree(hIn,hNode,hData);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*DisplaythetreeinKeyorder.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(!NoPrint)dot.gif{
InBlock.gif_tprintf(_T(
"Sortedfile:%s/n"),argv[iFile]);
InBlock.gifScan(pRoot);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif}
__finallydot.gif{/**//*Heapsandfilehandlearealwaysclosed*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Destroythetwoheapsanddatastructures.*/
InBlock.gif
if(hNode!=NULL)HeapDestroy(hNode);
InBlock.gif
if(hNode!=NULL)HeapDestroy(hData);
InBlock.gifhNode
=NULL;hData=NULL;
InBlock.gif
if(hIn!=INVALID_HANDLE_VALUE)CloseHandle(hIn);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif}
/**//*Endofmainfileprocessingloopandtryblock.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif__except(EXCEPTION_EXECUTE_HANDLER)
dot.gif{
InBlock.gif_stprintf(ErrorMessage,_T(
"/n%s%s"),_T("sortBTerroronfile:"),argv[iFile]);
InBlock.gifReportError(ErrorMessage,
0,TRUE);
ExpandedSubBlockEnd.gif}

InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gifLPTNODEFillTree(HANDLEhIn,HANDLEhNode,HANDLEhData)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Scantheinputfile,creatingabinarysearchtreeinthe
ExpandedBlockEnd.gifhNodeheapwithdatapointerstothehDataheap.
*/

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Usethecallingprogram'sexceptionhandler.*/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifLPTNODEpRoot
=NULL,pNode;
InBlock.gifDWORDnRead,i;
InBlock.gifBOOLAtCR;
InBlock.gifTCHARDataHold[MAX_DATA_LEN];
InBlock.gifLPTSTRpString;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Opentheinputfile.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
while(TRUE)dot.gif{
InBlock.gifpNode
=HeapAlloc(hNode,HEAP_ZERO_MEMORY,NODE_SIZE);
InBlock.gifpNode
->pData=NULL;
InBlock.gif(pNode
->Left)=pNode->Right=NULL;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Readthekey.Returnifdone.*/
InBlock.gif
if(!ReadFile(hIn,pNode->Key,TKEY_SIZE,
InBlock.gif
&nRead,NULL)||nRead!=TKEY_SIZE)
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Assumeendoffileonerror.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
returnpRoot;/**//*Readthedatauntiltheendofline.*/
ExpandedSubBlockStart.gifContractedSubBlock.gifAtCR
=FALSE;/**//*LastcharacterwasnotaCR.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(i=0;i<MAX_DATA_LEN;i++)dot.gif{
InBlock.gifReadFile(hIn,
&DataHold[i],TSIZE,&nRead,NULL);
InBlock.gif
if(AtCR&&DataHold[i]==LF)break;
InBlock.gifAtCR
=(DataHold[i]==CR);
ExpandedSubBlockEnd.gif}

InBlock.gifDataHold[i
-1]='/0';
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*DataHoldcontainsthedatawithoutthekey.
ExpandedSubBlockEnd.gifCombinetheKeyandtheData.
*/

InBlock.gif
InBlock.gifpString
=HeapAlloc(hData,HEAP_ZERO_MEMORY,
InBlock.gif(SIZE_T)(KEY_SIZE
+_tcslen(DataHold)+1)*TSIZE);
InBlock.gifmemcpy(pString,pNode
->Key,TKEY_SIZE);
InBlock.gifpString[KEY_SIZE]
='/0';
InBlock.gif_tcscat(pString,DataHold);
InBlock.gifpNode
->pData=pString;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Insertthenewnodeintothesearchtree.*/
InBlock.gifInsertTree(
&pRoot,pNode);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif}
/**//*Endofwhile(TRUE)loop*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
returnNULL;/**//*Failure*/
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLInsertTree(LPPTNODEppRoot,LPTNODEpNode)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Insertthenewnode,pNode,intothebinarysearchtree,pRoot.*/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(*ppRoot==NULL)dot.gif{
InBlock.gif
*ppRoot=pNode;
InBlock.gif
returnTRUE;
ExpandedSubBlockEnd.gif}

InBlock.gif
if(KeyCompare(pNode->Key,(*ppRoot)->Key)<0)
InBlock.gifInsertTree(
&((*ppRoot)->Left),pNode);
InBlock.gif
else
InBlock.gifInsertTree(
&((*ppRoot)->Right),pNode);
InBlock.gif
returnTRUE;
ExpandedBlockEnd.gif}

None.gif
None.gif
int KeyCompare(LPCTSTRpKey1,LPCTSTRpKey2)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Comparetworecordsofgenericcharacters.
ExpandedBlockEnd.gifThekeypositionandlengthareglobalvariables.
*/

ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
return_tcsncmp(pKey1,pKey2,KEY_SIZE);
ExpandedBlockEnd.gif}

None.gif
None.gif
static BOOLScan(LPTNODEpNode)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Scanandprintthecontentsofabinarytree.*/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(pNode==NULL)
InBlock.gif
returnTRUE;
InBlock.gifScan(pNode
->Left);
InBlock.gif_tprintf(_T(
"%s/n"),pNode->pData);
InBlock.gifScan(pNode
->Right);
InBlock.gif
returnTRUE;
ExpandedBlockEnd.gif}

None.gif

None.gif #include " EvryThng.h "
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Definitionsoftherecordstructureinthesortfile.*/
None.gif
None.gif
#define DATALEN56
None.gif
#define KEY_SIZE8
None.gif
ExpandedBlockStart.gifContractedBlock.giftypedef
struct _RECORD dot.gif {
InBlock.gifTCHARKey[KEY_SIZE];
InBlock.gifTCHARData[DATALEN];
ExpandedBlockEnd.gif}
RECORD;
None.gif
None.gif
#define RECSIZEsizeof(RECORD)
None.giftypedefRECORD
* LPRECORD;
None.gif
int KeyCompare(LPCTSTR,LPCTSTR);
None.gif
None.gif
int _tmain( int argc,LPTSTRargv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Thefileisthefirstargument.Sortingisdoneinplace.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Sortingisdonebyfilememorymapping.*/
InBlock.gif
InBlock.gifHANDLEhFile
=INVALID_HANDLE_VALUE,hMap=NULL;
InBlock.gifHANDLEhStdOut
=GetStdHandle(STD_OUTPUT_HANDLE);
InBlock.gifLPVOIDpFile
=NULL;
InBlock.gifDWORDFsLow,Result
=2;
InBlock.gifTCHARTempFile[MAX_PATH];
InBlock.gifLPTSTRpTFile;
InBlock.gif
InBlock.gifBOOLNoPrint;
InBlock.gif
intiFirstFile;
InBlock.gif
InBlock.gifiFirstFile
=Options(argc,argv,_T("n"),&NoPrint,NULL);
InBlock.gif
InBlock.gif
if(argc<=iFirstFile)
InBlock.gifReportError(_T(
"Usage:sortFL[options]files"),1,FALSE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif_try
dot.gif{/**//*try-except*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Copytheinputfiletoatempoutputfilethatwillbesorted.
ExpandedSubBlockEnd.gifDonotaltertheinputfile.
*/

InBlock.gif
InBlock.gif_stprintf(TempFile,_T(
"%s%s"),argv[iFirstFile],_T(".tmp"));
InBlock.gif
InBlock.gifCopyFile(argv[iFirstFile],TempFile,TRUE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifResult
=1;/**//*tmpfileisnewandshouldbedeleted.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Openthefile(usethetemporarycopy).*/
InBlock.gif
InBlock.gifhFile
=CreateFile(TempFile,GENERIC_READ
InBlock.gif
|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
InBlock.gif
InBlock.gif
if(hFile==INVALID_HANDLE_VALUE)
InBlock.gifReportException(_T(
"Fileopenfailed."),2);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Getfilesize.
ExpandedSubBlockEnd.gifIfthefileistoolarge,catchthatwhenitismapped.
*/

InBlock.gif
InBlock.gifFsLow
=GetFileSize(hFile,NULL);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(FsLow==0)dot.gif{/**//*Thereisnothingtosort*/
InBlock.gifCloseHandle(hFile);
ExpandedSubBlockStart.gifContractedSubBlock.gif
return0;/**//*Youmightwanttoputoutaninformationalmessage*/
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Createafilemappingobject.
ExpandedSubBlockEnd.gifUsethefilesizebutaddspaceforthenullcharacter.
*/

InBlock.gif
InBlock.gifhMap
=CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,FsLow+TSIZE,NULL);
InBlock.gif
if(hMap==NULL)
InBlock.gifReportException(_T(
"CreateFilemapfailed."),3);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifpFile
=MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0/**//*FsLow+TSIZE*/,0);
InBlock.gif
if(pFile==NULL)
InBlock.gifReportException(_T(
"MapViewfailed"),4);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Nowsortthefile.
ExpandedSubBlockEnd.gifPerformthesortwiththeClibrary-inmappedmemory.
*/

InBlock.gif
InBlock.gifqsort(pFile,FsLow
/RECSIZE,RECSIZE,KeyCompare);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Printouttheentiresortedfile.Treatitasonesinglestring.*/
InBlock.gif
InBlock.gifpTFile
=(LPTSTR)pFile;
InBlock.gifpTFile[FsLow
/TSIZE]='/0';
InBlock.gif
if(!NoPrint)
InBlock.gifPrintMsg(hStdOut,pFile);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Indicatesuccessfulcompletion.*/
InBlock.gif
InBlock.gifResult
=0;
ExpandedSubBlockStart.gifContractedSubBlock.gifReportException(EMPTY,
5);/**//*Forceanexceptiontocleanup.*/
InBlock.gif
return0;
ExpandedSubBlockStart.gifContractedSubBlock.gif}
/**//*Endofinnertry-except.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif_except(EXCEPTION_EXECUTE_HANDLER)
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Catchanyexceptionhere.Indicateanyerror.
InBlock.gifThisisthenormaltermination.Deletethetemporary
ExpandedSubBlockEnd.giffileandFreealltheresources.
*/

InBlock.gif
InBlock.gif
if(pFile!=NULL)
InBlock.gifUnmapViewOfFile(pFile);
InBlock.gif
if(hMap!=NULL)
InBlock.gifCloseHandle(hMap);
InBlock.gif
if(hFile!=INVALID_HANDLE_VALUE)
InBlock.gifCloseHandle(hFile);
InBlock.gif
if(Result!=2)
InBlock.gifDeleteFile(TempFile);
InBlock.gif
returnResult;
ExpandedSubBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif}
/**/ /*Endof_tmain*/
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*CODEFROMHERETOENDISNOTINCLUDEDINTEXT.*/
None.gif
None.gif
int KeyCompare(LPCTSTRpKey1,LPCTSTRpKey2)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Comparetworecordsofgenericcharacters.
ExpandedBlockEnd.gifThekeypositionandlengthareglobalvariables.
*/

ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
return_tcsncmp(pKey1,pKey2,KEY_SIZE);
ExpandedBlockEnd.gif}

None.gif
None.gif

None.gif #include " EvryThng.h "
None.gif
None.gif
int KeyCompare(LPCTSTR,LPCTSTR);
None.gifDWORDCreateIndexFile(DWORD,LPCTSTR,LPTSTR);
ExpandedBlockStart.gifContractedBlock.gifDWORD_PTRKStart,KSize;
/**/ /*Keystartposition&size(TCHAR).*/
None.gifBOOLRevrs;
None.gif
None.gif
int _tmain( int argc,LPTSTRargv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Thefileisthefirstargument.Sortingisdoneinplace.*/
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Sortingisdonebyfilememorymapping.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifHANDLEhInFile,hInMap;
/**//*Inputfilehandles.*/
ExpandedSubBlockStart.gifContractedSubBlock.gifHANDLEhXFile,hXMap;
/**//*Indexfilehandles.*/
InBlock.gifHANDLEhStdOut
=GetStdHandle(STD_OUTPUT_HANDLE);
InBlock.gifBOOLIdxExists,NoPrint;
InBlock.gifDWORDFsIn,FsX,RSize,iKey,nWrite,
*pSizes;
InBlock.gifLPTSTRpInFile
=NULL;
InBlock.gifLPBYTEpXFile
=NULL,pX;
InBlock.gifTCHAR_based(pInFile)
*pIn;
InBlock.gifTCHARIdxFlNam[MAX_PATH],ChNewLine
='/n';
InBlock.gif
intFlIdx;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Determinetheoptions.*/
InBlock.gif
InBlock.gifFlIdx
=Options(argc,argv,_T("rIn"),&Revrs,&IdxExists,&NoPrint,NULL);
InBlock.gif
if(FlIdx>=argc)
InBlock.gifReportError(_T(
"Nofilenameoncommandline."),1,FALSE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step1:OpenandMaptheInputFile.*/
InBlock.gif
InBlock.gifhInFile
=CreateFile(argv[FlIdx],GENERIC_READ|GENERIC_WRITE,
InBlock.gif
0,NULL,OPEN_EXISTING,0,NULL);
InBlock.gif
if(hInFile==INVALID_HANDLE_VALUE)
InBlock.gifReportError(_T(
"Failedtoopeninputfile."),2,TRUE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Createafilemappingobject.Usethefilesize.*/
InBlock.gif
InBlock.gifhInMap
=CreateFileMapping(hInFile,NULL,PAGE_READWRITE,0,0,NULL);
InBlock.gif
if(hInMap==NULL)
InBlock.gifReportError(_T(
"Failedtocreateinputfilemapping."),3,TRUE);
InBlock.gifpInFile
=MapViewOfFile(hInMap,FILE_MAP_ALL_ACCESS,0,0,0);
InBlock.gif
if(pInFile==NULL)
InBlock.gifReportError(_T(
"Failedtomapinputfile."),4,TRUE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Getthefilesize.
ExpandedSubBlockEnd.gifAsthemappingsucceeded,thefilesizeislessthan2GB.
*/

InBlock.gif
InBlock.gifFsIn
=GetFileSize(hInFile,NULL);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Createtheindexfilename.*/
InBlock.gif
InBlock.gif_stprintf(IdxFlNam,_T(
"%s%s"),argv[FlIdx],_T(".idx"));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Steps2and3,ifnecessary.*/
InBlock.gif
InBlock.gif
if(!IdxExists)
InBlock.gifRSize
=CreateIndexFile(FsIn,IdxFlNam,pInFile);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step4.Maptheindexfile.*/
InBlock.gif
InBlock.gifhXFile
=CreateFile(IdxFlNam,GENERIC_READ|GENERIC_WRITE,
InBlock.gif
0,NULL,OPEN_EXISTING,0,NULL);
InBlock.gif
if(hXFile==INVALID_HANDLE_VALUE)
InBlock.gifReportError(_T(
"Failedtoopenexistingindexfile."),5,TRUE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Createafilemappingobject.Usethefilesize.*/
InBlock.gif
InBlock.gifhXMap
=CreateFileMapping(hXFile,NULL,PAGE_READWRITE,0,0,NULL);
InBlock.gif
if(hXMap==NULL)
InBlock.gifReportError(_T(
"Failedtocreateindexfilemapping."),6,TRUE);
InBlock.gifpXFile
=MapViewOfFile(hXMap,FILE_MAP_ALL_ACCESS,0,0,0);
InBlock.gif
if(pXFile==NULL)
InBlock.gifReportError(_T(
"Failedtomapindexfile."),7,TRUE);
InBlock.gifFsX
=GetFileSize(hXFile,NULL);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Getthekeysize/keystartandadjustthefilesizeforthe
ExpandedSubBlockEnd.gifKeySize/KeyStartfields.Computetherecordsizefromthekeysize.
*/

InBlock.gif
InBlock.gifpSizes
=(LPDWORD)pXFile;KSize=*pSizes;
InBlock.gifKStart
=*(pSizes+1);
InBlock.gifFsX
-=2*sizeof(DWORD);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step5.Sorttheindexfileusingqsort.*/
InBlock.gif
InBlock.gif
if(!IdxExists)
InBlock.gifqsort(pXFile
+2*sizeof(DWORD),FsX/RSize,RSize,KeyCompare);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step6.Outputtheinputfileinsortedorder.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Pointtotheaddressoftheinputfilestring.
ExpandedSubBlockEnd.gifStartintheInputfile.
*/

InBlock.gif
InBlock.gifpX
=pXFile+2*sizeof(DWORD)+RSize-sizeof(LPTSTR);
InBlock.gif
InBlock.gif
if(!NoPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(iKey=0;iKey<FsX/RSize;iKey++)dot.gif{
InBlock.gifWriteFile(hStdOut,
&ChNewLine,TSIZE,&nWrite,NULL);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*ThecastonpXisimportant,asitisapointertoa
ExpandedSubBlockEnd.gifbyteandweneedthefourbytesofabasedpointer.
*/

InBlock.gifpIn
=(TCHAR_based(pInFile)*)*(DWORD_PTR*)pX;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
while((*pIn!=CR||*(pIn+1)!=LF)&&(SIZE_T)pIn<(SIZE_T)FsIn)dot.gif{
InBlock.gifWriteFile(hStdOut,pIn,TSIZE,
&nWrite,NULL);
InBlock.gifpIn
++;
ExpandedSubBlockEnd.gif}

InBlock.gifpX
+=RSize;
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Done.Freeallthehandlesandmaps.*/
InBlock.gif
InBlock.gifUnmapViewOfFile(pInFile);
InBlock.gifCloseHandle(hInMap);
InBlock.gifCloseHandle(hInFile);
InBlock.gifUnmapViewOfFile(pXFile);
InBlock.gifCloseHandle(hXMap);
InBlock.gifCloseHandle(hXFile);
InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif
None.gifDWORDCreateIndexFile(DWORDFsIn,LPCTSTRIdxFlNam,LPTSTRpInFile)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*PerformSteps2-3asdefinedinprogramdescription.*/
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Thisstepwillbeskippediftheoptionsspecifyuseofanexistingindexfile.*/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifHANDLEhXFile;
InBlock.gifTCHAR_based(pInFile)
*pInScan=0;
InBlock.gifDWORDnWrite;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step2a:Createanindexfile.
ExpandedSubBlockEnd.gifDonotmapityetasitslengthisnotknown.
*/

InBlock.gif
InBlock.gifhXFile
=CreateFile(IdxFlNam,GENERIC_READ|GENERIC_WRITE,
InBlock.gifFILE_SHARE_READ,NULL,CREATE_ALWAYS,
0,NULL);
InBlock.gif
if(hXFile==INVALID_HANDLE_VALUE)
InBlock.gifReportError(_T(
"Failuretocreateindexfile."),8,TRUE);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step2b:Getthefirstkeyanddeterminekeysizeandstart.*/
InBlock.gif
InBlock.gifKStart
=(DWORD_PTR)pInScan;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Computedstartofkeyfield.*/
InBlock.gif
while(*pInScan!=''&&*pInScan!='/t')pInScan++;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Computedendofkeyfield*/
InBlock.gif
InBlock.gifKSize
=((DWORD_PTR)pInScan-KStart)/TSIZE;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Computedkeyfieldsizeincharacters.*/
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Step2c.Step3.Scanthecompletefile,writingkeys
ExpandedSubBlockEnd.gifandrecordpointerstothekeyfile.
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*TheeightbytescontaintheKeySize/KeyStart.
ExpandedSubBlockEnd.gifThisisnecessarysothatwecanre-usetheindexfile.
*/

InBlock.gif
InBlock.gifWriteFile(hXFile,
&KSize,sizeof(DWORD),&nWrite,NULL);
InBlock.gifWriteFile(hXFile,
&KStart,sizeof(DWORD),&nWrite,NULL);
ExpandedSubBlockStart.gifContractedSubBlock.gifpInScan
=/**//*(TCHAR_based(pInFile)*)*/0;
ExpandedSubBlockStart.gifContractedSubBlock.gif
while((DWORD_PTR)pInScan<FsIn)dot.gif{
InBlock.gifWriteFile(hXFile,pInScan
+KStart,KSize*TSIZE,&nWrite,NULL);
InBlock.gifWriteFile(hXFile,
&pInScan,sizeof(LPTSTR),&nWrite,NULL);
InBlock.gif
while((DWORD)(DWORD_PTR)pInScan<FsIn&&((*pInScan!=CR)
ExpandedSubBlockStart.gifContractedSubBlock.gif
||(*(pInScan+1)!=LF)))dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gifpInScan
++;/**//*Skiptoendofline.*/
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gifpInScan
+=2;/**//*SkippastCR,LF.*/
ExpandedSubBlockEnd.gif}

InBlock.gifCloseHandle(hXFile);
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*Sizeofanindividualrecord.*/
InBlock.gif
returnKSize*TSIZE+sizeof(LPTSTR);
ExpandedBlockEnd.gif}

None.gif
None.gif
int KeyCompare(LPCTSTRpKey1,LPCTSTRpKey2)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*Comparetworecordsofgenericcharacters.
ExpandedBlockEnd.gifThekeypositionandlengthareglobalvariables.
*/

ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifDWORDi;
InBlock.gifTCHARt1,t2;
InBlock.gif
intResult=0;
ExpandedSubBlockStart.gifContractedSubBlock.gif
for(i=0;i<KSize&&Result==0;i++)dot.gif{
InBlock.gift1
=*pKey1;
InBlock.gift2
=*pKey2;
InBlock.gif
if(t1<t2)
InBlock.gifResult
=-1;
InBlock.gif
if(t1>t2)
InBlock.gifResult
=+1;
InBlock.gifpKey1
++;
InBlock.gifpKey2
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
returnRevrs?-Result:Result;
ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值