summaryrefslogtreecommitdiff
path: root/plugins/Import/src/patterns.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2019-04-25 12:54:22 +0300
committerGeorge Hazan <ghazan@miranda.im>2019-04-25 12:54:22 +0300
commit462deb27452322b8540f6d31230e766760b817bb (patch)
tree3b65a39d7089730a444205cb1d0f9ccad6db302c /plugins/Import/src/patterns.cpp
parent644e81723335e445a7e53c6d858867ba416b31cd (diff)
first version of pattern import that could be compiled
Diffstat (limited to 'plugins/Import/src/patterns.cpp')
-rw-r--r--plugins/Import/src/patterns.cpp129
1 files changed, 128 insertions, 1 deletions
diff --git a/plugins/Import/src/patterns.cpp b/plugins/Import/src/patterns.cpp
index 67da571557..f893ade734 100644
--- a/plugins/Import/src/patterns.cpp
+++ b/plugins/Import/src/patterns.cpp
@@ -22,12 +22,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "stdafx.h"
+#include <memory>
+#include <vector>
+
void CMPlugin::LoadPatterns()
{
wchar_t wszPath[MAX_PATH], wszFullPath[MAX_PATH];
GetModuleFileNameW(m_hInst, wszPath, _countof(wszPath));
if (auto *p = wcsrchr(wszPath, '\\'))
- p[1] = 0;
+ *p = 0;
mir_snwprintf(wszFullPath, L"%s\\Import\\*.ini", wszPath);
@@ -54,6 +57,9 @@ void CMPlugin::LoadPattern(const wchar_t *pwszFileName)
pNew->wszName = buf;
pNew->iType = GetPrivateProfileIntW(L"General", L"Type", 1, pwszFileName);
+ if (GetPrivateProfileStringW(L"General", L"DefaultExtension", L"", buf, _countof(buf), pwszFileName))
+ pNew->wszExt = buf;
+
if (GetPrivateProfileStringW(L"General", L"Charset", L"", buf, _countof(buf), pwszFileName)) {
if (!wcsicmp(buf, L"ANSI"))
pNew->iCodePage = GetPrivateProfileIntW(L"General", L"Codepage", CP_ACP, pwszFileName);
@@ -104,3 +110,124 @@ void CMPlugin::LoadPattern(const wchar_t *pwszFileName)
m_patterns.insert(pNew.release());
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// pattern-based database driver
+
+class CDbxPattern : public MDatabaseReadonly, public MZeroedObject
+{
+ FILE *m_hFile;
+
+ std::vector<DWORD> m_events;
+ std::vector<DWORD>::iterator m_curr;
+
+public:
+ CDbxPattern()
+ {}
+
+ ~CDbxPattern()
+ {
+ if (m_hFile != INVALID_HANDLE_VALUE)
+ ::CloseHandle(m_hFile);
+ }
+
+ void Load()
+ {
+ wchar_t wszLine[2048];
+ do {
+ m_events.push_back(ftell(m_hFile));
+ }
+ while (fgetws(wszLine, _countof(wszLine), m_hFile));
+
+ fseek(m_hFile, 0, SEEK_SET);
+ }
+
+ int Open(const wchar_t *profile)
+ {
+ m_hFile = _wfopen(profile, L"rb");
+ return (m_hFile == nullptr) ? EGROKPRF_CANTREAD : EGROKPRF_NOERROR;
+ }
+
+ // mcontacts format always store history for one contact only
+ STDMETHODIMP_(LONG) GetContactCount(void) override
+ {
+ return 1;
+ }
+
+ STDMETHODIMP_(LONG) GetEventCount(MCONTACT) override
+ {
+ return (LONG)m_events.size();
+ }
+
+ STDMETHODIMP_(BOOL) GetEvent(MEVENT dwOffset, DBEVENTINFO *dbei) override
+ {
+ fseek(m_hFile, dwOffset, SEEK_SET);
+
+ wchar_t wszLine[2048];
+ fgetws(wszLine, _countof(wszLine), m_hFile);
+ return 1;
+ }
+
+ STDMETHODIMP_(MEVENT) FindFirstEvent(MCONTACT) override
+ {
+ m_curr = m_events.begin();
+ return *m_curr;
+ }
+
+ STDMETHODIMP_(MEVENT) FindNextEvent(MCONTACT, MEVENT) override
+ {
+ if (m_curr == m_events.end())
+ return 0;
+
+ ++m_curr;
+ return *m_curr;
+ }
+
+ STDMETHODIMP_(MEVENT) FindLastEvent(MCONTACT) override
+ {
+ m_curr = m_events.end();
+ return *m_curr;
+ }
+
+ STDMETHODIMP_(MEVENT) FindPrevEvent(MCONTACT, MEVENT) override
+ {
+ if (m_curr == m_events.begin())
+ return 0;
+
+ --m_curr;
+ return *m_curr;
+ }
+};
+
+/////////////////////////////////////////////////////////////////////////////////////////
+// database link functions
+
+static int pattern_makeDatabase(const wchar_t*)
+{
+ return 1;
+}
+
+static int pattern_grokHeader(const wchar_t *profile)
+{
+ return CDbxPattern().Open(profile);
+}
+
+static MDatabaseCommon* pattern_load(const wchar_t *profile, BOOL)
+{
+ std::auto_ptr<CDbxPattern> db(new CDbxPattern());
+ if (db->Open(profile))
+ return nullptr;
+
+ db->Load();
+ return db.release();
+}
+
+DATABASELINK g_patternDbLink =
+{
+ 0,
+ "pattern",
+ L"Pattern-based file driver",
+ pattern_makeDatabase,
+ pattern_grokHeader,
+ pattern_load
+};