| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This file contains functions providing UTF-8 support. |
| 4 |
* |
| 5 |
* Copyright (C) 2008-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 6 |
* |
| 7 |
* This file is part of Gregorio. |
| 8 |
* |
| 9 |
* Gregorio is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* Gregorio is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with Gregorio. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> /* for strlen */ |
| 26 |
#include <stdlib.h> |
| 27 |
#include "struct.h" |
| 28 |
#include "unicode.h" |
| 29 |
#include "messages.h" |
| 30 |
#include "support.h" |
| 31 |
|
| 32 |
/* |
| 33 |
* |
| 34 |
* This file contains some functions to handle UTF-8 correctly. We chose to |
| 35 |
* embed them instead of relying on locale or glib, for simplification: with |
| 36 |
* these functions we don't rely on anything locale-dependant (such as |
| 37 |
* wchar_t). The reason is that Windows XP (...) is not UTF-8 and thus would |
| 38 |
* need the glib in oder to run, which make dependencies much bigger. |
| 39 |
* |
| 40 |
*/ |
| 41 |
|
| 42 |
/* an utf8 version of mbstowcs */ |
| 43 |
|
| 44 |
/* expects a buffer of size n + 1 */ |
| 45 |
static bool gregorio_mbstowcs(grewchar *dest, const char *src, size_t n) |
| 46 |
{ |
| 47 |
bool success = true; |
| 48 |
unsigned char bytes_to_come; |
| 49 |
grewchar result = 0; |
| 50 |
unsigned char c; |
| 51 |
size_t res = 0; /* number of bytes we've done so far */ |
| 52 |
gregorio_not_null(src, gregorio_mbstowcs, return false); |
| 53 |
gregorio_not_null(dest, gregorio_mbstowcs, return false); |
| 54 |
while (success && *src && res < n) { |
| 55 |
c = (unsigned char) (*src); |
| 56 |
if (c < 128) { /* 0100xxxx */ |
| 57 |
/* one-byte symbol */ |
| 58 |
bytes_to_come = 0; |
| 59 |
result = c; |
| 60 |
} else if (c >= 240) { /* 1111xxxx */ |
| 61 |
/* start of a four-byte symbol */ |
| 62 |
/* printf("%d\n", c); */ |
| 63 |
bytes_to_come = 3; |
| 64 |
result = (result << 3) | (c & 7); |
| 65 |
} else if (c >= 224) { /* 1110xxxx */ |
| 66 |
/* start of a three-byte symbol */ |
| 67 |
/* printf("%d\n", c); */ |
| 68 |
bytes_to_come = 2; |
| 69 |
result = (result << 4) | (c & 15); |
| 70 |
} else if (c >= 192) { /* 1100xxxx */ |
| 71 |
/* start of a two-byte symbol */ |
| 72 |
/* printf("%d\n", c); */ |
| 73 |
bytes_to_come = 1; |
| 74 |
result = (result << 5) | (c & 31); |
| 75 |
} else { |
| 76 |
/* printf("%s %d %d\n", src, res, c); */ |
| 77 |
gregorio_message(_("malformed UTF-8 sequence1"), |
| 78 |
"gregorio_mbstowcs", VERBOSITY_ERROR, 0); |
| 79 |
success = false; |
| 80 |
break; |
| 81 |
} |
| 82 |
while (bytes_to_come > 0) { |
| 83 |
bytes_to_come--; |
| 84 |
src++; |
| 85 |
c = (unsigned char) (*src); |
| 86 |
if (c < 192 && c >= 128) /* 1000xxxx */ |
| 87 |
{ |
| 88 |
result = (result << 6) | (c & 63); |
| 89 |
} else { |
| 90 |
gregorio_message(_("malformed UTF-8 sequence2"), |
| 91 |
"gregorio_mbstowcs", VERBOSITY_ERROR, 0); |
| 92 |
success = false; |
| 93 |
break; |
| 94 |
} |
| 95 |
} |
| 96 |
dest[res] = result; |
| 97 |
|
| 98 |
res++; |
| 99 |
result = 0; |
| 100 |
src++; |
| 101 |
} |
| 102 |
dest[res] = 0; |
| 103 |
return success; |
| 104 |
} |
| 105 |
|
| 106 |
/* the value returned by this function must be freed! */ |
| 107 |
grewchar *gregorio_build_grewchar_string_from_buf(const char *const buf) |
| 108 |
{ |
| 109 |
size_t len; |
| 110 |
grewchar *gwstring; |
| 111 |
/* this doesn't currently happen, but it's safer to keep this check */ |
| 112 |
if (buf == NULL) { |
| 113 |
return NULL; /* LCOV_EXCL_LINE */ |
| 114 |
} |
| 115 |
len = strlen(buf); /* to get the length of the syllable in ASCII */ |
| 116 |
gwstring = (grewchar *) gregorio_malloc((len + 1) * sizeof(grewchar)); |
| 117 |
gregorio_mbstowcs(gwstring, buf, len); /* converting into grewchar */ |
| 118 |
/* no need to check the return code; if it failed, the error state would |
| 119 |
* already be in place */ |
| 120 |
return gwstring; |
| 121 |
} |
| 122 |
|
| 123 |
/* the function to build a gregorio_character list from a buffer. */ |
| 124 |
|
| 125 |
gregorio_character *gregorio_build_char_list_from_buf(const char *const buf) |
| 126 |
{ |
| 127 |
int i = 0; |
| 128 |
grewchar *gwstring; |
| 129 |
gregorio_character *current_character = NULL; |
| 130 |
/* this doesn't currently happen, but it's safer to keep this check */ |
| 131 |
if (buf == NULL) { |
| 132 |
return NULL; /* LCOV_EXCL_LINE */ |
| 133 |
} |
| 134 |
gwstring = gregorio_build_grewchar_string_from_buf(buf); |
| 135 |
/* we add the corresponding characters in the list of gregorio_characters */ |
| 136 |
while (gwstring[i]) { |
| 137 |
gregorio_add_character(¤t_character, gwstring[i]); |
| 138 |
i++; |
| 139 |
} |
| 140 |
free(gwstring); |
| 141 |
gregorio_go_to_first_character_c(¤t_character); |
| 142 |
return current_character; |
| 143 |
} |
| 144 |
|
| 145 |
void gregorio_print_unichar(FILE *f, const grewchar to_print) |
| 146 |
{ |
| 147 |
if (to_print <= 0x7F) { |
| 148 |
fprintf(f, "%c", (unsigned char) to_print); |
| 149 |
return; |
| 150 |
} |
| 151 |
if (to_print >= 0x80 && to_print <= 0x7FF) { |
| 152 |
fprintf(f, "%c%c", 0xC0 | (to_print >> 6), 0x80 | (to_print & 0x3F)); |
| 153 |
return; |
| 154 |
} |
| 155 |
if ((to_print >= 0x800 && to_print <= 0xD7FF) || |
| 156 |
(to_print >= 0xE000 && to_print <= 0xFFFF)) { |
| 157 |
fprintf(f, "%c%c%c", 0xE0 | (to_print >> 12), |
| 158 |
0x80 | ((to_print >> 6) & 0x3F), 0x80 | (to_print & 0x3F)); |
| 159 |
return; |
| 160 |
} |
| 161 |
if (to_print >= 0x10000 && to_print <= 0x10FFFF) { |
| 162 |
fprintf(f, "%c%c%c%c", 0xF0 | (to_print >> 18), |
| 163 |
0x80 | ((to_print >> 12) & 0x3F), |
| 164 |
0x80 | ((to_print >> 6) & 0x3F), 0x80 | (to_print & 0x3F)); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void gregorio_print_unistring(FILE *f, const grewchar *first_char) |
| 169 |
{ |
| 170 |
while (*first_char != 0) { |
| 171 |
gregorio_print_unichar(f, *first_char); |
| 172 |
first_char++; |
| 173 |
} |
| 174 |
} |