OSDN Git Service

Initial revision
[winmerge-jp/winmerge-jp.git] / ArchiveSupport / Merge7z / tools.cpp
1 /* File:        tools.cpp
2  * Author:      Jochen Tucht 2003/12/09
3  *                      Copyright (C) Jochen Tucht
4  *
5  * Purpose:     supplementary classes and functions for Merge7z
6  *
7  * Remarks:     
8  *
9  *      *** SECURITY ALERT ***
10  *      Be aware of 2. a) of the GNU General Public License. Please log your changes
11  *      at the end of this comment.
12  *
13  * License:     This program is free software; you can redistribute it and/or modify
14  *                      it under the terms of the GNU General Public License as published by
15  *                      the Free Software Foundation; either version 2 of the License, or
16  *                      (at your option) any later version.
17  *
18  *                      This program is distributed in the hope that it will be useful,
19  *                      but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *                      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  *                      GNU General Public License for more details.
22  *
23  *                      You should have received a copy of the GNU General Public License
24  *                      along with this program; if not, write to the Free Software
25  *                      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27
28 DATE:           BY:                                     DESCRIPTION:
29 ==========      ==================      ================================================
30
31 */
32
33 #include "stdafx.h"
34
35 #ifndef StrIsIntlEqual
36 #ifdef UNICODE
37 #define StrIsIntlEqual          StrIsIntlEqualW
38 #else
39 #define StrIsIntlEqual          StrIsIntlEqualA
40 #endif
41 #endif
42
43 #include "tools.h"
44
45 /**
46  * @brief Construct and throw a Complain object containing an error message.
47  */
48 Complain::Complain(LPCTSTR format, ...)
49 {
50         wvsprintf(msg, format, (va_list)(&format + 1));
51         throw this;
52 }
53
54 /**
55  * @brief Report error to user. To be called from within catch block.
56  */
57 int Complain::Alert(HWND hwndParent, UINT flags)
58 {
59         return MessageBox(hwndParent, msg, 0, flags);
60 }
61
62 /**
63  * @brief Complain that some DLL failed to CreateObject() something.
64  */
65 void ComplainCreateObject(HMODULE handle, LPCTSTR name)
66 {
67         TCHAR module[MAX_PATH];
68         ::GetModuleFileName(handle, module, sizeof module);
69         Complain(_T("%.300s Failed to create %.300s"), module, name);
70
71
72 /**
73  * @brief Complain that something could not be found.
74  */
75 void ComplainNotFound(LPCTSTR name)
76 {
77         Complain(_T("Not found: %.300s"), name);
78 }
79
80 /**
81  * @brief Complain that something could not be opened.
82  */
83 void ComplainCantOpen(LPCTSTR name)
84 {
85         Complain(_T("Can't open: %.300s"), name);
86 }
87
88 /**
89  * @brief Release interface until ref count reaches 0.
90  *
91  * Very bad practice in general, but helps avoiding resource leaks
92  * due to inaccurate ref counting.
93  */
94 void NTAPI Release(IUnknown *punk)
95 {
96         while (punk)
97         {       
98                 ULONG Release = punk->Release();
99                 if (Release == 0)
100                 {
101                         punk = 0;
102                 }
103         }
104 }
105
106 /**
107  * @brief Find a keyword within an EnumList, and return its numeric value.
108  */
109 EnumList::Find(LPCTSTR r, BOOL fCaseSens) const
110 {
111         static const TCHAR trim[] = _T(".;:() ");
112         int cch = StrCSpn(r += StrSpn(r, trim), trim);
113         int Find = 0;
114         LPCTSTR q = buffer;
115         while (LPCTSTR p = StrChr(q, '('))
116         {
117                 q = StrChr(++p, ')');
118                 if (StrIsIntlEqual(fCaseSens, p, r, cch) && q - p == cch)
119                         break;
120                 ++Find;
121         }
122         return Find;
123 }