📄 datagen.cpp
字号:
#include <algorithm>
#include "Logger.h"
#include "DataGen.h"
#include <io.h>
#include <iostream>
#include "MyDB.h"
string itostr(int value) {
ostringstream stm;
stm << value;
return stm.str();
}
string trim(string s, string drop = " ") {
// trim right
s.erase(s.find_last_not_of(drop)+1);
// trim left
return s.erase(0,s.find_first_not_of(drop));
}
DataGen::DataGen() {
m_seprator = "→";
}
DataGen::~DataGen() {
MyDB::Destroy();
Logger::Destroy();
}
char * DataGen::ReadFile(string file) {
char *buffer = NULL;
FILE *mFile = fopen(file.c_str(), "rb");
if (mFile != NULL)
{
fseek(mFile, 0, SEEK_END);
long mFileSize = ftell(mFile);
fseek(mFile, 0, SEEK_SET);
buffer = new char[mFileSize];
fread(buffer, 1, mFileSize, mFile);
fclose(mFile);
}
return buffer;
}
void DataGen::GetAllLines(vector<string> &lines, Comment &comment) {
string sql = string("select name from Line");
CppSQLite3Query rst = MyDB::GetInstance()->Execute(sql);
while(!rst.eof()) {
const char * name = rst.fieldValue(0);
lines.push_back(string(name));
rst.nextRow();
}
sql = string("select * from comment");
rst = MyDB::GetInstance()->Execute(sql);
while(!rst.eof()) {
strcpy(comment.mCity , rst.fieldValue(0));
strcpy(comment.mAuthor , rst.fieldValue(1));
strcpy(comment.mTime , rst.fieldValue(2));
strcpy(comment.mComment , rst.fieldValue(3));
rst.nextRow();
}
}
void DataGen::ClearData() {
string sql = string("delete from Line");
MyDB::GetInstance()->Execute(sql);
sql = string("delete from SingleLineStop");
MyDB::GetInstance()->Execute(sql);
}
void DataGen::InitDir(char *dir) {
char files[255];
strcpy(files, dir);
strcat(files, "\\*.txt");
_finddata_t file;
long h_handle;
if ( (h_handle=_findfirst( files, &file))==-1l)
{
cerr<<"error, can't find"<<endl;
exit(1);
}
do
{
char *filename = file.name;
Logger::GetInstance()->Log(string("===处理文件:") + filename);
printf("正在处理文件:%s...\n", filename);
char *buffer = ReadFile(string(dir) + "\\" + filename);
if (buffer == NULL) return;
string fileContent(buffer);
unsigned int pos1 = 0;
unsigned int pos2 = fileContent.find("\r\n", 0);
pos1 = pos2 +2;
pos2 = fileContent.find("\r\n", pos1);
string name = trim(fileContent.substr(pos1, pos2-pos1));
pos1 = pos2 +2;
pos2 = fileContent.find("\r\n", pos1);
string comment = trim(fileContent.substr(pos1, pos2-pos1));
pos1 = pos2 +2;
pos2 = fileContent.find("\r\n", pos1);
string content;
if (pos2 != string::npos) {
content = trim(fileContent.substr(pos1, pos2-pos1));
} else {
content = trim(fileContent.substr(pos1));
}
if (content.size() == 0) {
pos1 = pos2 +2;
pos2 = fileContent.find("\r\n", pos1);
if (pos2 != string::npos) {
content = trim(fileContent.substr(pos1, pos2-pos1));
} else {
content = trim(fileContent.substr(pos1));
}
}
string content1 = "";
string content2 = "";
int type = ONE;
if (content.size()> 0) {
if (content.find("上行:")==0) {
type = TWO;
int pos3 = content.find("下行:", 1);
if (pos3 == string::npos) {
content1 = content;
pos1 = pos2 +2;
pos2 = fileContent.find("\r\n", pos1);
if (pos2 !=string::npos) {
content2 = trim(fileContent.substr(pos1, pos2-pos1));
} else {
content2 = trim(fileContent.substr(pos1));
}
} else {
content1 = trim(content.substr(0, pos3));
content2 = trim(content.substr(pos3));
}
} else if (content.find("环行:")==0) {
if (name.find("内环") != string::npos || name.find("内") == name.size()-2) {
type = CIRCLE_IN;
content1 = content;
} else if (name.find("外环") != string::npos || name.find("外") == name.size()-2) {
type = CIRCLE_OUT;
content1 = content;
} else {
type = CIRCLE;
content1 = content;
content2 = content1;
}
} else {
type = ONE;
content1 = content;
content2 = content1;
}
}
SaveLine(type, name, comment, content1, content2);
delete buffer;
} while ( _findnext( h_handle, &file )==0 && file.attrib == 32) ;
_findclose(h_handle);
}
void DataGen::SaveLine(const int type, const string name, const string comment, string content1, string content2) {
/* string sql = string("select * from Line where name like '%") + name + "%'";
CppSQLite3Query ret = MyDB::GetInstance()->Execute(sql);
if (!ret.eof()) return;
*/
string sql;
int pos = content1.find("站)");
if (pos != string::npos) content1 = trim(content1.substr(0, pos+3));
if (type != CIRCLE_IN && type != CIRCLE_OUT) {
pos = content2.find("站)");
if (pos != string::npos) content2 = trim(content2.substr(0, pos+3));
sql = string("insert into Line values('") + name + "', '" + comment + "', '" + content1 +
"', '" + content2 + "')";
MyDB::GetInstance()->Execute(sql);
SaveStops(type, name + "上行", content1);
SaveStops(type, name + "下行", content2);
} else {
sql = string("insert into Line values('") + name + "', '" + comment + "', '" + content1 +
"', '')";
MyDB::GetInstance()->Execute(sql);
SaveStops(type, name, content1);
}
printf(" 线路[%s]处理完毕。\n", name.c_str());
}
void DataGen::SaveStops(const int type, const string name, string line) {
line = line.substr(type!=ONE?6:0);
int pos = line.find("(共");
if (pos != string::npos) line = trim(line.substr(0, pos));
unsigned int pos1 = 0;
unsigned int pos2 = line.find(this->m_seprator);
vector<string> stops;
while (pos2 != string::npos) {
string stopstr = trim(line.substr(pos1, pos2-pos1));
stops.push_back(stopstr);
pos1 = pos2+m_seprator.size();
pos2 = line.find(this->m_seprator, pos1);
}
string stopstr = line.substr(pos1);
//pos = stopstr.find("(共");
//if (pos>0) stopstr = trim(stopstr.substr(0, pos));
stops.push_back(stopstr);
int curPos = 0;
bool bDouble = (type!=ONE && type!=CIRCLE);
if (name.find("下行") == string::npos) {
for (vector<string>::iterator it = stops.begin();it != stops.end();it++) {
string sql = string("insert into SingleLineStop values('") +name + "', " + itostr(curPos) +", '" +
(*it) + "')";
MyDB::GetInstance()->Execute(sql);
curPos++;
}
} else {
for (vector<string>::reverse_iterator it = stops.rbegin();it != stops.rend();it++) {
string sql = string("insert into SingleLineStop values('") + name + "', " + itostr(curPos) +", '" +
(*it) + "')";
MyDB::GetInstance()->Execute(sql);
curPos++;
}
}
}
void DataGen::DeleteLine(const char *oldName) {
char sql[3000];
sprintf(sql, "delete from Line where name = '%s'", oldName);
MyDB::GetInstance()->Execute(sql);
sprintf(sql, "delete from SingleLineStop where name='%s上行' or name='%s下行'", oldName, oldName);
MyDB::GetInstance()->Execute(sql);
}
void DataGen::SaveLine(const Line &line) {
char sql[3000];
sprintf(sql, "select name from Line where name = '%s'", line.mName.c_str());
CppSQLite3Query rst = MyDB::GetInstance()->Execute(sql);
if (!rst.eof()) {
throw "该线路已经存在";
}
try {
sprintf(sql, "insert into Line values('%s','%s','%s','%s',%s)", line.mName.c_str(), line.mComment.c_str(), line.mUp.c_str(),
line.mIsSame?"":line.mDown.c_str(),line.mIsSame?"1":"0 ");
MyDB::GetInstance()->Execute(sql);
if (line.mIsSame) {
SaveStops(line.mName + "上行", line.mUp, false);
SaveStops(line.mName + "下行", line.mUp, true);
//} else if (line.mDown.compare("") == 0) {
// UpdateStops(line.mName + "环行", line.mUp);
} else {
SaveStops(line.mName + "上行", line.mUp, false);
SaveStops(line.mName + "下行", line.mDown, false);
}
} catch (CppSQLite3Exception ex) {
throw "保存数据出错";
}
printf(" 线路[%s]处理完毕。\n", line.mName.c_str());
}
void DataGen::SaveStops(const string name, string line, bool isSame) {
if (line.size() == 0) return;
int pos = line.find("(共");
if (pos != string::npos) line = trim(line.substr(0, pos));
unsigned int pos1 = 0;
unsigned int pos2 = line.find(m_seprator);
vector<string> stops;
while (pos2 != string::npos) {
string stopstr = trim(line.substr(pos1, pos2-pos1));
stops.push_back(stopstr);
pos1 = pos2+m_seprator.size();
pos2 = line.find(m_seprator, pos1);
}
string stopstr = line.substr(pos1);
stops.push_back(stopstr);
int curPos = 0;
if (isSame && name.find("下行") != string::npos ) {
for (vector<string>::reverse_iterator it = stops.rbegin();it != stops.rend();it++) {
string sql = string("insert into SingleLineStop values('") + name + "', " + itostr(curPos) +", '" +
(*it) + "')";
MyDB::GetInstance()->Execute(sql);
curPos++;
}
} else {
for (vector<string>::iterator it = stops.begin();it != stops.end();it++) {
string sql = string("insert into SingleLineStop values('") +name + "', " + itostr(curPos) +", '" +
(*it) + "')";
MyDB::GetInstance()->Execute(sql);
curPos++;
}
}
}
void DataGen::UpdateComment(Comment &comment) {
MyDB::GetInstance()->Execute("delete from Comment");
char sql[3000];
sprintf(sql, "insert into Comment values('%s','%s','%s','%s')", comment.mCity, comment.mAuthor, comment.mTime, comment.mComment);
MyDB::GetInstance()->Execute(sql);
}
void DataGen::GetLine(const char *name, Line &line) {
string sql = string("select * from Line where name = '") + name + "'";
CppSQLite3Query rst = MyDB::GetInstance()->Execute(sql);
while(!rst.eof()) {
line.mName = rst.fieldValue(0);
line.mComment = rst.fieldValue(1);
line.mUp = rst.fieldValue(2);
line.mDown = rst.fieldValue(3);
line.mIsSame = rst.getIntField(4)==1;
rst.nextRow();
}
}
void DataGen::GetStops(const char *name, vector<string> &stops1, vector<string> &stops2) {
string sql = string("select stop from SingleLineStop where name = '") + name + "环行' or name='" + name + "上行'" ;
CppSQLite3Query rst = MyDB::GetInstance()->Execute(sql);
while(!rst.eof()) {
stops1.push_back(rst.fieldValue(0));
rst.nextRow();
}
sql = string("select stop from SingleLineStop where name = '") + name + "下行'";
rst = MyDB::GetInstance()->Execute(sql);
while(!rst.eof()) {
stops2.push_back(rst.fieldValue(0));
rst.nextRow();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -