diff options
author | George Hazan <george.hazan@gmail.com> | 2013-08-11 10:41:37 +0000 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2013-08-11 10:41:37 +0000 |
commit | 94dcc72f13094157f344f284aec4edf0c0f5faa1 (patch) | |
tree | 74c078daadd4abe5aea9e9e0a73b4dd7c4a7fde1 /src/mir_core | |
parent | 42ff2169dbddd25d06404623dba2fff222d17c73 (diff) |
ProtoGetAvatarFormat, ProtoGetAvatarFileFormat, ProtoGetBufferFormat, ProtoGetAvatarExtension - standard helpers for protocol avatars' processing instead of the zoo that existed previously
git-svn-id: http://svn.miranda-ng.org/main/trunk@5645 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'src/mir_core')
-rw-r--r-- | src/mir_core/mir_core.def | 4 | ||||
-rw-r--r-- | src/mir_core/protos.cpp | 100 |
2 files changed, 104 insertions, 0 deletions
diff --git a/src/mir_core/mir_core.def b/src/mir_core/mir_core.def index 0c559cae83..32b1adecd8 100644 --- a/src/mir_core/mir_core.def +++ b/src/mir_core/mir_core.def @@ -218,3 +218,7 @@ json_type @215 json_write @216
json_write_formatted @217
mir_subclassWindowFull @218
+ProtoGetAvatarFormat @219
+ProtoGetAvatarExtension @220
+ProtoGetBufferFormat @221
+ProtoGetAvatarFileFormat @222
diff --git a/src/mir_core/protos.cpp b/src/mir_core/protos.cpp index 098bf280d4..369f8b7abd 100644 --- a/src/mir_core/protos.cpp +++ b/src/mir_core/protos.cpp @@ -141,3 +141,103 @@ MIR_CORE_DLL(HANDLE) ProtoForkThreadEx(PROTO_INTERFACE *pThis, ProtoThreadFunc p UINT lthreadID;
return (HANDLE)::mir_forkthreadowner((pThreadFuncOwner) *(void**)&pFunc, pThis, param, threadID ? threadID : <hreadID);
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+MIR_CORE_DLL(LPCTSTR) ProtoGetAvatarExtension(int format)
+{
+ if (format == PA_FORMAT_PNG)
+ return _T(".png");
+ if (format == PA_FORMAT_JPEG)
+ return _T(".jpg");
+ if (format == PA_FORMAT_ICON)
+ return _T(".ico");
+ if (format == PA_FORMAT_BMP)
+ return _T(".bmp");
+ if (format == PA_FORMAT_GIF)
+ return _T(".gif");
+ if (format == PA_FORMAT_SWF)
+ return _T(".swf");
+ if (format == PA_FORMAT_XML)
+ return _T(".xml");
+
+ return _T("");
+}
+
+MIR_CORE_DLL(int) ProtoGetAvatarFormat(const TCHAR *ptszFileName)
+{
+ if (ptszFileName == NULL)
+ return PA_FORMAT_UNKNOWN;
+
+ const TCHAR *ptszExt = _tcsrchr(ptszFileName, '.');
+ if (ptszExt == NULL)
+ return PA_FORMAT_UNKNOWN;
+
+ if (!_tcsicmp(ptszExt, _T(".png")))
+ return PA_FORMAT_PNG;
+
+ if (!_tcsicmp(ptszExt, _T(".jpg")) || !_tcsicmp(ptszExt, _T(".jpeg")))
+ return PA_FORMAT_JPEG;
+
+ if (!_tcsicmp(ptszExt, _T(".ico")))
+ return PA_FORMAT_ICON;
+
+ if (!_tcsicmp(ptszExt, _T(".bmp")) || _tcsicmp(ptszExt, _T(".rle")))
+ return PA_FORMAT_BMP;
+
+ if (!_tcsicmp(ptszExt, _T(".gif")))
+ return PA_FORMAT_GIF;
+
+ if (!_tcsicmp(ptszExt, _T(".swf")))
+ return PA_FORMAT_SWF;
+
+ if (!_tcsicmp(ptszExt, _T(".xml")))
+ return PA_FORMAT_XML;
+
+ return PA_FORMAT_UNKNOWN;
+}
+
+MIR_CORE_DLL(int) ProtoGetBufferFormat(const void *pBuffer, const TCHAR **ptszExtension)
+{
+ if (!memcmp(pBuffer, "%PNG", 4)) {
+ if (ptszExtension) *ptszExtension = _T(".png");
+ return PA_FORMAT_PNG;
+ }
+
+ if (!memcmp(pBuffer, "GIF8", 4)) {
+ if (ptszExtension) *ptszExtension = _T(".gif");
+ return PA_FORMAT_GIF;
+ }
+
+ if (!memicmp(pBuffer, "<?xml", 5)) {
+ if (ptszExtension) *ptszExtension = _T(".xml");
+ return PA_FORMAT_XML;
+ }
+
+ if (!memcmp(pBuffer, "\xFF\xD8\xFF\xE0", 4) || !memcmp(pBuffer, "\xFF\xD8\xFF\xE1", 4)) {
+ if (ptszExtension) *ptszExtension = _T(".jpg");
+ return PA_FORMAT_JPEG;
+ }
+
+ if (!memcmp(pBuffer, "BM", 2)) {
+ if (ptszExtension) *ptszExtension = _T(".bmp");
+ return PA_FORMAT_BMP;
+ }
+
+ if (ptszExtension) *ptszExtension = _T("");
+ return PA_FORMAT_UNKNOWN;
+}
+
+MIR_CORE_DLL(int) ProtoGetAvatarFileFormat(const TCHAR *ptszFileName)
+{
+ HANDLE hFile = CreateFile(ptszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (hFile == INVALID_HANDLE_VALUE)
+ return PA_FORMAT_UNKNOWN;
+
+ DWORD dwBytes;
+ char buf[32];
+ BOOL res = ReadFile(hFile, buf, SIZEOF(buf), &dwBytes, NULL);
+ CloseHandle(hFile);
+
+ return (res && dwBytes == SIZEOF(buf)) ? ProtoGetBufferFormat(buf) : PA_FORMAT_UNKNOWN;
+}
|