我试图使用boost :: format,其中我的格式化字符串是下面的HTML。我打算在由%s占位符指定的位置插入3个std :: strings。boost :: format - 尝试使用HTML作为格式化字符串 - 需要一些帮助
换句话说 - 我打开下面的* .html文件进行阅读,将它的内容读入单个std :: string并将其用作格式化程序。接下来,我试图做到以下几点:
std::string output = (boost::format(formatter) % str1 % str2 % str3).str();
凡str1-3是含有我试图插入文本字符串 - 明显。 格式尝试会引发格式字符串格式不正确的异常。我一直试图分析它在过去2小时的更好的一部分,但我失败了,我需要一些帮助。
下面的HTML有什么问题 - 为什么它不能成为正确的格式化字符串?我应该知道哪些限制?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>KP&D</title>
<style type="text/css">
html, body
{
height: 100%;
margin: 0;
padding: 0;
}
img#bg
{
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
}
#content
{
position:relative;
z-index:1;
}
</style>
</head>
<body>
<img src="Images/PageBackground.png" alt="background image" id="bg" />
<div id="content">
<br/>
<img src="Images/MyLogoReflected.png" alt="logo image"/>
<br />
<img src="Images/PDC_StatusPage.png" alt="remote status page image" />
<br />
<img src="Images/PDC_RemoteConfiguration.png" alt="remote config image" />
<br />
%s
<br />
<img src="Images/PDC_RemoteSubsystemStatus.png" alt="remote status image" />
<br />
%s
<br />
<img src="Images/PDC_RemoteConnectivityStatus.png" alt="remote status image" />
<br />
%s
<br />
</div>
</body>
</html>
这里的负责加载forementioned文件的代码片段:
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
int main()
{
std::ifstream ifs("welcome.html"); // the html is in that file
if(!ifs.good())
return 1;
std::string buffer = "";
while(!ifs.eof())
{
char buf[256];
ifs.getline(buf, 256);
buffer += buf;
}
buffer = boost::trim_right_copy(buffer);
const std::string str1 = "aaa";
const std::string str2 = "bbb";
const std::string str3 = "ccc";
std::string Out = "";
try{
Out = (boost::format(buffer)
% str1
% str2
% str3
).str();
} catch(std::exception &e)
{
err = e.what();
return 1;
}
return 0;
}
除了CSS%S,你也有一个无效的转义'&'在标题中,并且如果将文本字符串格式化为不带'&'的HTML文本,则可能存在跨站点脚本安全漏洞。 – bobince 2009-11-17 21:03:59
你如何将该字符串传递到Boost.Format?这是一个文字吗?如果是这样的话,你就有“内部的字符,它们会导致错误,如果你正在从一个文件中读取,你确定你正在读取整个文件,而不是用空格或换行符分隔吗?” – coppro 2009-11-17 21:10:13