aboutsummaryrefslogtreecommitdiffhomepage
path: root/win32
diff options
context:
space:
mode:
authornyamatongwe <unknown>2003-04-18 01:39:55 +0000
committernyamatongwe <unknown>2003-04-18 01:39:55 +0000
commit55fdc0e6745bb50304e73e1c11c95d6c847b4274 (patch)
tree60beced0238d3852a891da2629baac76c18657c2 /win32
parent25a167ffe9c75c993f2c2a45ad3ebe3295e3d41a (diff)
downloadscintilla-mirror-55fdc0e6745bb50304e73e1c11c95d6c847b4274.tar.gz
Patch from Simon to make ExternalLexer platform independent.
Diffstat (limited to 'win32')
-rw-r--r--win32/ExternalLexer.cxx312
-rw-r--r--win32/ExternalLexer.h84
-rw-r--r--win32/PlatWin.cxx30
-rw-r--r--win32/ScintillaWin.cxx4
-rw-r--r--win32/scintilla.mak4
5 files changed, 33 insertions, 401 deletions
diff --git a/win32/ExternalLexer.cxx b/win32/ExternalLexer.cxx
deleted file mode 100644
index 41cc812bc..000000000
--- a/win32/ExternalLexer.cxx
+++ /dev/null
@@ -1,312 +0,0 @@
-// Scintilla source code edit control
-/** @file ExternalLexer.cxx
- ** Support external lexers in DLLs.
- **/
-// Copyright 2001 Simon Steele <ss@pnotepad.org>, portions copyright Neil Hodgson.
-// The License.txt file describes the conditions under which this software may be distributed.
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <ctype.h>
-
-#define _WIN32_WINNT 0x0400
-#include <windows.h>
-
-#include "Platform.h"
-#include "SciLexer.h"
-#include "PropSet.h"
-#include "Accessor.h"
-#include "DocumentAccessor.h"
-#include "KeyWords.h"
-#include "ExternalLexer.h"
-
-// Initialise the static vars...
-int LexerManager::UseCount = 0;
-LexerLibrary *LexerManager::first = NULL;
-LexerLibrary *LexerManager::last = NULL;
-LexerManager *LexerManager::firstlm = NULL;
-
-//------------------------------------------
-//
-// ExternalLexerModule
-//
-//------------------------------------------
-
-char **WordListsToStrings(WordList *val[]) {
- int dim = 0;
- while (val[dim])
- dim++;
- char **wls = new char * [dim + 1];
- for (int i = 0;i < dim;i++) {
- SString words;
- words = "";
- for (int n = 0; n < val[i]->len; n++) {
- words += val[i]->words[n];
- if (n != val[i]->len - 1)
- words += " ";
- }
- wls[i] = new char[words.length() + 1];
- strcpy(wls[i], words.c_str());
- }
- wls[dim] = 0;
- return wls;
-}
-
-void DeleteWLStrings(char *strs[]) {
- int dim = 0;
- while (strs[dim]) {
- delete strs[dim];
- dim++;
- }
- delete [] strs;
-}
-
-void ExternalLexerModule::Lex(unsigned int startPos, int lengthDoc, int initStyle,
- WordList *keywordlists[], Accessor &styler) const {
- if (!fneLexer)
- return ;
-
- char **kwds = WordListsToStrings(keywordlists);
- char *ps = styler.GetProperties();
-
- // The accessor passed in is always a DocumentAccessor so this cast and the subsequent
- // access will work. Can not use the stricter dynamic_cast as that requires RTTI.
- DocumentAccessor &da = static_cast<DocumentAccessor &>(styler);
- WindowID wID = da.GetWindow();
-
- fneLexer(externalLanguage, startPos, lengthDoc, initStyle, kwds, wID, ps);
-
- delete ps;
- DeleteWLStrings(kwds);
-}
-
-void ExternalLexerModule::Fold(unsigned int startPos, int lengthDoc, int initStyle,
- WordList *keywordlists[], Accessor &styler) const {
- if (!fneFolder)
- return ;
-
- char **kwds = WordListsToStrings(keywordlists);
- char *ps = styler.GetProperties();
-
- // The accessor passed in is always a DocumentAccessor so this cast and the subsequent
- // access will work. Can not use the stricter dynamic_cast as that requires RTTI.
- DocumentAccessor &da = static_cast<DocumentAccessor &>(styler);
- WindowID wID = da.GetWindow();
-
- fneFolder(externalLanguage, startPos, lengthDoc, initStyle, kwds, wID, ps);
-
- delete ps;
- DeleteWLStrings(kwds);
-}
-
-void ExternalLexerModule::SetExternal(ExtLexerFunction fLexer, ExtFoldFunction fFolder, int index) {
- fneLexer = fLexer;
- fneFolder = fFolder;
- externalLanguage = index;
-}
-
-//------------------------------------------
-//
-// LexerLibrary
-//
-//------------------------------------------
-
-LexerLibrary::LexerLibrary(LPCTSTR ModuleName) {
- // Initialise some members...
- first = NULL;
- last = NULL;
-
- // Load the DLL
- m_hModule = LoadLibrary(ModuleName);
- if (m_hModule) {
- m_sModuleName = ModuleName;
- GetLexerCountFn GetLexerCount = (GetLexerCountFn)GetProcAddress(m_hModule, "GetLexerCount");
-
- if (GetLexerCount) {
- ExternalLexerModule *lex;
- LexerMinder *lm;
-
- // Find functions in the DLL
- GetLexerNameFn GetLexerName = (GetLexerNameFn)GetProcAddress(m_hModule, "GetLexerName");
- ExtLexerFunction Lexer = (ExtLexerFunction)GetProcAddress(m_hModule, "Lex");
- ExtFoldFunction Folder = (ExtFoldFunction)GetProcAddress(m_hModule, "Fold");
-
- // Assign a buffer for the lexer name.
- char lexname[100];
- strcpy(lexname, "");
-
- int nl = GetLexerCount();
-
- for (int i = 0; i < nl; i++) {
- GetLexerName(i, lexname, 100);
- lex = new ExternalLexerModule(SCLEX_AUTOMATIC, NULL, lexname, NULL);
-
- // Create a LexerMinder so we don't leak the ExternalLexerModule...
- lm = new LexerMinder;
- lm->self = lex;
- lm->next = NULL;
- if (first != NULL) {
- last->next = lm;
- last = lm;
- } else {
- first = lm;
- last = lm;
- }
-
- // The external lexer needs to know how to call into its DLL to
- // do its lexing and folding, we tell it here. Folder may be null.
- lex->SetExternal(Lexer, Folder, i);
-
- }
- }
- }
- next = NULL;
-}
-
-LexerLibrary::~LexerLibrary() {
- Release();
-}
-
-void LexerLibrary::Release() {
- //TODO maintain a list of lexers created, and delete them!
- LexerMinder *lm;
- LexerMinder *next;
- lm = first;
- while (NULL != lm) {
- next = lm->next;
- delete lm->self;
- delete lm;
- lm = next;
- }
-
- first = NULL;
- last = NULL;
-
- // Release the DLL
- if (NULL != m_hModule) {
- FreeLibrary(m_hModule);
- }
-}
-
-//------------------------------------------
-//
-// LexerManager
-//
-//------------------------------------------
-
-int FindLastSlash(char *inp) {
- int ret = -1;
- for (int i = static_cast<int>(strlen(inp)) - 1; i >= 0; i--) {
- if (inp[i] == '\\' || inp[i] == '/') {
- // if you don't like break:
- /*
- if (ret == -1)
- */
- ret = i;
- break;
- }
- }
- return ret;
-}
-
-LexerManager::LexerManager() {
-
- UseCount++;
- if (1 == UseCount) {
- firstlm = this;
- m_bLoaded = false;
- }
-}
-
-void LexerManager::EnumerateLexers() {
- HANDLE hFind;
- WIN32_FIND_DATA FindFileData;
-
- char path[MAX_PATH + 1];
-
- GetModuleFileName(GetModuleHandle(NULL), path, MAX_PATH);
-
- int i = FindLastSlash(path);
-
- if (i == -1)
- i = static_cast<int>(strlen(path));
-
- SString sPath(path, 0, i);
-
- // Ensure a trailing slash...
- if ( sPath[sPath.size() - 1] != '/' && sPath[sPath.size() - 1] != '\\' ) {
- sPath += '\\';
- }
-
- SString sPattern(sPath);
- sPattern += "*.lexer";
-
- hFind = FindFirstFile(sPattern.c_str(), &FindFileData);
- if (hFind != INVALID_HANDLE_VALUE) {
- //Found the first file...
- BOOL found = TRUE;
- SString to_open;
-
- while (found) {
- to_open = sPath;
- to_open += FindFileData.cFileName;
- LexerLibrary *lib = new LexerLibrary(to_open.c_str());
- if (NULL != first) {
- last->next = lib;
- last = lib;
- } else {
- first = lib;
- last = lib;
- }
- found = FindNextFile(hFind, &FindFileData);
- }
-
- FindClose(hFind);
-
- }
-}
-
-LexerManager::~LexerManager() {
- // If this is the last LexerManager to be freed then
- // we delete all of our LexerLibrarys.
- UseCount--;
- if (0 == UseCount) {
- if (NULL != first) {
- LexerLibrary *cur = first;
- LexerLibrary *next = first->next;
- while (cur) {
- delete cur;
- cur = next;
- }
- first = NULL;
- last = NULL;
- }
- }
- if (this == firstlm)
- firstlm = NULL;
-}
-
-void LexerManager::Load()
-{
- if(!m_bLoaded)
- {
- m_bLoaded = true;
- EnumerateLexers();
- }
-}
-
-// Return a LexerManager, or create one and then return it.
-LexerManager *LexerManager::GetInstance() {
- if(!firstlm)
- firstlm = new LexerManager;
- return firstlm;
-}
-
-LMMinder::~LMMinder()
-{
- LexerManager *rem = LexerManager::firstlm;
- if(rem)
- delete rem;
-}
-
-LMMinder minder;
diff --git a/win32/ExternalLexer.h b/win32/ExternalLexer.h
deleted file mode 100644
index 188ded7a5..000000000
--- a/win32/ExternalLexer.h
+++ /dev/null
@@ -1,84 +0,0 @@
-// Scintilla source code edit control
-/** @file ExternalLexer.h
- ** Support external lexers in DLLs.
- **/
-// Copyright 2001 Simon Steele <ss@pnotepad.org>, portions copyright Neil Hodgson.
-// The License.txt file describes the conditions under which this software may be distributed.
-
-#ifndef EXTERNALLEXER_H
-#define EXTERNALLEXER_H
-
-// External Lexer function definitions...
-typedef void (__stdcall *ExtLexerFunction)(unsigned int lexer, unsigned int startPos, int length, int initStyle,
- char *words[], WindowID window, char *props);
-typedef void (__stdcall *ExtFoldFunction)(unsigned int lexer, unsigned int startPos, int length, int initStyle,
- char *words[], WindowID window, char *props);
-typedef void* (__stdcall *GetLexerFunction)(unsigned int Index);
-typedef int (__stdcall *GetLexerCountFn)();
-typedef void (__stdcall *GetLexerNameFn)(unsigned int Index, char *name, int buflength);
-
-// Sub-class of LexerModule to use an external lexer.
-class ExternalLexerModule : protected LexerModule {
-protected:
- ExtLexerFunction fneLexer;
- ExtFoldFunction fneFolder;
- int externalLanguage;
- char name[100];
-public:
- ExternalLexerModule(int language_, LexerFunction fnLexer_,
- const char *languageName_=0, LexerFunction fnFolder_=0) : LexerModule(language_, fnLexer_, 0, fnFolder_){
- strncpy(name, languageName_, sizeof(name));
- languageName = name;
- };
- virtual void Lex(unsigned int startPos, int lengthDoc, int initStyle,
- WordList *keywordlists[], Accessor &styler) const;
- virtual void Fold(unsigned int startPos, int lengthDoc, int initStyle,
- WordList *keywordlists[], Accessor &styler) const;
- virtual void SetExternal(ExtLexerFunction fLexer, ExtFoldFunction fFolder, int index);
-};
-
-// LexerMinder points to an ExternalLexerModule - so we don't leak them.
-class LexerMinder {
-public:
- ExternalLexerModule *self;
- LexerMinder *next;
-};
-
-// LexerLibrary exists for every External Lexer DLL, contains LexerMinders.
-class LexerLibrary {
-public:
- LexerLibrary(LPCTSTR ModuleName);
- ~LexerLibrary();
- void Release();
- // Variables
- LexerLibrary *next;
- SString m_sModuleName;
-private:
- HMODULE m_hModule;
- LexerMinder *first;
- LexerMinder *last;
-};
-
-// LexerManager manages external lexers, contains LexerLibrarys.
-class LexerManager {
- friend class LMMinder;
-public:
- LexerManager();
- ~LexerManager();
- void Load();
- static LexerManager *GetInstance();
-private:
- bool m_bLoaded;
- void EnumerateLexers();
- static int UseCount;
- static LexerLibrary *first;
- static LexerLibrary *last;
- static LexerManager *firstlm;
-};
-
-class LMMinder {
-public:
- ~LMMinder();
-};
-
-#endif
diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx
index bf6112d57..c61a67ca9 100644
--- a/win32/PlatWin.cxx
+++ b/win32/PlatWin.cxx
@@ -1288,6 +1288,36 @@ double ElapsedTime::Duration(bool reset) {
return result;
}
+class DynamicLibraryImpl : public DynamicLibrary {
+protected:
+ HMODULE h;
+public:
+ DynamicLibraryImpl(const char *modulePath) {
+ h = ::LoadLibrary(modulePath);
+ }
+
+ virtual ~DynamicLibraryImpl() {
+ if (h != NULL)
+ ::FreeLibrary(h);
+ }
+
+ // Use GetProcAddress to get a pointer to the relevant function.
+ virtual Function *FindFunction(const char *name) {
+ if (h != NULL) {
+ return reinterpret_cast<Function*>( ::GetProcAddress(h, name) );
+ } else
+ return NULL;
+ }
+
+ virtual bool IsValid() {
+ return h != NULL;
+ }
+};
+
+DynamicLibrary *DynamicLibrary::Load(const char *modulePath) {
+ return static_cast<DynamicLibrary *>( new DynamicLibraryImpl(modulePath) );
+}
+
ColourDesired Platform::Chrome() {
return ::GetSysColor(COLOR_3DFACE);
}
diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx
index 724df8359..a49daf61b 100644
--- a/win32/ScintillaWin.cxx
+++ b/win32/ScintillaWin.cxx
@@ -842,7 +842,7 @@ sptr_t ScintillaWin::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam
#ifdef SCI_LEXER
case SCI_LOADLEXERLIBRARY:
- //LexerManager::GetInstance()->Load(reinterpret_cast<const char*>(lParam));
+ LexerManager::GetInstance()->Load(reinterpret_cast<const char*>(lParam));
break;
#endif
@@ -2026,8 +2026,6 @@ bool Scintilla_RegisterClasses(void *hInstance) {
bool result = ScintillaWin::Register(reinterpret_cast<HINSTANCE>(hInstance));
#ifdef SCI_LEXER
Scintilla_LinkLexers();
- LexerManager *lexMan = LexerManager::GetInstance();
- lexMan->Load();
#endif
return result;
}
diff --git a/win32/scintilla.mak b/win32/scintilla.mak
index 2865ef725..c60c903b9 100644
--- a/win32/scintilla.mak
+++ b/win32/scintilla.mak
@@ -226,10 +226,10 @@ $(DIR_O)\Editor.obj: ../src/Editor.cxx ../include/Platform.h \
../src/CellBuffer.h ../src/KeyMap.h ../src/Indicator.h \
../src/LineMarker.h ../src/Style.h ../src/ViewStyle.h \
../src/Document.h ../src/Editor.h ../src/XPM.h
-$(DIR_O)\ExternalLexer.obj: ExternalLexer.cxx ../include/Platform.h \
+$(DIR_O)\ExternalLexer.obj: ../src/ExternalLexer.cxx ../include/Platform.h \
../include/SciLexer.h ../include/PropSet.h ../include/SString.h \
../include/Accessor.h ../src/DocumentAccessor.h ../include/KeyWords.h \
- ExternalLexer.h
+ ../src/ExternalLexer.h
$(DIR_O)\Indicator.obj: ../src/Indicator.cxx ../include/Platform.h \
../include/Scintilla.h ../src/Indicator.h
$(DIR_O)\KeyMap.obj: ../src/KeyMap.cxx ../include/Platform.h \