StdStrFile

#pragma once
#include <list>
#include <string>
#include <vector>
#include <sstream>

class CStdStrFile
{
public:
    //字符操作
    std::string AddSlashIfNeeded(const std::string strDir, const char& cDir = ‘\\‘);
    std::string GetDirOfDir(const std::string& strDir, const char& cDir = ‘\\‘);
    std::string GetDirOfFile(const std::string& strFile, const char& cDir = ‘\\‘);
    std::string GetSuffixOfFile(const std::string& strFile, bool bWithDot = true);
    std::string GetNameOfDir(const std::string& strDir, const char& cDir = ‘\\‘);
    std::string GetNameOfFile(const std::string& strFile, const bool& bWithSuffix = true, const char& cDir = ‘\\‘);
    std::string ReplaceSuffix(const std::string& strFilePath, const std::string& strNewSuffix);
    std::string Trim(const std::string& strOri, const char& cToTrim = ‘ ‘);
    std::string TrimHead(const std::string& strOri, const char& cToTrim = ‘ ‘);
    std::string TrimTail(const std::string& strOri, const char& cToTrim = ‘ ‘);

    std::vector<std::string> Split(const std::string& str, const std::string& pattern);

    //文件操作
    bool CopyAFile(const std::string& strSrcFileName, std::string& strDstFileName, const bool& bFailIfExists);
    bool IfExistsFile(const std::string& strFilePath);

    int ParseTXTFile(const std::string& strFilePath, std::list<std::string>& lContentInFile);
    int ParseTXTFile(const std::string& strFilePath, std::vector<std::string>& vContentInFile);
    int SaveTXTFile(const std::string& strTxtPath, std::list<std::string>& lContent, bool bAppend = false);
    int SaveTXTFile(const std::string& strTxtPath, std::vector<std::string>& vContent, bool bAppend = false);
    int SaveTXTFile(const std::string& strTxtPath, const std::string& strLine, bool bAppend = false);

    template <class T>
    bool VectorContains(std::vector<T>& vTs, const T& value);
    template <class T>
    bool VectorContains(std::vector<T>& vTsSum, std::vector<T>& vTsPart);
    template <class T>
    int VectorFind(std::vector<T>& vTs, const T& value, bool bPositiveGoing = true);
    template <class T>
    int ConvertFromString(T &value, const std::string &s);
    template <class T>
    std::string ConvertToString(T value);
};

template <class T>
bool CStdStrFile::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 CStdStrFile::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 CStdStrFile::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 = vTs.size() - 1; i >= 0; --i)
        {
            if (vTs[i] == value)
            {
                return i;
            }
        }
    }

    return -1;
}

template <class T>
int CStdStrFile::ConvertFromString(T &value, const std::string &s)
{
    std::stringstream ss(s);
    ss >> value;

    return 0;
}

template <class T>
std::string CStdStrFile::ConvertToString(T value)
{
    std::stringstream ss;
    ss << value;

    return ss.str();
}

std::string ToString(const double& dValue);
#include "StdStrFile.h"
#include <fstream>

std::string CStdStrFile::GetDirOfDir(const std::string& strDir, const char& cDir/* = ‘\\‘*/)
{
    std::string strDirPath(strDir);

    TrimTail(strDirPath, cDir);

    int index = (int)strDirPath.rfind(cDir);

    if (index != -1)
    {
        return strDirPath.substr(0, index);
    }
    else
    {
        return strDirPath;
    }
}

std::string CStdStrFile::GetDirOfFile(const std::string& strFile, const char& cDir /*= ‘\\‘*/)
{
    std::string strFilePath(strFile);

    TrimTail(strFilePath, cDir);

    int index = (int)strFilePath.rfind(cDir);

    if (index != -1)
    {
        return strFilePath.substr(0, index);
    }
    else
    {
        return strFilePath;
    }
}

std::string CStdStrFile::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 CStdStrFile::GetNameOfDir(const std::string& strDir, const char& cDir /*= ‘\\‘*/)
{
    std::string strDirPath(strDir);
    TrimTail(strDirPath, cDir);
    int index = (int)strDirPath.rfind(‘\\‘);

    return strDirPath.substr(index + 1, strDirPath.length() - index - 1);
}

std::string CStdStrFile::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 CStdStrFile::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 CStdStrFile::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 CStdStrFile::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);
        }
    }
}

bool CStdStrFile::IfExistsFile(const std::string& strFilePath)
{
    std::fstream _file;
    _file.open(strFilePath, std::ios::in);
    bool bRes = (_file != 0);
    _file.close();

    return bRes;
}

bool CStdStrFile::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 && IfExistsFile(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;
}

std::string CStdStrFile::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> CStdStrFile::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;
}

int CStdStrFile::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 CStdStrFile::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 CStdStrFile::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 CStdStrFile::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 CStdStrFile::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 CStdStrFile::AddSlashIfNeeded(const std::string strDir, const char& cDir/* = ‘\\‘*/)
{
    std::string strDirNew(strDir);
    if (strDir[strDir.length() - 1] != cDir)
    {
        strDirNew += cDir;
    }

    return strDirNew;
}

std::string ToString(const double& dValue)
{
    char tmp[256];
    sprintf_s(tmp, "%.10lf", dValue);

    return std::string(tmp);
}
时间: 2024-10-05 10:26:41

StdStrFile的相关文章

MfcStrFile

/* ******* MfcStrFile.h ********** ********* 字符串.文件.目录操作函数声明 ********** */ /* author: autumoon */ #pragma once #ifdef _X86_ #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' pro