DittoChaiScript.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "stdafx.h"
  2. #include "DittoChaiScript.h"
  3. #include "Shared\TextConvert.h"
  4. #include "Md5.h"
  5. #include "Misc.h"
  6. #include "CP_Main.h"
  7. #include <regex>
  8. CDittoChaiScript::CDittoChaiScript(IClip *pClip, std::string activeApp)
  9. {
  10. m_pClip = pClip;
  11. m_activeApp = activeApp;
  12. }
  13. CDittoChaiScript::~CDittoChaiScript()
  14. {
  15. }
  16. std::string CDittoChaiScript::GetAsciiString()
  17. {
  18. std::string s = "";
  19. if (m_pClip)
  20. {
  21. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(CF_TEXT);
  22. if (pFormat)
  23. {
  24. char *stringData = (char *)GlobalLock(pFormat->Data());
  25. if (stringData != NULL)
  26. {
  27. s = stringData;
  28. GlobalUnlock(pFormat->Data());
  29. }
  30. }
  31. }
  32. return s;
  33. }
  34. void CDittoChaiScript::SetAsciiString(std::string stringVal)
  35. {
  36. if (m_pClip)
  37. {
  38. m_pClip->Clips()->DeleteAll();
  39. HGLOBAL hGlobal = ::NewGlobalP((LPVOID)stringVal.c_str(), stringVal.size()+1);
  40. ASSERT(hGlobal);
  41. m_pClip->Clips()->AddNew(CF_TEXT, hGlobal);
  42. }
  43. }
  44. std::string CDittoChaiScript::GetClipMD5(std::string clipboardFormat)
  45. {
  46. CMd5 md5;
  47. md5.MD5Init();
  48. std::string md5String;
  49. if (m_pClip)
  50. {
  51. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  52. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  53. if (pFormat)
  54. {
  55. SIZE_T size = ::GlobalSize(pFormat->Data());
  56. void* pv = GlobalLock(pFormat->Data());
  57. if (pv != NULL)
  58. {
  59. md5.MD5Update((unsigned char*)pv, (unsigned int)size);
  60. GlobalUnlock(pFormat->Data());
  61. md5String = md5.MD5FinalToString();
  62. }
  63. }
  64. }
  65. return md5String;
  66. }
  67. SIZE_T CDittoChaiScript::GetClipSize(std::string clipboardFormat)
  68. {
  69. SIZE_T size = 0;
  70. if (m_pClip)
  71. {
  72. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  73. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  74. if (pFormat)
  75. {
  76. size = ::GlobalSize(pFormat->Data());
  77. }
  78. }
  79. return size;
  80. }
  81. BOOL CDittoChaiScript::FormatExists(std::string clipboardFormat)
  82. {
  83. BOOL exists = FALSE;
  84. if (m_pClip)
  85. {
  86. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  87. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  88. if (pFormat)
  89. {
  90. exists = TRUE;
  91. }
  92. }
  93. return exists;
  94. }
  95. BOOL CDittoChaiScript::RemoveFormat(std::string clipboardFormat)
  96. {
  97. BOOL removed = FALSE;
  98. if (m_pClip)
  99. {
  100. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  101. if (m_pClip->Clips()->RemoveFormat(formatId))
  102. {
  103. removed = TRUE;
  104. }
  105. }
  106. return removed;
  107. }
  108. BOOL CDittoChaiScript::SetParentId(int parentId)
  109. {
  110. BOOL set = FALSE;
  111. if (m_pClip)
  112. {
  113. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lID = %d"), parentId);
  114. if (q.eof() == false)
  115. {
  116. m_pClip->Parent(parentId);
  117. }
  118. }
  119. return set;
  120. }
  121. BOOL CDittoChaiScript::AsciiTextMatchesRegex(std::string regex)
  122. {
  123. BOOL matches = false;
  124. auto ascii = GetAsciiString();
  125. std::regex integer(regex);
  126. if (regex_match(ascii, integer))
  127. {
  128. matches = true;
  129. }
  130. return matches;
  131. }