代码暂时不完善,只实现了基本逻辑,字符串判断逻辑有缺陷
#include "iostream"
#include "fstream"
#include "io.h"
#include "string"
#include "direct.h"
#include "stdio.h"
#define NORMAL 0
#define ERROR -1
using namespace std;
class Struct_Export
{
public:
Struct_Export(const char* pathout)
{
setOutfile(pathout);
}
~Struct_Export()
{
;
}
int Struct_Export_Action()
{
}
void setOutfile(const char * path)
{
Outfile = string(path);
}
string getOutfile()
{
return Outfile;
}
int FindStructFromFile(const char* path)
{
ifstream infile;
ofstream outfile;
string data;
size_t index;
infile.open(path);
outfile.open(getOutfile().c_str(), ios::app);
while (!infile.eof())
{
getline(infile, data);
if ((index = data.find("struct")) != string::npos) //找到struct起始位置
{
string substr = data.substr(index + 6, data.length());
if (substr.find("{") != string::npos )
{
outfile << data << endl;
while (!infile.eof()) //打印当前struct的内容至output文件
{
getline(infile, data);
outfile << data << endl;
if (data.find("}") != string::npos) //打印至 } 结尾
{
break;
}
}
outfile << endl;
}
}
}
infile.close();
return NORMAL;
}
int FindFileFromDir(const char* strPath)
{
int iRet = 0;
intptr_t handle;
_finddata_t findData;
handle = _findfirst(strPath, &findData);
if (handle == -1)
{
cout << "Failed to find first file!
" << endl;
return NULL;
}
do
{
if (findData.attrib & _A_SUBDIR && !(strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0))
{
string subdir(strPath);
subdir.insert(subdir.find("*"), string(findData.name) + "\");
FindFileFromDir(subdir.c_str());
}
else if((strcmp(findData.name, ".") != 0 && strcmp(findData.name, "..") != 0))
{
string file_pathname(strPath);
file_pathname = file_pathname.substr(0, file_pathname.length() - 3) + string(findData.name);
iRet = FindStructFromFile(file_pathname.c_str());
if (iRet != NORMAL)
{
cout << "Call FindStrctFromFile failed!
" << endl;
return ERROR;
}
}
} while (_findnext(handle, &findData) == 0);
return NORMAL;
}
private:
string Outfile;
};
int main(int argc, char* argv[])
{
if (argc < 3)
{
cout << "Struct_export must have input!" << endl;
cout << "Struct_export -dir pathname" << endl;
exit(1);
}
int iRet = 0;
string dir = string(argv[2]);
char Output[_MAX_PATH];
_getcwd(Output, _MAX_PATH);
if (dir[dir.length()] != '\')
{
dir = dir + '\';
}
dir = dir + "*.*";
Struct_Export Export_Acion( (string(Output)+string("\stoutput.txt")).c_str() );
if (!strcmp(argv[1], "-dir"))
{
iRet = Export_Acion.FindFileFromDir(dir.c_str());
if (iRet != NORMAL)
{
cout << "Call Export_Acion.FindFileFromDir failed!" << endl;
}
}
return 0;
}