-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwsUtilStr.pp
147 lines (127 loc) · 4.8 KB
/
wsUtilStr.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{ Copyright (C) 2020-2023 by Bill Stewart (bstewart at iname.com)
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
unit wsUtilStr;
interface
uses
Windows;
// Scans a string for digit characters and returns found digits in OutputStr;
// returns false if InputStr contains no digits
function GetDigitsInString(const InputStr: string; var OutputStr: string): Boolean;
// Returns I as a string
function IntToStr(const I: Integer): string;
// Removes trailing '\' from string unless string is 3 characters long and
// second character is ':'
function RemoveBackslashUnlessRoot(S: string): string;
// Converts the specified string to a Unicode string
function AnsiToUnicodeString(const S: RawByteString; const CodePage: UINT): string;
// Converts the specified Unicode string to a regular (multi-byte) string and
// returns the resulting multi-byte string
function UnicodeStringToAnsi(const S: string; const CodePage: UINT): RawByteString;
implementation
function GetDigitsInString(const InputStr: string; var OutputStr: string): Boolean;
const
Digits: string = '0123456789';
var
DigitStr: string;
I: Integer;
begin
DigitStr := '';
for I := 1 to Length(InputStr) do
if Pos(InputStr[I], Digits) > 0 then
DigitStr := DigitStr + InputStr[I];
result := DigitStr <> '';
if result then
OutputStr := DigitStr;
end;
function IntToStr(const I: Integer): string;
begin
Str(I, result);
end;
function RemoveBackslashUnlessRoot(S: string): string;
begin
result := '';
if s <> '' then
begin
if (Length(S) = 3) and (S[2] = ':') and (S[3] = '\') then
exit(S);
while S[Length(S)] = '\' do
SetLength(S, Length(S) - 1);
result := S;
end;
end;
function AnsiToUnicodeString(const S: RawByteString; const CodePage: UINT): string;
var
NumChars, BufSize: DWORD;
pBuffer: PChar;
begin
result := '';
// Get number of characters needed for buffer
NumChars := MultiByteToWideChar(CodePage, // UINT CodePage
0, // DWORD dwFlags
PAnsiChar(S), // LPCCH lpMultiByteStr
-1, // int cbMultiByte
nil, // LPWSTR lpWideCharStr
0); // int cchWideChar
if NumChars > 0 then
begin
BufSize := NumChars * SizeOf(Char);
GetMem(pBuffer, BufSize);
if MultiByteToWideChar(CodePage, // UINT CodePage
0, // DWORD dwFlags
PAnsiChar(S), // LPCCH lpMultiByteStr
-1, // int cbMultiByte
pBuffer, // LPWSTR lpWideCharStr
NumChars) > 0 then // int cchWideChar
begin
result := pBuffer;
end;
FreeMem(pBuffer);
end;
end;
function UnicodeStringToAnsi(const S: string; const CodePage: UINT): RawByteString;
var
NumChars, BufSize: DWORD;
pBuffer: PAnsiChar;
begin
result := '';
// Get number of characters needed for buffer
NumChars := WideCharToMultiByte(CodePage, // UINT CodePage
0, // DWORD dwFlags
PChar(S), // LPCWCH lpWideCharStr
-1, // int cchWideChar
nil, // LPSTR lpMultiByteStr
0, // int cbMultiByte
nil, // LPCCH lpDefaultChar
nil); // LPBOOL lpUsedDefaultChar
if NumChars > 0 then
begin
BufSize := NumChars * SizeOf(AnsiChar);
GetMem(pBuffer, BufSize);
if WideCharToMultiByte(CodePage, // UINT CodePage
0, // DWORD dwFlags
PChar(S), // LPCWCH lpWideCharStr
-1, // int cchWideChar
pBuffer, // LPSTR lpMultiByteStr
NumChars, // int cbMultiByte
nil, // LPCCH lpDefaultChar
nil) > 0 then // LPBOOL lpUsedDefaultChar
begin
result := pBuffer;
end;
FreeMem(pBuffer);
end;
end;
begin
end.