check windows return character

简介:

#ifndef _FILE_CHECK_H
#define _FILE_CHECK_H
#include <string.h>
#include <vector>

const int LINEBUFF_SIZE = 1024;
const std::string TAB_REPLACE = "   ";
const std::string TAB_STRING = "\t";
const std::string WINDOWS_RETURN = "\r\n";
const std::string UNIX_RETURN = "\n";

class FileCheck
{
public:
    FileCheck(void);
    ~FileCheck(void);

   bool checkFiles(const std::vector<std::string>& inFiles, std::vector<std::string>& messages);
   bool modifyFiles(const std::vector<std::string>& inFiles);
   void setOnlyCreateTmpFile(bool bval);
   int delTempFiles();

private:
   bool modifyOneFile(const std::string& inFileName);
   bool checkOneFile(const std::string& inFileName, std::vector<std::string> & message);
   void replaceString(const std::string & seach, const std::string& replace,std::string & inoutStr);
   void trim(std::string &line);
   bool checkSpaceLastCharacter(std::string &line, bool winrt);
   std::string getTempFileName(const std::string fileName);
   void updateFile(const std::string& inFile, const std::string& outFile);
   bool m_OnlyCreateTmp;
   std::vector<std::string> m_TmpFiles;
};

#endif _FILE_CHECK_H

 

#include "stdafx.h" //remove later
#include "FileCheck.h"
#include <time.h>

FileCheck::FileCheck(void)
: m_OnlyCreateTmp(false)
{
}

FileCheck::~FileCheck(void)
{
}

bool FileCheck::modifyFiles(const std::vector<std::string>& inFiles)
{
   std::vector<std::string>::const_iterator it = inFiles.begin();
   for(; it != inFiles.end(); ++it)
   {
      modifyOneFile(*it);
   }
   return true;
}

bool FileCheck::modifyOneFile(const std::string& inFileName)
{
   if (inFileName.empty())
    {
        return false;
    }

    FILE * fp = NULL;
   FILE * fpCopy = NULL;
    
   fp = fopen(inFileName.c_str(), "rb");

   std::string temFileName = getTempFileName(inFileName);

   fpCopy = fopen(temFileName.c_str(),"wb");

   if (!fp || !fpCopy)
   {
      return false;
   }

   while (!feof(fp))
    {
      char buffer[LINEBUFF_SIZE + 1];
      memset(buffer,0,sizeof(buffer));
      int linNum = 1;
      if (fgets(buffer,LINEBUFF_SIZE,fp))
      {
         std::string line = buffer;
         trim(line);
         replaceString(TAB_STRING,TAB_REPLACE,line);
         replaceString(WINDOWS_RETURN,UNIX_RETURN,line);
         memcpy(buffer,line.c_str(),line.size());
         fwrite(buffer,sizeof(char),line.size(),fpCopy);
         linNum++;
      }
    }

   if (fp)
   {
      fclose(fp);
   }

   if(fpCopy)
   {
      fclose(fpCopy);
   }

   if (!m_OnlyCreateTmp)
   {
      updateFile(temFileName,inFileName);

      remove(temFileName.c_str());
   }
   else
   {
      m_TmpFiles.push_back(temFileName);
   }
   return true;
}

void FileCheck::replaceString(const std::string & seach, const std::string& replace,std::string & inoutStr)
{
   if (!inoutStr.empty())
   {
      size_t pos = 0;
      pos = inoutStr.find(seach,pos);
      while (pos != std::string::npos)
      {
         inoutStr.replace(pos,seach.size(),replace);
         pos+=seach.size();
         pos = inoutStr.find(seach,pos);
      }
   }
}

void FileCheck::trim(std::string &line)
{
   if (!line.empty())
   {
      size_t endPos = line.length() - 1;
      while (endPos)
      {
         if (isspace(line[endPos]))
         {
            endPos --;
         }
         else
         {
            break;
         }
      }

      if (endPos < line.length() - 1 )
      {
         line.replace(endPos+1,(line.length() - endPos - 2),"");
      }
   }

}

bool FileCheck::checkSpaceLastCharacter(std::string &line ,bool winrt)
{
   bool ret = false;

   if (!line.empty())
   {
      size_t endPos = line.length() - 1;
      if (endPos)
      {
         endPos --;
         if (winrt && endPos)
         {
            endPos--;
            if(endPos && isspace(line[endPos]))
            {
               ret = true;
            }
         }
         else if (endPos)
         {
            if(isspace(line[endPos]))
            {
               ret = true;
            }            
         }
      }
   }

   return ret;
}

std::string FileCheck::getTempFileName(const std::string fileName)
{
   char buffer[255];
   memset(buffer,0,255);
   const time_t t = time(NULL);
   struct tm* current_time = localtime(&t);
   if (current_time)
   {
      sprintf_s(buffer,"%d%d%d%d%d%d",
         current_time->tm_year + 1900,
         current_time->tm_mon + 1,
         current_time->tm_mday,
         current_time->tm_hour,
         current_time->tm_min,
         current_time->tm_sec);
   }

   std::string tmpFileName;
   size_t dotPos = fileName.rfind('.');
   if (dotPos != std::string::npos)
   {
      std::string extName = fileName.substr(dotPos);
      tmpFileName = fileName.substr(0,dotPos);
      tmpFileName += "_template";
      tmpFileName += buffer;
      tmpFileName += extName;
   }
   return tmpFileName;
}

void FileCheck::updateFile(const std::string& inFile, const std::string& outFile)
{
   if (inFile.empty() || outFile.empty())
   {
      return;
   }

   FILE * fpFrom = fopen(inFile.c_str(),"rb");
   FILE * fpTo = fopen(outFile.c_str(),"wb");
   
   if (fpFrom && fpTo)
   {
      fseek(fpFrom,0,SEEK_END);
      long len = ftell(fpFrom);
      fseek(fpFrom,0,SEEK_SET);
      char *temp = new char[len];
      memset(temp,0,len);
      fread(temp,sizeof(char),len,fpFrom);
      fwrite(temp,sizeof(char),len,fpTo);
      delete [] temp;
      fclose(fpFrom);
      fclose(fpTo);
   }
}

void FileCheck::setOnlyCreateTmpFile(bool bval)
{
   m_OnlyCreateTmp = bval;
}

int FileCheck::delTempFiles()
{
   int rmNum = 0;

   if (m_OnlyCreateTmp && m_TmpFiles.size() > 0)
   {
      std::vector<std::string>::const_iterator it = m_TmpFiles.begin();
      for (; it != m_TmpFiles.end(); ++it)
      {
         rmNum++;
         remove((*it).c_str());
      }
   }

   return rmNum;
}


bool FileCheck::checkFiles(const std::vector<std::string>& inFiles, std::vector<std::string>& messages)
{
   std::vector<std::string>::const_iterator it = inFiles.begin();
   for(; it != inFiles.end(); ++it)
   {
      checkOneFile(*it,messages);
   }
   return true;
}

bool FileCheck::checkOneFile(const std::string &inFileName, std::vector<std::string> & messages)
{
   if (inFileName.empty())
    {
        return false;
    }

    FILE * fp = NULL;
    
   fp = fopen(inFileName.c_str(), "rb");

   if (!fp)
   {
      return false;
   }

   int linNum = 1;

   while (!feof(fp))
    {
      char buffer[LINEBUFF_SIZE + 1];
      char msgBuffer[LINEBUFF_SIZE + 1];
      memset(buffer,0,sizeof(buffer));
      memset(msgBuffer,0,sizeof(msgBuffer));
      bool isReturn = false;
      
      if (fgets(buffer,LINEBUFF_SIZE,fp))
      {
         std::string line = buffer;
         std::string msg;
         if (line.find(TAB_STRING,0) != std::string::npos)
         {
            sprintf_s(msgBuffer, sizeof(msgBuffer), "tab:%d ", linNum);
            msg.append(msgBuffer);
         }

         if (line.find(WINDOWS_RETURN,0) != std::string::npos)
         {
            sprintf_s(msgBuffer, sizeof(msgBuffer), "return:%d ", linNum);
            msg.append(msgBuffer);
            isReturn = true;
         }

         if (checkSpaceLastCharacter(line,isReturn))
         {
            sprintf_s(msgBuffer, sizeof(msgBuffer), "space:%d ", linNum);
            msg.append(msgBuffer);
         }

         if (!msg.empty())
         {
            msg.append("-->");
            msg.append(inFileName);
            messages.push_back(msg);
         }
         linNum++;
      }
    }

   if (fp)
   {
      fclose(fp);
   }

   return true;
}



本文转自莫水千流博客园博客,原文链接:http://www.cnblogs.com/zhoug2020/p/4576313.html,如需转载请自行联系原作者

相关文章
|
1月前
|
安全 数据安全/隐私保护 Windows
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
解锁安全之门,Windows Server 2019密码修改攻略大揭秘
|
1月前
|
存储 安全 网络安全
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
铁壁如墙-WINDOWS SERVER 2019勒索病毒终极防御指南
|
1月前
|
网络协议 数据安全/隐私保护 Windows
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
Windows Server 各版本搭建域控制器实现通过域管理用户(03~19)
45 1
|
1月前
|
存储 数据安全/隐私保护 索引
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
Windows Server 各版本搭建文件服务器实现共享文件(03~19)
157 1
|
1月前
|
数据安全/隐私保护 虚拟化 Windows
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
69 0
|
3月前
|
缓存 网络协议 数据安全/隐私保护
[运维笔记] - (命令).Windows server常用网络相关命令总结
[运维笔记] - (命令).Windows server常用网络相关命令总结
189 0
|
1月前
|
安全 数据安全/隐私保护 Windows
无远不至的掌控力:探索Windows Server 2019的远程访问绝招
无远不至的掌控力:探索Windows Server 2019的远程访问绝招
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
105 0
|
1月前
|
存储 网络安全 数据安全/隐私保护
Windows Server 2019 IIS HTTPS证书部署流程详解
Windows Server 2019 IIS HTTPS证书部署流程详解
|
1月前
|
存储 Windows
windows server 2019 云服务器看不见硬盘的解决方案
windows server 2019 云服务器看不见硬盘的解决方案