Use CAutoGeneralHandle
[TortoiseGit.git] / src / ResText / Utils.cpp
blobeafdb7f0af5a931b60fbddeb3deb80ff4ab39557
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006, 2012 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #include "StdAfx.h"
19 #include ".\utils.h"
20 #include "..\Utils\FormatMessageWrapper.h"
22 CUtils::CUtils(void)
26 CUtils::~CUtils(void)
30 void CUtils::StringExtend(LPTSTR str)
32 TCHAR * cPos = str;
35 cPos = _tcschr(cPos, '\\');
36 if (cPos)
38 memmove(cPos+1, cPos, _tcslen(cPos)*sizeof(TCHAR));
39 *cPos = '\\';
40 *(cPos+1) = '\\';
41 cPos++;
42 cPos++;
44 } while (cPos != NULL);
45 cPos = str;
48 cPos = _tcschr(cPos, '\n');
49 if (cPos)
51 memmove(cPos+1, cPos, _tcslen(cPos)*sizeof(TCHAR));
52 *cPos = '\\';
53 *(cPos+1) = 'n';
55 } while (cPos != NULL);
56 cPos = str;
59 cPos = _tcschr(cPos, '\r');
60 if (cPos)
62 memmove(cPos+1, cPos, _tcslen(cPos)*sizeof(TCHAR));
63 *cPos = '\\';
64 *(cPos+1) = 'r';
66 } while (cPos != NULL);
67 cPos = str;
70 cPos = _tcschr(cPos, '\t');
71 if (cPos)
73 memmove(cPos+1, cPos, _tcslen(cPos)*sizeof(TCHAR));
74 *cPos = '\\';
75 *(cPos+1) = 't';
77 } while (cPos != NULL);
78 cPos = str;
81 cPos = _tcschr(cPos, '"');
82 if (cPos)
84 memmove(cPos+1, cPos, _tcslen(cPos)*sizeof(TCHAR));
85 *cPos = '\\';
86 *(cPos+1) = '"';
87 cPos++;
88 cPos++;
90 } while (cPos != NULL);
93 void CUtils::StringCollapse(LPTSTR str)
95 TCHAR * cPos = str;
98 cPos = _tcschr(cPos, '\\');
99 if (cPos)
101 if (*(cPos+1) == 'n')
103 *cPos = '\n';
104 memmove(cPos+1, cPos+2, (_tcslen(cPos+2)+1)*sizeof(TCHAR));
106 else if (*(cPos+1) == 'r')
108 *cPos = '\r';
109 memmove(cPos+1, cPos+2, (_tcslen(cPos+2)+1)*sizeof(TCHAR));
111 else if (*(cPos+1) == 't')
113 *cPos = '\t';
114 memmove(cPos+1, cPos+2, (_tcslen(cPos+2)+1)*sizeof(TCHAR));
116 else if (*(cPos+1) == '"')
118 *cPos = '"';
119 memmove(cPos+1, cPos+2, (_tcslen(cPos+2)+1)*sizeof(TCHAR));
121 else if (*(cPos+1) == '\\')
123 *cPos = '\\';
124 memmove(cPos+1, cPos+2, (_tcslen(cPos+2)+1)*sizeof(TCHAR));
126 cPos++;
128 } while (cPos != NULL);
131 void CUtils::Error()
133 CFormatMessageWrapper errorDetails;
134 if (errorDetails)
135 _ftprintf (stderr, _T("%s\n"), (LPCTSTR)errorDetails);
138 void CUtils::SearchReplace(std::wstring& str, const std::wstring& toreplace, const std::wstring& replacewith)
140 std::wstring result;
141 std::wstring::size_type pos = 0;
142 for ( ; ; ) // while (true)
144 std::wstring::size_type next = str.find(toreplace, pos);
145 result.append(str, pos, next-pos);
146 if( next != std::string::npos )
148 result.append(replacewith);
149 pos = next + toreplace.size();
151 else
153 break; // exit loop
156 str.swap(result);