File indexing completed on 2024-04-28 16:08:34

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include "uiexception.h"
0022 
0023 #include <string.h>
0024 #include <stdarg.h>
0025 #include <stdio.h>
0026 
0027 void UiException::setStringParam(const char* s) {
0028     size_t length = strlen(s);
0029     char* buffer =  new char[length + 1];
0030     strncpy(buffer, s, length + 1);
0031     stringParam = buffer;
0032 }
0033 
0034 
0035 UiException::UiException(Warning w) : err(UiException::IsWarning),
0036         warn(w), stringParam(0), out(0) {
0037 }
0038 
0039 UiException::UiException(Warning w, const char* param)
0040         : err(UiException::IsWarning), warn(w), stringParam(0), out(0) {
0041     setStringParam(param);
0042 }
0043 
0044 UiException::UiException(Error e) : err(e), warn(UiException::IsError),
0045         stringParam(0), out(0) {
0046 }
0047 
0048 UiException::UiException(Error e, const char* param)
0049         : err(e), warn(UiException::IsError), stringParam(0), out(0) {
0050     setStringParam(param);
0051 }
0052 
0053 UiException::~UiException() throw() {
0054     delete[] out;
0055     delete[] stringParam;
0056 }
0057 
0058 UiException::Warning UiException::warning() const {
0059     return warn;
0060 }
0061 
0062 UiException::Error UiException::error() const {
0063     return err;
0064 }
0065 
0066 const char* formatError(const char* format, ...) {
0067   va_list args;
0068   va_start(args, format);
0069   int n = vsnprintf (0, 0, format, args);
0070   va_end(args);
0071   char* p = new char[n];
0072   va_start(args, format);
0073   vsnprintf(p, n, format, args);
0074   va_end(args);
0075   return p;
0076 }
0077 
0078 const char* UiException::what() const throw () {
0079     if (out)
0080         return out;
0081     switch (err) {
0082     case IsWarning:
0083         switch (warn) {
0084         case couldNotOpenFile:
0085             out = formatError("Could not open file %s", stringParam);
0086             return out;
0087         case invalidAudioFormat:
0088             return "Did not understand the format of that audio file";
0089         case unsupportedImageType:
0090             return "That type of audio file is not supported";
0091         case failedToCopyFilesToWorkspace:
0092             out = formatError("Failed to copy the following files to the workspace: %s", stringParam);
0093             return out;
0094         case failedToInitializeAudioDriver:
0095             return "Failed To Initialize Audio Driver";
0096         case failedToWriteToPreferencesFile:
0097             out = formatError("Preferences file could not be written to: %s", stringParam);
0098             return out;
0099         default:
0100             break;
0101         }
0102         break;
0103     case failedToGetExclusiveLock:
0104         return "Failed to get an exclusive lock on command.log. Perhaps Stopmotion is already running.";
0105     case preferencesFileUnreadable:
0106         out = formatError("Preferences file cannot be read: %s", stringParam);
0107         return out;
0108     case preferencesFileMalformed:
0109         out = formatError("Preferences file is malformed: %s", stringParam);
0110         return out;
0111     case ArbitraryError:
0112         return stringParam;
0113     default:
0114         break;
0115     }
0116     return "Internal error: Threw a type of exception that was not handled.";
0117 }
0118 
0119 const char* UiException::string() const {
0120     return stringParam;
0121 }