| 1 |
/* Error handling APIs. |
| 2 |
* |
| 3 |
* Copyright (C) 2016 Reece H. Dunn |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, see: <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "espeak__config.h" |
| 20 |
#include "melder.h" |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include "espeak_ng.h" |
| 29 |
|
| 30 |
#include "error.h" |
| 31 |
#include "common.h" // for strncpy0 |
| 32 |
|
| 33 |
espeak_ng_STATUS |
| 34 |
create_file_error_context(espeak_ng_ERROR_CONTEXT *context, |
| 35 |
espeak_ng_STATUS status, |
| 36 |
const char *filename) |
| 37 |
{ |
| 38 |
if (context) { |
| 39 |
if (*context) { |
| 40 |
free((*context)->name); |
| 41 |
} else { |
| 42 |
*context = (espeak_ng_ERROR_CONTEXT) malloc(sizeof(espeak_ng_ERROR_CONTEXT_)); |
| 43 |
if (!*context) |
| 44 |
return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 45 |
} |
| 46 |
(*context)->type = ERROR_CONTEXT_FILE; |
| 47 |
(*context)->name = strdup(filename); |
| 48 |
(*context)->version = 0; |
| 49 |
(*context)->expected_version = 0; |
| 50 |
} |
| 51 |
return status; |
| 52 |
} |
| 53 |
|
| 54 |
espeak_ng_STATUS |
| 55 |
create_version_mismatch_error_context(espeak_ng_ERROR_CONTEXT *context, |
| 56 |
const char *path_home, |
| 57 |
int version, |
| 58 |
int expected_version) |
| 59 |
{ |
| 60 |
if (context) { |
| 61 |
if (*context) { |
| 62 |
free((*context)->name); |
| 63 |
} else { |
| 64 |
*context = (espeak_ng_ERROR_CONTEXT) malloc(sizeof(espeak_ng_ERROR_CONTEXT_)); |
| 65 |
if (!*context) |
| 66 |
return static_cast<espeak_ng_STATUS> (ENOMEM); |
| 67 |
} |
| 68 |
(*context)->type = ERROR_CONTEXT_VERSION; |
| 69 |
(*context)->name = strdup(path_home); |
| 70 |
(*context)->version = version; |
| 71 |
(*context)->expected_version = expected_version; |
| 72 |
} |
| 73 |
return ENS_VERSION_MISMATCH; |
| 74 |
} |
| 75 |
|
| 76 |
#pragma GCC visibility push(default) |
| 77 |
|
| 78 |
ESPEAK_NG_API void |
| 79 |
espeak_ng_ClearErrorContext(espeak_ng_ERROR_CONTEXT *context) |
| 80 |
{ |
| 81 |
if (context && *context) { |
| 82 |
free((*context)->name); |
| 83 |
free(*context); |
| 84 |
*context = NULL; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
ESPEAK_NG_API void |
| 89 |
espeak_ng_GetStatusCodeMessage(espeak_ng_STATUS status, |
| 90 |
char *buffer, |
| 91 |
size_t length) |
| 92 |
{ |
| 93 |
switch (status) |
| 94 |
{ |
| 95 |
case ENS_COMPILE_ERROR: |
| 96 |
strncpy0(buffer, "Compile error", length); |
| 97 |
break; |
| 98 |
case ENS_VERSION_MISMATCH: |
| 99 |
strncpy0(buffer, "Wrong version of espeak-ng-data", length); |
| 100 |
break; |
| 101 |
case ENS_FIFO_BUFFER_FULL: |
| 102 |
strncpy0(buffer, "The FIFO buffer is full", length); |
| 103 |
break; |
| 104 |
case ENS_NOT_INITIALIZED: |
| 105 |
strncpy0(buffer, "The espeak-ng library has not been initialized", length); |
| 106 |
break; |
| 107 |
case ENS_AUDIO_ERROR: |
| 108 |
strncpy0(buffer, "Cannot initialize the audio device", length); |
| 109 |
break; |
| 110 |
case ENS_VOICE_NOT_FOUND: |
| 111 |
strncpy0(buffer, "The specified espeak-ng voice does not exist", length); |
| 112 |
break; |
| 113 |
case ENS_MBROLA_NOT_FOUND: |
| 114 |
strncpy0(buffer, "Could not load the mbrola.dll file", length); |
| 115 |
break; |
| 116 |
case ENS_MBROLA_VOICE_NOT_FOUND: |
| 117 |
strncpy0(buffer, "Could not load the specified mbrola voice file", length); |
| 118 |
break; |
| 119 |
case ENS_EVENT_BUFFER_FULL: |
| 120 |
strncpy0(buffer, "The event buffer is full", length); |
| 121 |
break; |
| 122 |
case ENS_NOT_SUPPORTED: |
| 123 |
strncpy0(buffer, "The requested functionality has not been built into espeak-ng", length); |
| 124 |
break; |
| 125 |
case ENS_UNSUPPORTED_PHON_FORMAT: |
| 126 |
strncpy0(buffer, "The phoneme file is not in a supported format", length); |
| 127 |
break; |
| 128 |
case ENS_NO_SPECT_FRAMES: |
| 129 |
strncpy0(buffer, "The spectral file does not contain any frame data", length); |
| 130 |
break; |
| 131 |
case ENS_EMPTY_PHONEME_MANIFEST: |
| 132 |
strncpy0(buffer, "The phoneme manifest file does not contain any phonemes", length); |
| 133 |
break; |
| 134 |
case ENS_UNKNOWN_PHONEME_FEATURE: |
| 135 |
strncpy0(buffer, "The phoneme feature is not recognised", length); |
| 136 |
break; |
| 137 |
case ENS_UNKNOWN_TEXT_ENCODING: |
| 138 |
strncpy0(buffer, "The text encoding is not supported", length); |
| 139 |
break; |
| 140 |
default: |
| 141 |
if ((status & ENS_GROUP_MASK) == ENS_GROUP_ERRNO) |
| 142 |
#ifdef _WIN32 |
| 143 |
strerror_s(buffer, length, status); |
| 144 |
#else |
| 145 |
strerror_r(status, buffer, length); |
| 146 |
#endif |
| 147 |
else |
| 148 |
snprintf(buffer, length, "Unspecified error 0x%x", status); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
ESPEAK_NG_API void |
| 154 |
espeak_ng_PrintStatusCodeMessage(espeak_ng_STATUS status, |
| 155 |
FILE *out, |
| 156 |
espeak_ng_ERROR_CONTEXT context) |
| 157 |
{ |
| 158 |
char error[512]; |
| 159 |
espeak_ng_GetStatusCodeMessage(status, error, sizeof(error)); |
| 160 |
if (context) { |
| 161 |
switch (context->type) |
| 162 |
{ |
| 163 |
case ERROR_CONTEXT_FILE: |
| 164 |
Melder_throw (U"Error processing file\"", Melder_peek8to32_u (context->name), |
| 165 |
U"\":", Melder_peek8to32_u (error) |
| 166 |
); |
| 167 |
break; |
| 168 |
case ERROR_CONTEXT_VERSION: |
| 169 |
Melder_throw (U"eSpeak error: ", Melder_peek8to32_u (error), U" at \"", |
| 170 |
Melder_peek8to32_u (context->name), U"\" (expected ", context->expected_version, |
| 171 |
U", got ", context->version |
| 172 |
); |
| 173 |
break; |
| 174 |
} |
| 175 |
} else |
| 176 |
Melder_throw (U"eSpeak error: ", Melder_peek8to32_u (error)); |
| 177 |
} |
| 178 |
|
| 179 |
#pragma GCC visibility pop |