Skip to content

Commit

Permalink
Store emphClasses inside translation table
Browse files Browse the repository at this point in the history
so that getEmphClasses can be called for a table that was not the last
to be compiled.

Also move MAX_EMPH_CLASSES to louis.h so that it can be used from
check_yaml.c
  • Loading branch information
bertfrees committed Jan 5, 2016
1 parent 1e6265a commit b02b171
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 38 deletions.
50 changes: 25 additions & 25 deletions liblouis/compileTranslationTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,11 @@ typedef struct
}
CharsString;

#define MAX_EMPH_CLASSES 10 // {emph_1...emph_10} in typeform enum (liblouis.h)

static int errorCount;
static int warningCount;
static TranslationTableHeader *table;
static TranslationTableOffset tableSize;
static TranslationTableOffset tableUsed;
static const char * emphClasses[MAX_EMPH_CLASSES + 1];

/* for accessing emphasis classes declared in last compiled table from check_brl.c
* array must be at least (MAX_EMPH_CLASSES + 1) long
*/
void
getEmphClasses(const char ** array)
{
int i;
for (i = 0; emphClasses[i]; i++)
array[i] = strdup(emphClasses[i]);
array[i] = NULL;
}

typedef struct
{
Expand Down Expand Up @@ -4056,8 +4041,8 @@ compileRule (FileInfo * nested)
for (k = 0; k < emphClass.length; k++)
s[k] = (char)emphClass.chars[k];
s[k++] = '\0';
for (i = 0; emphClasses[i]; i++)
if (strcmp(s, emphClasses[i]) == 0)
for (i = 0; table->emphClasses[i]; i++)
if (strcmp(s, table->emphClasses[i]) == 0)
{
logMessage (LOG_WARN, "Duplicate emphasis class: %s", s);
warningCount++;
Expand Down Expand Up @@ -4109,8 +4094,8 @@ compileRule (FileInfo * nested)
}
break;
}
emphClasses[i] = s;
emphClasses[i+1] = NULL;
table->emphClasses[i] = s;
table->emphClasses[i+1] = NULL;
return 1;
}
else
Expand Down Expand Up @@ -4140,8 +4125,8 @@ compileRule (FileInfo * nested)
for (k = 0; k < emphClass.length; k++)
s[k] = (char)emphClass.chars[k];
s[k++] = '\0';
for (i = 0; emphClasses[i]; i++)
if (strcmp(s, emphClasses[i]) == 0)
for (i = 0; table->emphClasses[i]; i++)
if (strcmp(s, table->emphClasses[i]) == 0)
{
/* TODO: compileBrailleIndicator could be called directly here which would remove
the need for values CTO_SingleLetterItal to CTO_LenTransNotePhrase.
Expand Down Expand Up @@ -5776,9 +5761,7 @@ compileTranslationTable (const char *tableList)
allocateHeader (NULL);

/* Initialize emphClasses array */
for (i = 0; emphClasses[i]; i++)
free(emphClasses[i]);
emphClasses[0] = NULL;
table->emphClasses[0] = NULL;

/* Compile things that are necesary for the proper operation of
liblouis or liblouisxml or liblouisutdml */
Expand Down Expand Up @@ -5894,6 +5877,19 @@ getLastTableList ()
return scratchBuf;
}

/* for accessing emphasis classes declared in table from check_brl.c
* array must be at least (MAX_EMPH_CLASSES + 1) long
*/
void
getEmphClasses(const char* tableList, const char ** array)
{
int i = 0;
if (getTable(tableList))
for (; table->emphClasses[i]; i++)
array[i] = strdup(table->emphClasses[i]);
array[i] = NULL;
}

void *EXPORT_CALL
lou_getTable (const char *tableList)
{
Expand Down Expand Up @@ -6056,7 +6052,11 @@ lou_free ()
currentEntry = tableChain;
while (currentEntry)
{
free (currentEntry->table);
int i;
TranslationTableHeader *t = (TranslationTableHeader *)currentEntry->table;
for (i = 0; t->emphClasses[i]; i++)
free(t->emphClasses[i]);
free (t);
previousEntry = currentEntry;
currentEntry = currentEntry->next;
free (previousEntry);
Expand Down
3 changes: 3 additions & 0 deletions liblouis/louis.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ extern "C"

#define MAXSTRING 2048

#define MAX_EMPH_CLASSES 10 // {emph_1...emph_10} in typeforms enum (liblouis.h)

typedef unsigned int TranslationTableOffset;
#define OFFSETSIZE sizeof (TranslationTableOffset)

Expand Down Expand Up @@ -600,6 +602,7 @@ extern "C"
TranslationTableOffset numberSign;
TranslationTableOffset noContractSign;
widechar seqPatterns[SEQPATTERNSIZE];
char* emphClasses[MAX_EMPH_CLASSES + 1];
int seqPatternsCount;
/*Do not change the order of the following emphasis rule pointers!
*/
Expand Down
19 changes: 7 additions & 12 deletions tests/check_yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdio.h>
#include <assert.h>
#include <error.h>
#include "liblouis.h"
#include "louis.h"
#include "brl_checks.h"

#define EXIT_SKIPPED 77
Expand Down Expand Up @@ -225,7 +225,7 @@ read_typeform_string(yaml_parser_t *parser, formtype* typeform, typeforms kind,
}

formtype*
read_typeforms (yaml_parser_t *parser, int len) {
read_typeforms (yaml_parser_t *parser, char *tables_list, int len) {
yaml_event_t event;
formtype *typeform = calloc(len, sizeof(formtype));
int parse_error = 1;
Expand All @@ -248,9 +248,9 @@ read_typeforms (yaml_parser_t *parser, int len) {
read_typeform_string(parser, typeform, word_reset, len);
} else {
int i;
static const char* emph_classes [11];
static const char* emph_classes [MAX_EMPH_CLASSES + 1];
typeforms kind = plain_text;
getEmphClasses(emph_classes); // get declared emphasis classes
getEmphClasses(tables_list, emph_classes); // get declared emphasis classes
for (i = 0; emph_classes[i]; i++) {
if (strcmp(event.data.scalar.value, emph_classes[i]) == 0) {
yaml_event_delete(&event);
Expand Down Expand Up @@ -288,7 +288,7 @@ read_typeforms (yaml_parser_t *parser, int len) {
}

void
read_options (yaml_parser_t *parser, int len,
read_options (yaml_parser_t *parser, char *tables_list, int len,
int *xfail, translationModes *mode,
formtype **typeform, int **cursorPos) {
yaml_event_t event;
Expand All @@ -312,7 +312,7 @@ read_options (yaml_parser_t *parser, int len,
*mode = read_mode(parser);
} else if (!strcmp(option_name, "typeform")) {
yaml_event_delete(&event);
*typeform = read_typeforms(parser, len);
*typeform = read_typeforms(parser, tables_list, len);
} else if (!strcmp(option_name, "cursorPos")) {
yaml_event_delete(&event);
*cursorPos = read_cursorPos(parser, len);
Expand Down Expand Up @@ -368,7 +368,7 @@ read_test(yaml_parser_t *parser, char *tables_list, int direction, int hyphenati

if (event.type == YAML_MAPPING_START_EVENT) {
yaml_event_delete(&event);
read_options(parser, my_strlen_utf8_c(word), &xfail, &mode, &typeform, &cursorPos);
read_options(parser, tables_list, my_strlen_utf8_c(word), &xfail, &mode, &typeform, &cursorPos);

if (!yaml_parser_parse(parser, &event) ||
(event.type != YAML_SEQUENCE_END_EVENT))
Expand Down Expand Up @@ -424,11 +424,6 @@ read_tests(yaml_parser_t *parser, char *tables_list, int direction, int hyphenat

yaml_event_delete(&event);

// compile table a first time so that declared emphasis classes, which are
// required to parse the "typeform" options, are available during the first
// call to read_typeforms
lou_getTable(tables_list);

int done = 0;
while (!done) {
if (!yaml_parser_parse(parser, &event)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/emphclass.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ main (int argc, char **argv)
fprintf(stderr, "%s should be valid\n", table);
return 1;
}
getEmphClasses(emph_classes);
getEmphClasses(table, emph_classes);
result |= check_translation(table,
"foobar",
typeform("foo", "+++++"),
Expand Down

0 comments on commit b02b171

Please sign in to comment.