2 Helper functions for mangling file names in UDF/ECMA-167 file systems.
4 Copyright (C) 2014-2017 Paulo Alcantara <pcacjr@zytor.com>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
12 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Trim the leading and trailing spaces for a give Unicode string.
20 @param[in] String The Unicode string to trim.
22 @return A pointer to the trimmed string.
32 for ( ; *String
!= L
'\0' && *String
== L
' '; String
++) {
36 TempString
= String
+ StrLen (String
) - 1;
37 while ((TempString
>= String
) && (*TempString
== L
' ')) {
41 *(TempString
+ 1) = L
'\0';
47 Replace the content of a Unicode string with the content of another Unicode
50 @param[in] Destination A pointer to a Unicode string.
51 @param[in] Source A pointer to a Unicode string.
56 IN CHAR16
*Destination
,
57 IN CONST CHAR16
*Source
60 CONST CHAR16
*EndString
;
62 EndString
= Source
+ StrLen (Source
);
63 while (Source
<= EndString
) {
64 *Destination
++ = *Source
++;
69 Remove one or more consecutive backslashes starting from the second character
70 of a given Unicode string.
72 @param[in] String A pointer to a Unicode string.
74 @return A pointer to the modified string.
78 ExcludeTrailingBackslashes (
84 switch (*(String
+ 1)) {
94 while (*TempString
!= L
'\0' && *TempString
== L
'\\') {
98 if (TempString
- 1 > String
) {
99 ReplaceLeft (String
+ 1, TempString
);
109 Mangle a filename by cutting off trailing whitespaces, "\\", "." and "..".
111 @param[in] FileName Filename.
113 @retval The mangled Filename.
121 CHAR16
*FileNameSavedPointer
;
122 CHAR16
*TempFileName
;
125 if (FileName
== NULL
|| *FileName
== L
'\0') {
130 FileName
= TrimString (FileName
);
131 if (*FileName
== L
'\0') {
135 if ((StrLen (FileName
) > 1) && (FileName
[StrLen (FileName
) - 1] == L
'\\')) {
136 FileName
[StrLen (FileName
) - 1] = L
'\0';
139 FileNameSavedPointer
= FileName
;
141 if (FileName
[0] == L
'.') {
142 if (FileName
[1] == L
'.') {
143 if (FileName
[2] == L
'\0') {
148 } else if (FileName
[1] == L
'\0') {
153 while (*FileName
!= L
'\0') {
154 if (*FileName
== L
'\\') {
155 FileName
= ExcludeTrailingBackslashes (FileName
);
156 } else if (*FileName
== L
'.') {
157 switch (*(FileName
+ 1)) {
162 TempFileName
= FileName
+ 1;
163 TempFileName
= ExcludeTrailingBackslashes (TempFileName
);
164 ReplaceLeft (FileName
, TempFileName
);
167 if ((*(FileName
- 1) != L
'\\') && ((*(FileName
+ 2) != L
'\\') ||
168 (*(FileName
+ 2) != L
'\0'))) {
174 TempFileName
= FileName
- 1;
175 while (TempFileName
>= FileNameSavedPointer
) {
176 if (*TempFileName
== L
'\\') {
177 if (++BackslashesNo
== 2) {
187 if ((*TempFileName
== L
'.') && (*(TempFileName
+ 1) == L
'.')) {
190 if (*(FileName
+ 2) != L
'\0') {
191 ReplaceLeft (TempFileName
, FileName
+ 3);
192 if (*(TempFileName
- 1) == L
'\\') {
193 FileName
= TempFileName
;
194 ExcludeTrailingBackslashes (TempFileName
- 1);
195 TempFileName
= FileName
;
198 *TempFileName
= L
'\0';
201 FileName
= TempFileName
;
213 FileName
= FileNameSavedPointer
;
214 if ((StrLen (FileName
) > 1) && (FileName
[StrLen (FileName
) - 1] == L
'\\')) {
215 FileName
[StrLen (FileName
) - 1] = L
'\0';