DittoChaiScript.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, std::string activeAppTitle)
  9. {
  10. m_pClip = pClip;
  11. m_activeApp = activeApp;
  12. m_activeAppTitle = activeAppTitle;
  13. }
  14. CDittoChaiScript::~CDittoChaiScript()
  15. {
  16. }
  17. std::string CDittoChaiScript::GetAsciiString()
  18. {
  19. std::string s = "";
  20. if (m_pClip)
  21. {
  22. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(CF_TEXT);
  23. if (pFormat)
  24. {
  25. char *stringData = (char *)GlobalLock(pFormat->Data());
  26. if (stringData != NULL)
  27. {
  28. s = stringData;
  29. GlobalUnlock(pFormat->Data());
  30. }
  31. }
  32. }
  33. return s;
  34. }
  35. void CDittoChaiScript::SetAsciiString(std::string stringVal)
  36. {
  37. if (m_pClip)
  38. {
  39. m_pClip->Clips()->DeleteAll();
  40. HGLOBAL hGlobal = ::NewGlobalP((LPVOID)stringVal.c_str(), stringVal.size()+1);
  41. ASSERT(hGlobal);
  42. m_pClip->Clips()->AddNew(CF_TEXT, hGlobal);
  43. }
  44. }
  45. std::string CDittoChaiScript::GetClipMD5(std::string clipboardFormat)
  46. {
  47. CMd5 md5;
  48. md5.MD5Init();
  49. std::string md5String;
  50. if (m_pClip)
  51. {
  52. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  53. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  54. if (pFormat)
  55. {
  56. SIZE_T size = ::GlobalSize(pFormat->Data());
  57. void* pv = GlobalLock(pFormat->Data());
  58. if (pv != NULL)
  59. {
  60. md5.MD5Update((unsigned char*)pv, (unsigned int)size);
  61. GlobalUnlock(pFormat->Data());
  62. md5String = md5.MD5FinalToString();
  63. }
  64. }
  65. }
  66. return md5String;
  67. }
  68. SIZE_T CDittoChaiScript::GetClipSize(std::string clipboardFormat)
  69. {
  70. SIZE_T size = 0;
  71. if (m_pClip)
  72. {
  73. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  74. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  75. if (pFormat)
  76. {
  77. size = ::GlobalSize(pFormat->Data());
  78. }
  79. }
  80. return size;
  81. }
  82. BOOL CDittoChaiScript::FormatExists(std::string clipboardFormat)
  83. {
  84. BOOL exists = FALSE;
  85. if (m_pClip)
  86. {
  87. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  88. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  89. if (pFormat)
  90. {
  91. exists = TRUE;
  92. }
  93. }
  94. return exists;
  95. }
  96. BOOL CDittoChaiScript::RemoveFormat(std::string clipboardFormat)
  97. {
  98. BOOL removed = FALSE;
  99. if (m_pClip)
  100. {
  101. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  102. if (m_pClip->Clips()->RemoveFormat(formatId))
  103. {
  104. removed = TRUE;
  105. }
  106. }
  107. return removed;
  108. }
  109. BOOL CDittoChaiScript::SetParentId(int parentId)
  110. {
  111. BOOL set = FALSE;
  112. if (m_pClip)
  113. {
  114. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lID = %d"), parentId);
  115. if (q.eof() == false)
  116. {
  117. m_pClip->Parent(parentId);
  118. }
  119. }
  120. return set;
  121. }
  122. BOOL CDittoChaiScript::AsciiTextMatchesRegex(std::string regex)
  123. {
  124. BOOL matches = false;
  125. auto ascii = GetAsciiString();
  126. std::regex integer(regex);
  127. if (regex_match(ascii, integer))
  128. {
  129. matches = true;
  130. }
  131. return matches;
  132. }
  133. void CDittoChaiScript::AsciiTextReplaceRegex(std::string regex, std::string replaceWith)
  134. {
  135. BOOL matches = false;
  136. auto ascii = GetAsciiString();
  137. std::regex integer(regex);
  138. auto newAscii = regex_replace(ascii, integer, replaceWith);
  139. SetAsciiString(newAscii);
  140. }
  141. void CDittoChaiScript::SetMakeTopSticky()
  142. {
  143. m_pClip->SetSaveToDbSticky(AddToDbStickyEnum::MAKE_LAST_STICKY);
  144. }
  145. void CDittoChaiScript::SetMakeLastSticky()
  146. {
  147. m_pClip->SetSaveToDbSticky(AddToDbStickyEnum::MAKE_LAST_STICKY);
  148. }
  149. void CDittoChaiScript::SetReplaceTopSticky()
  150. {
  151. m_pClip->SetSaveToDbSticky(AddToDbStickyEnum::REPLACE_TOP_STICKY);
  152. }