bool FunctionListPanel::serialize(const generic_string & outputFilename) { generic_string fname; if (outputFilename.empty()) // if outputFilename is not given, get the current file path by adding the file extension { Buffer* currentBuf = (*_ppEditView)->getCurrentBuffer(); const TCHAR *fullFilePath = currentBuf->getFullPathName(); // Export function list from an existing file bool exportFuncntionList = (NppParameters::getInstance())->doFunctionListExport(); if (exportFuncntionList && ::PathFileExists(fullFilePath)) { fname = fullFilePath; fname += TEXT(".result"); } else return false; } else { fname = outputFilename; } FILE * f = generic_fopen(fname.c_str(), TEXT("w+")); if (!f) return false; for (const auto & info : _foundFuncInfos) { generic_string entryName; if (!info._data2.empty()) { entryName = info._data2; entryName += TEXT("=>"); } entryName += info._data; entryName += TEXT("\n"); WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); UINT cp = static_cast<UINT>((*_ppEditView)->execute(SCI_GETCODEPAGE)); const char *textA = wmc->wchar2char(entryName.c_str(), cp); string entryNameA = textA; fwrite(entryNameA.c_str(), sizeof(entryNameA.c_str()[0]), entryNameA.length(), f); } fflush(f); fclose(f); return true; }
int PluginsManager::loadPlugin(const generic_string& pluginFilePath, std::vector<generic_string> & dll2Remove) { PluginInfo *pi = new PluginInfo; try { pi->_moduleName = PathFindFileName(pluginFilePath.c_str()); pi->_hLib = ::LoadLibrary(pluginFilePath.c_str()); if (!pi->_hLib) throw generic_string(TEXT("Load Library is failed.\nMake \"Runtime Library\" setting of this project as \"Multi-threaded(/MT)\" may cure this problem.")); pi->_pFuncIsUnicode = (PFUNCISUNICODE)GetProcAddress(pi->_hLib, "isUnicode"); #ifdef UNICODE if (!pi->_pFuncIsUnicode || !pi->_pFuncIsUnicode()) throw generic_string(TEXT("This ANSI plugin is not compatible with your Unicode Notepad++.")); #else if (pi->_pFuncIsUnicode) throw generic_string(TEXT("This Unicode plugin is not compatible with your ANSI mode Notepad++.")); #endif pi->_pFuncSetInfo = (PFUNCSETINFO)GetProcAddress(pi->_hLib, "setInfo"); if (!pi->_pFuncSetInfo) throw generic_string(TEXT("Missing \"setInfo\" function")); pi->_pFuncGetName = (PFUNCGETNAME)GetProcAddress(pi->_hLib, "getName"); if (!pi->_pFuncGetName) throw generic_string(TEXT("Missing \"getName\" function")); pi->_pBeNotified = (PBENOTIFIED)GetProcAddress(pi->_hLib, "beNotified"); if (!pi->_pBeNotified) throw generic_string(TEXT("Missing \"beNotified\" function")); pi->_pMessageProc = (PMESSAGEPROC)GetProcAddress(pi->_hLib, "messageProc"); if (!pi->_pMessageProc) throw generic_string(TEXT("Missing \"messageProc\" function")); pi->_pFuncSetInfo(_nppData); pi->_pFuncGetFuncsArray = (PFUNCGETFUNCSARRAY)GetProcAddress(pi->_hLib, "getFuncsArray"); if (!pi->_pFuncGetFuncsArray) throw generic_string(TEXT("Missing \"getFuncsArray\" function")); pi->_funcItems = pi->_pFuncGetFuncsArray(&pi->_nbFuncItem); if ((!pi->_funcItems) || (pi->_nbFuncItem <= 0)) throw generic_string(TEXT("Missing \"FuncItems\" array, or the nb of Function Item is not set correctly")); pi->_pluginMenu = ::CreateMenu(); GetLexerCountFn GetLexerCount = (GetLexerCountFn)::GetProcAddress(pi->_hLib, "GetLexerCount"); // it's a lexer plugin if (GetLexerCount) { GetLexerNameFn GetLexerName = (GetLexerNameFn)::GetProcAddress(pi->_hLib, "GetLexerName"); if (!GetLexerName) throw generic_string(TEXT("Loading GetLexerName function failed.")); GetLexerStatusTextFn GetLexerStatusText = (GetLexerStatusTextFn)::GetProcAddress(pi->_hLib, "GetLexerStatusText"); if (!GetLexerStatusText) throw generic_string(TEXT("Loading GetLexerStatusText function failed.")); // Assign a buffer for the lexer name. char lexName[MAX_EXTERNAL_LEXER_NAME_LEN]; lexName[0] = '\0'; TCHAR lexDesc[MAX_EXTERNAL_LEXER_DESC_LEN]; lexDesc[0] = '\0'; int numLexers = GetLexerCount(); NppParameters * nppParams = NppParameters::getInstance(); ExternalLangContainer *containers[30]; #ifdef UNICODE WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); #endif for (int x = 0; x < numLexers; x++) { GetLexerName(x, lexName, MAX_EXTERNAL_LEXER_NAME_LEN); GetLexerStatusText(x, lexDesc, MAX_EXTERNAL_LEXER_DESC_LEN); #ifdef UNICODE const TCHAR *pLexerName = wmc->char2wchar(lexName, CP_ACP); #else const TCHAR *pLexerName = lexName; #endif if (!nppParams->isExistingExternalLangName(pLexerName) && nppParams->ExternalLangHasRoom()) containers[x] = new ExternalLangContainer(pLexerName, lexDesc); else containers[x] = NULL; } TCHAR xmlPath[MAX_PATH]; lstrcpy(xmlPath, nppParams->getNppPath().c_str()); PathAppend(xmlPath, TEXT("plugins\\Config")); PathAppend(xmlPath, pi->_moduleName.c_str()); PathRemoveExtension(xmlPath); PathAddExtension(xmlPath, TEXT(".xml")); if (!PathFileExists(xmlPath)) { memset(xmlPath, 0, MAX_PATH * sizeof(TCHAR)); lstrcpy(xmlPath, nppParams->getAppDataNppDir() ); PathAppend(xmlPath, TEXT("plugins\\Config")); PathAppend(xmlPath, pi->_moduleName.c_str()); PathRemoveExtension( xmlPath ); PathAddExtension( xmlPath, TEXT(".xml") ); if (! PathFileExists( xmlPath ) ) { throw generic_string(generic_string(xmlPath) + TEXT(" is missing.")); } } TiXmlDocument *_pXmlDoc = new TiXmlDocument(xmlPath); if (!_pXmlDoc->LoadFile()) { delete _pXmlDoc; _pXmlDoc = NULL; throw generic_string(generic_string(xmlPath) + TEXT(" failed to load.")); } for (int x = 0; x < numLexers; x++) // postpone adding in case the xml is missing/corrupt if (containers[x] != NULL) nppParams->addExternalLangToEnd(containers[x]); nppParams->getExternalLexerFromXmlTree(_pXmlDoc); nppParams->getExternalLexerDoc()->push_back(_pXmlDoc); #ifdef UNICODE const char *pDllName = wmc->wchar2char(pluginFilePath.c_str(), CP_ACP); #else const char *pDllName = pluginFilePath.c_str(); #endif ::SendMessage(_nppData._scintillaMainHandle, SCI_LOADLEXERLIBRARY, 0, (LPARAM)pDllName); } _pluginInfos.push_back(pi); return (_pluginInfos.size() - 1); } catch(generic_string s) { s += TEXT("\n\n"); s += USERMSG; if (::MessageBox(NULL, s.c_str(), pluginFilePath.c_str(), MB_YESNO) == IDYES) { dll2Remove.push_back(pluginFilePath); } delete pi; return -1; } catch(...) { generic_string msg = TEXT("Fail loaded"); msg += TEXT("\n\n"); msg += USERMSG; if (::MessageBox(NULL, msg.c_str(), pluginFilePath.c_str(), MB_YESNO) == IDYES) { dll2Remove.push_back(pluginFilePath); } delete pi; return -1; } }
inline bool FileManager::loadFileData(Document doc, const TCHAR * filename, char* data, Utf8_16_Read * UnicodeConvertor, LangType & language, int & encoding, FormatType* pFormat) { FILE *fp = generic_fopen(filename, TEXT("rb")); if (!fp) return false; //Get file size _fseeki64 (fp , 0 , SEEK_END); unsigned __int64 fileSize =_ftelli64(fp); rewind(fp); // size/6 is the normal room Scintilla keeps for editing, but here we limit it to 1MiB when loading (maybe we want to load big files without editing them too much) unsigned __int64 bufferSizeRequested = fileSize + min(1<<20,fileSize/6); // As a 32bit application, we cannot allocate 2 buffer of more than INT_MAX size (it takes the whole address space) if(bufferSizeRequested > INT_MAX) { ::MessageBox(NULL, TEXT("File is too big to be opened by Notepad++"), TEXT("File open problem"), MB_OK|MB_APPLMODAL); /* _nativeLangSpeaker.messageBox("NbFileToOpenImportantWarning", _pPublicInterface->getHSelf(), TEXT("File is too big to be opened by Notepad++"), TEXT("File open problem"), MB_OK|MB_APPLMODAL); */ fclose(fp); return false; } //Setup scratchtilla for new filedata _pscratchTilla->execute(SCI_SETSTATUS, SC_STATUS_OK); // reset error status _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, doc); bool ro = _pscratchTilla->execute(SCI_GETREADONLY) != 0; if (ro) { _pscratchTilla->execute(SCI_SETREADONLY, false); } _pscratchTilla->execute(SCI_CLEARALL); if (language < L_EXTERNAL) { _pscratchTilla->execute(SCI_SETLEXER, ScintillaEditView::langNames[language].lexerID); } else { int id = language - L_EXTERNAL; TCHAR * name = NppParameters::getInstance()->getELCFromIndex(id)._name; WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); const char *pName = wmc->wchar2char(name, CP_ACP); _pscratchTilla->execute(SCI_SETLEXERLANGUAGE, 0, (LPARAM)pName); } if (encoding != -1) _pscratchTilla->execute(SCI_SETCODEPAGE, SC_CP_UTF8); bool success = true; FormatType format = FormatType::unknown; __try { // First allocate enough memory for the whole file (this will reduce memory copy during loading) _pscratchTilla->execute(SCI_ALLOCATE, WPARAM(bufferSizeRequested)); if (_pscratchTilla->execute(SCI_GETSTATUS) != SC_STATUS_OK) throw; size_t lenFile = 0; size_t lenConvert = 0; //just in case conversion results in 0, but file not empty bool isFirstTime = true; int incompleteMultibyteChar = 0; do { lenFile = fread(data+incompleteMultibyteChar, 1, blockSize-incompleteMultibyteChar, fp) + incompleteMultibyteChar; if (lenFile == 0) break; if (isFirstTime) { // check if file contain any BOM if (Utf8_16_Read::determineEncoding((unsigned char *)data, lenFile) != uni8Bit) { // if file contains any BOM, then encoding will be erased, // and the document will be interpreted as UTF encoding = -1; } else if (encoding == -1) { if (NppParameters::getInstance()->getNppGUI()._detectEncoding) encoding = detectCodepage(data, lenFile); } if (language == L_TEXT) { // check the language du fichier language = detectLanguageFromTextBegining((unsigned char *)data, lenFile); } isFirstTime = false; } if (encoding != -1) { if (encoding == SC_CP_UTF8) { // Pass through UTF-8 (this does not check validity of characters, thus inserting a multi-byte character in two halfs is working) _pscratchTilla->execute(SCI_APPENDTEXT, lenFile, (LPARAM)data); } else { WcharMbcsConvertor* wmc = WcharMbcsConvertor::getInstance(); int newDataLen = 0; const char *newData = wmc->encode(encoding, SC_CP_UTF8, data, lenFile, &newDataLen, &incompleteMultibyteChar); _pscratchTilla->execute(SCI_APPENDTEXT, newDataLen, (LPARAM)newData); } if (format == FormatType::unknown) format = getEOLFormatForm(data, lenFile, FormatType::unknown); } else { lenConvert = UnicodeConvertor->convert(data, lenFile); _pscratchTilla->execute(SCI_APPENDTEXT, lenConvert, (LPARAM)(UnicodeConvertor->getNewBuf())); } if (_pscratchTilla->execute(SCI_GETSTATUS) != SC_STATUS_OK) throw; if (incompleteMultibyteChar != 0) { // copy bytes to next buffer memcpy(data, data+blockSize-incompleteMultibyteChar, incompleteMultibyteChar); } } while (lenFile > 0); } __except(EXCEPTION_EXECUTE_HANDLER) //TODO: should filter correctly for other exceptions; the old filter(GetExceptionCode(), GetExceptionInformation()) was only catching access violations { ::MessageBox(NULL, TEXT("File is too big to be opened by Notepad++"), TEXT("File open problem"), MB_OK|MB_APPLMODAL); success = false; } fclose(fp); // broadcast the format if (pFormat != nullptr) *pFormat = (format != FormatType::unknown) ? format : FormatType::osdefault; _pscratchTilla->execute(SCI_EMPTYUNDOBUFFER); _pscratchTilla->execute(SCI_SETSAVEPOINT); if (ro) _pscratchTilla->execute(SCI_SETREADONLY, true); _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); return success; }
int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector<generic_string> & dll2Remove) { const TCHAR *pluginFileName = ::PathFindFileName(pluginFilePath); if (isInLoadedDlls(pluginFileName)) return 0; NppParameters * nppParams = NppParameters::getInstance(); PluginInfo *pi = new PluginInfo; try { pi->_moduleName = pluginFileName; if (GetBinaryArchitectureType(pluginFilePath) != ARCH_TYPE) throw generic_string(ARCH_ERR_MSG); pi->_hLib = ::LoadLibrary(pluginFilePath); if (!pi->_hLib) { generic_string lastErrorMsg = GetLastErrorAsString(); if (lastErrorMsg.empty()) throw generic_string(TEXT("Load Library has failed.\nChanging the project's \"Runtime Library\" setting to \"Multi-threaded(/MT)\" might solve this problem.")); else throw generic_string(lastErrorMsg.c_str()); } pi->_pFuncIsUnicode = (PFUNCISUNICODE)GetProcAddress(pi->_hLib, "isUnicode"); if (!pi->_pFuncIsUnicode || !pi->_pFuncIsUnicode()) throw generic_string(TEXT("This ANSI plugin is not compatible with your Unicode Notepad++.")); pi->_pFuncSetInfo = (PFUNCSETINFO)GetProcAddress(pi->_hLib, "setInfo"); if (!pi->_pFuncSetInfo) throw generic_string(TEXT("Missing \"setInfo\" function")); pi->_pFuncGetName = (PFUNCGETNAME)GetProcAddress(pi->_hLib, "getName"); if (!pi->_pFuncGetName) throw generic_string(TEXT("Missing \"getName\" function")); pi->_funcName = pi->_pFuncGetName(); pi->_pBeNotified = (PBENOTIFIED)GetProcAddress(pi->_hLib, "beNotified"); if (!pi->_pBeNotified) throw generic_string(TEXT("Missing \"beNotified\" function")); pi->_pMessageProc = (PMESSAGEPROC)GetProcAddress(pi->_hLib, "messageProc"); if (!pi->_pMessageProc) throw generic_string(TEXT("Missing \"messageProc\" function")); pi->_pFuncSetInfo(_nppData); pi->_pFuncGetFuncsArray = (PFUNCGETFUNCSARRAY)GetProcAddress(pi->_hLib, "getFuncsArray"); if (!pi->_pFuncGetFuncsArray) throw generic_string(TEXT("Missing \"getFuncsArray\" function")); pi->_funcItems = pi->_pFuncGetFuncsArray(&pi->_nbFuncItem); if ((!pi->_funcItems) || (pi->_nbFuncItem <= 0)) throw generic_string(TEXT("Missing \"FuncItems\" array, or the nb of Function Item is not set correctly")); pi->_pluginMenu = ::CreateMenu(); GetLexerCountFn GetLexerCount = (GetLexerCountFn)::GetProcAddress(pi->_hLib, "GetLexerCount"); // it's a lexer plugin if (GetLexerCount) { GetLexerNameFn GetLexerName = (GetLexerNameFn)::GetProcAddress(pi->_hLib, "GetLexerName"); if (!GetLexerName) throw generic_string(TEXT("Loading GetLexerName function failed.")); GetLexerStatusTextFn GetLexerStatusText = (GetLexerStatusTextFn)::GetProcAddress(pi->_hLib, "GetLexerStatusText"); if (!GetLexerStatusText) throw generic_string(TEXT("Loading GetLexerStatusText function failed.")); // Assign a buffer for the lexer name. char lexName[MAX_EXTERNAL_LEXER_NAME_LEN]; lexName[0] = '\0'; TCHAR lexDesc[MAX_EXTERNAL_LEXER_DESC_LEN]; lexDesc[0] = '\0'; int numLexers = GetLexerCount(); ExternalLangContainer *containers[30]; WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); for (int x = 0; x < numLexers; ++x) { GetLexerName(x, lexName, MAX_EXTERNAL_LEXER_NAME_LEN); GetLexerStatusText(x, lexDesc, MAX_EXTERNAL_LEXER_DESC_LEN); const TCHAR *pLexerName = wmc->char2wchar(lexName, CP_ACP); if (!nppParams->isExistingExternalLangName(pLexerName) && nppParams->ExternalLangHasRoom()) containers[x] = new ExternalLangContainer(pLexerName, lexDesc); else containers[x] = NULL; } TCHAR xmlPath[MAX_PATH]; lstrcpy(xmlPath, nppParams->getNppPath().c_str()); PathAppend(xmlPath, TEXT("plugins\\Config")); PathAppend(xmlPath, pi->_moduleName.c_str()); PathRemoveExtension(xmlPath); PathAddExtension(xmlPath, TEXT(".xml")); if (!PathFileExists(xmlPath)) { lstrcpyn(xmlPath, TEXT("\0"), MAX_PATH ); lstrcpy(xmlPath, nppParams->getAppDataNppDir() ); PathAppend(xmlPath, TEXT("plugins\\Config")); PathAppend(xmlPath, pi->_moduleName.c_str()); PathRemoveExtension( xmlPath ); PathAddExtension( xmlPath, TEXT(".xml") ); if (! PathFileExists( xmlPath ) ) { throw generic_string(generic_string(xmlPath) + TEXT(" is missing.")); } } TiXmlDocument *pXmlDoc = new TiXmlDocument(xmlPath); if (!pXmlDoc->LoadFile()) { delete pXmlDoc; pXmlDoc = NULL; throw generic_string(generic_string(xmlPath) + TEXT(" failed to load.")); } for (int x = 0; x < numLexers; ++x) // postpone adding in case the xml is missing/corrupt { if (containers[x] != NULL) nppParams->addExternalLangToEnd(containers[x]); } nppParams->getExternalLexerFromXmlTree(pXmlDoc); nppParams->getExternalLexerDoc()->push_back(pXmlDoc); const char *pDllName = wmc->wchar2char(pluginFilePath, CP_ACP); ::SendMessage(_nppData._scintillaMainHandle, SCI_LOADLEXERLIBRARY, 0, reinterpret_cast<LPARAM>(pDllName)); } addInLoadedDlls(pluginFilePath, pluginFileName); _pluginInfos.push_back(pi); return static_cast<int32_t>(_pluginInfos.size() - 1); } catch (std::exception& e) { ::MessageBoxA(NULL, e.what(), "Exception", MB_OK); return -1; } catch (generic_string s) { s += TEXT("\n\n"); s += pluginFileName; s += USERMSG; if (::MessageBox(NULL, s.c_str(), pluginFilePath, MB_YESNO) == IDYES) { dll2Remove.push_back(pluginFilePath); } delete pi; return -1; } catch (...) { generic_string msg = TEXT("Failed to load"); msg += TEXT("\n\n"); msg += pluginFileName; msg += USERMSG; if (::MessageBox(NULL, msg.c_str(), pluginFilePath, MB_YESNO) == IDYES) { dll2Remove.push_back(pluginFilePath); } delete pi; return -1; } }