#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;
}

check windows return character的更多相关文章

  1. ABAP程序中退出操作(CHECK, EXIT, RETURN, LEAVE PROGRAM)

    这里总结一下几个常用的退出操作:  CHECK.(SAP官方推荐只在循环中使用) 1)CHECK 后面要跟一个表达式,当表达式值为假(false)时,CHECK发生作用,退出循环(LOOP)或处理程序 ...

  2. How to check Windows 7 OS is permanently activated?[Windows 7]

    Press Windows + R, then you can enter : slmgr.vbs -xpr

  3. DB INIT IN WINDOWS (FOR 12C)

    Uat       oracleDB-Uat  192.168.63.121        Windows server 2012 R2         2核12G,硬盘160G 内置用户是oaadm ...

  4. wxpython wx.windows的API

    wx.Window is the base class for all windows and represents any visible object on screen. All control ...

  5. [转]Installing SharePoint 2013 on Windows Server 2012 R2

    转自:http://www.avivroth.com/2013/07/09/installing-sharepoint-2013-on-windows-server-2012-r2-preview/ ...

  6. windows环境:idea或者eclipse指定用户名操作hadoop集群

    方法 在系统的环境变量或java JVM变量添加HADOOP_USER_NAME(具体值视情况而定). 比如:idea里面可以如下添加HADOOP_USER_NAME=hdfs 原理:直接看源码 /h ...

  7. dhcpsrv:windows系统的优秀开源免费dhcp serve软件

    概述: 官方网站 :http://www.dhcpserver.de/ 写博客时的可免费下载版本  2.52, 或者在cnblogs 本地下载 --========================== ...

  8. Python2.7在Windows下CMD编码为65001/utf-8时print报错[Errno 0]/[Errno 2]

    使用python2.7处理unicode的字符串,环境变量已设置PYTHONIOENCODING为utf-8,cmd编码为utf-8时print unicode字符串会报错[Errno 0]或[Err ...

  9. python2.7.6安装easy_install (windows 64 环境)

    1.复制以下代码保存到easy_install.py文件中(文件名可随意命名)并将该文件放到python的安装路径中(如:D:\Python27) #!/usr/bin/env python &quo ...

随机推荐

  1. 基于Mongodb的轻量级领域驱动框架(序)

    混园子也有些年头了,从各个大牛那儿学了很多东西.技术这东西和中国的料理一样,其中技巧和经验,代代相传(这不是舌尖上的中国广告).转身回头一望,几年来自己也积累了一些东西,五花八门涉猎到各种方向,今日开 ...

  2. LoadRunner8 安装步骤

    一.介绍 LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试.企业使用L ...

  3. ActiveMQ简单介绍以及安装

    概述 首先简单的介绍一下MQ,MQ英文名MessageQueue,中文名也就是大家用的消息队列,干嘛用的呢,说白了就是一个消息的接受和转发的容器,可用于消息推送. ActiveMQ是Apache所提供 ...

  4. 详解TCP和UDP数据段的首部格式

    TCP数据段的首部格式: 源端口号(16) 目的端口号(16) 序列号(32) 确认应答号(32) 数据偏移(4) 保留(6) 代码位(6) 窗口(16) 校验和(16) 紧急指针 选项(长度可变) ...

  5. chrome开发配置(四)生成项目及配置库引用

    1.运行gclient runhooks --force .会重新下载个python 2.7版本的,并且下载好几个文件,大概1个多小时:

  6. 《Java编程那点事儿》读书笔记(七)——多线程

    1.继承Thread类 通过编写新的类继承Thread类可以实现多线程,其中线程的代码必须书写在run方法内部或者在run方法内部进行调用. public class NewThread extend ...

  7. MTK

    1.mt_boot_init->boot_linux_from_storage->boot_linux->boot_linux_fdt

  8. 详细记录python的range()函数用法

    使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 这里记 ...

  9. 18.allegro区域约束规则设置

    一.线宽和线间距 --- ---------------- 然后再电路板上创建一个区域 ----- ---- --- ---- ------------------------------

  10. [HDOJ2512]一卡通大冒险(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2512 给一个数n,问1~n这n个数内的划分.设dp(i,j)为i划分为j个集合时有多少个. 初始化条件 ...