C语言函数:找到字符串中指定字符串并替换,输出为DLL

// convert9.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "convert9.h"
#include "windows.h"
#include "stdio.h"
#include "string.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
)
{
    switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
    }
    return TRUE;
}
extern "C" _declspec(dllexport) int convert(char *convert, char *cont)
{
char StartFlag[5]="<";//左尖括号
    char EndFlag[5]=">";//右尖括号
    char flag_st[1000]="";//搜索存放左尖括号位置
    char flag_ed[1000]="";//搜索存放右尖括号位置
    char sharp_left[2]="<";
char sharp_right[2]=">";
char tmp[1000];//备份
char tmp1[1000];
char tmp2[1000];
    long unsigned num_pos=0;

//备份cont
strcpy(tmp, cont);
//左尖括号替换
    for (num_pos=0;num_pos < strlen(cont);)
    {
strcpy(flag_st,"");
strncat(flag_st, cont+num_pos, 4);
if (strcmp(flag_st, StartFlag) != 0)
{
num_pos++;
if (num_pos > strlen(cont))
{
printf("------------------------");
printf("Start flag not found !");
                 printf("------------------------");
break;
}
}
else
{
strcpy(tmp1, "");//tmp1变量初始化
             strncat(tmp1, cont, num_pos);//截止到"&lt;"前的内容存入变量tmp1
strcat(tmp1, sharp_left);//tmp1连接左尖括号存入tmp1
strcpy(tmp2, cont+num_pos+4);//将"&lt;"后的内容存入变量tmp2
strcpy(cont, tmp1);
strcat(cont, tmp2);//组合变量tmp1和tmp2内容,存入变量cont
}
}

//右边尖括号替换
for (num_pos=0; num_pos < strlen(cont);)
    {
strcpy(flag_ed, "");
strncat(flag_ed, cont+num_pos, 4);
if (strcmp(flag_ed, EndFlag) != 0)
{
num_pos++;
if (num_pos > strlen(cont))
{
printf("------------------------");
printf("End flag not found !");
                 printf("------------------------");
break;
}
}
else
{
strcpy(tmp1, "");//tmp1变量初始化
strncat(tmp1, cont, num_pos);//截止到"&lt;"前的内容存入变量tmp1
strcat(tmp1, sharp_right);//tmp1连接右尖括号存入tmp1
strcpy(tmp2, cont+num_pos+4);//将"&lt;"后的内容存入变量tmp2
strcpy(cont, tmp1);
strcat(cont, tmp2);//组合变量tmp1和tmp2内容,存入变量cont
}
    }
//结果存入conv
strcpy(convert, cont);
strcpy(cont, tmp);
return 0;
}

// This is an example of an exported variable
CONVERT9_API int nConvert9=0;

// This is an example of an exported function.
CONVERT9_API int fnConvert9(void)
{
return 42;
}

// This is the constructor of a class that has been exported.
// see convert9.h for the class definition
CConvert9::CConvert9()
{
return;
}

猜你喜欢

转载自ktc7000.iteye.com/blog/1753861