This commit is contained in:
Igor Pavlov
2022-01-22 18:43:09 +00:00
committed by Kornel
parent 52eeaf1ad6
commit c3529a41f5
88 changed files with 3474 additions and 435 deletions
+13 -2
View File
@@ -1096,14 +1096,14 @@ UString::UString(char c)
UString::UString(const wchar_t *s)
{
unsigned len = MyStringLen(s);
const unsigned len = MyStringLen(s);
SetStartLen(len);
wmemcpy(_chars, s, len + 1);
}
UString::UString(const char *s)
{
unsigned len = MyStringLen(s);
const unsigned len = MyStringLen(s);
SetStartLen(len);
wchar_t *chars = _chars;
for (unsigned i = 0; i < len; i++)
@@ -1111,6 +1111,17 @@ UString::UString(const char *s)
chars[len] = 0;
}
UString::UString(const AString &s)
{
const unsigned len = s.Len();
SetStartLen(len);
wchar_t *chars = _chars;
const char *s2 = s.Ptr();
for (unsigned i = 0; i < len; i++)
chars[i] = (unsigned char)s2[i];
chars[len] = 0;
}
UString::UString(const UString &s)
{
SetStartLen(s._len);
+83 -6
View File
@@ -1,7 +1,7 @@
// Common/String.h
// Common/MyString.h
#ifndef __COMMON_STRING_H
#define __COMMON_STRING_H
#ifndef __COMMON_MY_STRING_H
#define __COMMON_MY_STRING_H
#include <string.h>
@@ -15,6 +15,17 @@
#include "MyVector.h"
/* if (DEBUG_FSTRING_INHERITS_ASTRING is defined), then
FString inherits from AString, so we can find bugs related to FString at compile time.
DON'T define DEBUG_FSTRING_INHERITS_ASTRING in release code */
// #define DEBUG_FSTRING_INHERITS_ASTRING
#ifdef DEBUG_FSTRING_INHERITS_ASTRING
class FString;
#endif
#ifdef _MSC_VER
#ifdef _NATIVE_WCHAR_T_DEFINED
#define MY_NATIVE_WCHAR_T_DEFINED
@@ -290,6 +301,12 @@ class AString
FORBID_STRING_OPS_AString(long)
FORBID_STRING_OPS_AString(unsigned long)
#ifdef DEBUG_FSTRING_INHERITS_ASTRING
AString(const FString &s);
AString &operator=(const FString &s);
AString &operator+=(const FString &s);
#endif
public:
explicit AString();
explicit AString(char c);
@@ -550,12 +567,18 @@ class UString
FORBID_STRING_OPS_2(UString, char)
#ifdef DEBUG_FSTRING_INHERITS_ASTRING
UString(const FString &s);
UString &operator=(const FString &s);
UString &operator+=(const FString &s);
#endif
public:
UString();
explicit UString(wchar_t c);
explicit UString(char c);
explicit UString(const char *s);
// UString(const AString &s);
explicit UString(const AString &s);
UString(const wchar_t *s);
UString(const UString &s);
~UString() { MY_STRING_DELETE(_chars); }
@@ -795,7 +818,16 @@ class UString2
FORBID_STRING_OPS_UString2(short)
UString2 &operator=(wchar_t c);
UString2(wchar_t c);
UString2(const AString &s);
UString2 &operator=(const AString &s);
UString2 &operator+=(const AString &s);
#ifdef DEBUG_FSTRING_INHERITS_ASTRING
UString2(const FString &s);
UString2 &operator=(const FString &s);
UString2 &operator+=(const FString &s);
#endif
public:
UString2(): _chars(NULL), _len(0) {}
@@ -871,9 +903,11 @@ typedef CObjectVector<CSysString> CSysStringVector;
// ---------- FString ----------
#ifndef DEBUG_FSTRING_INHERITS_ASTRING
#ifdef _WIN32
#define USE_UNICODE_FSTRING
#endif
#endif
#ifdef USE_UNICODE_FSTRING
@@ -893,12 +927,55 @@ typedef CObjectVector<CSysString> CSysStringVector;
#define __FTEXT(quote) quote
typedef char FChar;
#ifdef DEBUG_FSTRING_INHERITS_ASTRING
class FString: public AString
{
// FString &operator=(const char *s);
FString &operator=(const AString &s);
// FString &operator+=(const AString &s);
public:
FString(const AString &s): AString(s.Ptr()) {}
FString(const FString &s): AString(s.Ptr()) {}
FString(const char *s): AString(s) {}
FString() {}
FString &operator=(const FString &s) { AString::operator=((const AString &)s); return *this; }
FString &operator=(char c) { AString::operator=(c); return *this; }
FString &operator+=(char c) { AString::operator+=(c); return *this; }
FString &operator+=(const FString &s) { AString::operator+=((const AString &)s); return *this; }
FString Left(unsigned count) const { return FString(AString::Left(count)); }
};
void operator+(const AString &s1, const FString &s2);
void operator+(const FString &s1, const AString &s2);
inline FString operator+(const FString &s1, const FString &s2)
{
AString s =(const AString &)s1 + (const AString &)s2;
return FString(s.Ptr());
// return FString((const AString &)s1 + (const AString &)s2);
}
inline FString operator+(const FString &s1, const FChar *s2)
{
return s1 + (FString)s2;
}
/*
inline FString operator+(const FChar *s1, const FString &s2)
{
return (FString)s1 + s2;
}
*/
inline FString fas2fs(const char *s) { return FString(s); }
#else // DEBUG_FSTRING_INHERITS_ASTRING
typedef AString FString;
#define fas2fs(_x_) (_x_)
#endif // DEBUG_FSTRING_INHERITS_ASTRING
UString fs2us(const FChar *s);
UString fs2us(const FString &s);
FString us2fs(const wchar_t *s);
#define fas2fs(_x_) (_x_)
#define fs2fas(_x_) (_x_)
#endif
+120 -38
View File
@@ -29,6 +29,70 @@ bool IsPath1PrefixedByPath2(const wchar_t *s1, const wchar_t *s2)
// #include <stdio.h>
/*
static int MyStringCompare_PathLinux(const wchar_t *s1, const wchar_t *s2) throw()
{
for (;;)
{
wchar_t c1 = *s1++;
wchar_t c2 = *s2++;
if (c1 != c2)
{
if (c1 == 0) return -1;
if (c2 == 0) return 1;
if (c1 == '/') c1 = 0;
if (c2 == '/') c2 = 0;
if (c1 < c2) return -1;
if (c1 > c2) return 1;
continue;
}
if (c1 == 0) return 0;
}
}
*/
static int MyStringCompare_Path(const wchar_t *s1, const wchar_t *s2) throw()
{
for (;;)
{
wchar_t c1 = *s1++;
wchar_t c2 = *s2++;
if (c1 != c2)
{
if (c1 == 0) return -1;
if (c2 == 0) return 1;
if (IS_PATH_SEPAR(c1)) c1 = 0;
if (IS_PATH_SEPAR(c2)) c2 = 0;
if (c1 < c2) return -1;
if (c1 > c2) return 1;
continue;
}
if (c1 == 0) return 0;
}
}
static int MyStringCompareNoCase_Path(const wchar_t *s1, const wchar_t *s2) throw()
{
for (;;)
{
wchar_t c1 = *s1++;
wchar_t c2 = *s2++;
if (c1 != c2)
{
if (c1 == 0) return -1;
if (c2 == 0) return 1;
if (IS_PATH_SEPAR(c1)) c1 = 0;
if (IS_PATH_SEPAR(c2)) c2 = 0;
c1 = MyCharUpper(c1);
c2 = MyCharUpper(c2);
if (c1 < c2) return -1;
if (c1 > c2) return 1;
continue;
}
if (c1 == 0) return 0;
}
}
int CompareFileNames(const wchar_t *s1, const wchar_t *s2) STRING_UNICODE_THROW
{
/*
@@ -37,9 +101,10 @@ int CompareFileNames(const wchar_t *s1, const wchar_t *s2) STRING_UNICODE_THROW
printf("\n S2: %ls", s2);
printf("\n");
*/
// 21.07 : we parse PATH_SEPARATOR so: 0 < PATH_SEPARATOR < 1
if (g_CaseSensitive)
return MyStringCompare(s1, s2);
return MyStringCompareNoCase(s1, s2);
return MyStringCompare_Path(s1, s2);
return MyStringCompareNoCase_Path(s1, s2);
}
#ifndef USE_UNICODE_FSTRING
@@ -47,9 +112,7 @@ int CompareFileNames(const char *s1, const char *s2)
{
const UString u1 = fs2us(s1);
const UString u2 = fs2us(s2);
if (g_CaseSensitive)
return MyStringCompare(u1, u2);
return MyStringCompareNoCase(u1, u2);
return CompareFileNames(u1, u2);
}
#endif
@@ -312,16 +375,18 @@ void CCensorNode::AddItem(bool include, CItem &item, int ignoreWildcardIndex)
subNode.AddItem(include, item, ignoreWildcardIndex - 1);
}
void CCensorNode::AddItem(bool include, const UString &path, bool recursive, bool forFile, bool forDir, bool wildcardMatching)
/*
void CCensorNode::AddItem(bool include, const UString &path, const CCensorPathProps &props)
{
CItem item;
SplitPathToParts(path, item.PathParts);
item.Recursive = recursive;
item.ForFile = forFile;
item.ForDir = forDir;
item.WildcardMatching = wildcardMatching;
item.Recursive = props.Recursive;
item.ForFile = props.ForFile;
item.ForDir = props.ForDir;
item.WildcardMatching = props.WildcardMatching;
AddItem(include, item);
}
*/
bool CCensorNode::NeedCheckSubDirs() const
{
@@ -439,21 +504,6 @@ bool CCensorNode::CheckPathToRoot(bool include, const UString &path, bool isFile
}
*/
void CCensorNode::AddItem2(bool include, const UString &path, bool recursive, bool wildcardMatching)
{
if (path.IsEmpty())
return;
bool forFile = true;
bool forFolder = true;
UString path2 (path);
if (IsPathSepar(path.Back()))
{
path2.DeleteBack();
forFile = false;
}
AddItem(include, path2, recursive, forFile, forFolder, wildcardMatching);
}
void CCensorNode::ExtendExclude(const CCensorNode &fromNodes)
{
ExcludeItems += fromNodes.ExcludeItems;
@@ -509,9 +559,12 @@ static unsigned GetNumPrefixParts(const UStringVector &pathParts)
{
if (pathParts.IsEmpty())
return 0;
/* empty last part could be removed already from (pathParts),
if there was tail path separator (slash) in original full path string. */
#ifdef _WIN32
if (IsDriveColonName(pathParts[0]))
return 1;
if (!pathParts[0].IsEmpty())
@@ -552,7 +605,8 @@ static unsigned GetNumPrefixParts(const UStringVector &pathParts)
#endif
}
void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &path, bool recursive, bool wildcardMatching)
void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &path,
const CCensorPathProps &props)
{
if (path.IsEmpty())
throw "Empty file path";
@@ -560,12 +614,26 @@ void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &pat
UStringVector pathParts;
SplitPathToParts(path, pathParts);
CCensorPathProps props2 = props;
bool forFile = true;
if (pathParts.Back().IsEmpty())
bool forDir = true;
const UString &back = pathParts.Back();
if (back.IsEmpty())
{
// we have tail path separator. So it's directory.
// we delete tail path separator here even for "\" and "c:\"
forFile = false;
pathParts.DeleteBack();
}
else
{
if (props.MarkMode == kMark_StrictFile
|| (props.MarkMode == kMark_StrictFile_IfWildcard
&& DoesNameContainWildcard(back)))
forDir = false;
}
UString prefix;
@@ -582,6 +650,7 @@ void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &pat
if (pathMode != k_AbsPath)
{
// detection of the number of Skip Parts for prefix
ignoreWildcardIndex = -1;
const unsigned numPrefixParts = GetNumPrefixParts(pathParts);
@@ -589,6 +658,7 @@ void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &pat
if (pathMode != k_FullPath)
{
// if absolute path, then all parts before last part will be in prefix
if (numPrefixParts != 0 && pathParts.Size() > numPrefixParts)
numSkipParts = pathParts.Size() - 1;
}
@@ -610,12 +680,13 @@ void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &pat
}
}
// we split (pathParts) to (prefix) and (pathParts).
for (unsigned i = 0; i < numSkipParts; i++)
{
{
const UString &front = pathParts.Front();
// WIN32 doesn't support wildcards in file names
if (wildcardMatching)
if (props.WildcardMatching)
if (i >= numPrefixParts && DoesNameContainWildcard(front))
break;
prefix += front;
@@ -640,17 +711,29 @@ void CCensor::AddItem(ECensorPathMode pathMode, bool include, const UString &pat
pathParts.Clear();
pathParts.Add(UString("*"));
forFile = true;
wildcardMatching = true;
recursive = false;
forDir = true;
props2.WildcardMatching = true;
props2.Recursive = false;
}
}
/*
// not possible now
if (!forDir && !forFile)
{
UString s ("file path was blocked for files and directories: ");
s += path;
throw s;
// return; // for debug : ignore item (don't create Item)
}
*/
CItem item;
item.PathParts = pathParts;
item.ForDir = true;
item.ForDir = forDir;
item.ForFile = forFile;
item.Recursive = recursive;
item.WildcardMatching = wildcardMatching;
item.Recursive = props2.Recursive;
item.WildcardMatching = props2.WildcardMatching;
Pairs[(unsigned)index].Head.AddItem(include, item, ignoreWildcardIndex);
}
@@ -691,18 +774,17 @@ void CCensor::AddPathsToCensor(ECensorPathMode censorPathMode)
FOR_VECTOR(i, CensorPaths)
{
const CCensorPath &cp = CensorPaths[i];
AddItem(censorPathMode, cp.Include, cp.Path, cp.Recursive, cp.WildcardMatching);
AddItem(censorPathMode, cp.Include, cp.Path, cp.Props);
}
CensorPaths.Clear();
}
void CCensor::AddPreItem(bool include, const UString &path, bool recursive, bool wildcardMatching)
void CCensor::AddPreItem(bool include, const UString &path, const CCensorPathProps &props)
{
CCensorPath &cp = CensorPaths.AddNew();
cp.Path = path;
cp.Include = include;
cp.Recursive = recursive;
cp.WildcardMatching = wildcardMatching;
cp.Props = props;
}
}
+49 -13
View File
@@ -52,6 +52,25 @@ struct CItem
};
const Byte kMark_FileOrDir = 0;
const Byte kMark_StrictFile = 1;
const Byte kMark_StrictFile_IfWildcard = 2;
struct CCensorPathProps
{
bool Recursive;
bool WildcardMatching;
Byte MarkMode;
CCensorPathProps():
Recursive(false),
WildcardMatching(true),
MarkMode(kMark_FileOrDir)
{}
};
class CCensorNode MY_UNCOPYABLE
{
CCensorNode *Parent;
@@ -94,8 +113,19 @@ public:
int FindSubNode(const UString &path) const;
void AddItem(bool include, CItem &item, int ignoreWildcardIndex = -1);
void AddItem(bool include, const UString &path, bool recursive, bool forFile, bool forDir, bool wildcardMatching);
void AddItem2(bool include, const UString &path, bool recursive, bool wildcardMatching);
// void AddItem(bool include, const UString &path, const CCensorPathProps &props);
void Add_Wildcard()
{
CItem item;
item.PathParts.Add(L"*");
item.Recursive = false;
item.ForFile = true;
item.ForDir = true;
item.WildcardMatching = true;
AddItem(
true // include
, item);
}
// NeedCheckSubDirs() returns true, if there are IncludeItems rules that affect items in subdirs
bool NeedCheckSubDirs() const;
@@ -144,14 +174,11 @@ struct CCensorPath
{
UString Path;
bool Include;
bool Recursive;
bool WildcardMatching;
CCensorPathProps Props;
CCensorPath():
Include(true),
Recursive(false),
WildcardMatching(true)
{}
Include(true)
{}
};
@@ -174,19 +201,28 @@ public:
bool AllAreRelative() const
{ return (Pairs.Size() == 1 && Pairs.Front().Prefix.IsEmpty()); }
void AddItem(ECensorPathMode pathMode, bool include, const UString &path, bool recursive, bool wildcardMatching);
void AddItem(ECensorPathMode pathMode, bool include, const UString &path, const CCensorPathProps &props);
// bool CheckPath(bool isAltStream, const UString &path, bool isFile) const;
void ExtendExclude();
void AddPathsToCensor(NWildcard::ECensorPathMode censorPathMode);
void AddPreItem(bool include, const UString &path, bool recursive, bool wildcardMatching);
void AddPreItem(const UString &path)
void AddPreItem(bool include, const UString &path, const CCensorPathProps &props);
void AddPreItem_NoWildcard(const UString &path)
{
AddPreItem(true, path, false, false);
CCensorPathProps props;
props.WildcardMatching = false;
AddPreItem(
true, // include
path, props);
}
void AddPreItem_Wildcard()
{
AddPreItem(true, UString("*"), false, true);
CCensorPathProps props;
// props.WildcardMatching = true;
AddPreItem(
true, // include
UString("*"), props);
}
};