StdStrFile
#pragma once #include <list> #include <string> #include <vector> #include <sstream> class CStdStr { public: CStdStr(); ~CStdStr(); //字符操作 static std::string AddSlashIfNeeded(const std::string strDir, const char& cDir = '\\'); static std::string GetDirOfDir(const std::string& strDir, const char& cDir = '\\'); static std::string GetDirOfFile(const std::string& strFile, const char& cDir = '\\'); static std::string GetSuffixOfFile(const std::string& strFile, bool bWithDot = true); static std::string GetNameOfDir(const std::string& strDir, const char& cDir = '\\'); static std::string GetNameOfFile(const std::string& strFile, const bool& bWithSuffix = true, const char& cDir = '\\'); static std::string ReplaceSuffix(const std::string& strFilePath, const std::string& strNewSuffix); static std::string Trim(const std::string& strOri, const char& cToTrim = ' '); static std::string TrimHead(const std::string& strOri, const char& cToTrim = ' '); static std::string TrimTail(const std::string& strOri, const char& cToTrim = ' '); static std::vector<std::string> Split(const std::string& str, const std::string& pattern); }; class CStdFile { public: CStdFile(); ~CStdFile(); //文件操作 static bool CopyAFile(const std::string& strSrcFileName, std::string& strDstFileName, const bool& bFailIfExists); static bool IfAccessFile(const std::string& strFilePath); static int ParseTXTFile(const std::string& strFilePath, std::list<std::string>& lContentInFile); static int ParseTXTFile(const std::string& strFilePath, std::vector<std::string>& vContentInFile); static int SaveTXTFile(const std::string& strTxtPath, std::list<std::string>& lContent, bool bAppend = false); static int SaveTXTFile(const std::string& strTxtPath, std::vector<std::string>& vContent, bool bAppend = false); static int SaveTXTFile(const std::string& strTxtPath, const std::string& strLine, bool bAppend = false); }; class CStdTpl { public: CStdTpl() { } ~CStdTpl() { } template <class T> static bool VectorContains(std::vector<T>& vTs, const T& value); template <class T> static bool VectorContains(std::vector<T>& vTsSum, std::vector<T>& vTsPart); template <class T> static int VectorFind(std::vector<T>& vTs, const T& value, bool bPositiveGoing = true); template <class T> static int ConvertFromString(T &value, const std::string &s); template <class T> static std::string ConvertToString(T value); }; template <class T> bool CStdTpl::VectorContains(std::vector<T>& vTs, const T& value) { for (int i = 0; i < vTs.size(); ++i) { if (vTs[i] == value) { return true; } } return false; } template <class T> bool CStdTpl::VectorContains(std::vector<T>& vTsSum, std::vector<T>& vTsPart) { for (int i = 0; i < vTsPart.size(); ++i) { if (!VectorContains(vTsSum, vTsPart[i])) { return false; } } return true; } template <class T> int CStdTpl::VectorFind(std::vector<T>& vTs, const T& value, bool bPositiveGoing /*= true*/) { if (bPositiveGoing) { for (int i = 0; i < vTs.size(); ++i) { if (vTs[i] == value) { return i; } } } else { for (int i = int(vTs.size() - 1); i >= 0; --i) { if (vTs[i] == value) { return i; } } } return -1; } template <class T> int CStdTpl::ConvertFromString(T &value, const std::string &s) { std::stringstream ss(s); ss >> value; return 0; } template <class T> std::string CStdTpl::ConvertToString(T value) { std::stringstream ss; ss << value; return ss.str(); } std::string ToString(const double& dValue);
#include "StdStrFile.h" #include <fstream> CStdStr::CStdStr() { } CStdStr::~CStdStr() { } std::string CStdStr::AddSlashIfNeeded(const std::string strDir, const char& cDir/* = '\\'*/) { std::string strDirNew(strDir); if (strDir[strDir.length() - 1] != cDir) { strDirNew += cDir; } return strDirNew; } std::string CStdStr::GetDirOfDir(const std::string& strDir, const char& cDir/* = '\\'*/) { std::string strDirPath(strDir); strDirPath = TrimTail(strDirPath, cDir); int index = (int)strDirPath.rfind(cDir); if (index != -1) { return strDirPath.substr(0, index); } else { return strDirPath; } } std::string CStdStr::GetDirOfFile(const std::string& strFile, const char& cDir /*= '\\'*/) { std::string strFilePath(strFile); strFilePath = TrimTail(strFilePath, cDir); int index = (int)strFilePath.rfind(cDir); if (index != -1) { return strFilePath.substr(0, index); } else { return strFilePath; } } std::string CStdStr::GetSuffixOfFile(const std::string& strFile, bool bWithDot /*= true*/) { std::string strFileName = GetNameOfFile(strFile); int index = (int)strFileName.rfind("."); if (index != -1) { if (bWithDot) { return strFileName.substr(index); } else { return strFileName.substr(index + 1); } } else { return ""; } } std::string CStdStr::GetNameOfDir(const std::string& strDir, const char& cDir /*= '\\'*/) { std::string strDirPath(strDir); strDirPath = TrimTail(strDirPath, cDir); int index = (int)strDirPath.rfind('\\'); return strDirPath.substr(index + 1, strDirPath.length() - index - 1); } std::string CStdStr::Trim(const std::string& strOri, const char& cToTrim/* = ' '*/) { std::string text(strOri); if (!text.empty()) { text.erase(0, text.find_first_not_of(" \n\r\t")); text.erase(text.find_last_not_of(" \n\r\t") + 1); text.erase(0, text.find_first_not_of(cToTrim)); text.erase(text.find_last_not_of(cToTrim) + 1); } return text; } std::string CStdStr::TrimHead(const std::string& strOri, const char& cToTrim/* = ' '*/) { std::string s(strOri); size_t i = 0; for (; i < s.length(); ++i) { if (s[i] != ' ' && s[i] != '\t'&&s[i] != '\r'&&s[i] != '\n' && s[i] != cToTrim) break; } s = s.substr(i, s.length() - i); return s; } std::string CStdStr::TrimTail(const std::string& strOri, const char& cToTrim/* = ' '*/) { std::string s(strOri); int i = (int)s.length() - 1; for (; i > 0; --i) { if (s[i] != ' ' && s[i] != '\t'&&s[i] != '\r'&&s[i] != '\n' && s[i] != cToTrim) break; } s = s.substr(0, i + 1); return s; } std::string CStdStr::GetNameOfFile(const std::string& strFile, const bool& bWithSuffix /*= true*/, const char& cDir/* = '\\'*/) { int index = (int)strFile.rfind(cDir); std::string strFileName = strFile.substr(index + 1, strFile.length() - index - 1); if (bWithSuffix) { return strFileName; } else { int nIndexOfDot = (int)strFileName.rfind('.'); if (nIndexOfDot == -1) { return strFileName; } else { return strFileName.substr(0, nIndexOfDot); } } } std::string CStdStr::ReplaceSuffix(const std::string& strFilePath, const std::string& strNewSuffix) { int nIndex = (int)strFilePath.rfind('.'); if (nIndex != -1) { return strFilePath.substr(0, nIndex) + strNewSuffix; } else { return strFilePath + strNewSuffix; } } std::vector<std::string> CStdStr::Split(const std::string& str, const std::string& pattern) { std::string strBak(str); std::string::size_type pos; std::vector<std::string> result; //扩展字符串以方便操作 strBak += pattern; size_t size = strBak.size(); for (size_t i = 0; i < size; i++) { pos = strBak.find(pattern, i); if (pos < size) { std::string s = strBak.substr(i, pos - i); result.push_back(s); i = (int)pos + (int)pattern.size() - 1; } } return result; } CStdFile::CStdFile() { } CStdFile::~CStdFile() { } bool CStdFile::IfAccessFile(const std::string& strFilePath) { std::fstream _file; _file.open(strFilePath, std::ios::in); bool bRes = (_file != 0); _file.close(); return bRes; } bool CStdFile::CopyAFile(const std::string& strSrcFileName, std::string& strDstFileName, const bool& bFailIfExists) { std::ifstream in; std::ofstream out; in.open(strSrcFileName, std::ios::binary);//打开源文件 if (in.fail())//打开源文件失败 { in.close(); out.close(); return false; } if (bFailIfExists && IfAccessFile(strDstFileName)) { out.close(); in.close(); return false; } out.open(strDstFileName, std::ios::binary);//创建目标文件 if (out.fail())//创建文件失败 { out.close(); in.close(); return false; } //复制文件 out << in.rdbuf(); out.close(); in.close(); return true; } int CStdFile::ParseTXTFile(const std::string& strFilePath, std::list<std::string>& lContentInFile) { lContentInFile.clear(); std::ifstream in(strFilePath); std::string line; if (in) // 有该文件 { while (getline(in, line)) // line中不包括每行的换行符 { lContentInFile.push_back(line); } } in.close(); return (int)lContentInFile.size(); } int CStdFile::ParseTXTFile(const std::string& strFilePath, std::vector<std::string>& vContentInFile) { vContentInFile.clear(); std::ifstream in(strFilePath); std::string line; if (in) // 有该文件 { while (getline(in, line)) // line中不包括每行的换行符 { vContentInFile.push_back(line); } } in.close(); return (int)vContentInFile.size(); } int CStdFile::SaveTXTFile(const std::string& strTxtPath, std::list<std::string>& lContent, bool bAppend /*= false*/) { std::ofstream file; if (bAppend) { file.open(strTxtPath, std::ios::in | std::ios::out | std::ios::app | std::ios::ate); } else { file.open(strTxtPath, std::ios::in | std::ios::out | std::ios::trunc); } for (std::list<std::string>::iterator it = lContent.begin(); it != lContent.end(); ++it) { file << *it; } file.close(); return 0; } int CStdFile::SaveTXTFile(const std::string& strTxtPath, std::vector<std::string>& vContent, bool bAppend /*= false*/) { std::ofstream file; if (bAppend) { file.open(strTxtPath, std::ios::in | std::ios::out | std::ios::app | std::ios::ate); } else { file.open(strTxtPath, std::ios::in | std::ios::out | std::ios::trunc); } for (std::vector<std::string>::iterator it = vContent.begin(); it != vContent.end(); ++it) { file << *it; } file.close(); return 0; } int CStdFile::SaveTXTFile(const std::string& strTxtPath, const std::string& strLine, bool bAppend /*= false*/) { std::ofstream file; if (bAppend) { file.open(strTxtPath, std::ios::in | std::ios::out | std::ios::app | std::ios::ate); } else { file.open(strTxtPath, std::ios::in | std::ios::out | std::ios::trunc); } file << strLine; file.close(); return 0; } std::string ToString(const double& dValue) { char tmp[256]; sprintf_s(tmp, "%.10lf", dValue); return std::string(tmp); }