| 68 | |
| 69 | namespace xlsxDocumentCpp { |
| 70 | std::string copyTag(const std::string &sFrom, const std::string &sTo, const std::string &tag) { |
| 71 | const std::string tagToFindStart = "<" + tag; |
| 72 | const std::string tagToFindEnd = "</" + tag; |
| 73 | const std::string tagEnd = "</" + tag + ">"; |
| 74 | |
| 75 | // search all occurrences of tag in 'sFrom' |
| 76 | std::string sFromData = ""; |
| 77 | size_t startIndex = 0; |
| 78 | while (true) { |
| 79 | std::size_t startPos = sFrom.find(tagToFindStart, startIndex); |
| 80 | if (startPos != std::string::npos) { |
| 81 | std::size_t endPos = sFrom.find(tagToFindEnd, startPos); |
| 82 | std::string tagEndTmp = tagEnd; |
| 83 | if (endPos == std::string::npos) { // second try to find the ending, maybe it is "/>" |
| 84 | endPos = sFrom.find("/>", startPos); |
| 85 | tagEndTmp = "/>"; |
| 86 | } |
| 87 | if (endPos != std::string::npos) { |
| 88 | sFromData += sFrom.substr(startPos, endPos - startPos) + tagEndTmp; |
| 89 | startIndex = endPos + strlen(tagEndTmp.c_str()); |
| 90 | } |
| 91 | else { |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | else { |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | std::string sOut = sTo; // copy 'sTo' in the output string |
| 101 | |
| 102 | if (!sFromData.empty()) { // tag found in 'from'? |
| 103 | // search all occurrences of tag in 'sOut' and delete them |
| 104 | int firstPosTag = -1; |
| 105 | while (true) { |
| 106 | std::size_t startPos = sOut.find(tagToFindStart); |
| 107 | if (startPos != std::string::npos) { |
| 108 | std::size_t endPos = sOut.find(tagToFindEnd); |
| 109 | std::string tagEndTmp = tagEnd; |
| 110 | if (endPos == std::string::npos) { // second try to find the ending, maybe it is "/>" |
| 111 | endPos = sOut.find("/>", startPos); |
| 112 | tagEndTmp = "/>"; |
| 113 | } |
| 114 | if (endPos != std::string::npos) { |
| 115 | if (firstPosTag < 0) |
| 116 | firstPosTag = startPos; |
| 117 | std::string stringBefore = sOut.substr(0, startPos); |
| 118 | endPos += strlen(tagEndTmp.c_str()); |
| 119 | std::string stringAfter = sOut.substr(endPos, strlen(sOut.c_str()) - endPos); |
| 120 | sOut = stringBefore + stringAfter; |
| 121 | } |
| 122 | else { |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | else { |
| 127 | break; |