直接贴源码吧,去掉Resource.h中重复无效的ID
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
typedef struct {
string line_header;
string line_id_name;
int line_id_value;
bool ishex;//value值是16进制?
bool isdef;//是id定义字符串?
}FILE_ONE_LINE;
void handle_error(char* err)
{
cout << err << endl;
system("pause");
exit(-1);
}
bool chisblank(char ch)
{
return ch == '\t' ||
ch == '\r' ||
ch == '\n' ||
ch == 32/*空格*/ ||
ch == '\0';
}
int main(int argc, char* argv[])
{
string pfname;
if (argc >= 2)
pfname = argv[1];
else
pfname = "resource.h";
ifstream is(pfname);
if (!is)
{
string str = "ifstream:open file failed";
handle_error((char*)str.data());
}
///文件读开始处理
list<FILE_ONE_LINE> l;
char temp[2000];
char str[200];
while (!is.eof())
{
is.getline(temp, 2000);
FILE_ONE_LINE line;
line.ishex = false;
if (temp[0] == '#' &&
temp[1] == 'd' &&
temp[2] == 'e' &&
temp[3] == 'f' &&
temp[4] == 'i' &&
temp[5] == 'n' &&
temp[6] == 'e')
{
line.isdef = true;
char* p = temp;
int seg = 0;
while (1)
{
while (chisblank(*p) && (*p == 32 || *p == '\t'))
{
p++;
}
int i = 0;
for (; !chisblank(*p); i++, p++)
{
str[i] = *p;
}
if (seg == 0)
{
str[i] = '\0';
line.line_header = str;
}
else if (seg == 1)
{
for (; i < 40; i++)
str[i] = ' ';
str[i] = '\0';
line.line_id_name = str;
}
else if (seg == 2)
{
str[i] = '\0';
if (str[0] == '0' && str[1] == 'x')
{
line.ishex = true;
sscanf_s(str, "%x", &line.line_id_value);
}
else
line.line_id_value = atoi(str);
}
seg++;
if (*p == '\n' || *p == '\0' || seg > 2)
break;
}
l.push_back(line);
//cout<<line.line_header<<'\t'<<line.line_id_name<<'\t'<<line.line_id_value<<endl;
}
else
{
line.line_header = temp;
line.isdef = false;
l.push_back(line);
//cout<<line.line_header<<endl;
}
}
if (is)
is.close();
///文件读处理完毕
int index = -1;
list<FILE_ONE_LINE>::iterator it = l.begin();
for (; it != l.end(); it++)
{
if (index < 0)
index = it->line_id_value;
if (it->line_id_value >= 100 && index < 100)
index = it->line_id_value;
if (it->line_id_value >= 1000 && index < 1000)
index = it->line_id_value;
if (it->line_id_value >= 30000 && index < 30000)
index = it->line_id_value;
if (it->isdef)
it->line_id_value = index++;
}
///文件写开始
ofstream os(pfname);
if (!os)
{
is.close();
string str = "ostream:Open file failed";
handle_error((char*)str.data());
}
for (it = l.begin(); it != l.end(); it++)
{
if (it->isdef)
{
if (!it->ishex)
sprintf_s(temp, "%d", it->line_id_value);
else
sprintf_s(temp, "0x%04x", it->line_id_value);
os << it->line_header << " " << it->line_id_name << " " << temp << '\n';
}
else
{
os << it->line_header << '\n';
}
}
if (os)
os.close();
///文件写处理完毕
l.clear();
cout << "changed!OK!" << endl;
system("pause");
return 0;
}