#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::d eltempfiles ()
{
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