您的位置:首页 > 编程语言 > C语言/C++

解决VC++ MFC程序resource.h头文件中ID重复问题

2012-12-05 16:02 381 查看
一般MFC开发的时候,如果有些资源是从其他工程中移植到本工程的,

而在资源移植的时候都要将对应的资源ID复制到本工程的resource.h文件中。

此时不管你在不在本工程中添加资源ID,resource.h文件中的ID可能都有重复的,一般再添加资源ID后,肯定有重复的。

可用下列代码解决此问题,将代码复制到空的win32控制台应用程序,将ressource.h拖入编译后产生可执行文件,即可解决此问题。

#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[])
{
char * pfname=NULL;
if(argc>=2)
pfname = argv[1];
else
pfname = "resource.h";
ifstream is(pfname);
if(!is)
{
handle_error("ifstream:open file failed");
}
///文件读开始处理
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(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();
handle_error("ostream:Open file failed");
}
for(it = l.begin();it != l.end();it ++)
{
if(it ->isdef)
{
if(!it ->ishex)
sprintf(temp,"%d",it ->line_id_value);
else
sprintf(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;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: