博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
删除HTML标记
阅读量:7040 次
发布时间:2019-06-28

本文共 2841 字,大约阅读时间需要 9 分钟。

#ifndef REQUIRE_H
#define REQUIRE_H
#include <cstdio>
#include <cstdlib>
#include <fstream>
inline void require(bool requirement,const char* msg="Requirement failed")
{
//为旧式编译器提供的局部语句"using namespace std":
using namespace std;
if(!requirement)
{
fputs(msg,stderr);
fputs("\n",stderr);
exit(EXIT_FAILURE);
}
}
inline void requireArgs(int argc,int args,const char* msg="Must use %d arguments")
{
using namespace std;
if(argc!=args+1)
{
fprintf(stderr,msg,args);
fputs("\n",stderr);
exit(EXIT_FAILURE);
}
}
inline void requireMinArgs(int argc,int minArgs,const char* msg="Must use at lease %d arguments")
{
using namespace std;
if(argc<minArgs+1)
{
fprintf(stderr,msg,minArgs);
fputs("\n",stderr);
exit(EXIT_FAILURE);
}
}
//三个重载的assure函数,分别用于ifstream,ofstream,fstream文件流的存在合法性检测
inline void assure(std::ifstream& in,const char* filename="")
{
using namespace std;
if(!in)
{
fprintf(stderr,"Could not open file%s\n",filename);
exit(EXIT_FAILURE);
}
}
inline void assure(std::ofstream& in,const char* filename="")
{
using namespace std;
if(!in)
{
fprintf(stderr,"Could not open file%s\n",filename);
exit(EXIT_FAILURE);
}
}
inline void assure(std::fstream& in,const char* filename="")
{
using namespace std;
if(!in)
{
fprintf(stderr,"Could not open file%s\n",filename);
exit(EXIT_FAILURE);
}
}

#endif

#ifndef REPLACEALL_H

#define REPLACEALL_H
#include <string>
using std::string;
std::string& replaceAll(std::string& context,const std::string& from,const std::string& to)
{
size_t lookHere=0;
size_t foundHere;
while((foundHere=context.find(from,lookHere))!=string::npos)
{
context.replace(foundHere,from.size(),to);
lookHere=foundHere+to.size();
}
return context;
}
#endif

#include <cstddef>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include "ReplaceAll.h"
#include "require.h"
using namespace std;
string& stripHTMLTags(string& s) throw(runtime_error)//-->warning
{
size_t leftPos;
while((leftPos=s.find('<'))!=string::npos)
{
size_t rightPos=s.find('>',leftPos+1);
if(rightPos==string::npos)
{
ostringstream msg;
msg<<"Incomplete HTML tag starting in position "
<<leftPos;
throw runtime_error(msg.str());
}
s.erase(leftPos,rightPos-leftPos+1);
}
//移除所有的特殊HTML字符
replaceAll(s,"&lt","<");
replaceAll(s,"&gt",">");
replaceAll(s,"&amp","&");
replaceAll(s,"&nbsp"," ");
//Etc...
return s;
}
int main(int argc,char* argv[1])
{
requireArgs(argc,1,"usage: HTMLStripper2 InputFile");//这种方法应该在命令行(进入当前可执行文件目录后)中输入删除HTML标记.exe test1.html
ifstream in(argv[1]);
assure(in,argv[1]);
//读取整个文件到字符串,然后分割字符串
ostringstream ss;
ss<<in.rdbuf();
try
{
string s=ss.str();
cout<<stripHTMLTags(s)<<endl;
return EXIT_SUCCESS;
}
catch(runtime_error& x)
{
cout<<x.what()<<endl;
return EXIT_FAILURE;
}
system("pause");
return 0;
}

转载于:https://www.cnblogs.com/yuanmu/archive/2012/11/16/5447338.html

你可能感兴趣的文章
jQuery开发者眼中的AngularJS
查看>>
【DAY9】 关于多线程熊吃蜜Demo1的作业实验
查看>>
Python实现多属性排序
查看>>
nginx 访问日志分析
查看>>
RabbitMQ之消息确认机制(事务+Confirm)
查看>>
给出一个数组,计算数组中少了哪个数据的实现
查看>>
USB-232卡 配置
查看>>
C#窗体程序皮肤设置
查看>>
T-SQL.字符串函数
查看>>
mysql慢查询
查看>>
offices文件打开乱码问题如何处理
查看>>
抓屏程序
查看>>
many-to-many出现的问题
查看>>
第5章 配置邮箱服务
查看>>
node.js的一个简单框架
查看>>
PPT如何保存还原已剪裁图片的原始版本
查看>>
lnmp一键安装之-php
查看>>
ajax 同步和异步的区别
查看>>
linux shell单引号、双引号及无引号区别(考试题答案系列)--看到这篇文章之后我豁然开朗...
查看>>
排错 zabbix-agent 主机重启无法被监控
查看>>