一段莫名其妙错误的代码

来源:互联网 发布:人工智能的评论 编辑:程序博客网 时间:2024/04/28 14:50

////////////////////////////////////////////////////////////////////////////////////
// ProFileInfo.cpp CProFileInfo类的实现文件
//模块:  ProInnerVisitor
//功能:
//   定义CProFileInfo类,完成proe文件的创建时间以及创建人等信息
//=================================================================================
//History:
//=================================================================================
// 
//=================================================================================

#include "stdafx.h"
#include "ProFileInfo.h"
#include <string>
#include <list>
#include <cmath>
#include <Windows.h>
#include <fstream>
#include <iostream>

#include <ProMenu.h>
#include <ProMdl.h>
#include <ProWindows.h>
#include <ProUtil.h>
#include <comutil.h>

using namespace std;


CProFileInfo::CProFileInfo(string fileName)
{
 m_Creator.clear();
 m_Time.clear();

 m_FileName = fileName;
}

CProFileInfo::~CProFileInfo()
{
 m_Creator.clear();
 m_Time.clear();
}

//获得创建人信息
int CProFileInfo::GetFileCreator(string &strCreator)
{
 int ret;

 if (strCreator.c_str() == NULL)
 {
  return -1;
 }
 
 ret = ExcuteProCmd();
 if (ret != 0)
 {
  m_Creator = "NoCreator";
 }

 strCreator = m_Creator;
 return 0;
}

//获得上次修改时间
int CProFileInfo::GetLastModefyTime(string &strTime)
{
 if (strTime.c_str() == NULL)
 {
  return -1;
 }

 strTime = m_Time;
 return 0;
}

//执行"审计追踪"操作
int CProFileInfo::ExcuteProCmd()
{
 ProError err;
 int  ret;

 //宏命令为
 //~ Activate `main_dlg_cur` `Info.psh_info_audit_trail`
 wchar_t wszCmd[] = L"~ Activate `main_dlg_cur` `Info.psh_info_audit_trail`"; 
 err = ProMacroLoad( wszCmd );
 if (err != PRO_TK_NO_ERROR)
 {
  return -1;
 }
 err =ProMacroExecute();
 if (err != PRO_TK_NO_ERROR)
 {
  return -1;
 }
 Sleep(1000*1);

 //关闭浏览器窗口
 int id;
 double size;
 err = ProWindowCurrentGet(&id);
 if (err == PRO_TK_NO_ERROR)
 {
  ProWindowBrowserSizeSet(id,0);
 }
 
 
 //获取信息
 FILE_MSG_LIST fileinfo;
 ret = GetAuditInfo(&fileinfo);
 if (ret != 0)
 {
  return -1;
 }

 return 0;
}

//返回文件信息,并将信息存入数据结构链表中
int CProFileInfo::GetAuditInfo(FILE_MSG_LIST *fileInfoList)
{
 //获得启动目录
 ProPath currPath;
 ProDirectoryCurrentGet(currPath);
 
 //解析全路径名,以此获得将要生成的文件名
 ProPath fullName;
 BSTR bsFullName;
 bsFullName = _com_util::ConvertStringToBSTR( m_FileName.c_str() );
 swprintf(fullName,L"%s",bsFullName);
 ProPath path;
 ProName name;
 ProName extName;
 int iVer;
 ProFilenameParse(fullName,path,name,extName,&iVer);

    //查找扩展名为.usr.*的文件,且文件名与刚才解析的文件名相同
 HANDLE hFile = NULL;
 int iFind = 1;
 WIN32_FIND_DATA wfd;
 wchar_t wszFindName[256];
 ZeroMemory(wszFindName,256*2);
 
 wchar_t wszTemp[256];
 swprintf(wszTemp,L"%s.usr.*",name);
 wcscat(wszFindName,currPath);
 wcscat(wszFindName,wszTemp);
 
 hFile = ::FindFirstFile(_com_util::ConvertBSTRToString(wszFindName) ,&wfd);
 FILETIME ftNearest;
 long lCmpRes;
 WIN32_FIND_DATA FindDataTag;
 if (hFile != INVALID_HANDLE_VALUE)
 {
  ftNearest = wfd.ftCreationTime;
  FindDataTag = wfd;

  while (iFind)
  {
   //现在查找时间最近的一个文件,如果不是,那么删除
   FILETIME ftCur = wfd.ftCreationTime;
   lCmpRes = CompareFileTime(&ftNearest,&ftCur);
   if ( lCmpRes < 0 )
   {
    //将旧的文件删除
    DeleteFile(FindDataTag.cFileName);

    ftNearest = ftCur;    
                FindDataTag = wfd;
   }   
   else if( lCmpRes > 0 )
   {
    DeleteFile(wfd.cFileName);
   }
   
   iFind = ::FindNextFile(hFile,&wfd);
  }
 }
 else
 {
  return -1; //cannot find the generate file
 }

 //读取文件中的内容
 ASSERT(FindDataTag.cFileName);

 ifstream openfile(FindDataTag.cFileName);
 if ( !openfile )
 {
  return -1; //can not open the file
 }

 char info[256];
 for (int i = 0 ; i < 5 ; i++)
 {
  openfile.getline(info,256);
 }
 
 //将获取内容存入成员变量中
 //获取修改时间
 string handleInfo(info);
 string::size_type index = handleInfo.find_first_of('|');
 if (index != string::npos)
 {
  char *szTag = new char[index];
  ZeroMemory(szTag,index);
  handleInfo.copy(szTag,index);
  m_Time = szTag;
  delete [] szTag;
 }

 //获取修改人
 string::size_type index1=0,index2=0;
 index = 0;
 for(int i=0; i < 4 ; ++i)
 {
  index = handleInfo.find('|',index+1);
  if (index == string::npos)
  {
   return -1; //invalid message
  }

  if ( 2 == i)
  {
   index1 = index;
  }
  if ( 3 == i)
  {
   index2 = index;
  }
 }
 int size = index2 - index1;
 char *szTag1 = new char[size];
 ZeroMemory(szTag1,size);
 handleInfo.copy(szTag1,size-1,index1+1);
 m_Creator = szTag1;
 delete [] szTag1;

 //删除生成的文件
 DeleteFile(FindDataTag.cFileName);
 return 0;
}

 


 

原创粉丝点击