| 41 | #include <iomanip> |
| 42 | |
| 43 | TSTRING cDisplayUtil::FormatMultiLineString(const TSTRING& str, int nOffset, int nWidth) |
| 44 | { |
| 45 | TOSTRINGSTREAM sstr; |
| 46 | TSTRING strT; |
| 47 | bool fFirstLine = true; |
| 48 | for (TSTRING::const_iterator i = str.begin(); i != str.end(); i = *i ? i + 1 : i) |
| 49 | { |
| 50 | // return found -- add line to output string |
| 51 | if (_T('\n') == *i) |
| 52 | { |
| 53 | // only do offset for strings after the first |
| 54 | if (fFirstLine) |
| 55 | { |
| 56 | fFirstLine = false; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | // add offset |
| 61 | for (int j = 0; j < nOffset; j++) |
| 62 | sstr << _T(" "); |
| 63 | |
| 64 | // set width |
| 65 | sstr << std::setw(nWidth); |
| 66 | } |
| 67 | |
| 68 | // add to stringstream |
| 69 | sstr << strT << std::endl; |
| 70 | |
| 71 | // erase temp string |
| 72 | strT.erase(); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | // add char to string |
| 77 | strT.append(i, (TSTRING::const_iterator)(*i ? i + 1 : i)); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // add last portion -- no endl |
| 82 | // we want our client to be able to say "out << PropAsString() << endl;" |
| 83 | |
| 84 | // add offset |
| 85 | if (!fFirstLine) |
| 86 | { |
| 87 | for (int j = 0; j < nOffset; j++) |
| 88 | sstr << _T(" "); |
| 89 | } |
| 90 | |
| 91 | // set width |
| 92 | sstr << std::setw(nWidth); |
| 93 | |
| 94 | // now add last string |
| 95 | sstr << strT; |
| 96 | |
| 97 | return (sstr.str()); |
| 98 | } |