windows实用命令集合

 windows实用命令集合     -|天地一沙鸥 发表于 2005-4-21 11:03:00

 


    winver---------检查windows版本
    wmimgmt.msc----打开windows管理体系结构(wmi)
    wupdmgr--------windows更新程序
    wscript--------windows脚本宿主设置
    write----------写字板
    winmsd---------系统信息
    wiaacmgr-------扫描仪和照相机向导
    winchat--------xp自带局域网聊天
    mem.exe--------显示内存使用情况
    msconfig.exe---系统配置实用程序
    mplayer2-------简易widnows media player
    mspaint--------画图板
    mstsc----------远程桌面连接
    mplayer2-------媒体播放机
    magnify--------放大镜实用程序
    mmc------------打开控制台
    mobsync--------同步命令

 

------------------------------------------------------------------------------

     dxdiag---------检查directx信息
    drwtsn32------ 系统医生
    devmgmt.msc--- 设备管理器
    dfrg.msc-------磁盘碎片整理程序
    diskmgmt.msc---磁盘管理实用程序
    dcomcnfg-------打开系统组件服务
    ddeshare-------打开dde共享设置
    dvdplay--------dvd播放器

 

--------------------------------------------------------------------------------

    net stop messenger-----停止信使服务
    net start messenger----开始信使服务
    notepad--------打开记事本
    nslookup-------网络管理的工具向导
    ntbackup-------系统备份和还原
    narrator-------屏幕"讲述人"
    ntmsmgr.msc----移动存储管理器
    ntmsoprq.msc---移动存储管理员操作请求
    netstat -an----(tc)命令检查接口

 

--------------------------------------------------------------------------------

    syncapp--------创建一个公文包
    sysedit--------系统配置编辑器
    sigverif-------文件签名验证程序
    sndrec32-------录音机
    shrpubw--------创建共享文件夹
    secpol.msc-----本地安全策略
    syskey---------系统加密,一旦加密就不能解开,保护windows xp系统的双重密码
    services.msc---本地服务设置
    sndvol32-------音量控制程序
    sfc.exe--------系统文件检查器
    sfc /scannow---windows文件保护

 

--------------------------------------------------------------------------------

    tsshutdn-------60秒倒计时关机命令
    tourstart------xp简介(安装完成后出现的漫游xp程序)
    taskmgr--------任务管理器

 

--------------------------------------------------------------------------------

    eventvwr-------事件查看器
    eudcedit-------造字程序
    explorer-------打开资源管理器

 

--------------------------------------------------------------------------------

    packager-------对象包装程序
    perfmon.msc----计算机性能监测程序
    progman--------程序管理器

 

--------------------------------------------------------------------------------

    regedit.exe----注册表
    rsop.msc-------组策略结果集
    regedt32-------注册表编辑器
    rononce -p ----15秒关机
    regsvr32 /u *.dll----停止dll文件运行
    regsvr32 /u zipfldr.dll------取消zip支持

 

--------------------------------------------------------------------------------

    cmd.exe--------cmd命令提示符
    chkdsk.exe-----chkdsk磁盘检查
    certmgr.msc----证书管理实用程序
    calc-----------启动计算器
    charmap--------启动字符映射表
    cliconfg-------sql server 客户端网络实用程序
    clipbrd--------剪贴板查看器
    conf-----------启动netmeeting
    compmgmt.msc---计算机管理
    cleanmgr-------垃圾整理
    ciadv.msc------索引服务程序

 


--------------------------------------------------------------------------------

    osk------------打开屏幕键盘
    odbcad32-------odbc数据源管理器
    oobe/msoobe /a----检查xp是否激活
    lusrmgr.msc----本机用户和组
    logoff---------注销命令 

 


--------------------------------------------------------------------------------

    iexpress-------木马捆绑工具,系统自带

 

--------------------------------------------------------------------------------

    nslookup-------ip地址侦测器

 

--------------------------------------------------------------------------------

    fsmgmt.msc-----共享文件夹管理器

 

--------------------------------------------------------------------------------

 

     utilman--------辅助工具管理器


--------------------------------------------------------------------------------

     gpedit.msc-----组策略

 


 
[阅读全文 | 回复 | 引用通告] 

--------------------------------------------------------------------------------
 
·[实用技巧]利用google突破各种封锁来下载你要的东西     -|天地一沙鸥 发表于 2005-4-21 10:51:00 
 

在搜索框上输入: "index of/ "  inurl:lib

再按搜索你将进入许多图书馆,并且一定能下载自己喜欢的书籍。

 

在搜索框上输入: "index of /"  cnki

再按搜索你就可以找到许多图书馆的CNKI、VIP、超星等入口!

 

在搜索框上输入: "index of /"  ppt

再按搜索你就可以突破网站入口下载powerpint作品!

 

在搜索框上输入: "index of /"  mp3

再按搜索你就可以突破网站入口下载mp3、rm等影视作品!

 

在搜索框上输入: "index of /"  swf

再按搜索你就可以突破网站入口下载flash作品!

 

在搜索框上输入: "index of /"  要下载的软件名

再按搜索你就可以突破网站入口下载软件!

 

注意引号应是英文的!

 

再透露一下,如果你输入:

 

"index of /"  AVI

 

你会找到什么呢?同理,把AVI换为MPEG看看又会找到什么呢?呵呵!接下来不用我再教了吧?
 
[阅读全文 | 回复 | 引用通告] 

--------------------------------------------------------------------------------
 
·[AI]一点遗传算法代码(转)     -|天地一沙鸥 发表于 2005-4-21 10:37:00

下面是我前年做的GACS(连续空间的遗传算法),没有用到特殊算子。
另外还有一个曲线拟合的简单例子,可以参考使用方法。
#ifndef _GACS_H
#define _GACS_H

#i nclude <stdio.h>
#i nclude <stdlib.h>

typedef float ALLELE;

typedef struct{
     ALLELE *chrom;
     float   fitness;           // fitness of Chromosome
}INDIVIDUAL;

class TPopulation{
   public:
      INDIVIDUAL *pop;
      int   size;               // Size of population
      int   lchrom;             // Length of chromosome
      float sumfitness,average;
      INDIVIDUAL *fmin,*fmax;

   TPopulation(int popsize,int strlength);
   ~TPopulation();

   INDIVIDUAL* CreatIndividual();
   void   ClrIndividual(INDIVIDUAL *p);
   void   InitIndividual(INDIVIDUAL *p=NULL);
   inline INDIVIDUAL &Individual(int i){ return pop[i];};
   void   FillFitness();
//   virtual void   FillFitness();
//   virtual void  ObjectFunction(INDIVIDUAL &x)=0; // Object's fitness
function

   virtual void Statistics();
};

class TGACS : public TPopulation{
   public:
      float pcross;             // Pobobility of Crossover
      float pmutation;          // Pobobility of Mutation
      int gen;                  // Counter of generation

   TGACS(int size,int strlength,float pm=0.0333,float pc=0.6):
      TPopulation(size,strlength)
      {gen=0; pcross=pc; pmutation=pm;  } ;
   virtual INDIVIDUAL& Select();
   virtual void  Crossover(INDIVIDUAL &parent1,INDIVIDUAL &parent2,
                           INDIVIDUAL &child1, INDIVIDUAL &child2);
   virtual ALLELE Mutation(ALLELE alleleval);
//   virtual void  ObjectFunction(INDIVIDUAL &x)=0; // Object's fitness
function
   virtual void   Generate();
};

extern void  ObjectFunction(INDIVIDUAL &x); // Object's fitness function
inline float random();
inline int   flip(float p);

#endif
/********************************************************************
   This Program is a Simple Genetic Algrithm according to
     Genetic Algrithm in search, Optimization & Machine Learning
     Written by David E. Goldberg,1989

     Author: Wang Jun,             1994.10
                Robot Lab. A.C. dept. HUST
*********************************************************************/

#i nclude <mem.h>
#i nclude "gacs.h"

TPopulation::TPopulation(int popsize,int strlength)
{
   pop=NULL;
   size=popsize;  lchrom=strlength;
   average=0;
   randomize();
  InitIndividual();
}

TPopulation::~TPopulation()
{
   ClrIndividual(pop);
}

void TPopulation::ClrIndividual(INDIVIDUAL *p)
{
   for(int i=0;i<size;i++)      delete p[i].chrom;
   delete  p;
}

INDIVIDUAL *TPopulation::CreatIndividual()
{
   INDIVIDUAL *p;

   p=new INDIVIDUAL[size+1];
   for(int i=0;i<size;i++){
      p[i].chrom=new ALLELE[lchrom+1];
      p[i].chrom[lchrom]=0;
   }
   return p;
}

void TPopulation::InitIndividual(INDIVIDUAL *p)
{
   int i,j;

   if(pop)    ClrIndividual(pop);
   if(p==NULL){
      pop=CreatIndividual();
      for(i=0;i<size;i++)
         for(j=0;j<lchrom;j++)
            Individual(i).chrom[j]=random();
   }
   else pop=p;
   FillFitness();
   Statistics();
}

/*  Statistic of population
 *    To get the sum & average of fitness
 *       and the max & min fitness indivdual
 */
void  TPopulation::Statistics()
{
   fmin=&Individual(0);
   fmax=&Individual(0);
   sumfitness=Individual(0).fitness;
   for(int j=1;j<size;j++){
      INDIVIDUAL &x=Individual(j);
      if(x.fitness>fmax->fitness) fmax=&x;      // To find the maxfitness
one
      if(x.fitness<fmin->fitness) fmin=&x;      // To find the minfitness
one
      sumfitness += x.fitness;
   }
   average=sumfitness/size;
}

void TPopulation::FillFitness()
{
   for(int i=0;i<size;i++) ObjectFunction(Individual(i));
}

/*  Select a single INDIVIDUAL via roulette wheel selection
 *    through a roulette with a slot
 */
INDIVIDUAL &TGACS::Select()
{
   float rnd,partsum;
   int j;

   rnd = random()*sumfitness;
   for(partsum=0.0,j=0;j<size;j++){
      partsum += Individual(j).fitness;
      if(partsum>=rnd) break;
   }
   return Individual(j);
}

// cross 2 parent strings, place in 2 child strings
void  TGACS::Crossover(INDIVIDUAL &parent1,INDIVIDUAL &parent2,
                             INDIVIDUAL &child1, INDIVIDUAL &child2)
{
   int j,jcross;

   if(flip(pcross))   jcross=random(lchrom-1);
  else jcross=lchrom;
   for(j=0;j<jcross;j++){
      child1.chrom[j]=Mutation(parent1.chrom[j]);
      child2.chrom[j]=Mutation(parent2.chrom[j]);
   }
   for(;j<lchrom;j++){
      child1.chrom[j]=Mutation(parent2.chrom[j]);
      child2.chrom[j]=Mutation(parent1.chrom[j]);
   }
}

ALLELE TGACS::Mutation(ALLELE alleleval)
{
   if(!flip(pmutation))  return alleleval;
   float t=alleleval+( (random(2)) ? (random()/2) : (-random()/2) );
   if(t>1) t=1;
   if(t<0) t=0;
   return    t;
}

/*
 Creat next generation
     Select -->  Crossover  -->  Mutation
     *** Keep 2 copies of the best one
 */
void TGACS::Generate()
{
   INDIVIDUAL *newpop;
   ALLELE t;
   newpop=CreatIndividual();
   memcpy(newpop[0].chrom,fmax->chrom,(sizeof t)*lchrom);
   memcpy(newpop[1].chrom,fmax->chrom,(sizeof t)*lchrom);
   for(int i=2;i<size;i+=2){
      INDIVIDUAL &mate1=Select();
      INDIVIDUAL &mate2=Select();
      Crossover(mate1,mate2,newpop[i],newpop[i+1]);
   }
   InitIndividual(newpop);
   gen++;
}

/* return a random float between 0 and 1
 */
inline float random()
/* return a random float between 0 and 1
 */
inline float random()
{
   return rand()/(float)RAND_MAX;
}

/* return TRUE by probobility of p
 */
inline int flip(float p)
{
   return random()<p;
}

GACS -- Genetic Algorithms in Continuous Space
----------------------------------------------
      Worked by Wang Jun, 1994, ROBOT LAB HUST


Structure:
----------
        ALLELE          float;          parameter
        chrom           ALLELE[lchrom];
        INDIVIDUAL      chrom,fitness;
Class:
------
        TPOPULATION     INDIVIDUAL[size];
                        FillFitness();
                        Statistics();
        TGACS           TPOPULATION;
                        Select();
                        Crossover();
                       Mutation();
                        Generate();
        TGACSUser       TGACS;
                        decode();
                        ObjectFunction();


Notes:
------
1. Every ALLELE means 1 parameter and regulized to [0,1].
2. When Crossover, keep 2 copies of the best individual.


 
[阅读全文 | 回复 | 引用通告] 

--------------------------------------------------------------------------------
 
·[VC++(MFC)]几个 Microsoft-Specific Predefined Macros     -|天地一沙鸥 发表于 2005-4-17 21:41:00

Table 1.2   Microsoft-Specific Predefined Macros

Macro Description
_CHAR_UNSIGNED Default char type is unsigned. Defined when /J is specified. 
__cplusplus Defined for C++ programs only.
_CPPRTTI Defined for code compiled with /GR (Enable Run-Time Type Information).
_CPPUNWIND Defined for code compiled with /GX (Enable Exception Handling).
_DLL Defined when /MD or /MDd (Multithread DLL) is specified.
_M_ALPHA Defined for DEC ALPHA platforms. It is defined as 1 by the ALPHA compiler, and it is not defined if another compiler is used.
_M_IX86 Defined for x86 processors. See Table 1.3 for more details.
_M_MPPC Defined for Power Macintosh platforms. Default is 601 (/QP601). See Table 1.4 for more details.
_M_MRX000 Defined for MIPS platforms. Default is 4000 (/QMR4000). See Table 1.5 for more details.
_M_PPC Defined for PowerPC platforms. Default is 604 (/QP604). See Table 1.6 for more details.
_MFC_VER Defines the MFC version. Defined as 0x0421 for Microsoft Foundation Class Library 4.21. Always defined.
_MSC_EXTENSIONS This macro is defined when compiling with the /Ze compiler option (the default).  Its value, when defined, is 1.
_MSC_VER Defines the compiler version. Defined as 1200 for Microsoft Visual C++ 6.0. Always defined.
_MT Defined when /MD or /MDd (Multithreaded DLL) or /MT or /MTd (Multithreaded) is specified. 
_WIN32 Defined for applications for Win32®. Always defined. 


As shown in following tables, the compiler generates a value for the preprocessor identifiers that reflect the processor option specified.

Form MSDN 
[阅读全文 | 回复 | 引用通告] 

--------------------------------------------------------------------------------
 
·[Codes]获得CPU的信息     -|天地一沙鸥 发表于 2005-4-17 21:38:00

#i nclude <conio.h>
#i nclude <iostream>

#pragma hdrstop
#pragma inline
//#pragma argsused

using namespace std;

int main()
{
 char OEMString[13];

 int iEAXValue, iEBXValue, iECXValue, iEDXValue;

 _asm
 {
  mov eax, 0
  cpuid
  mov DWORD PTR OEMString, ebx
  mov DWORD PTR OEMString + 4, edx
  mov DWORD PTR OEMString + 8, ecx
  mov BYTE PTR OEMString + 12, 0
 }

 cout << "This CPU's OEM String is:"
   << OEMString
   << endl;

 _asm
 {
  mov eax, 1
  cpuid
  mov iEAXValue, eax
  mov iEBXValue, ebx
  mov iECXValue, ecx
  mov iEDXValue, edx
 }

 if(iEDXValue & 0x800000)
  cout << "This is MMX CPU"
    << endl;

 int iCPUFamily = (0xf00 & iEAXValue) >> 8;
 
 cout << "CPU Family is:"
   << iCPUFamily
   << endl;

 _asm
 {
  mov eax, 2
  cpuid
 }

 return 0;
}
 
[阅读全文 | 回复 | 引用通告] 

--------------------------------------------------------------------------------
 
·[VC++(MFC)]FMD开发文集 -- CArchive原理     -|天地一沙鸥 发表于 2005-4-17 1:06:00 
 
   
 

 

FMD开发文集 -- CArchive原理
作者:冯明德

MFC 提供CArchive类实现数据的缓冲区读写,同时定义了类对象的存储与读取方案。 以下对CArchvie 的内部实现作分析。

1.概述
2.内部数据
3.基本数据读写
4.缓冲区的更新
5.指定长度数据段落的读写
6.字符串的读写
7.CObject派生对象的读写

 一.概述

CArchive使用了缓冲区,即一段内存空间作为临时数据存储地,对CArchive的读写都先依次排列到此缓冲区,当缓冲区满或用户要求时,将此段整理后的数据读写到指定的存储煤质。
当建立CArchive对象时,应指定其模式是用于缓冲区读,还是用于缓冲区写。
可以这样理解,CArchive对象相当于铁路的货运练调度站,零散的货物被收集,当总量到达火车运量的时候,由火车装运走。
当接到火车的货物时,则货物由被分散到各自的货主。与货运不同的是,交货、取货是按时间循序执行的,而不是凭票据。因此必须保证送货的和取货的货主按同样的循序去存或取。
对于大型的货物,则是拆散成火车单位,运走,取货时,依次取各部分,组装成原物。

 二.内部数据
缓冲区指针 BYTE* m_lpBufStart,指向缓冲区,这个缓冲区有可能是底层CFile(如派生类CMemFile)对象提供的,但一般是CArchive自己建立的。
缓冲区尾部指针 BYTE* m_lpBufMax;
缓冲区当前位置指针 BYTE* m_lpBufCur;
初始化时,如果是读模式,当前位置在尾部,如果是写模式,当前位置在头部:

m_lpBufCur = (IsLoading()) ? m_lpBufMax : m_lpBufStart;
 三.基本数据读写
对于基本的数据类型,例如字节、双字等,可以直接使用">>"、"<<"符号进行读出、写入。

//操作符定义捕:
 
//插入操作
CArchive& operator<<(BYTE by);
CArchive& operator<<(WORD w);
CArchive& operator<<(LONG l);
CArchive& operator<<(DWORD dw);
CArchive& operator<<(float f);
CArchive& operator<<(double d);
CArchive& operator<<(int i);
CArchive& operator<<(short w);
CArchive& operator<<(char ch);
CArchive& operator<<(unsigned u);

//提取操作
CArchive& operator>>(BYTE& by);
CArchive& operator>>(WORD& w);
CArchive& operator>>(DWORD& dw);
CArchive& operator>>(LONG& l);
CArchive& operator>>(float& f);
CArchive& operator>>(double& d);

CArchive& operator>>(int& i);
CArchive& operator>>(short& w);
CArchive& operator>>(char& ch);
CArchive& operator>>(unsigned& u);
下面以双字为例,分析原码
双字的插入(写)

CArchive& CArchive::operator<<(DWORD dw)
{
 if (m_lpBufCur + sizeof(DWORD) > m_lpBufMax) //缓冲区空间不够
  Flush();  //缓冲区内容提交到实际存储煤质。

 if (!(m_nMode & bNoByteSwap))
  _AfxByteSwap(dw, m_lpBufCur);  //处理字节顺序
 else
  *(DWORD*)m_lpBufCur = dw;      //添入缓冲区

 m_lpBufCur += sizeof(DWORD);     //移动当前指针
 return *this;
}

双字的提取(读)
CArchive& CArchive::operator>>(DWORD& dw)
{
 if (m_lpBufCur + sizeof(DWORD) > m_lpBufMax) //缓冲区要读完了
  FillBuffer(sizeof(DWORD) - (UINT)(m_lpBufMax - m_lpBufCur));  //重新读入内容到缓冲区

 dw = *(DWORD*)m_lpBufCur;  //读取双字
 m_lpBufCur += sizeof(DWORD); //移动当前位置指针

 if (!(m_nMode & bNoByteSwap))
  _AfxByteSwap(dw, (BYTE*)&dw);  //处理字节顺序
 return *this;
}

 四.缓冲区的更新

以上操作中,当缓冲区将插入满或缓冲区将提取空时,都将对缓冲区进行更新处理。

缓冲区将插入满时调用Flush();
void CArchive::Flush()
{
 ASSERT_VALID(m_pFile);
 ASSERT(m_bDirectBuffer || m_lpBufStart != NULL);
 ASSERT(m_bDirectBuffer || m_lpBufCur != NULL);
 ASSERT(m_lpBufStart == NULL ||
  AfxIsValidAddress(m_lpBufStart, m_lpBufMax - m_lpBufStart, IsStoring()));
 ASSERT(m_lpBufCur == NULL ||
  AfxIsValidAddress(m_lpBufCur, m_lpBufMax - m_lpBufCur, IsStoring()));

 if (IsLoading())
 {
  // unget the characters in the buffer, seek back unused amount
  if (m_lpBufMax != m_lpBufCur)
   m_pFile-> Seek(-(m_lpBufMax - m_lpBufCur), CFile::current);
  m_lpBufCur = m_lpBufMax;    // 指向尾
 }
 else   //写模式
 {
  if (!m_bDirectBuffer)
  {
   // 内容写入到文件
   if (m_lpBufCur != m_lpBufStart)
    m_pFile-> Write(m_lpBufStart, m_lpBufCur - m_lpBufStart);
  }
  else
  {
   //如果是直接针对内存区域的的(例如CMemFile中) (只需移动相关指针,指向新的一块内存)
   if (m_lpBufCur != m_lpBufStart)
    m_pFile-> GetBufferPtr(CFile::bufferCommit, m_lpBufCur - m_lpBufStart);
   // get next buffer
   VERIFY(m_pFile-> GetBufferPtr(CFile::bufferWrite, m_nBufSize,
    (void**)&m_lpBufStart, (void**)&m_lpBufMax) == (UINT)m_nBufSize);
   ASSERT((UINT)m_nBufSize == (UINT)(m_lpBufMax - m_lpBufStart));
  }
  m_lpBufCur = m_lpBufStart; //指向缓冲区首
 }
}
缓冲区将提取空,会调用FillBuffer。 nBytesNeeded为当前剩余部分上尚有用的字节
void CArchive::FillBuffer(UINT nBytesNeeded)
{
 ASSERT_VALID(m_pFile);
 ASSERT(IsLoading());
 ASSERT(m_bDirectBuffer || m_lpBufStart != NULL);
 ASSERT(m_bDirectBuffer || m_lpBufCur != NULL);
 ASSERT(nBytesNeeded > 0);
 ASSERT(nBytesNeeded <= (UINT)m_nBufSize);
 ASSERT(m_lpBufStart == NULL ||
  AfxIsValidAddress(m_lpBufStart, m_lpBufMax - m_lpBufStart, FALSE));
 ASSERT(m_lpBufCur == NULL ||
  AfxIsValidAddress(m_lpBufCur, m_lpBufMax - m_lpBufCur, FALSE));

 UINT nUnused = m_lpBufMax - m_lpBufCur;
 ULONG nTotalNeeded = ((ULONG)nBytesNeeded) + nUnused;

 // 从文件中读取
 if (!m_bDirectBuffer)
 {
  ASSERT(m_lpBufCur != NULL);
  ASSERT(m_lpBufStart != NULL);
  ASSERT(m_lpBufMax != NULL);

  if (m_lpBufCur > m_lpBufStart)
  {
   //保留剩余的尚未处理的部分,将它们移动到头
   if ((int)nUnused > 0)
   {
    memmove(m_lpBufStart, m_lpBufCur, nUnused);
    m_lpBufCur = m_lpBufStart;
    m_lpBufMax = m_lpBufStart + nUnused;
   }

   // read to satisfy nBytesNeeded or nLeft if possible
   UINT nRead = nUnused;
   UINT nLeft = m_nBufSize-nUnused;
   UINT nBytes;
   BYTE* lpTemp = m_lpBufStart + nUnused;
   do
   {
    nBytes = m_pFile-> Read(lpTemp, nLeft);
    lpTemp = lpTemp + nBytes;
    nRead += nBytes;
    nLeft -= nBytes;
   }
   while (nBytes > 0 && nLeft > 0 && nRead < nBytesNeeded);

   m_lpBufCur = m_lpBufStart;
   m_lpBufMax = m_lpBufStart + nRead;
  }
 }
 else
 {
  // 如果是针对内存区域(CMemFile),移动相关指针,指向新的一块内存
  if (nUnused != 0)
   m_pFile-> Seek(-(LONG)nUnused, CFile::current);
  UINT nActual = m_pFile-> GetBufferPtr(CFile::bufferRead, m_nBufSize,
   (void**)&m_lpBufStart, (void**)&m_lpBufMax);
  ASSERT(nActual == (UINT)(m_lpBufMax - m_lpBufStart));
  m_lpBufCur = m_lpBufStart;
 }

 // not enough data to fill request?
 if ((ULONG)(m_lpBufMax - m_lpBufCur) < nTotalNeeded)
  AfxThrowArchiveException(CArchiveException::endOfFile);
}

 五.指定长度数据段落的读写

以下分析
UINT Read(void* lpBuf, UINT nMax); 读取长度为nMax的数据
void Write(const void* lpBuf, UINT nMax); 写入指定长度nMax的数据
对于大段数据的读写,先使用当前缓冲区中的内容或空间读取或写入,若这些空间够用了,则结束。
否则,从剩余的数据中找出最大的缓冲区整数倍大小的一块数据,直接读写到存储煤质(不反复使用缓冲区)。
剩余的余数部分,再使用缓冲区读写。
(说明:缓冲区读写的主要目的是将零散的数据以缓冲区大小为尺度来处理。对于大型数据,其中间的部分,不是零散的数据,使用缓冲区已经没有意思,故直接读写)
①读取

UINT CArchive::Read(void* lpBuf, UINT nMax)
{
 ASSERT_VALID(m_pFile);
 if (nMax == 0)
  return 0;

 UINT nMaxTemp = nMax;  //还需要读入的长度,读入一部分,就减相应数值,直到此数值变为零
 
 //处理当前缓冲区中剩余部分。
 //如果要求读入字节小于缓冲区中剩余部分,则第一部分为要求读入的字节数,
 //否则读入全部剩余部分 
 UINT nTemp = min(nMaxTemp, (UINT)(m_lpBufMax - m_lpBufCur));  
 memcpy(lpBuf, m_lpBufCur, nTemp);
 m_lpBufCur += nTemp;
 lpBuf = (BYTE*)lpBuf + nTemp; //移动读出内容所在区域的指针
 nMaxTemp -= nTemp;

 //当前缓冲区中剩余部分不够要求读入的长度。
 //还有字节需要读,则需要根据需要执行若干次填充缓冲区,读出,直到读出指定字节。
 if (nMaxTemp != 0) 
 {
  //计算出去除尾数部分的字节大小(整数个缓冲区大小)
  //对于这些部分,字节从文件对象中读出,放到输出缓冲区
  nTemp = nMaxTemp - (nMaxTemp % m_nBufSize); 
  UINT nRead = 0;

  UINT nLeft = nTemp;
  UINT nBytes;
  do
  {
   nBytes = m_pFile-> Read(lpBuf, nLeft); //要求读入此整数缓冲区部分大小
   lpBuf = (BYTE*)lpBuf + nBytes;
   nRead += nBytes;
   nLeft -= nBytes;
  }
  while ((nBytes > 0) && (nLeft > 0)); 知道读入了预定大小,或到达文件尾

  nMaxTemp -= nRead;

  if (nRead == nTemp) //读入的字节等于读入的整数倍部分  该读最后的余数部分了
  {
   // 建立装有此最后余数部分的内容的CArchive的工作缓冲区。
   if (!m_bDirectBuffer)
   {
    UINT nLeft = max(nMaxTemp, (UINT)m_nBufSize);
    UINT nBytes;
    BYTE* lpTemp = m_lpBufStart;
    nRead = 0;
    do
    {
     nBytes = m_pFile-> Read(lpTemp, nLeft);  //从文件中读入到CArchive缓冲区
     lpTemp = lpTemp + nBytes;
     nRead += nBytes;
     nLeft -= nBytes;
    }
    while ((nBytes > 0) && (nLeft > 0) && nRead < nMaxTemp);

    m_lpBufCur = m_lpBufStart;
    m_lpBufMax = m_lpBufStart + nRead;
   }
   else
   {
    nRead = m_pFile-> GetBufferPtr(CFile::bufferRead, m_nBufSize,
     (void**)&m_lpBufStart, (void**)&m_lpBufMax);
    ASSERT(nRead == (UINT)(m_lpBufMax - m_lpBufStart));
    m_lpBufCur = m_lpBufStart;
   }

   //读出此剩余部分到输出
   nTemp = min(nMaxTemp, (UINT)(m_lpBufMax - m_lpBufCur));
   memcpy(lpBuf, m_lpBufCur, nTemp);
   m_lpBufCur += nTemp;
   nMaxTemp -= nTemp;
  }
  
 }
 return nMax - nMaxTemp;
}

②保存,写入
void CArchive::Write(const void* lpBuf, UINT nMax)
{
 if (nMax == 0)
  return;
 
 //读入可能的部分到缓冲区当前的剩余部分 
 UINT nTemp = min(nMax, (UINT)(m_lpBufMax - m_lpBufCur));
 memcpy(m_lpBufCur, lpBuf, nTemp);
 m_lpBufCur += nTemp;
 lpBuf = (BYTE*)lpBuf + nTemp;
 nMax -= nTemp;

 if (nMax > 0)  //还有未写入的部分
 {
  Flush();    //将当前缓冲区写入到存储煤质

  //计算出整数倍缓冲区大小的字节数
  nTemp = nMax - (nMax % m_nBufSize);
  m_pFile-> Write(lpBuf, nTemp);  //直接写到文件
  lpBuf = (BYTE*)lpBuf + nTemp;
  nMax -= nTemp;


  //剩余部分添加到缓冲区
  if (m_bDirectBuffer)
  {
   // sync up direct mode buffer to new file position
   VERIFY(m_pFile-> GetBufferPtr(CFile::bufferWrite, m_nBufSize,
    (void**)&m_lpBufStart, (void**)&m_lpBufMax) == (UINT)m_nBufSize);
   ASSERT((UINT)m_nBufSize == (UINT)(m_lpBufMax - m_lpBufStart));
   m_lpBufCur = m_lpBufStart;
  }

  // copy remaining to active buffer
  ASSERT(nMax < (UINT)m_nBufSize);
  ASSERT(m_lpBufCur == m_lpBufStart);
  memcpy(m_lpBufCur, lpBuf, nMax);
  m_lpBufCur += nMax;
 }
}

 六.字符串的读写

①CArchive提供的WriteString和ReadString

字符串写
void CArchive::WriteString(LPCTSTR lpsz)
{
 ASSERT(AfxIsValidString(lpsz));
 Write(lpsz, lstrlen(lpsz) * sizeof(TCHAR));  //调用Write,将字符串对应的一段数据写入
}

字符串读(读取一行字符串)
LPTSTR CArchive::ReadString(LPTSTR lpsz, UINT nMax)
{
 // if nMax is negative (such a large number doesn''t make sense given today''s
 // 2gb address space), then assume it to mean "keep the newline".
 int nStop = (int)nMax < 0 ? -(int)nMax : (int)nMax;
 ASSERT(AfxIsValidAddress(lpsz, (nStop+1) * sizeof(TCHAR)));

 _TUCHAR ch;
 int nRead = 0;

 TRY
 {
  while (nRead < nStop)
  {
   *this >> ch;  //读出一个字节

   // stop and end-of-line (trailing ''/n'' is ignored)  遇换行—回车
   if (ch == ''/n'' || ch == ''/r'')
   {
    if (ch == ''/r'')
     *this >> ch;
    // store the newline when called with negative nMax
    if ((int)nMax != nStop)
     lpsz[nRead++] = ch;
    break;
   }
   lpsz[nRead++] = ch;
  }
 }
 CATCH(CArchiveException, e)
 {
  if (e-> m_cause == CArchiveException::endOfFile)
  {
   DELETE_EXCEPTION(e);
   if (nRead == 0)
    return NULL;
  }
  else
  {
   THROW_LAST();
  }
 }
 END_CATCH

 lpsz[nRead] = ''/0'';
 return lpsz;
}

ReadString到CString对象,可以多行字符
BOOL CArchive::ReadString(CString& rString)
{
 rString = &afxChNil;    // empty string without deallocating
 const int nMaxSize = 128;
 LPTSTR lpsz = rString.GetBuffer(nMaxSize);
 LPTSTR lpszResult;
 int nLen;
 for (;;)
 {
  lpszResult = ReadString(lpsz, (UINT)-nMaxSize); // store the newline
  rString.ReleaseBuffer();

  // if string is read completely or EOF
  if (lpszResult == NULL ||
   (nLen = lstrlen(lpsz)) < nMaxSize ||
   lpsz[nLen-1] == ''/n'')
  {
   break;
  }

  nLen = rString.GetLength();
  lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
 }

 // remove ''/n'' from end of string if present
 lpsz = rString.GetBuffer(0);
 nLen = rString.GetLength();
 if (nLen != 0 && lpsz[nLen-1] == ''/n'')
  rString.GetBufferSetLength(nLen-1);

 return lpszResult != NULL;
}

②使用CString对象的"<<"与">>"符读写字符串
CString定义了输入输出符,可以象基本类型的数据一样使用CArchive 的操作符定义

friend CArchive& AFXAPI operator<<(CArchive& ar, const CString& string);
friend CArchive& AFXAPI operator>>(CArchive& ar, CString& string);
// CString serialization code
// String format:
//      UNICODE strings are always prefixed by 0xff, 0xfffe
//      if < 0xff chars: len:BYTE, TCHAR chars
//      if >= 0xff characters: 0xff, len:WORD, TCHAR chars
//      if >= 0xfffe characters: 0xff, 0xffff, len:DWORD, TCHARs

CArchive& AFXAPI operator<<(CArchive& ar, const CString& string)
{
 // special signature to recognize unicode strings
#ifdef _UNICODE
 ar << (BYTE)0xff;
 ar << (WORD)0xfffe;
#endif

 if (string.GetData()-> nDataLength < 255)
 {
  ar << (BYTE)string.GetData()-> nDataLength;
 }
 else if (string.GetData()-> nDataLength < 0xfffe)
 {
  ar << (BYTE)0xff;
  ar << (WORD)string.GetData()-> nDataLength;
 }
 else
 {
  ar << (BYTE)0xff;
  ar << (WORD)0xffff;
  ar << (DWORD)string.GetData()-> nDataLength;
 }
 ar.Write(string.m_pchData, string.GetData()-> nDataLength*sizeof(TCHAR));
 return ar;
}

// return string length or -1 if UNICODE string is found in the archive
AFX_STATIC UINT AFXAPI _AfxReadStringLength(CArchive& ar)
{
 DWORD nNewLen;

 // attempt BYTE length first
 BYTE bLen;
 ar >> bLen;

 if (bLen < 0xff)
  return bLen;

 // attempt WORD length
 WORD wLen;
 ar >> wLen;
 if (wLen == 0xfffe)
 {
  // UNICODE string prefix (length will follow)
  return (UINT)-1;
 }
 else if (wLen == 0xffff)
 {
  // read DWORD of length
  ar >> nNewLen;
  return (UINT)nNewLen;
 }
 else
  return wLen;
}

CArchive& AFXAPI operator>>(CArchive& ar, CString& string)
{
#ifdef _UNICODE
 int nConvert = 1;   // if we get ANSI, convert
#else
 int nConvert = 0;   // if we get UNICODE, convert
#endif

 UINT nNewLen = _AfxReadStringLength(ar);
 if (nNewLen == (UINT)-1)
 {
  nConvert = 1 - nConvert;
  nNewLen = _AfxReadStringLength(ar);
  ASSERT(nNewLen != -1);
 }

 // set length of string to new length
 UINT nByteLen = nNewLen;
#ifdef _UNICODE
 string.GetBufferSetLength((int)nNewLen);
 nByteLen += nByteLen * (1 - nConvert);  // bytes to read
#else
 nByteLen += nByteLen * nConvert;    // bytes to read
 if (nNewLen == 0)
  string.GetBufferSetLength(0);
 else
  string.GetBufferSetLength((int)nByteLen+nConvert);
#endif

 // read in the characters
 if (nNewLen != 0)
 {
  ASSERT(nByteLen != 0);

  // read new data
  if (ar.Read(string.m_pchData, nByteLen) != nByteLen)
   AfxThrowArchiveException(CArchiveException::endOfFile);

  // convert the data if as necessary
  if (nConvert != 0)
  {
#ifdef _UNICODE
   CStringData* pOldData = string.GetData();
   LPSTR lpsz = (LPSTR)string.m_pchData;
#else
   CStringData* pOldData = string.GetData();
   LPWSTR lpsz = (LPWSTR)string.m_pchData;
#endif
   lpsz[nNewLen] = ''/0'';    // must be NUL terminated
   string.Init();   // don''t delete the old data
   string = lpsz;   // convert with operator=(LPWCSTR)
   CString::FreeData(pOldData);
  }
 }
 return ar;
}

 七.CObject派生对象的读写
MFC中多数类都从CObject类派生,CObject类与CArchive类有着良好的合作关系,能实现将对象序列化储存到文件或其他媒介中去,或者读取预先储存的对象,动态建立对象等功能。

①CObject定义了针对CArvhive的输入输出操作符,可以向其他基本数据类型一样使用"<<"、"<<"符号

CArchive& AFXAPI operator<<(CArchive& ar, const CObject* pOb)
 { ar.WriteObject(pOb); return ar; }
CArchive& AFXAPI operator>>(CArchive& ar, CObject*& pOb)
 { pOb = ar.ReadObject(NULL); return ar; }

当使用这些符号时,实际上执行的是CArchive的WriteObject和ReadObject成员
②WriteObject与ReadObject

在WriteObject与ReadObject中先写入或读取运行时类信息(CRuntimeClas),再调用Serialze(..),按其中的代码读写具体的对象数据。

因此,只要在CObject派生类中重载Serilize()函数,写入具体的读写过程,就可以使对象具有存储与创建能力。


//将对象写入到缓冲区
void CArchive::WriteObject(const CObject* pOb)
{
 DWORD nObIndex;
 // make sure m_pStoreMap is initialized
 MapObject(NULL);

 if (pOb == NULL)
 {
  // save out null tag to represent NULL pointer
  *this << wNullTag;
 }
 else if ((nObIndex = (DWORD)(*m_pStoreMap)[(void*)pOb]) != 0)
  // assumes initialized to 0 map
 {
  // save out index of already stored object
  if (nObIndex < wBigObjectTag)
   *this << (WORD)nObIndex;
  else
  {
   *this << wBigObjectTag;
   *this << nObIndex;
  }
 }
 else
 {
  // write class of object first
  CRuntimeClass* pClassRef = pOb-> GetRuntimeClass();
  WriteClass(pClassRef);  //写入运行类信息

  // enter in stored object table, checking for overflow
  CheckCount();
  (*m_pStoreMap)[(void*)pOb] = (void*)m_nMapCount++;

  // 调用CObject的Serialize成员,按其中的代码写入类中数据。
  ((CObject*)pOb)-> Serialize(*this);
 }
}


CObject* CArchive::ReadObject(const CRuntimeClass* pClassRefRequested)
{

 // attempt to load next stream as CRuntimeClass
 UINT nSchema;
 DWORD obTag;
 //先读入运行时类信息
 CRuntimeClass* pClassRef = ReadClass(pClassRefRequested, &nSchema, &obTag);

 // check to see if tag to already loaded object
 CObject* pOb;
 if (pClassRef == NULL)
 {
  if (obTag > (DWORD)m_pLoadArray-> GetUpperBound())
  {
   // tag is too large for the number of objects read so far
   AfxThrowArchiveException(CArchiveException::badIndex,
    m_strFileName);
  }

  pOb = (CObject*)m_pLoadArray-> GetAt(obTag);
  if (pOb != NULL && pClassRefRequested != NULL &&
    !pOb-> IsKindOf(pClassRefRequested))
  {
   // loaded an object but of the wrong class
   AfxThrowArchiveException(CArchiveException::badClass,
    m_strFileName);
  }
 }
 else
 {
  // 建立对象
  pOb = pClassRef-> CreateObject();
  if (pOb == NULL)
   AfxThrowMemoryException();

  // Add to mapping array BEFORE de-serializing
  CheckCount();
  m_pLoadArray-> InsertAt(m_nMapCount++, pOb);

  // Serialize the object with the schema number set in the archive
  UINT nSchemaSave = m_nObjectSchema;
  m_nObjectSchema = nSchema;
  pOb-> Serialize(*this); //调用CObject的Serialize,按其中代码读入对象数据。
  m_nObjectSchema = nSchemaSave;
  ASSERT_VALID(pOb);
 }

 return pOb;
}


③运行时类信息的读写
为了避免众多重复的同类对象写入重复的类信息,CArchive中使用CMap对象储存和检索类信息。


void CArchive::WriteClass(const CRuntimeClass* pClassRef)
{
 ASSERT(pClassRef != NULL);
 ASSERT(IsStoring());    // proper direction

 if (pClassRef-> m_wSchema == 0xFFFF)
 {
  TRACE1("Warning: Cannot call WriteClass/WriteObject for %hs./n",
   pClassRef-> m_lpszClassName);
  AfxThrowNotSupportedException();
 }

 // make sure m_pStoreMap is initialized
 MapObject(NULL);

 // write out class id of pOb, with high bit set to indicate
 // new object follows

 // ASSUME: initialized to 0 map
 DWORD nClassIndex;
 if ((nClassIndex = (DWORD)(*m_pStoreMap)[(void*)pClassRef]) != 0)
 {
  // previously seen class, write out the index tagged by high bit
  if (nClassIndex < wBigObjectTag)
   *this << (WORD)(wClassTag | nClassIndex);
  else
  {
   *this << wBigObjectTag;
   *this << (dwBigClassTag | nClassIndex);
  }
 }
 else
 {
  // store new class
  *this << wNewClassTag;
  pClassRef-> Store(*this);

  // store new class reference in map, checking for overflow
  CheckCount();
  (*m_pStoreMap)[(void*)pClassRef] = (void*)m_nMapCount++;
 }
}


CRuntimeClass* CArchive::ReadClass(const CRuntimeClass* pClassRefRequested,
 UINT* pSchema, DWORD* pObTag)
{
 ASSERT(pClassRefRequested == NULL ||
  AfxIsValidAddress(pClassRefRequested, sizeof(CRuntimeClass), FALSE));
 ASSERT(IsLoading());    // proper direction

 if (pClassRefRequested != NULL && pClassRefRequested-> m_wSchema == 0xFFFF)
 {
  TRACE1("Warning: Cannot call ReadClass/ReadObject for %hs./n",
   pClassRefRequested-> m_lpszClassName);
  AfxThrowNotSupportedException();
 }

 // make sure m_pLoadArray is initialized
 MapObject(NULL);

 // read object tag - if prefixed by wBigObjectTag then DWORD tag follows
 DWORD obTag;
 WORD wTag;
 *this >> wTag;
 if (wTag == wBigObjectTag)
  *this >> obTag;
 else
  obTag = ((wTag & wClassTag) << 16) | (wTag & ~wClassTag);

 // check for object tag (throw exception if expecting class tag)
 if (!(obTag & dwBigClassTag))
 {
  if (pObTag == NULL)
   AfxThrowArchiveException(CArchiveException::badIndex, m_strFileName);

  *pObTag = obTag;
  return NULL;
 }

 CRuntimeClass* pClassRef;
 UINT nSchema;
 if (wTag == wNewClassTag)
 {
  // new object follows a new class id
  if ((pClassRef = CRuntimeClass::Load(*this, &nSchema)) == NULL)
   AfxThrowArchiveException(CArchiveException::badClass, m_strFileName);

  // check nSchema against the expected schema
  if ((pClassRef-> m_wSchema & ~VERSIONABLE_SCHEMA) != nSchema)
  {
   if (!(pClassRef-> m_wSchema & VERSIONABLE_SCHEMA))
   {
    // schema doesn''t match and not marked as VERSIONABLE_SCHEMA
    AfxThrowArchiveException(CArchiveException::badSchema,
     m_strFileName);
   }
   else
   {
    // they differ -- store the schema for later retrieval
    if (m_pSchemaMap == NULL)
     m_pSchemaMap = new CMapPtrToPtr;
    ASSERT_VALID(m_pSchemaMap);
    m_pSchemaMap-> SetAt(pClassRef, (void*)nSchema);
   }
  }
  CheckCount();
  m_pLoadArray-> InsertAt(m_nMapCount++, pClassRef);
 }
 else
 {
  // existing class index in obTag followed by new object
  DWORD nClassIndex = (obTag & ~dwBigClassTag);
  if (nClassIndex == 0 || nClassIndex > (DWORD)m_pLoadArray-> GetUpperBound())
   AfxThrowArchiveException(CArchiveException::badIndex,
    m_strFileName);

  pClassRef = (CRuntimeClass*)m_pLoadArray-> GetAt(nClassIndex);
  ASSERT(pClassRef != NULL);

  // determine schema stored against objects of this type
  void* pTemp;
  BOOL bFound = FALSE;
  nSchema = 0;
  if (m_pSchemaMap != NULL)
  {
   bFound = m_pSchemaMap-> Lookup( pClassRef, pTemp );
   if (bFound)
    nSchema = (UINT)pTemp;
  }
  if (!bFound)
   nSchema = pClassRef-> m_wSchema & ~VERSIONABLE_SCHEMA;
   }

 // check for correct derivation

                                                

你可能感兴趣的:(windows,null,Random,float,byte,statistics)