AccessToSqlite.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // AccessToSqlite.cpp : Defines the initialization routines for the DLL.
  2. //
  3. #include "stdafx.h"
  4. #include "AccessToSqlite.h"
  5. #include "OpenAccessdatabase.h"
  6. #include "Convert.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. //
  11. //TODO: If this DLL is dynamically linked against the MFC DLLs,
  12. // any functions exported from this DLL which call into
  13. // MFC must have the AFX_MANAGE_STATE macro added at the
  14. // very beginning of the function.
  15. //
  16. // For example:
  17. //
  18. // extern "C" BOOL PASCAL EXPORT ExportedFunction()
  19. // {
  20. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  21. // // normal function body here
  22. // }
  23. //
  24. // It is very important that this macro appear in each
  25. // function, prior to any calls into MFC. This means that
  26. // it must appear as the first statement within the
  27. // function, even before any object variable declarations
  28. // as their constructors may generate calls into the MFC
  29. // DLL.
  30. //
  31. // Please see MFC Technical Notes 33 and 58 for additional
  32. // details.
  33. //
  34. // CAccessToSqliteApp
  35. BEGIN_MESSAGE_MAP(CAccessToSqliteApp, CWinApp)
  36. END_MESSAGE_MAP()
  37. // CAccessToSqliteApp construction
  38. CAccessToSqliteApp::CAccessToSqliteApp()
  39. {
  40. // TODO: add construction code here,
  41. // Place all significant initialization in InitInstance
  42. }
  43. // The one and only CAccessToSqliteApp object
  44. CAccessToSqliteApp theApp;
  45. // CAccessToSqliteApp initialization
  46. BOOL CAccessToSqliteApp::InitInstance()
  47. {
  48. CWinApp::InitInstance();
  49. return TRUE;
  50. }
  51. int CAccessToSqliteApp::ExitInstance()
  52. {
  53. //cleanup dao
  54. AfxDaoTerm();
  55. return CWinApp::ExitInstance();
  56. }
  57. // asserts if hDest isn't big enough
  58. void CopyToGlobalHP( HGLOBAL hDest, LPVOID pBuf, ULONG ulBufLen )
  59. {
  60. ASSERT( hDest && pBuf && ulBufLen );
  61. LPVOID pvData = GlobalLock(hDest);
  62. ASSERT( pvData );
  63. ULONG size = (ULONG)GlobalSize(hDest);
  64. ASSERT( size >= ulBufLen ); // assert if hDest isn't big enough
  65. memcpy(pvData, pBuf, ulBufLen);
  66. GlobalUnlock(hDest);
  67. }
  68. void CopyToGlobalHH( HGLOBAL hDest, HGLOBAL hSource, ULONG ulBufLen )
  69. {
  70. ASSERT( hDest && hSource && ulBufLen );
  71. LPVOID pvData = GlobalLock(hSource);
  72. ASSERT( pvData );
  73. ULONG size = (ULONG)GlobalSize(hSource);
  74. ASSERT( size >= ulBufLen ); // assert if hSource isn't big enough
  75. CopyToGlobalHP(hDest, pvData, ulBufLen);
  76. GlobalUnlock(hSource);
  77. }
  78. HGLOBAL NewGlobalP( LPVOID pBuf, UINT nLen )
  79. {
  80. ASSERT( pBuf && nLen );
  81. HGLOBAL hDest = GlobalAlloc( GMEM_MOVEABLE | GMEM_SHARE, nLen );
  82. ASSERT( hDest );
  83. CopyToGlobalHP( hDest, pBuf, nLen );
  84. return hDest;
  85. }
  86. HGLOBAL NewGlobal(UINT nLen)
  87. {
  88. ASSERT(nLen);
  89. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  90. return hDest;
  91. }
  92. HGLOBAL NewGlobalH( HGLOBAL hSource, UINT nLen )
  93. {
  94. ASSERT( hSource && nLen );
  95. LPVOID pvData = GlobalLock( hSource );
  96. HGLOBAL hDest = NewGlobalP( pvData, nLen );
  97. GlobalUnlock( hSource );
  98. return hDest;
  99. }
  100. //Do not change these these are stored in the database
  101. CLIPFORMAT GetFormatID(LPCSTR cbName)
  102. {
  103. if(STRCMP(cbName, "CF_TEXT") == 0)
  104. return CF_TEXT;
  105. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  106. return CF_METAFILEPICT;
  107. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  108. return CF_SYLK;
  109. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  110. return CF_DIF;
  111. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  112. return CF_TIFF;
  113. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  114. return CF_OEMTEXT;
  115. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  116. return CF_DIB;
  117. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  118. return CF_PALETTE;
  119. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  120. return CF_PENDATA;
  121. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  122. return CF_RIFF;
  123. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  124. return CF_WAVE;
  125. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  126. return CF_UNICODETEXT;
  127. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  128. return CF_ENHMETAFILE;
  129. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  130. return CF_HDROP;
  131. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  132. return CF_LOCALE;
  133. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  134. return CF_OWNERDISPLAY;
  135. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  136. return CF_DSPTEXT;
  137. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  138. return CF_DSPBITMAP;
  139. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  140. return CF_DSPMETAFILEPICT;
  141. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  142. return CF_DSPENHMETAFILE;
  143. return ::RegisterClipboardFormat(cbName);
  144. }
  145. //Do not change these these are stored in the database
  146. CString GetFormatName(CLIPFORMAT cbType)
  147. {
  148. switch(cbType)
  149. {
  150. case CF_TEXT:
  151. return _T("CF_TEXT");
  152. case CF_BITMAP:
  153. return _T("CF_BITMAP");
  154. case CF_METAFILEPICT:
  155. return _T("CF_METAFILEPICT");
  156. case CF_SYLK:
  157. return _T("CF_SYLK");
  158. case CF_DIF:
  159. return _T("CF_DIF");
  160. case CF_TIFF:
  161. return _T("CF_TIFF");
  162. case CF_OEMTEXT:
  163. return _T("CF_OEMTEXT");
  164. case CF_DIB:
  165. return _T("CF_DIB");
  166. case CF_PALETTE:
  167. return _T("CF_PALETTE");
  168. case CF_PENDATA:
  169. return _T("CF_PENDATA");
  170. case CF_RIFF:
  171. return _T("CF_RIFF");
  172. case CF_WAVE:
  173. return _T("CF_WAVE");
  174. case CF_UNICODETEXT:
  175. return _T("CF_UNICODETEXT");
  176. case CF_ENHMETAFILE:
  177. return _T("CF_ENHMETAFILE");
  178. case CF_HDROP:
  179. return _T("CF_HDROP");
  180. case CF_LOCALE:
  181. return _T("CF_LOCALE");
  182. case CF_OWNERDISPLAY:
  183. return _T("CF_OWNERDISPLAY");
  184. case CF_DSPTEXT:
  185. return _T("CF_DSPTEXT");
  186. case CF_DSPBITMAP:
  187. return _T("CF_DSPBITMAP");
  188. case CF_DSPMETAFILEPICT:
  189. return _T("CF_DSPMETAFILEPICT");
  190. case CF_DSPENHMETAFILE:
  191. return _T("CF_DSPENHMETAFILE");
  192. default:
  193. //Not a default type get the name from the clipboard
  194. if (cbType != 0)
  195. {
  196. TCHAR szFormat[256];
  197. GetClipboardFormatName(cbType, szFormat, 256);
  198. return szFormat;
  199. }
  200. break;
  201. }
  202. return _T("ERROR");
  203. }
  204. CString StrF(const TCHAR * pszFormat, ...)
  205. {
  206. ASSERT( AfxIsValidString( pszFormat ) );
  207. CString str;
  208. va_list argList;
  209. va_start( argList, pszFormat );
  210. str.FormatV( pszFormat, argList );
  211. va_end( argList );
  212. return str;
  213. }
  214. #define _CRT_SECURE_NO_DEPRECATE 1
  215. void AppendToFile(const TCHAR* fn, const TCHAR* msg)
  216. {
  217. #ifdef _UNICODE
  218. FILE *file = _wfopen(fn, _T("a"));
  219. #else
  220. FILE *file = fopen(fn, _T("a"));
  221. #endif
  222. ASSERT( file );
  223. #ifdef _UNICODE
  224. fwprintf(file, msg);
  225. #else
  226. fprintf(file, msg);
  227. #endif
  228. fclose(file);
  229. }
  230. CString GetExeFileName()
  231. {
  232. CString sExeName;
  233. GetModuleFileName(NULL, sExeName.GetBuffer(_MAX_PATH),_MAX_PATH);
  234. sExeName.ReleaseBuffer();
  235. return sExeName;
  236. }
  237. CString GetFilePath(CString csFileName)
  238. {
  239. long lSlash = csFileName.ReverseFind('\\');
  240. if(lSlash > -1)
  241. {
  242. csFileName = csFileName.Left(lSlash + 1);
  243. }
  244. return csFileName;
  245. }
  246. CString GetFileName(CString csFileName)
  247. {
  248. long lSlash = csFileName.ReverseFind('\\');
  249. if(lSlash > -1)
  250. {
  251. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  252. }
  253. return csFileName;
  254. }
  255. void log(const TCHAR* msg, CString csFile, long lLine)
  256. {
  257. ASSERT(AfxIsValidString(msg));
  258. CTime time = CTime::GetCurrentTime();
  259. CString csText = time.Format("[%Y/%m/%d %I:%M:%S %p - ");
  260. CString csFileLine;
  261. csFile = GetFileName(csFile);
  262. csFileLine.Format(_T("%s %d] "), csFile, lLine);
  263. csText += csFileLine;
  264. csText += msg;
  265. csText += "\n";
  266. OutputDebugString(csText);
  267. CString csExeFile = GetExeFileName();
  268. csExeFile = GetFilePath(csExeFile);
  269. csExeFile += "Ditto3Conversion.log";
  270. AppendToFile(csExeFile, csText);
  271. }
  272. extern "C" __declspec(dllexport) BOOL ConvertDatabase(const TCHAR *pNewDatabase, const TCHAR *pOldDatabase)
  273. {
  274. BOOL bRet = FALSE;
  275. try
  276. {
  277. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  278. COpenAccessdatabase Verify;
  279. //make sure that all the updates have been run on the old db
  280. if(Verify.ValidDB(pOldDatabase, TRUE) == TRUE)
  281. {
  282. theApp.m_AccessDatabase.Open(pOldDatabase);
  283. theApp.m_db.open(pNewDatabase);
  284. CConvert Convert;
  285. Convert.SetupProgressWnd();
  286. Convert.ConvertGroups(0);
  287. Convert.ConvertNonGroups();
  288. Convert.ConvertTypes();
  289. theApp.m_AccessDatabase.Close();
  290. theApp.m_db.close();
  291. bRet = TRUE;
  292. }
  293. }
  294. catch(CDaoException* e)
  295. {
  296. TCHAR cError[100];
  297. e->GetErrorMessage(cError, 100);
  298. Log(StrF(_T("CDaoException - %s"), cError));
  299. ASSERT(FALSE);
  300. e->Delete();
  301. }
  302. catch (CppSQLite3Exception& e)
  303. {
  304. Log(StrF(_T("SQLITE Exception %d - %s"), e.errorCode(), e.errorMessage()));
  305. ASSERT(FALSE);
  306. }
  307. return bRet;
  308. }