-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMsg.cpp
More file actions
121 lines (107 loc) · 4.14 KB
/
Msg.cpp
File metadata and controls
121 lines (107 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright (C) 2005 Serge Wautier - appTranslator
//
// appTranslator - The ultimate localization tool for your Visual C++ applications
// http://www.apptranslator.com
//
// This source code is provided 'as-is', without any express or implied
// warranty. In no event will the author be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this source code must not be misrepresented; you must not
// claim that you wrote the original source code. If you use this source code
// in a product, an acknowledgment in the product documentation and in the
// About box is required, mentioning appTranslator and http://www.apptranslator.com
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original source code.
//
// 3. This notice may not be removed or altered from any source distribution
//
//
// Msg.cpp: Implementation of the CMsg and CFMsg classes
//
// CMsg : Wrapper around LoadString(). CString that takes a resource string index in its c'tor
//
// CFMsg : Wrapper around the API FormatMessage().
//
#include "stdafx.h"
#include "Msg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// CMsg : CString-derived class used to load strings form the resource string table
// The index in the string table is passed to the c'tor.
// Since CMsg derives from CString, it has an LPCTSTR operator, hence can replace a string literal.
// e.g.: SetWindowText(CMsg(IDS_TITLE)); // Weather Forecast.
//
CMsg::CMsg(UINT nID) // nID = resource ID of string (in string table)
{
if (!LoadString(nID))
{ // Error : string not found !
TRACE(_T("Unknown resource string id : %d\n"),nID);
ASSERT(false);
CString::operator=(_T("???")); // In case of error, set string contents to ???. Compatible with VC6 and VC7.
}
}
//
// CFMsg: Creates a formatted message. Wrapper around the API FormatMessage(). Kind of super-printf().
// Since CFMsg derives from CString, it has an LPCTSTR operator, hence can replace a string literal.
// e.g.: AfxMessageBox( CFMsg(IDS_AGE, szName, nAge), MB_ICONINFORMATION );
// // IDS_AGE : %1 is %2!d! years old.
// // Message : Jane is 27 years old.
//
CFMsg::CFMsg(UINT nFormatID,...) // nFormatID = resource string containing text to format
{ // uses FormatMessage() arguments notation : %1 %2 ...
// Get format string from string table
CString strFormat;
if (!strFormat.LoadString(nFormatID))
{ // Format string not found -> Load error string : ???
TRACE(_T("Unknown resource string id : %d\n"),nFormatID);
ASSERT(false);
CString::operator=(_T("???")); // In case of error, set string contents to ???. Compatible with VC6 and VC7.
}
else
{ // OK, format string was loaded successfully
// Format message into temporary buffer lpszTemp
va_list argList;
va_start(argList, nFormatID);
LPTSTR lpszTemp;
if (::FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
strFormat, 0, 0, (LPTSTR)&lpszTemp, 0, &argList) == 0 ||
lpszTemp == NULL)
{
AfxThrowMemoryException();
}
// Copy lpszTemp into the result string
CString::operator=(lpszTemp); // Compatible with VC6 and VC7.
// Clean-up
LocalFree(lpszTemp);
va_end(argList);
}
}
CFMsg::CFMsg(LPCTSTR lpszFormat,...)
{
// Format message into temporary buffer lpszTemp
va_list argList;
va_start(argList, lpszFormat);
LPTSTR lpszTemp;
if (::FormatMessage(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER,
lpszFormat, 0, 0, (LPTSTR)&lpszTemp, 0, &argList) == 0 ||
lpszTemp == NULL)
{
AfxThrowMemoryException();
}
// Copy lpszTemp into the result string
CString::operator=(lpszTemp); // Compatible with VC6 and VC7.
// Clean-up
LocalFree(lpszTemp);
va_end(argList);
}