| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 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 | |
| 29 | BasicUsageEnvironment0::BasicUsageEnvironment0(TaskScheduler& taskScheduler) |
| 30 | : UsageEnvironment(taskScheduler), |
| 31 | fBufferMaxSize(RESULT_MSG_BUFFER_MAX) { |
| 32 | reset(); |
| 33 | } |
| 34 | |
| 35 | BasicUsageEnvironment0::~BasicUsageEnvironment0() { |
| 36 | } |
| 37 | |
| 38 | void BasicUsageEnvironment0::reset() { |
| 39 | fCurBufferSize = 0; |
| 40 | fResultMsgBuffer[fCurBufferSize] = '\0'; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | // Implementation of virtual functions: |
| 45 | |
| 46 | char const* BasicUsageEnvironment0::getResultMsg() const { |
| 47 | return fResultMsgBuffer; |
| 48 | } |
| 49 | |
| 50 | void BasicUsageEnvironment0::setResultMsg(MsgString msg) { |
| 51 | reset(); |
| 52 | appendToResultMsg(msg); |
| 53 | } |
| 54 | |
| 55 | void BasicUsageEnvironment0::setResultMsg(MsgString msg1, MsgString msg2) { |
| 56 | setResultMsg(msg1); |
| 57 | appendToResultMsg(msg2); |
| 58 | } |
| 59 | |
| 60 | void BasicUsageEnvironment0::setResultMsg(MsgString msg1, MsgString msg2, |
| 61 | MsgString msg3) { |
| 62 | setResultMsg(msg1, msg2); |
| 63 | appendToResultMsg(msg3); |
| 64 | } |
| 65 | |
| 66 | void 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 | |
| 90 | void 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 | |
| 105 | void BasicUsageEnvironment0::reportBackgroundError() { |
| 106 | fputs(getResultMsg(), stderr); |
| 107 | } |
| 108 | |
| 109 | |