1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved.
17// Basic Usage Environment: for a simple, non-scripted, console application
18// Implementation
19
20#include "BasicUsageEnvironment0.hh"
21#include <stdio.h>
22#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_WCE)
23#define snprintf _snprintf
24#endif
25
26
27////////// BasicUsageEnvironment //////////
28
29BasicUsageEnvironment0::BasicUsageEnvironment0(TaskScheduler& taskScheduler)
30 : UsageEnvironment(taskScheduler),
31 fBufferMaxSize(RESULT_MSG_BUFFER_MAX) {
32 reset();
33}
34
35BasicUsageEnvironment0::~BasicUsageEnvironment0() {
36}
37
38void BasicUsageEnvironment0::reset() {
39 fCurBufferSize = 0;
40 fResultMsgBuffer[fCurBufferSize] = '\0';
41}
42
43
44// Implementation of virtual functions:
45
46char const* BasicUsageEnvironment0::getResultMsg() const {
47 return fResultMsgBuffer;
48}
49
50void BasicUsageEnvironment0::setResultMsg(MsgString msg) {
51 reset();
52 appendToResultMsg(msg);
53}
54
55void BasicUsageEnvironment0::setResultMsg(MsgString msg1, MsgString msg2) {
56 setResultMsg(msg1);
57 appendToResultMsg(msg2);
58}
59
60void BasicUsageEnvironment0::setResultMsg(MsgString msg1, MsgString msg2,
61 MsgString msg3) {
62 setResultMsg(msg1, msg2);
63 appendToResultMsg(msg3);
64}
65
66void BasicUsageEnvironment0::setResultErrMsg(MsgString msg, int err) {
67 setResultMsg(msg);
68
69 if (err == 0) err = getErrno();
70#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN32_WCE)
71#ifndef _UNICODE
72 char errMsg[RESULT_MSG_BUFFER_MAX] = "\0";
73 if (0 != FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, errMsg, sizeof(errMsg)/sizeof(errMsg[0]), NULL)) {
74 // Remove all trailing '\r', '\n' and '.'
75 for (char* p = errMsg + strlen(errMsg); p != errMsg && (*p == '\r' || *p == '\n' || *p == '.' || *p == '\0'); --p) {
76 *p = '\0';
77 }
78 } else
79 snprintf(errMsg, sizeof(errMsg)/sizeof(errMsg[0]), "error %d", err);
80 appendToResultMsg(errMsg);
81#endif
82#else
83 appendToResultMsg(strerror(err));
84#endif
85}
86
87
88
89
90void BasicUsageEnvironment0::appendToResultMsg(MsgString msg) {
91 char* curPtr = &fResultMsgBuffer[fCurBufferSize];
92 unsigned spaceAvailable = fBufferMaxSize - fCurBufferSize;
93 unsigned msgLength = strlen(msg);
94
95 // Copy only enough of "msg" as will fit:
96 if (msgLength > spaceAvailable-1) {
97 msgLength = spaceAvailable-1;
98 }
99
100 memmove(curPtr, (char*)msg, msgLength);
101 fCurBufferSize += msgLength;
102 fResultMsgBuffer[fCurBufferSize] = '\0';
103}
104
105void BasicUsageEnvironment0::reportBackgroundError() {
106 fputs(getResultMsg(), stderr);
107}
108
109