2009-11-17 28 views
0

我试图使用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/>&nbsp; 
<img src="Images/MyLogoReflected.png" alt="logo image"/> 
<br />&nbsp; 
<img src="Images/PDC_StatusPage.png" alt="remote status page image" /> 
<br />&nbsp; 
<img src="Images/PDC_RemoteConfiguration.png" alt="remote config image" /> 
<br />&nbsp; 
%s 
<br />&nbsp; 
<img src="Images/PDC_RemoteSubsystemStatus.png" alt="remote status image" /> 
<br />&nbsp; 
%s 
<br />&nbsp; 
<img src="Images/PDC_RemoteConnectivityStatus.png" alt="remote status image" /> 
<br />&nbsp; 
%s 
<br />&nbsp; 
</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; 
} 
+2

除了CSS%S,你也有一个无效的转义'&'在标题中,并且如果将文本字符串格式化为不带'&'的HTML文本,则可能存在跨站点脚本安全漏洞。 – bobince 2009-11-17 21:03:59

+0

你如何将该字符串传递到Boost.Format?这是一个文字吗?如果是这样的话,你就有“内部的字符,它们会导致错误,如果你正在从一个文件中读取,你确定你正在读取整个文件,而不是用空格或换行符分隔吗?” – coppro 2009-11-17 21:10:13

回答

4

boost::format使用%format specifications;所以你的CSS中的%字符令人困惑;它试图将它们评估为格式规范。你需要用%%替换那些在输出中只是字面%的字符。

如果这没有帮助,那么我会建议尝试将您的模板编辑成更小的部分,直到找到尽可能小的部分来演示问题。一旦你这样做,你可能会发现自己的问题,但如果没有,编辑您的帖子,以包括仍然显示问题的小片段(最好是1或2行,每个小于80个字符),以及确切的错误你从Boost获得。同样,如果您发布了用于读取文件的代码片段并调用boost::format;一个只有几行代码的完整程序用于读取模板,并打印boost::format的输出,可以让我们看到代码中是否有可能导致问题的任何东西(并且可能会帮助您隔离问题你自己)。

您发布的代码(编辑了一点,所以它的实际工作,你离开了的buffererr声明),工作只是与您发布,如果所有的CSS的%标志被%%替换的模板罚款,正如我最初的建议。下面是编辑的代码(包括输出,以检查它的工作原理):

#include <string> 
#include <fstream> 
#include <boost/algorithm/string.hpp> 
#include <boost/format.hpp> 
#include <iostream> 

int main() 
{ 
    std::ifstream ifs("template.html"); // the html is in that file 
    std::string buffer, err; 

    if(!ifs.good()) 
     return 1; 

    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(); 
     std::cout << err << std::endl; 
     return 1; 
    } 

    std::cout << Out; 

    return 0; 
} 

而这里的编辑模板:

<!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/>&nbsp; 
<img src="Images/MyLogoReflected.png" alt="logo image"/> 
<br />&nbsp; 
<img src="Images/PDC_StatusPage.png" alt="remote status page image" /> 
<br />&nbsp; 
<img src="Images/PDC_RemoteConfiguration.png" alt="remote config image" /> 
<br />&nbsp; 
%s 
<br />&nbsp; 
<img src="Images/PDC_RemoteSubsystemStatus.png" alt="remote status image" /> 
<br />&nbsp; 
%s 
<br />&nbsp; 
<img src="Images/PDC_RemoteConnectivityStatus.png" alt="remote status image" /> 
<br />&nbsp; 
%s 
<br />&nbsp; 
</div> 
</body> 
</html> 
+0

我按照你的建议。它没有帮助 - 字符串仍然不合格PS。VS现在说,100 %%不是一个有效的高度值等是预期的吗? – Maciek 2009-11-17 21:00:31

+1

是的,因为100 %%不是一个当它通过Boost.Format时,它会变成有效的,因为它取代了double% – coppro 2009-11-17 21:09:11

+0

我已经添加了一些关于如何减少问题的注释,以帮助我们隔离发生了什么。消息,以及可能的代码片段,演示如何在文件中读取并调用'boost :: format',也会有所帮助。 – 2009-11-17 21:29:44

0

我已经成功地找到问题。

优化文件读取后&修复%字符它工作正常。非常感谢你们。

这是一个很酷的片段,我就如何一次读取整个文件的字符串:)

buffer = std::string((std::istreambuf_iterator<char>(ifs)), 
          std::istreambuf_iterator<char>());