void splittext(const char *text, u32t width, TStrings &lst, u32t flags) {
   lst.Clear();
   if (!text || width<8) return;
   const char *ps = text, *pps;
   char    softcr = flags&SplitText_HelpMode?'&':0;
   int         ln = 0;
   spstr linehead;
   lst.Add(spstr());
   do {
      while (*ps && (strchr(AllowSet,*ps) || *ps==softcr)) {
         register char cr=*ps++;
         if (cr!=' ') {
            if (cr==softcr) {
               pps = ps;
               if (*pps) pps++;
               while (*pps==' ' || *pps=='\t') pps++;
               /* get 1 char after eol and all spaces after it and use result
                  as a header for all next lines until real cr */
               linehead = spstr(ps, pps-ps).expandtabs(4);
               ps = pps;
            } else {
               lst.Add(spstr());
               if (cr=='\r' && *ps=='\n') ps++;
               linehead.clear();
               if (lst[ln].lastchar()==' ') lst[ln].dellast();
               ln++;
            }
         }
      }
      if (*ps==0) break;
      pps = strpbrk(ps, softcr ? CarrySet AllowSet "&" : CarrySet AllowSet);
      int carry = pps&&strchr(CarrySet,*pps)?1:0;
      spstr curr(ps,pps?pps-ps+carry:0), sum;
      sum = lst[ln]+curr;
      sum.expandtabs(4);

      if ((flags&SplitText_NoAnsi?strlen:str_length)(sum())<width-2)
         lst[ln]=sum;
      else {
         lst.Add(linehead+curr);
         if (lst[ln].lastchar()==' ') lst[ln].dellast();
         ln++;
      }
      if (!carry||pps&&pps[1]==' ') lst[ln]+=' ';
      ps=pps;
      if (carry) ps++;
   } while (pps);
}
Exemple #2
0
std::string HtmlStrDecode(const std::string& htmlStr, const char* encTo, bool decodeChar){

    if(htmlStr.empty() || encTo == NULL)
        return "";

    std::string destStr = htmlStr;
    std::string tmpStr = destStr; 
    boost::smatch what;

    // dec unicode convert  &#24180;
    boost::regex unicodeDecReg("&#\\d{1,6};");
    std::string::const_iterator start = tmpStr.begin();
    std::string::const_iterator end = tmpStr.end();
    while ( boost::regex_search(start, end, what, unicodeDecReg) )
    {
        // split unicode number
        std::string unicode(what[0].str(), 2, what[0].str().size() - 3);
        // convert unicode to char
        std::string dest = ConvChar(atoi(unicode.c_str()), encTo, "UTF-16");
        // find next unicode number
        start = what[0].second;
        // replace html unicode to char
        boost::replace_all(destStr, what[0].str(), dest);
    }

    // standard unicode convert  \u5e74
    boost::regex unicodeReg("\\\\u\\w{1,4}");
    tmpStr = destStr; start = tmpStr.begin(); end = tmpStr.end();
    while ( boost::regex_search(start, end, what, unicodeReg) )
    {
        std::string str(what[0].str(), 2, what[0].str().size() - 2);
        uint32_t unicode = hex_to_decimal(str.c_str(), str.size());
        std::string dest = ConvChar(unicode, encTo, "UTF-16");
        boost::replace_all(destStr, what[0].str(), dest);
        start = what[0].second;
    }

    // Hex unicode convert  &#x5e74;
    boost::regex unicodeHexReg("&#x\\w{1,4}");
    tmpStr = destStr; start = tmpStr.begin(); end = tmpStr.end();
    while ( boost::regex_search(start, end, what, unicodeHexReg) )
    {
        // split unicode number
        std::string str(what[0].str(), 3, what[0].str().size() - 3);
        uint32_t unicode = hex_to_decimal(str.c_str(), str.size());
        std::string dest = ConvChar(unicode, encTo, "UTF-16");
        boost::replace_all(destStr, what[0].str(), dest);
        start = what[0].second;
    }

    // special char convert  &nbsp;
    boost::regex spCharReg("&[A-Za-z0-9]{2,8};");
    tmpStr = destStr; start = tmpStr.begin(); end = tmpStr.end();
    while ( boost::regex_search(start, end, what, spCharReg) )
    {
        std::string spstr(what[0].str(), 1, what[0].str().size() - 2);
        std::map<std::string, uint32_t>::const_iterator it = HtmlSpMap.find(spstr);
        if(HtmlSpMap.end() !=  it){
            std::string dest = ConvChar(it->second, encTo, "UTF-16");
            boost::replace_all(destStr, what[0].str(), dest);
        }
        start = what[0].second;
    }

    return destStr;
}