1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2024 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <dave@mielke.cc>.
*/
#include "prologue.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "cmdline.h"
#include "log.h"
#include "file.h"
#include "ttb.h"
#include "ttb_internal.h"
#include "atb.h"
#include "atb_internal.h"
#include "ctb.h"
#include "ctb_internal.h"
BEGIN_OPTION_TABLE(programOptions)
END_OPTION_TABLE(programOptions)
typedef struct {
void *object;
const unsigned char *bytes;
size_t size;
} TableData;
typedef struct {
const char *extension;
int (*load) (const char *path, TableData *data);
void (*unload) (TableData *data);
} TableEntry;
static int
loadTextTable (const char *path, TableData *data) {
TextTable *table = compileTextTable(path);
if (!table) return 0;
data->object = table;
data->bytes = table->header.bytes;
data->size = table->size;
return 1;
}
static void
unloadTextTable (TableData *data) {
destroyTextTable(data->object);
}
static int
loadAttributesTable (const char *path, TableData *data) {
AttributesTable *table = compileAttributesTable(path);
if (!table) return 0;
data->object = table;
data->bytes = table->header.bytes;
data->size = table->size;
return 1;
}
static void
unloadAttributesTable (TableData *data) {
destroyAttributesTable(data->object);
}
static int
loadContractionTable (const char *path, TableData *data) {
ContractionTable *table = compileContractionTable(path);
if (!table) return 0;
data->object = table;
data->bytes = table->data.internal.header.bytes;
data->size = table->data.internal.size;
return 1;
}
static void
unloadContractionTable (TableData *data) {
destroyContractionTable(data->object);
}
static const TableEntry tableEntries[] = {
{
.extension = TEXT_TABLE_EXTENSION,
.load = loadTextTable,
.unload = unloadTextTable
}
,
{
.extension = ATTRIBUTES_TABLE_EXTENSION,
.load = loadAttributesTable,
.unload = unloadAttributesTable
}
,
{
.extension = CONTRACTION_TABLE_EXTENSION,
.load = loadContractionTable,
.unload = unloadContractionTable
}
,
{
.extension = NULL
}
};
static const TableEntry *
findTableEntry (const char *extension) {
const TableEntry *entry = tableEntries;
while (entry->extension) {
if (strcmp(entry->extension, extension) == 0) return entry;
entry += 1;
}
logMessage(LOG_ERR, "unrecognized file extension: %s", extension);
return NULL;
}
int
dumpBytes (FILE *stream, const unsigned char *bytes, size_t count) {
const unsigned char *byte = bytes;
const unsigned char *end = byte + count;
int first = 1;
int digits;
if (count) {
char buffer[0X10];
digits = snprintf(buffer, sizeof(buffer), "%X", (unsigned int)count-1);
} else {
digits = 1;
}
while (byte < end) {
while (!*byte && (byte < (end - 1))) byte += 1;
{
unsigned int counter = 0;
unsigned int maximum = 8;
if ((byte + maximum) != end) {
while (maximum > 1) {
if (byte[maximum-1]) break;
maximum -= 1;
}
}
while (byte < end) {
if (first) {
first = 0;
} else {
fprintf(stream, ",");
if (ferror(stream)) goto outputError;
if (!counter) {
fprintf(stream, "\n");
if (ferror(stream)) goto outputError;
}
}
if (!counter) {
fprintf(stream, "[0X%0*X] =", digits, (unsigned int)(byte-bytes));
if (ferror(stream)) goto outputError;
}
fprintf(stream, " 0X%02X", *byte++);
if (ferror(stdout)) goto outputError;
if (++counter == maximum) break;
}
}
}
if (!first) {
fprintf(stream, "\n");
if (ferror(stream)) goto outputError;
}
return 1;
outputError:
logMessage(LOG_ERR, "table write error: %s", strerror(errno));
return 0;
}
int
main (int argc, char *argv[]) {
ProgramExitStatus exitStatus;
char *path;
{
const CommandLineDescriptor descriptor = {
.options = &programOptions,
.applicationName = "tbl2hex",
.usage = {
.purpose = strtext("Write the hexadecimal array representation of a compiled table."),
.parameters = "table-file",
}
};
PROCESS_OPTIONS(descriptor, argc, argv);
}
if (argc == 0) {
logMessage(LOG_ERR, "missing table file.");
return PROG_EXIT_SYNTAX;
}
path = *argv++, argc--;
{
const char *extension = locatePathExtension(path);
if (extension) {
const TableEntry *entry = findTableEntry(extension);
if (entry) {
TableData data;
if (entry->load(path, &data)) {
if (dumpBytes(stdout, data.bytes, data.size)) {
exitStatus = PROG_EXIT_SUCCESS;
} else {
exitStatus = PROG_EXIT_FATAL;
}
entry->unload(&data);
} else {
exitStatus = PROG_EXIT_FATAL;
}
} else {
exitStatus = PROG_EXIT_SEMANTIC;
}
} else {
logMessage(LOG_ERR, "no file extension");
exitStatus = PROG_EXIT_SEMANTIC;
}
}
return exitStatus;
}
#include "ctb_internal.h"
const unsigned char *
getInternalContractionTableBytes (void) {
return NULL;
}
const ContractionTableTranslationMethods *
getContractionTableTranslationMethods_native (void) {
return NULL;
}
const ContractionTableTranslationMethods *
getContractionTableTranslationMethods_external (void) {
return NULL;
}
const ContractionTableTranslationMethods *
getContractionTableTranslationMethods_louis (void) {
return NULL;
}
|