Code: using enum class for FormatType

pull/113/merge
Damien GERARD 2015-08-14 05:57:19 -07:00
parent 3fb1d96671
commit 2ad6ba5a44
10 changed files with 188 additions and 104 deletions

View File

@ -1816,6 +1816,7 @@ void Notepad_plus::checkDocState()
bool isUserReadOnly = curBuf->getUserReadOnly();
::CheckMenuItem(_mainMenuHandle, IDM_EDIT_SETREADONLY, MF_BYCOMMAND | (isUserReadOnly?MF_CHECKED:MF_UNCHECKED));
}
enableCommand(IDM_FILE_DELETE, isFileExisting, MENU);
enableCommand(IDM_FILE_RENAME, isFileExisting, MENU);
@ -2116,23 +2117,20 @@ void Notepad_plus::setLangStatus(LangType langType)
}
void Notepad_plus::setDisplayFormat(formatType f)
void Notepad_plus::setDisplayFormat(FormatType format)
{
generic_string str;
switch (f)
const TCHAR* str = TEXT("??");
switch (format)
{
case MAC_FORMAT :
str = TEXT("Macintosh");
break;
case UNIX_FORMAT :
str = TEXT("UNIX");
break;
default :
str = TEXT("Dos\\Windows");
case FormatType::windows: str = TEXT("Dos\\Windows"); break;
case FormatType::macos: str = TEXT("Macintosh"); break;
case FormatType::unix: str = TEXT("UNIX"); break;
case FormatType::unknown: str = TEXT("Unknown"); assert(false); break;
}
_statusBar.setText(str.c_str(), STATUSBAR_EOF_FORMAT);
_statusBar.setText(str, STATUSBAR_EOF_FORMAT);
}
void Notepad_plus::setUniModeText()
{
Buffer *buf = _pEditView->getCurrentBuffer();
@ -3480,6 +3478,7 @@ void Notepad_plus::staticCheckMenuAndTB() const
checkMenuItem(IDM_VIEW_WRAP_SYMBOL, _pEditView->isWrapSymbolVisible());
}
void Notepad_plus::dynamicCheckMenuAndTB() const
{
//Format conversion
@ -3487,13 +3486,15 @@ void Notepad_plus::dynamicCheckMenuAndTB() const
checkUnicodeMenuItems();
}
void Notepad_plus::enableConvertMenuItems(formatType f) const
void Notepad_plus::enableConvertMenuItems(FormatType format) const
{
enableCommand(IDM_FORMAT_TODOS, (f != WIN_FORMAT), MENU);
enableCommand(IDM_FORMAT_TOUNIX, (f != UNIX_FORMAT), MENU);
enableCommand(IDM_FORMAT_TOMAC, (f != MAC_FORMAT), MENU);
enableCommand(IDM_FORMAT_TODOS, (format != FormatType::windows), MENU);
enableCommand(IDM_FORMAT_TOUNIX, (format != FormatType::unix), MENU);
enableCommand(IDM_FORMAT_TOMAC, (format != FormatType::macos), MENU);
}
void Notepad_plus::checkUnicodeMenuItems() const
{
Buffer *buf = _pEditView->getCurrentBuffer();

View File

@ -495,14 +495,14 @@ private:
void getMainClientRect(RECT & rc) const;
void staticCheckMenuAndTB() const;
void dynamicCheckMenuAndTB() const;
void enableConvertMenuItems(formatType f) const;
void enableConvertMenuItems(FormatType f) const;
void checkUnicodeMenuItems() const;
generic_string getLangDesc(LangType langType, bool getName = false);
void setLangStatus(LangType langType);
void setDisplayFormat(formatType f);
void setDisplayFormat(FormatType f);
int getCmdIDFromEncoding(int encoding) const;
void setUniModeText();
void checkLangsMenu(int id) const ;

View File

@ -318,19 +318,24 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lPa
return -1;
BufferID id = (BufferID)wParam;
Buffer * b = MainFileManager->getBufferByID(id);
return b->getFormat();
return static_cast<LRESULT>(b->getFormat());
}
case NPPM_SETBUFFERFORMAT:
{
if (!wParam)
return FALSE;
if (lParam < WIN_FORMAT || lParam >= UNIX_FORMAT)
FormatType newFormat = convertIntToFormatType(static_cast<int>(lParam), FormatType::unknown);
if (FormatType::unknown == newFormat)
{
assert(false and "invalid buffer format message");
return FALSE;
}
BufferID id = (BufferID)wParam;
Buffer * b = MainFileManager->getBufferByID(id);
b->setFormat((formatType)lParam);
b->setFormat(newFormat);
return TRUE;
}

View File

@ -1694,16 +1694,17 @@ void Notepad_plus::command(int id)
break;
}
case IDM_FORMAT_TODOS :
case IDM_FORMAT_TOUNIX :
case IDM_FORMAT_TOMAC :
case IDM_FORMAT_TODOS:
case IDM_FORMAT_TOUNIX:
case IDM_FORMAT_TOMAC:
{
Buffer * buf = _pEditView->getCurrentBuffer();
FormatType newFormat = (id == IDM_FORMAT_TODOS)
? FormatType::windows
: (id == IDM_FORMAT_TOUNIX) ? FormatType::unix : FormatType::macos;
int f = int((id == IDM_FORMAT_TODOS)?SC_EOL_CRLF:(id == IDM_FORMAT_TOUNIX)?SC_EOL_LF:SC_EOL_CR);
buf->setFormat((formatType)f);
_pEditView->execute(SCI_CONVERTEOLS, buf->getFormat());
Buffer* buf = _pEditView->getCurrentBuffer();
buf->setFormat(newFormat);
_pEditView->execute(SCI_CONVERTEOLS, static_cast<int>(buf->getFormat()));
break;
}

View File

@ -3953,7 +3953,24 @@ void NppParameters::feedGUIParameters(TiXmlNode *node)
{
int i;
if (element->Attribute(TEXT("format"), &i))
_nppGUI._newDocDefaultSettings._format = (formatType)i;
{
FormatType newFormat = FormatType::osdefault;
switch (i)
{
case static_cast<LPARAM>(FormatType::windows) :
newFormat = FormatType::windows;
break;
case static_cast<LPARAM>(FormatType::macos) :
newFormat = FormatType::macos;
break;
case static_cast<LPARAM>(FormatType::unix) :
newFormat = FormatType::unix;
break;
default:
assert(false and "invalid buffer format - fallback to default");
}
_nppGUI._newDocDefaultSettings._format = newFormat;
}
if (element->Attribute(TEXT("encoding"), &i))
_nppGUI._newDocDefaultSettings._unicodeMode = (UniMode)i;
@ -4992,7 +5009,7 @@ bool NppParameters::writeGUIParams()
}
else if (!lstrcmp(nm, TEXT("NewDocDefaultSettings")))
{
element->SetAttribute(TEXT("format"), _nppGUI._newDocDefaultSettings._format);
element->SetAttribute(TEXT("format"), static_cast<int>(_nppGUI._newDocDefaultSettings._format));
element->SetAttribute(TEXT("encoding"), _nppGUI._newDocDefaultSettings._unicodeMode);
element->SetAttribute(TEXT("lang"), _nppGUI._newDocDefaultSettings._lang);
element->SetAttribute(TEXT("codepage"), _nppGUI._newDocDefaultSettings._codepage);
@ -5268,7 +5285,7 @@ bool NppParameters::writeGUIParams()
{
TiXmlElement *GUIConfigElement = (GUIRoot->InsertEndChild(TiXmlElement(TEXT("GUIConfig"))))->ToElement();
GUIConfigElement->SetAttribute(TEXT("name"), TEXT("NewDocDefaultSettings"));
GUIConfigElement->SetAttribute(TEXT("format"), _nppGUI._newDocDefaultSettings._format);
GUIConfigElement->SetAttribute(TEXT("format"), static_cast<int>(_nppGUI._newDocDefaultSettings._format));
GUIConfigElement->SetAttribute(TEXT("encoding"), _nppGUI._newDocDefaultSettings._unicodeMode);
GUIConfigElement->SetAttribute(TEXT("lang"), _nppGUI._newDocDefaultSettings._lang);
GUIConfigElement->SetAttribute(TEXT("codepage"), _nppGUI._newDocDefaultSettings._codepage);
@ -6260,3 +6277,19 @@ void Date::now()
_month = timeinfo->tm_mon + 1;
_day = timeinfo->tm_mday;
}
FormatType convertIntToFormatType(int value, FormatType defvalue)
{
switch (value)
{
case static_cast<LPARAM>(FormatType::windows):
return FormatType::windows;
case static_cast<LPARAM>(FormatType::macos):
return FormatType::macos;
case static_cast<LPARAM>(FormatType::unix):
return FormatType::unix;
default:
return defvalue;
}
}

View File

@ -95,7 +95,28 @@ const int TAB_VERTICAL = 64; // 0100 0000
const int TAB_MULTILINE = 128; // 1000 0000
const int TAB_HIDE = 256; //1 0000 0000
enum formatType {WIN_FORMAT, MAC_FORMAT, UNIX_FORMAT};
enum class FormatType: std::uint8_t
{
windows,
macos,
unix,
// special values
unknown, // can not be the first value for legacy code
osdefault = windows,
};
/*!
** \brief Convert an int into a FormatType
** \param value An arbitrary int
** \param defvalue The default value to use if an invalid value is provided
*/
FormatType convertIntToFormatType(int value, FormatType defvalue = FormatType::osdefault);
enum UniMode {uni8Bit=0, uniUTF8=1, uni16BE=2, uni16LE=3, uniCookie=4, uni7Bit=5, uni16BE_NoBOM=6, uni16LE_NoBOM=7, uniEnd};
enum ChangeDetect {cdDisabled=0, cdEnabled=1, cdAutoUpdate=2, cdGo2end=3, cdAutoUpdateGo2end=4};
enum BackupFeature {bak_none = 0, bak_simple = 1, bak_verbose = 2};
@ -519,17 +540,19 @@ private :
int _nbLexerStyler;
};
struct NewDocDefaultSettings
struct NewDocDefaultSettings final
{
formatType _format;
UniMode _unicodeMode;
bool _openAnsiAsUtf8;
LangType _lang;
int _codepage; // -1 when not using
NewDocDefaultSettings():_format(WIN_FORMAT), _unicodeMode(uniCookie), _openAnsiAsUtf8(true), _lang(L_TEXT), _codepage(-1){};
FormatType _format = FormatType::osdefault;
UniMode _unicodeMode = uniCookie;
bool _openAnsiAsUtf8 = true;
LangType _lang = L_TEXT;
int _codepage = -1; // -1 when not using
};
struct LangMenuItem {
struct LangMenuItem
{
LangType _langType;
int _cmdID;
generic_string _langName;

View File

@ -49,6 +49,37 @@ long Buffer::_recentTagCtr = 0;
namespace // anonymous
{
static FormatType getEOLFormatForm(const char* const data, size_t length, FormatType defvalue = FormatType::osdefault)
{
assert(length == 0 or data != nullptr && "invalid buffer for getEOLFormatForm()");
for (size_t i = 0; i != length; ++i)
{
if (data[i] == CR)
{
if (i + 1 < length && data[i + 1] == LF)
return FormatType::windows;
return FormatType::macos;
}
if (data[i] == LF)
return FormatType::unix;
}
return defvalue; // fallback unknown
}
} // anonymous namespace
Buffer::Buffer(FileManager * pManager, BufferID id, Document doc, DocFileStatus type, const TCHAR *fileName)
// type must be either DOC_REGULAR or DOC_UNNAMED
: _pManager(pManager)
@ -72,7 +103,7 @@ Buffer::Buffer(FileManager * pManager, BufferID id, Document doc, DocFileStatus
checkFileState();
// reset after initialization
_isDirty = false;
_isDirty = false;
_canNotify = true;
_needLexer = false; // new buffers do not need lexing, Scintilla takes care of that
}
@ -560,21 +591,25 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc, int encodin
Utf8_16_Read UnicodeConvertor; //declare here so we can get information after loading is done
char data[blockSize + 8]; // +8 for incomplete multibyte char
formatType format;
bool res = loadFileData(doc, backupFileName?backupFileName:fullpath, data, &UnicodeConvertor, L_TEXT, encoding, &format);
FormatType bkformat = FormatType::unknown;
bool res = loadFileData(doc, backupFileName?backupFileName:fullpath, data, &UnicodeConvertor, L_TEXT, encoding, &bkformat);
if (res)
{
Buffer* newBuf = new Buffer(this, _nextBufferID, doc, DOC_REGULAR, fullpath);
BufferID id = (BufferID) newBuf;
newBuf->_id = id;
if (backupFileName != NULL)
{
newBuf->_backupFileName = backupFileName;
if (!PathFileExists(fullpath))
newBuf->_currentStatus = DOC_UNNAMED;
}
if (fileNameTimestamp != 0)
newBuf->_timeStamp = fileNameTimestamp;
_buffers.push_back(newBuf);
++_nrBufs;
Buffer* buf = _buffers.at(_nrBufs - 1);
@ -590,11 +625,11 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc, int encodin
// 3 formats : WIN_FORMAT, UNIX_FORMAT and MAC_FORMAT
if (nullptr != UnicodeConvertor.getNewBuf())
{
int format = getEOLFormatForm(UnicodeConvertor.getNewBuf(), UnicodeConvertor.getNewSize());
buf->setFormat(format == -1?WIN_FORMAT:(formatType)format);
FormatType format = getEOLFormatForm(UnicodeConvertor.getNewBuf(), UnicodeConvertor.getNewSize());
buf->setFormat(format);
}
else
buf->setFormat(WIN_FORMAT);
buf->setFormat(FormatType::osdefault);
UniMode um = UnicodeConvertor.getEncoding();
if (um == uni7Bit)
@ -607,7 +642,7 @@ BufferID FileManager::loadFile(const TCHAR * filename, Document doc, int encodin
// Test if encoding is set to UTF8 w/o BOM (usually for utf8 indicator of xml or html)
buf->setEncoding((encoding == SC_CP_UTF8)?-1:encoding);
buf->setUnicodeMode(uniCookie);
buf->setFormat(format);
buf->setFormat(bkformat);
}
//determine buffer properties
@ -631,28 +666,29 @@ bool FileManager::reloadBuffer(BufferID id)
buf->_canNotify = false; //disable notify during file load, we dont want dirty to be triggered
int encoding = buf->getEncoding();
char data[blockSize + 8]; // +8 for incomplete multibyte char
formatType format;
bool res = loadFileData(doc, buf->getFullPathName(), data, &UnicodeConvertor, buf->getLangType(), encoding, &format);
FormatType bkformat;
bool res = loadFileData(doc, buf->getFullPathName(), data, &UnicodeConvertor, buf->getLangType(), encoding, &bkformat);
buf->_canNotify = true;
if (res)
{
if (encoding == -1)
{
if (nullptr != UnicodeConvertor.getNewBuf())
{
int format = getEOLFormatForm(UnicodeConvertor.getNewBuf(), UnicodeConvertor.getNewSize());
buf->setFormat(format == -1?WIN_FORMAT:(formatType)format);
FormatType format = getEOLFormatForm(UnicodeConvertor.getNewBuf(), UnicodeConvertor.getNewSize());
buf->setFormat(format);
}
else
{
buf->setFormat(WIN_FORMAT);
}
buf->setFormat(FormatType::osdefault);
buf->setUnicodeMode(UnicodeConvertor.getEncoding());
}
else
{
buf->setEncoding(encoding);
buf->setFormat(format);
buf->setFormat(bkformat);
buf->setUnicodeMode(uniCookie);
}
}
@ -1209,7 +1245,7 @@ int FileManager::detectCodepage(char* buf, size_t len)
}
inline bool FileManager::loadFileData(Document doc, const TCHAR * filename, char* data, Utf8_16_Read * UnicodeConvertor,
LangType language, int & encoding, formatType *pFormat)
LangType language, int & encoding, FormatType* pFormat)
{
FILE *fp = generic_fopen(filename, TEXT("rb"));
if (!fp)
@ -1264,7 +1300,7 @@ inline bool FileManager::loadFileData(Document doc, const TCHAR * filename, char
_pscratchTilla->execute(SCI_SETCODEPAGE, SC_CP_UTF8);
bool success = true;
int format = -1;
FormatType format = FormatType::unknown;
__try
{
// First allocate enough memory for the whole file (this will reduce memory copy during loading)
@ -1314,8 +1350,8 @@ inline bool FileManager::loadFileData(Document doc, const TCHAR * filename, char
_pscratchTilla->execute(SCI_APPENDTEXT, newDataLen, (LPARAM)newData);
}
if (format == -1)
format = getEOLFormatForm(data, lenFile);
if (format == FormatType::unknown)
format = getEOLFormatForm(data, lenFile, FormatType::unknown);
}
else
{
@ -1343,8 +1379,9 @@ inline bool FileManager::loadFileData(Document doc, const TCHAR * filename, char
fclose(fp);
// broadcast the format
if (pFormat != nullptr)
*pFormat = (format == -1) ? WIN_FORMAT : (formatType)format;
*pFormat = (format != FormatType::unknown) ? format : FormatType::osdefault;
_pscratchTilla->execute(SCI_EMPTYUNDOBUFFER);
_pscratchTilla->execute(SCI_SETSAVEPOINT);
@ -1411,29 +1448,4 @@ int FileManager::docLength(Buffer* buffer) const
int docLen = _pscratchTilla->getCurrentDocLen();
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault);
return docLen;
}
int FileManager::getEOLFormatForm(const char* const data, size_t length) const
{
assert(data != nullptr && "invalid buffer for getEOLFormatForm()");
for (size_t i = 0; i != length; ++i)
{
if (data[i] == CR)
{
if (i+1 < length && data[i+1] == LF)
{
return int(WIN_FORMAT);
}
else
{
return int(MAC_FORMAT);
}
}
if (data[i] == LF)
{
return int(UNIX_FORMAT);
}
}
return -1;
}

View File

@ -105,14 +105,13 @@ public:
void destroyInstance() { delete _pSelf; };
int getFileNameFromBuffer(BufferID id, TCHAR * fn2copy);
int docLength(Buffer * buffer) const;
int getEOLFormatForm(const char* const data, size_t length) const;
size_t nextUntitledNewNumber() const;
private:
~FileManager();
int detectCodepage(char* buf, size_t len);
bool loadFileData(Document doc, const TCHAR* filename, char* buffer, Utf8_16_Read* UnicodeConvertor, LangType language, int& encoding, formatType* pFormat = nullptr);
bool loadFileData(Document doc, const TCHAR* filename, char* buffer, Utf8_16_Read* UnicodeConvertor, LangType language, int& encoding, FormatType* pFormat = nullptr);
private:
@ -196,11 +195,11 @@ public:
doNotify(BufferChangeReadonly);
}
formatType getFormat() const {
FormatType getFormat() const {
return _format;
}
void setFormat(formatType format) {
void setFormat(FormatType format) {
_format = format;
doNotify(BufferChangeFormat);
}
@ -365,7 +364,7 @@ private:
LangType _lang;
generic_string _userLangExt; // it's useful if only (_lang == L_USER)
bool _isDirty = false;
formatType _format;
FormatType _format = FormatType::osdefault;
UniMode _unicodeMode;
int _encoding = -1;
bool _isUserReadOnly = false;

View File

@ -1577,7 +1577,7 @@ void ScintillaEditView::bufferUpdated(Buffer * buffer, int mask)
if (mask & BufferChangeFormat)
{
execute(SCI_SETEOLMODE, _currentBuffer->getFormat());
execute(SCI_SETEOLMODE, static_cast<int>(_currentBuffer->getFormat()));
}
if (mask & BufferChangeReadonly)
{

View File

@ -1100,23 +1100,25 @@ INT_PTR CALLBACK DefaultNewDocDlg::run_dlgProc(UINT Message, WPARAM wParam, LPAR
NppGUI & nppGUI = (NppGUI & )pNppParam->getNppGUI();
NewDocDefaultSettings & ndds = (NewDocDefaultSettings &)nppGUI.getNewDocDefaultSettings();
switch (Message)
switch (Message)
{
case WM_INITDIALOG :
case WM_INITDIALOG:
{
int ID2Check = 0;
int ID2Check = IDC_RADIO_F_WIN;
switch (ndds._format)
{
case MAC_FORMAT :
case FormatType::windows:
ID2Check = IDC_RADIO_F_WIN;
break;
case FormatType::macos:
ID2Check = IDC_RADIO_F_MAC;
break;
case UNIX_FORMAT :
case FormatType::unix:
ID2Check = IDC_RADIO_F_UNIX;
break;
default : //WIN_FORMAT
ID2Check = IDC_RADIO_F_WIN;
case FormatType::unknown:
assert(false);
break;
}
::SendDlgItemMessage(_hSelf, ID2Check, BM_SETCHECK, BST_CHECKED, 0);
@ -1252,16 +1254,23 @@ INT_PTR CALLBACK DefaultNewDocDlg::run_dlgProc(UINT Message, WPARAM wParam, LPAR
}
case IDC_RADIO_F_MAC:
ndds._format = MAC_FORMAT;
{
ndds._format = FormatType::macos;
return TRUE;
}
case IDC_RADIO_F_UNIX:
ndds._format = UNIX_FORMAT;
{
ndds._format = FormatType::unix;
return TRUE;
}
case IDC_RADIO_F_WIN:
ndds._format = WIN_FORMAT;
{
ndds._format = FormatType::windows;
return TRUE;
}
default:
{
if (HIWORD(wParam) == CBN_SELCHANGE)
{
if (LOWORD(wParam) == IDC_COMBO_DEFAULTLANG)
@ -1279,6 +1288,7 @@ INT_PTR CALLBACK DefaultNewDocDlg::run_dlgProc(UINT Message, WPARAM wParam, LPAR
}
}
return FALSE;
}
}
}
return FALSE;