DittoChaiScript.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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::wstring CDittoChaiScript::GetUnicodeString()
  19. {
  20. std::wstring s = _T("");
  21. if (m_pClip)
  22. {
  23. IClipFormat* pFormat = m_pClip->Clips()->FindFormatEx(CF_UNICODETEXT);
  24. if (pFormat)
  25. {
  26. s = pFormat->GetAsCString();
  27. }
  28. else
  29. {
  30. s = CTextConvert::AnsiToUnicode(GetAsciiString().c_str());
  31. }
  32. }
  33. return s;
  34. }
  35. void CDittoChaiScript::SetUnicodeString(std::wstring stringVal)
  36. {
  37. if (m_pClip)
  38. {
  39. m_pClip->Clips()->DeleteAll();
  40. HGLOBAL hGlobal = ::NewGlobalP((LPVOID)stringVal.c_str(), (stringVal.size() + 1) * 2);
  41. ASSERT(hGlobal);
  42. m_pClip->Clips()->AddNew(CF_UNICODETEXT, hGlobal);
  43. }
  44. }
  45. std::string CDittoChaiScript::GetAsciiString()
  46. {
  47. std::string s = "";
  48. if (m_pClip)
  49. {
  50. IClipFormat* pFormat = m_pClip->Clips()->FindFormatEx(CF_TEXT);
  51. if (pFormat)
  52. {
  53. s = pFormat->GetAsCStringA();
  54. }
  55. }
  56. return s;
  57. }
  58. void CDittoChaiScript::SetAsciiString(std::string stringVal)
  59. {
  60. if (m_pClip)
  61. {
  62. m_pClip->Clips()->DeleteAll();
  63. HGLOBAL hGlobal = ::NewGlobalP((LPVOID)stringVal.c_str(), stringVal.size()+1);
  64. ASSERT(hGlobal);
  65. m_pClip->Clips()->AddNew(CF_TEXT, hGlobal);
  66. }
  67. }
  68. std::string CDittoChaiScript::GetClipMD5(std::string clipboardFormat)
  69. {
  70. CMd5 md5;
  71. md5.MD5Init();
  72. std::string md5String;
  73. if (m_pClip)
  74. {
  75. int formatId = GetFormatID(CTextConvert::AnsiToUnicode(clipboardFormat.c_str()));
  76. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  77. if (pFormat)
  78. {
  79. SIZE_T size = ::GlobalSize(pFormat->Data());
  80. void* pv = GlobalLock(pFormat->Data());
  81. if (pv != NULL)
  82. {
  83. md5.MD5Update((unsigned char*)pv, (unsigned int)size);
  84. GlobalUnlock(pFormat->Data());
  85. md5String = md5.MD5FinalToString();
  86. }
  87. }
  88. }
  89. return md5String;
  90. }
  91. SIZE_T CDittoChaiScript::GetClipSize(std::string clipboardFormat)
  92. {
  93. SIZE_T size = 0;
  94. if (m_pClip)
  95. {
  96. int formatId = GetFormatID(CTextConvert::AnsiToUnicode(clipboardFormat.c_str()));
  97. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  98. if (pFormat)
  99. {
  100. size = ::GlobalSize(pFormat->Data());
  101. }
  102. }
  103. return size;
  104. }
  105. BOOL CDittoChaiScript::FormatExists(std::string clipboardFormat)
  106. {
  107. BOOL exists = FALSE;
  108. if (m_pClip)
  109. {
  110. int formatId = GetFormatID(CTextConvert::AnsiToUnicode(clipboardFormat.c_str()));
  111. IClipFormat *pFormat = m_pClip->Clips()->FindFormatEx(formatId);
  112. if (pFormat)
  113. {
  114. exists = TRUE;
  115. }
  116. }
  117. return exists;
  118. }
  119. BOOL CDittoChaiScript::RemoveFormat(std::string clipboardFormat)
  120. {
  121. BOOL removed = FALSE;
  122. if (m_pClip)
  123. {
  124. int formatId = GetFormatID(CTextConvert::AnsiToUnicode(clipboardFormat.c_str()));
  125. if (m_pClip->Clips()->RemoveFormat(formatId))
  126. {
  127. removed = TRUE;
  128. }
  129. }
  130. return removed;
  131. }
  132. BOOL CDittoChaiScript::SetParentId(int parentId)
  133. {
  134. BOOL set = FALSE;
  135. if (m_pClip)
  136. {
  137. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lID = %d"), parentId);
  138. if (q.eof() == false)
  139. {
  140. m_pClip->Parent(parentId);
  141. set = TRUE;
  142. }
  143. }
  144. return set;
  145. }
  146. BOOL CDittoChaiScript::AsciiTextMatchesRegex(std::string regex)
  147. {
  148. BOOL matches = false;
  149. std::wstring unicode = GetUnicodeString();
  150. std::wregex integer(CTextConvert::AnsiToUnicode(regex.c_str()));
  151. if (regex_match(unicode, integer))
  152. {
  153. matches = true;
  154. }
  155. return matches;
  156. }
  157. void CDittoChaiScript::AsciiTextReplaceRegex(std::string regex, std::string replaceWith)
  158. {
  159. CStringA utf8 = CTextConvert::UnicodeToUTF8(GetUnicodeString().c_str());
  160. std::regex integer(regex.c_str());
  161. CStringA newUtf8 = std::regex_replace(utf8.GetBuffer(), integer, replaceWith).c_str();
  162. if (utf8 != newUtf8)
  163. {
  164. CString uni = CTextConvert::Utf8ToUnicode(newUtf8).GetBuffer();
  165. SetUnicodeString(uni.GetBuffer());
  166. SetAsciiString(CTextConvert::UnicodeToAnsi(uni).GetBuffer());
  167. }
  168. }
  169. void CDittoChaiScript::SetMakeTopSticky()
  170. {
  171. m_pClip->SetSaveToDbSticky(AddToDbStickyEnum::MAKE_LAST_STICKY);
  172. }
  173. void CDittoChaiScript::SetMakeLastSticky()
  174. {
  175. m_pClip->SetSaveToDbSticky(AddToDbStickyEnum::MAKE_LAST_STICKY);
  176. }
  177. void CDittoChaiScript::SetReplaceTopSticky()
  178. {
  179. m_pClip->SetSaveToDbSticky(AddToDbStickyEnum::REPLACE_TOP_STICKY);
  180. }
  181. BOOL CDittoChaiScript::DescriptionMatchesRegex(std::string regex)
  182. {
  183. BOOL matches = false;
  184. if (m_pClip)
  185. {
  186. std::wstring unicode = m_pClip->Description();
  187. std::wregex integer(CTextConvert::AnsiToUnicode(regex.c_str()));
  188. if (regex_match(unicode, integer))
  189. {
  190. matches = true;
  191. }
  192. }
  193. return matches;
  194. }
  195. void CDittoChaiScript::DescriptionReplaceRegex(std::string regex, std::string replaceWith)
  196. {
  197. if (m_pClip)
  198. {
  199. CStringA utf8 = CTextConvert::UnicodeToUTF8(m_pClip->Description());
  200. std::regex integer(regex.c_str());
  201. auto newAscii = std::regex_replace(utf8.GetBuffer(), integer, replaceWith);
  202. m_pClip->Description(CTextConvert::Utf8ToUnicode(newAscii.c_str()).GetBuffer());
  203. }
  204. }