-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAdviseCmd.cpp
186 lines (140 loc) · 6.75 KB
/
AdviseCmd.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
////////////////////////////////////////////////////////////////////////////////
//! \file AdviseCmd.cpp
//! \brief The AdviseCmd class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "AdviseCmd.hpp"
#include <Core/tiostream.hpp>
#include "CmdLineArgs.hpp"
#include <Core/CmdLineException.hpp>
#include <Core/InvalidArgException.hpp>
#include <NCL/DDEClient.hpp>
#include <NCL/DDECltConvPtr.hpp>
#include <WCL/Clipboard.hpp>
#include <Core/StringUtils.hpp>
#include <WCL/ConsoleApp.hpp>
#include "DDECmd.hpp"
#include "AdviseSink.hpp"
#include <NCL/DDELink.hpp>
#include "ValueFormatter.hpp"
////////////////////////////////////////////////////////////////////////////////
//! The table of command specific command line switches.
static Core::CmdLineSwitch s_switches[] =
{
{ USAGE, TXT("?"), nullptr, Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Display the command syntax") },
{ USAGE, nullptr, TXT("help"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Display the command syntax") },
{ SERVER, TXT("s"), TXT("server"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("server"), TXT("The DDE Server name") },
{ TOPIC, TXT("t"), TXT("topic"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("topic"), TXT("The DDE Server topic") },
{ ITEM, TXT("i"), TXT("item"), Core::CmdLineSwitch::MANY, Core::CmdLineSwitch::MULTIPLE, TXT("item"), TXT("The item name(s)") },
{ FORMAT, TXT("f"), TXT("format"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("format"), TXT("The clipboard format to use") },
{ LINK, TXT("l"), TXT("link"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("link"), TXT("The DDE link") },
{ NO_TRIM, TXT("nt"), TXT("no-trim"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::NONE, nullptr, TXT("Don't trim whitespace") },
{ OUT_FMT, TXT("of"), TXT("output-format"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("format"), TXT("The output format") },
{ DATE_FMT, TXT("df"), TXT("date-format"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("format"), TXT("The timestamp date format") },
{ TIME_FMT, TXT("tf"), TXT("time-format"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("format"), TXT("The timestamp time format") },
{ TIMEOUT, nullptr, TXT("timeout"), Core::CmdLineSwitch::ONCE, Core::CmdLineSwitch::SINGLE, TXT("timeout"), TXT("The timeout (ms) for the DDE reply") },
};
static size_t s_switchCount = ARRAY_SIZE(s_switches);
////////////////////////////////////////////////////////////////////////////////
//! Constructor.
AdviseCmd::AdviseCmd(int argc, tchar* argv[], WCL::ConsoleApp& app)
: WCL::ConsoleCmd(s_switches, s_switches+s_switchCount, argc, argv, USAGE)
, m_app(app)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
AdviseCmd::~AdviseCmd()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Get the description of the command.
const tchar* AdviseCmd::getDescription()
{
return TXT("Listen for updates to one or more items");
}
////////////////////////////////////////////////////////////////////////////////
//! Get the expected command usage.
const tchar* AdviseCmd::getUsage()
{
return TXT("USAGE: DDECmd advise [--server <server> --topic <topic> --item <item> ... | --link <link>] [--format <format>]");
}
////////////////////////////////////////////////////////////////////////////////
//! The implementation of the command.
int AdviseCmd::doExecute(tostream& out, tostream& /*err*/)
{
ASSERT(m_parser.getUnnamedArgs().at(0) == TXT("advise"));
// Type aliases.
typedef std::vector<tstring> Items;
typedef Core::CmdLineParser::StringVector::const_iterator ItemConstIter;
tstring server;
tstring topic;
Items items;
// Validate and extract the command line arguments.
if (m_parser.isSwitchSet(LINK))
{
if (m_parser.isSwitchSet(SERVER) || m_parser.isSwitchSet(TOPIC) || m_parser.isSwitchSet(ITEM))
throw Core::CmdLineException(TXT("The --link switch cannot be mixed with --server, --topic & --item"));
tstring link = m_parser.getSwitchValue(LINK);
tstring item;
if (!CDDELink::ParseLink(link, server, topic, item))
throw Core::InvalidArgException(Core::fmt(TXT("Invalid DDE link format '%s'"), link.c_str()));
items.push_back(item);
}
else
{
ASSERT(!m_parser.isSwitchSet(LINK));
if (!m_parser.isSwitchSet(SERVER))
throw Core::CmdLineException(TXT("No DDE server name specified [--server]"));
if (!m_parser.isSwitchSet(TOPIC))
throw Core::CmdLineException(TXT("No DDE server topic specified [--topic]"));
if (!m_parser.isSwitchSet(ITEM))
throw Core::CmdLineException(TXT("No item(s) specified [--item]"));
server = m_parser.getSwitchValue(SERVER);
topic = m_parser.getSwitchValue(TOPIC);
items = m_parser.getNamedArgs().find(ITEM)->second;
}
tstring formatName = TXT("CF_TEXT");
if (m_parser.isSwitchSet(FORMAT))
formatName = m_parser.getSwitchValue(FORMAT);
uint format = CClipboard::FormatHandle(formatName.c_str());
if (format == CF_NONE)
throw Core::InvalidArgException(Core::fmt(TXT("Invalid clipboard format '%s'"), formatName.c_str()));
const bool trimValue = !m_parser.isSwitchSet(NO_TRIM);
tstring valueFormat = (items.size() == 1) ? ValueFormatter::DEFAULT_SINGLE_ITEM_FORMAT
: ValueFormatter::DEFAULT_MULTI_ITEM_FORMAT;
if (m_parser.isSwitchSet(OUT_FMT))
valueFormat = m_parser.getSwitchValue(OUT_FMT);
tstring dateFormat = ValueFormatter::DEFAULT_DATE_FORMAT;
if (m_parser.isSwitchSet(DATE_FMT))
dateFormat = m_parser.getSwitchValue(DATE_FMT);
tstring timeFormat = ValueFormatter::DEFAULT_TIME_FORMAT;
if (m_parser.isSwitchSet(TIME_FMT))
timeFormat = m_parser.getSwitchValue(TIME_FMT);
const ValueFormatter formatter(valueFormat, trimValue, dateFormat, timeFormat);
// Test the formatter to check for errors in the format string.
formatter.format(TXT(""), TXT(""), TXT(""), TXT(""));
DWORD timeout = 0;
if (m_parser.isSwitchSet(TIMEOUT))
timeout = Core::parse<DWORD>(m_parser.getSwitchValue(TIMEOUT));
// Open the conversation.
CDDEClient client;
DDE::CltConvPtr conv(client.CreateConversation(server.c_str(), topic.c_str()));
if (timeout != 0)
conv->SetTimeout(timeout);
CEvent& abortEvent = m_app.getAbortEvent();
// Start listening for updates.
AdviseSink sink(out, formatter, abortEvent);
client.AddListener(&sink);
// Create the links...
for (ItemConstIter it = items.begin(); it != items.end(); ++it)
{
const tstring& item = *it;
conv->CreateLink(item.c_str(), format);
}
CMsgThread& thread = m_app.mainThread();
// Pump messages until the user presses Ctrl-C.
while (!abortEvent.IsSignalled() && thread.ProcessMsgQueue())
thread.WaitForMessageOrSignal(abortEvent.Handle());
return EXIT_SUCCESS;
}