HTML(:12) 코드를 제거하는 간단한 코드입니다. <, >사이의 모든 문자를 제거합니다. 만약 #include <stdio.h>이라는 문자열이 주어진다면 <stdio.h>는 제거가 될겁니다.
&와 같은 escape 문자는 &를 만난뒤 다음 10칸이내에 ;이 있으면 escape 문자로 판단 제거하는 방법을 사용했습니다. 한마디로, 웹페이지 출력과 관련된 모든책임을 사용자에게 지우는 프로그램(:12)입니다.
아래의 프로그램(:12)은 쭈욱 생각을 나열하는 식으로 작성이 되었습니다. 이렇게 작성한데에는 아래의 코드를 리펙토링(:12)과 성능향상을 위한 예제로 사용하기 위함입니다. 이와 관련되어서는 별도의 문서를 작성할 계획입니다. 리펙토리 전의 코드이므로 Version 0.1로 하겠습니다.
tag_strip Version 0.1
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
using namespace std;
enum tag_token {TAG_LT = '<', TAG_GT='>'};
int main(int argc, char **argv)
{
int fd;
int readn;
int ridx;
int widx;
char rbuf[1024] = {0x00,};
char wbuf[1024] = {0x00,};
int lt_flag = 0;
// used tokenizing
char seps[] = "()|{}, \t.;&-[]\"\'";
char *tr;
int tag_status = 0;
int offset = 0;
if (argc != 2)
{
fprintf(stderr, "Usage : %s [file]\n", argv[0]);
return 1;
}
if ((fd = open(argv[1], O_RDONLY)) < 0)
{
return 1;
}
while ((readn = read(fd, rbuf+offset, 1023)) > 0)
{
ridx = 0;
widx = 0;
while(ridx < readn)
{
if (rbuf[ridx] == TAG_LT)
{
lt_flag++;
}
if (rbuf[ridx] == TAG_GT)
{
lt_flag--;
if (lt_flag == 0)
{
ridx++;
continue;
}
}
if (lt_flag == 1)
{
ridx++;
continue;
}
// Special Chracter 제거
if (rbuf[ridx] == '&')
{
int i =0;
char special[10] = {0x00,};
for(i = 0; i < 10; i++)
{
if (rbuf[ridx+i] == ';')
{
ridx = (ridx+i);
break;
}
}
}
if (rbuf[ridx] == '\n' || rbuf[ridx] == '\r')
wbuf[widx] = ' ';
else
wbuf[widx] = rbuf[ridx];
widx++;
ridx++;
}
tr = strtok(wbuf, seps);
while (tr != NULL)
{
printf("%s ", tr);
tr = strtok(NULL, seps);
}
memset(rbuf, 0x00, 1024);
memset(wbuf, 0x00, 1024);
}
return 0;
}
tag_strip Version 0.1
Recent Posts
Archive Posts
Tags