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

时间: 2024-10-25 19:36:01

check windows return character的相关文章

wxpython wx.windows的API

wx.Window is the base class for all windows and represents any visible object on screen. All controls, top level windows and so on are windows. Sizers and device contexts are not, however, as they don't appear on screen themselves. Please note that a

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]或[Errno 2] #coding:utf-8 import os os.system("chcp 65001") a = u"你好こんにちは" print a 此时会报错,如果字符串只含ASCII字符就不会报错,如果cmd用其他编码则可能输出乱码但不会报错 经查这是windo

利用Python脚本管理Windows服务

Windows服务常用的功能就是启动服务,关闭服务,重启服务和查询服务运行状态,其中查询服务运行状态是其他三种操作的基础. 本文中提到的使用Python脚本管理Windows服务实际上是调用win32serviceutil模块,此模块来自pywin32包,此模块本身有管理服务的功能,有兴趣的可以去阅读它的部分源码. 本脚本存在的目的是为了熟练Python的语法和基本操作,Windows下有更好的命令行工具来管理服务,如sc.Powershell等.通常命令行工具的执行速度要比services.m

Windows 消息机制详解

总的来说: MSG包括: 窗口句柄,指示MSG发送的目的窗口 消息标识 lPARAM.wParam 发送时间 发送时的鼠标位置   关于消息队列: Windows系统有一个系统消息队列 每个线程都有一个自己的消 息队列(由于发送消息MSG需 要提供一个窗口HWnd,而基 本有窗口的线程,都是UI线 程),因此基本上如果线程使用了GDI函数,则windows给该线程分配一个线程消息队列,这个消息队列负责该线程的所有窗口的消息.   所有的窗口都有自己的句柄(HWND),消息被发送时,这个句柄就已经

【DirectX11-Tutorial】运行第一个win32程序A Primer of Basic Windows

DirectX11-Tutorial 本系列主要参考此博客的文章,同时会加上一点个人实践过程. ========================================== 分割线 ========================================== <span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h> // include the sta

What is the Windows Integrity Mechanism?(什么是Windows完整性机制)

The Windows integrity mechanism is a core component of the Windows security architecture that restricts the access permissions of applications that are running under the same user account and that are less trustworthy. (Windows完整性机制是Windows安全体系的核心组件,

UVa 10196 - Check The Check

题目:国际象棋,判断当前状态,哪一方被将军了.不会有同时被将军的情况. 分析:模拟.直接按照国际象棋的规则模拟即可. 把操作分成两种,单点判断和射线判断,写成函数减少公共代码,也降低错误率: 然后:兵.马.王(不用判断)都是单点判断,后.车.象都是射线判断. 每种情况,调用不同的方向向量即可. 说明:没有同时成立的情况,注意细节别写错就好了:还有最后那组别输出了. #include <iostream> #include <cstdlib> #include <cstdio&

Windows API参考大全新编

书名:新编Windows API参考大全 作者:本书编写组 页数:981页 开数:16开 字数:2392千字 出版日期:2000年4月第二次印刷 出版社:电子工业出版社 书号:ISBN 7-5053-5777-8 定价:98.00元 内容简介 作为Microsoft 32位平台的应用程序编程接口,Win32 API是从事Windows应用程序开发所必备的.本书首先对Win32 API函数做完整的概述:然后收录五大类函数:窗口管理.图形设备接口.系统服务.国际特性以及网络服务:在附录部分,讲解如何

check procee id exists

check exists, return 0 $ pgrep httpd 46775 $ kill -0 46775 $ echo $? 0 check non-exists, return 1 $ kill -0 999999899 bash: kill: (999999899) - No such process $ echo $? 1 kill process id stored in .pid file #!/bin/sh pid_file=/var/run/process.pid ki