Skip to content

Commit 1e21a20

Browse files
committed
add helper method to return a string representation of the encoding
1 parent f3c3a8c commit 1e21a20

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

TextFile.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// sktoolslib - common files for SK tools
22

3-
// Copyright (C) 2012, 2014, 2017-2022 - Stefan Kueng
3+
// Copyright (C) 2012, 2014, 2017-2023 - Stefan Kueng
44

55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -56,7 +56,7 @@ bool CTextFile::Save(LPCWSTR path, bool keepFileDate) const
5656
FILETIME lastWriteTime{};
5757
if (keepFileDate)
5858
{
59-
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
59+
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
6060
nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
6161
if (hFile != INVALID_HANDLE_VALUE)
6262
{
@@ -85,7 +85,7 @@ bool CTextFile::Save(LPCWSTR path, bool keepFileDate) const
8585
return true;
8686
}
8787

88-
bool CTextFile::Load(LPCWSTR path, UnicodeType &type, bool bUTF8, std::atomic_bool& bCancelled)
88+
bool CTextFile::Load(LPCWSTR path, UnicodeType &type, bool bUTF8, std::atomic_bool &bCancelled)
8989
{
9090
encoding = AutoType;
9191
type = AutoType;
@@ -283,7 +283,7 @@ bool CTextFile::Load(LPCWSTR path, UnicodeType &type, bool bUTF8, std::atomic_bo
283283
return false;
284284
}
285285
}
286-
else //if (encoding == ANSI)
286+
else // if (encoding == ANSI)
287287
{
288288
try
289289
{
@@ -435,12 +435,12 @@ CTextFile::UnicodeType CTextFile::CheckUnicodeType(BYTE *pBuffer, int cb) const
435435
{
436436
if (cb < 2)
437437
return Ansi;
438-
UINT16 *pVal16 = reinterpret_cast<UINT16 *>(pBuffer);
439-
UINT8 * pVal8 = reinterpret_cast<UINT8 *>(pVal16 + 1);
438+
UINT16 *pVal16 = reinterpret_cast<UINT16 *>(pBuffer);
439+
UINT8 *pVal8 = reinterpret_cast<UINT8 *>(pVal16 + 1);
440440
// scan the whole buffer for a 0x0000 sequence
441441
// if found, we assume a binary file
442-
int nNull = 0;
443-
int nDblNull = 0;
442+
int nNull = 0;
443+
int nDblNull = 0;
444444
for (int i = 0; i < (cb - 2); i = i + 2)
445445
{
446446
if (0x0000 == *pVal16++)
@@ -522,7 +522,7 @@ CTextFile::UnicodeType CTextFile::CheckUnicodeType(BYTE *pBuffer, int cb) const
522522
return Ansi;
523523
}
524524

525-
bool CTextFile::CalculateLines(std::atomic_bool& bCancelled)
525+
bool CTextFile::CalculateLines(std::atomic_bool &bCancelled)
526526
{
527527
// fill an array with starting positions for every line in the loaded file
528528
if (pFileBuf == nullptr)
@@ -593,6 +593,26 @@ std::wstring CTextFile::GetLineString(long lineNumber) const
593593
return line;
594594
}
595595

596+
std::wstring CTextFile::GetEncodingString(UnicodeType type)
597+
{
598+
switch (type)
599+
{
600+
case CTextFile::Ansi:
601+
return L"ANSI";
602+
case CTextFile::Unicode_Le:
603+
return L"UTF-16-LE";
604+
case CTextFile::Unicode_Be:
605+
return L"UTF-16-BE";
606+
case CTextFile::UTF8:
607+
return L"UTF8";
608+
case CTextFile::Binary:
609+
return L"BINARY";
610+
default:
611+
break;
612+
}
613+
return {};
614+
}
615+
596616
std::wstring CTextFile::GetFileNameWithoutExtension() const
597617
{
598618
return CPathUtils::GetFileNameWithoutExtension(filename);

TextFile.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// sktoolslib - common files for SK tools
22

3-
// Copyright (C) 2012, 2017-2022 - Stefan Kueng
3+
// Copyright (C) 2012, 2017-2023 - Stefan Kueng
44

55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -50,7 +50,7 @@ class CTextFile
5050
/**
5151
* Saves the file contents to disk at \c path.
5252
*/
53-
bool Save(LPCWSTR path, bool keepFileDate) const;
53+
bool Save(LPCWSTR path, bool keepFileDate) const;
5454

5555
/**
5656
* modifies the contents of a file.
@@ -59,17 +59,17 @@ class CTextFile
5959
* \param newLen length of the new file content in bytes
6060
* \note the old buffer is automatically freed.
6161
*/
62-
bool ContentsModified(std::unique_ptr<BYTE[]> pBuf, DWORD newLen);
62+
bool ContentsModified(std::unique_ptr<BYTE[]> pBuf, DWORD newLen);
6363

6464
/**
6565
* Returns the line number from a given character position inside the file.
6666
*/
67-
long LineFromPosition(long pos) const;
67+
long LineFromPosition(long pos) const;
6868

6969
/**
7070
* Returns the line from a given line number
7171
*/
72-
std::wstring GetLineString(long lineNumber) const;
72+
std::wstring GetLineString(long lineNumber) const;
7373

7474
/**
7575
* Returns the file content as a text string.
@@ -81,17 +81,22 @@ class CTextFile
8181
* Returns a pointer to the file contents. Call GetFileLength() to get
8282
* the size in number of bytes of this buffer.
8383
*/
84-
LPVOID GetFileContent() const { return pFileBuf.get(); }
84+
LPVOID GetFileContent() const { return pFileBuf.get(); }
8585

8686
/**
8787
* Returns the size of the file in bytes
8888
*/
89-
long GetFileLength() const { return fileLen; }
89+
long GetFileLength() const { return fileLen; }
9090

9191
/**
9292
* Returns the encoding of the file
9393
*/
94-
UnicodeType GetEncoding() const { return encoding; }
94+
UnicodeType GetEncoding() const { return encoding; }
95+
96+
/**
97+
* Returns a string representation of the encoding
98+
*/
99+
static std::wstring GetEncodingString(UnicodeType type);
95100

96101
/**
97102
* Returns the filename
@@ -101,26 +106,26 @@ class CTextFile
101106
/**
102107
* Returns the filename without the extension (if any)
103108
*/
104-
std::wstring GetFileNameWithoutExtension() const;
109+
std::wstring GetFileNameWithoutExtension() const;
105110

106111
/**
107112
* Returns the filename extension (if any)
108113
*/
109-
std::wstring GetFileNameExtension() const;
114+
std::wstring GetFileNameExtension() const;
110115

111116
/**
112117
* Replaces the file content.
113118
*/
114-
void SetFileContent(const std::wstring& content);
119+
void SetFileContent(const std::wstring& content);
115120

116-
bool HasBOM() const { return hasBOM; }
121+
bool HasBOM() const { return hasBOM; }
117122

118123
/**
119124
* Sets the number of null bytes that are allowed for
120125
* a file to still be considered text instead of binary
121126
* in the encoding detection. Default is 2.
122127
*/
123-
void SetNullbyteCountForBinary(int count) { nullByteCount = count; }
128+
void SetNullbyteCountForBinary(int count) { nullByteCount = count; }
124129

125130
protected:
126131
/**

0 commit comments

Comments
 (0)