Fix typos
[TortoiseGit.git] / src / ResText / Utils.cpp
blob9dc90114a8dfef36da2493035219485e7c27e0db
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2019 - TortoiseGit
4 // Copyright (C) 2003-2006, 2012-2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "Utils.h"
21 #include "FormatMessageWrapper.h"
23 CUtils::CUtils()
27 CUtils::~CUtils()
31 void CUtils::StringExtend(LPWSTR str)
33 wchar_t* cPos = str;
36 cPos = wcschr(cPos, '\\');
37 if (cPos)
39 memmove(cPos + 1, cPos, (wcslen(cPos) + 1) * sizeof(wchar_t));
40 *cPos = '\\';
41 *(cPos+1) = '\\';
42 cPos++;
43 cPos++;
45 } while (cPos);
46 cPos = str;
49 cPos = wcschr(cPos, '\n');
50 if (cPos)
52 memmove(cPos + 1, cPos, (wcslen(cPos) + 1) * sizeof(wchar_t));
53 *cPos = '\\';
54 *(cPos+1) = 'n';
56 } while (cPos);
57 cPos = str;
60 cPos = wcschr(cPos, '\r');
61 if (cPos)
63 memmove(cPos + 1, cPos, (wcslen(cPos) + 1) * sizeof(wchar_t));
64 *cPos = '\\';
65 *(cPos+1) = 'r';
67 } while (cPos);
68 cPos = str;
71 cPos = wcschr(cPos, '\t');
72 if (cPos)
74 memmove(cPos + 1, cPos, (wcslen(cPos) + 1) * sizeof(wchar_t));
75 *cPos = '\\';
76 *(cPos+1) = 't';
78 } while (cPos);
79 cPos = str;
82 cPos = wcschr(cPos, '"');
83 if (cPos)
85 memmove(cPos + 1, cPos, (wcslen(cPos) + 1) * sizeof(wchar_t));
86 *cPos = '\\';
87 *(cPos+1) = '"';
88 cPos++;
89 cPos++;
91 } while (cPos);
94 void CUtils::StringCollapse(LPWSTR str)
96 wchar_t* cPos = str;
99 cPos = wcschr(cPos, '\\');
100 if (cPos)
102 if (*(cPos+1) == 'n')
104 *cPos = '\n';
105 memmove(cPos+1, cPos + 2, (wcslen(cPos + 2) + 1) * sizeof(wchar_t));
107 else if (*(cPos+1) == 'r')
109 *cPos = '\r';
110 memmove(cPos+1, cPos + 2, (wcslen(cPos + 2) +1 ) * sizeof(wchar_t));
112 else if (*(cPos+1) == 't')
114 *cPos = '\t';
115 memmove(cPos+1, cPos + 2, (wcslen(cPos + 2) +1 ) * sizeof(wchar_t));
117 else if (*(cPos+1) == '"')
119 *cPos = '"';
120 memmove(cPos+1, cPos + 2, (wcslen(cPos + 2) +1 ) * sizeof(wchar_t));
122 else if (*(cPos+1) == '\\')
124 *cPos = '\\';
125 memmove(cPos+1, cPos + 2, (wcslen(cPos + 2) +1 ) * sizeof(wchar_t));
127 cPos++;
129 } while (cPos);
132 void CUtils::Error()
134 CFormatMessageWrapper errorDetails;
135 if (errorDetails)
136 fwprintf(stderr, L"%s\n", static_cast<LPCWSTR>(errorDetails));
139 void CUtils::SearchReplace(std::wstring& str, const std::wstring& toreplace, const std::wstring& replacewith)
141 std::wstring result;
142 std::wstring::size_type pos = 0;
143 for ( ; ; ) // while (true)
145 std::wstring::size_type next = str.find(toreplace, pos);
146 result.append(str, pos, next-pos);
147 if( next != std::string::npos )
149 result.append(replacewith);
150 pos = next + toreplace.size();
152 else
154 break; // exit loop
157 str.swap(result);