DittoChaiScript.cpp 4.2 KB

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