DittoChaiScript.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. char *stringData = (char *)GlobalLock(pFormat->Data());
  27. if (stringData != NULL)
  28. {
  29. s = stringData;
  30. GlobalUnlock(pFormat->Data());
  31. }
  32. }
  33. }
  34. return s;
  35. }
  36. void CDittoChaiScript::SetAsciiString(std::string stringVal)
  37. {
  38. if (m_pClip)
  39. {
  40. m_pClip->Clips()->DeleteAll();
  41. HGLOBAL hGlobal = ::NewGlobalP((LPVOID)stringVal.c_str(), stringVal.size()+1);
  42. ASSERT(hGlobal);
  43. m_pClip->Clips()->AddNew(CF_TEXT, hGlobal);
  44. }
  45. }
  46. std::string CDittoChaiScript::GetClipMD5(std::string clipboardFormat)
  47. {
  48. CMd5 md5;
  49. md5.MD5Init();
  50. std::string md5String;
  51. if (m_pClip)
  52. {
  53. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  54. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  55. if (pFormat)
  56. {
  57. SIZE_T size = ::GlobalSize(pFormat->Data());
  58. void* pv = GlobalLock(pFormat->Data());
  59. if (pv != NULL)
  60. {
  61. md5.MD5Update((unsigned char*)pv, (unsigned int)size);
  62. GlobalUnlock(pFormat->Data());
  63. md5String = md5.MD5FinalToString();
  64. }
  65. }
  66. }
  67. return md5String;
  68. }
  69. SIZE_T CDittoChaiScript::GetClipSize(std::string clipboardFormat)
  70. {
  71. SIZE_T size = 0;
  72. if (m_pClip)
  73. {
  74. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  75. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  76. if (pFormat)
  77. {
  78. size = ::GlobalSize(pFormat->Data());
  79. }
  80. }
  81. return size;
  82. }
  83. BOOL CDittoChaiScript::FormatExists(std::string clipboardFormat)
  84. {
  85. BOOL exists = FALSE;
  86. if (m_pClip)
  87. {
  88. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  89. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  90. if (pFormat)
  91. {
  92. exists = TRUE;
  93. }
  94. }
  95. return exists;
  96. }
  97. BOOL CDittoChaiScript::RemoveFormat(std::string clipboardFormat)
  98. {
  99. BOOL removed = FALSE;
  100. if (m_pClip)
  101. {
  102. int formatId = GetFormatID(CTextConvert::MultiByteToUnicodeString(clipboardFormat.c_str()));
  103. if (m_pClip->Clips()->RemoveFormat(formatId))
  104. {
  105. removed = TRUE;
  106. }
  107. }
  108. return removed;
  109. }
  110. BOOL CDittoChaiScript::SetParentId(int parentId)
  111. {
  112. BOOL set = FALSE;
  113. if (m_pClip)
  114. {
  115. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lID = %d"), parentId);
  116. if (q.eof() == false)
  117. {
  118. m_pClip->Parent(parentId);
  119. }
  120. }
  121. return set;
  122. }
  123. BOOL CDittoChaiScript::AsciiTextMatchesRegex(std::string regex)
  124. {
  125. BOOL matches = false;
  126. auto ascii = GetAsciiString();
  127. std::regex integer(regex);
  128. if (regex_match(ascii, integer))
  129. {
  130. matches = true;
  131. }
  132. return matches;
  133. }
  134. void CDittoChaiScript::AsciiTextReplaceRegex(std::string regex, std::string replaceWith)
  135. {
  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. }
  153. BOOL CDittoChaiScript::DescriptionMatchesRegex(std::string regex)
  154. {
  155. BOOL matches = false;
  156. if (m_pClip)
  157. {
  158. std:string ascii(CTextConvert::ConvertToChar(m_pClip->Description()).GetBuffer());
  159. std::regex integer(regex);
  160. if (regex_match(ascii, integer))
  161. {
  162. matches = true;
  163. }
  164. return matches;
  165. }
  166. }
  167. void CDittoChaiScript::DescriptionReplaceRegex(std::string regex, std::string replaceWith)
  168. {
  169. if (m_pClip)
  170. {
  171. std:string ascii(CTextConvert::ConvertToChar(m_pClip->Description()).GetBuffer());
  172. std::regex integer(regex);
  173. auto newAscii = regex_replace(ascii, integer, replaceWith);
  174. CString cstr(newAscii.c_str());
  175. m_pClip->Description(cstr);
  176. }
  177. }