Coverage Report

Created: 2026-06-10 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/espeak-ng/src/libespeak-ng/error.c
Line
Count
Source
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 "config.h"
20
21
#include <errno.h>
22
#include <stdint.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include <espeak-ng/espeak_ng.h>
28
29
#include "error.h"
30
#include "common.h"           // for strncpy0
31
32
espeak_ng_STATUS
33
create_file_error_context(espeak_ng_ERROR_CONTEXT *context,
34
                          espeak_ng_STATUS status,
35
                          const char *filename)
36
0
{
37
0
  if (context) {
38
0
    if (*context) {
39
0
      free((*context)->name);
40
0
    } else {
41
0
      *context = malloc(sizeof(espeak_ng_ERROR_CONTEXT_));
42
0
      if (!*context)
43
0
        return ENOMEM;
44
0
    }
45
0
    (*context)->type = ERROR_CONTEXT_FILE;
46
0
    (*context)->name = strdup(filename);
47
0
    (*context)->version = 0;
48
0
    (*context)->expected_version = 0;
49
0
  }
50
0
  return status;
51
0
}
52
53
espeak_ng_STATUS
54
create_version_mismatch_error_context(espeak_ng_ERROR_CONTEXT *context,
55
                                      const char *path_home,
56
                                      int version,
57
                                      int expected_version)
58
0
{
59
0
  if (context) {
60
0
    if (*context) {
61
0
      free((*context)->name);
62
0
    } else {
63
0
      *context = malloc(sizeof(espeak_ng_ERROR_CONTEXT_));
64
0
      if (!*context)
65
0
        return ENOMEM;
66
0
    }
67
0
    (*context)->type = ERROR_CONTEXT_VERSION;
68
0
    (*context)->name = strdup(path_home);
69
0
    (*context)->version = version;
70
0
    (*context)->expected_version = expected_version;
71
0
  }
72
0
  return ENS_VERSION_MISMATCH;
73
0
}
74
75
#pragma GCC visibility push(default)
76
77
ESPEAK_NG_API void
78
espeak_ng_ClearErrorContext(espeak_ng_ERROR_CONTEXT *context)
79
0
{
80
0
  if (context && *context) {
81
0
    free((*context)->name);
82
0
    free(*context);
83
0
    *context = NULL;
84
0
  }
85
0
}
86
87
ESPEAK_NG_API void
88
espeak_ng_GetStatusCodeMessage(espeak_ng_STATUS status,
89
                               char *buffer,
90
                               size_t length)
91
0
{
92
0
  switch (status)
93
0
  {
94
0
  case ENS_COMPILE_ERROR:
95
0
    strncpy0(buffer, "Compile error", length);
96
0
    break;
97
0
  case ENS_VERSION_MISMATCH:
98
0
    strncpy0(buffer, "Wrong version of espeak-ng-data", length);
99
0
    break;
100
0
  case ENS_FIFO_BUFFER_FULL:
101
0
    strncpy0(buffer, "The FIFO buffer is full", length);
102
0
    break;
103
0
  case ENS_NOT_INITIALIZED:
104
0
    strncpy0(buffer, "The espeak-ng library has not been initialized", length);
105
0
    break;
106
0
  case ENS_AUDIO_ERROR:
107
0
    strncpy0(buffer, "Cannot initialize the audio device", length);
108
0
    break;
109
0
  case ENS_VOICE_NOT_FOUND:
110
0
    strncpy0(buffer, "The specified espeak-ng voice does not exist", length);
111
0
    break;
112
0
  case ENS_MBROLA_NOT_FOUND:
113
0
    strncpy0(buffer, "Could not load the mbrola.dll file", length);
114
0
    break;
115
0
  case ENS_MBROLA_VOICE_NOT_FOUND:
116
0
    strncpy0(buffer, "Could not load the specified mbrola voice file", length);
117
0
    break;
118
0
  case ENS_EVENT_BUFFER_FULL:
119
0
    strncpy0(buffer, "The event buffer is full", length);
120
0
    break;
121
0
  case ENS_NOT_SUPPORTED:
122
0
    strncpy0(buffer, "The requested functionality has not been built into espeak-ng", length);
123
0
    break;
124
0
  case ENS_UNSUPPORTED_PHON_FORMAT:
125
0
    strncpy0(buffer, "The phoneme file is not in a supported format", length);
126
0
    break;
127
0
  case ENS_NO_SPECT_FRAMES:
128
0
    strncpy0(buffer, "The spectral file does not contain any frame data", length);
129
0
    break;
130
0
  case ENS_EMPTY_PHONEME_MANIFEST:
131
0
    strncpy0(buffer, "The phoneme manifest file does not contain any phonemes", length);
132
0
    break;
133
0
  case ENS_UNKNOWN_PHONEME_FEATURE:
134
0
    strncpy0(buffer, "The phoneme feature is not recognised", length);
135
0
    break;
136
0
  case ENS_UNKNOWN_TEXT_ENCODING:
137
0
    strncpy0(buffer, "The text encoding is not supported", length);
138
0
    break;
139
0
  default:
140
0
    if ((status & ENS_GROUP_MASK) == ENS_GROUP_ERRNO)
141
0
      strerror_r(status, buffer, length);
142
0
    else
143
0
      snprintf(buffer, length, "Unspecified error 0x%x", status);
144
0
    break;
145
0
  }
146
0
}
147
148
ESPEAK_NG_API void
149
espeak_ng_PrintStatusCodeMessage(espeak_ng_STATUS status,
150
                                 FILE *out,
151
                                 espeak_ng_ERROR_CONTEXT context)
152
0
{
153
0
  char error[512];
154
0
  espeak_ng_GetStatusCodeMessage(status, error, sizeof(error));
155
0
  if (context) {
156
0
    switch (context->type)
157
0
    {
158
0
    case ERROR_CONTEXT_FILE:
159
0
      fprintf(out, "Error processing file '%s': %s.\n", context->name, error);
160
0
      break;
161
0
    case ERROR_CONTEXT_VERSION:
162
0
      fprintf(out, "Error: %s at '%s' (expected 0x%x, got 0x%x).\n",
163
0
              error, context->name, context->expected_version, context->version);
164
0
      break;
165
0
    }
166
0
  } else
167
0
    fprintf(out, "Error: %s.\n", error);
168
0
}
169
170
#pragma GCC visibility pop