Skip to content

Commit

Permalink
增加两个模板
Browse files Browse the repository at this point in the history
  • Loading branch information
scarsty committed Jun 11, 2018
1 parent 94c19c5 commit e83e05a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
22 changes: 15 additions & 7 deletions libconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ std::string convert::readStringFromFile(const std::string& filename)
FILE* fp = fopen(filename.c_str(), "rb");
if (!fp)
{
fprintf(stderr, "Can not open file %s\n", filename.c_str());
fprintf(stderr, "Cannot open file %s\n", filename.c_str());
return "";
}
fseek(fp, 0, SEEK_END);
Expand Down Expand Up @@ -39,7 +39,7 @@ void convert::writeStringAppendToFile(const std::string& str, FILE* fp)
fputc('\n', fp);
}

int convert::replaceString(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0/*=0*/)
int convert::replaceString(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0 /*=0*/)
{
int pos = s.find(oldstring, pos0);
if (pos >= 0)
Expand All @@ -65,15 +65,21 @@ int convert::replaceAllString(std::string& s, const std::string& oldstring, cons
void convert::replaceStringInFile(const std::string& oldfilename, const std::string& newfilename, const std::string& oldstring, const std::string& newstring)
{
std::string s = readStringFromFile(oldfilename);
if (s.length() <= 0) { return; }
if (s.length() <= 0)
{
return;
}
replaceString(s, oldstring, newstring);
writeStringToFile(s, newfilename);
}

void convert::replaceAllStringInFile(const std::string& oldfilename, const std::string& newfilename, const std::string& oldstring, const std::string& newstring)
{
std::string s = readStringFromFile(oldfilename);
if (s.length() <= 0) { return; }
if (s.length() <= 0)
{
return;
}
replaceAllString(s, oldstring, newstring);
writeStringToFile(s, newfilename);
}
Expand Down Expand Up @@ -162,14 +168,17 @@ std::vector<std::string> convert::splitString(std::string str, std::string patte
{
pattern = ",;| ";
}
str += pattern[0]; //扩展字符串以方便操作
str += pattern[0]; //扩展字符串以方便操作
bool have_space = pattern.find(" ") != std::string::npos;
int size = str.size();
for (int i = 0; i < size; i++)
{
if (have_space)
{
while (str[i] == ' ') { i++; } //当空格作为分隔符时,连续空格视为一个
while (str[i] == ' ')
{
i++;
} //当空格作为分隔符时,连续空格视为一个
}
pos = str.find_first_of(pattern, i);
if (pos < size)
Expand All @@ -195,4 +204,3 @@ bool convert::isProChar(char c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'z') || (c >= '(' && c <= ')');
}

26 changes: 21 additions & 5 deletions libconvert.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once
#include <string>
#include <vector>
#include <stdarg.h>
#include <stdlib.h>
#include <string>
#include <vector>

namespace convert
{
//string functions
std::string readStringFromFile(const std::string& filename);
void writeStringToFile(const std::string& str, const std::string& filename);
void writeStringAppendToFile(const std::string& str, FILE *fp);
void writeStringAppendToFile(const std::string& str, FILE* fp);
int replaceString(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0 = 0);
int replaceAllString(std::string& s, const std::string& oldstring, const std::string& newstring);
void replaceStringInFile(const std::string& oldfilename, const std::string& newfilename, const std::string& oldstring, const std::string& newstring);
Expand All @@ -21,7 +21,7 @@ unsigned findTheLast(const std::string& s, const std::string& content);
std::vector<std::string> splitString(std::string str, std::string pattern = "", bool ignore_psspace = true);
bool isProChar(char c);

template<typename T>
template <typename T>
int findNumbers(const std::string& s, std::vector<T>* data)
{
int n = 0;
Expand All @@ -35,7 +35,9 @@ int findNumbers(const std::string& s, std::vector<T>* data)
{
str += c;
if (c >= '0' && c <= '9')
{ haveNum = true; }
{
haveNum = true;
}
}
if (!findNumChar || i == s.length() - 1)
{
Expand All @@ -51,4 +53,18 @@ int findNumbers(const std::string& s, std::vector<T>* data)
}
return n;
}

template <typename T>
int findNumbers(const std::string& s, std::vector<T>& data)
{
return findNumbers(s, &data);
}

template <typename T>
std::vector<T> findNumbers(const std::string& s)
{
std::vector<T> data;
findNumbers(s, &data);
return data;
}
};

0 comments on commit e83e05a

Please sign in to comment.