TODO:BAM -- line breaks TODO:BAM -- ToEncoding( const std::string& sIn, int maxLineLen = -1, int maxLen = -1 )
| 320 | // TODO:BAM -- line breaks |
| 321 | // TODO:BAM -- ToEncoding( const std::string& sIn, int maxLineLen = -1, int maxLen = -1 ) |
| 322 | std::string cQuotedPrintableEncoding::Encode(const std::string& sIn, int maxLineLen, int maxLen) |
| 323 | { |
| 324 | static const size_t _CHAR_AS_HEX_LEN = 2; |
| 325 | static const size_t _ENCODED_CHAR_LEN = _CHAR_AS_HEX_LEN + sizeof('='); |
| 326 | |
| 327 | std::string sOut; |
| 328 | sOut.resize(sIn.size() * _ENCODED_CHAR_LEN); // optimize. one char can turn into 3 |
| 329 | |
| 330 | std::string::const_iterator at; |
| 331 | std::string::size_type i = 0; |
| 332 | std::string::size_type lineLen = 0; |
| 333 | for (at = sIn.begin(); at != sIn.end(); ++at, lineLen += _ENCODED_CHAR_LEN) |
| 334 | { |
| 335 | if (NeedsEncoding(*at)) |
| 336 | { |
| 337 | ASSERT((unsigned char)*at <= 0xFF); |
| 338 | |
| 339 | std::string sTmp = EncodeChar(*at); |
| 340 | ASSERT(sTmp.length() == _CHAR_AS_HEX_LEN); |
| 341 | |
| 342 | sOut[i++] = '='; |
| 343 | |
| 344 | std::copy(&sTmp[0], &sTmp[_CHAR_AS_HEX_LEN], &sOut[i]); |
| 345 | ASSERT(_CHAR_AS_HEX_LEN > 0); |
| 346 | i += _CHAR_AS_HEX_LEN; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | sOut[i++] = *at; |
| 351 | } |
| 352 | |
| 353 | // |
| 354 | // if string is too long, just quit |
| 355 | // |
| 356 | if (maxLen != -1 && i >= maxLen - _ENCODED_CHAR_LEN) |
| 357 | { |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | // |
| 362 | // check line len |
| 363 | // |
| 364 | if (maxLineLen != -1 && lineLen >= maxLineLen - _ENCODED_CHAR_LEN) |
| 365 | { |
| 366 | // append EOL |
| 367 | sOut[i++] = '='; |
| 368 | sOut[i++] = '\r'; |
| 369 | sOut[i++] = '\n'; |
| 370 | lineLen = 0; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | sOut.resize(i); |
| 375 | |
| 376 | return sOut; |
| 377 | } |
| 378 | |
| 379 | std::string cMailMessageUtil::CreateEncodedText(const std::string& text) |