Skip to content

Commit

Permalink
Change brltty-ctb to use processInputFiles(). (dm)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveMielke committed Dec 28, 2015
1 parent 8de766e commit 60ca9ca
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 117 deletions.
17 changes: 14 additions & 3 deletions Headers/datafile.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,23 @@ typedef struct DataFileStruct DataFile;
#define DATA_OPERANDS_PROCESSOR(name) int name (DataFile *file, void *data)
typedef DATA_OPERANDS_PROCESSOR(DataOperandsProcessor);

extern int processDataFile (const char *name, DataOperandsProcessor *processLine, void *data);
typedef enum {
DFO_NO_COMMENTS = 0X01
} DataFileOptions;

typedef struct {
DataOperandsProcessor *processOperands;
void *data;
unsigned char options;
} DataFileParameters;

extern int processDataFile (const char *name, const DataFileParameters *parameters);
extern void reportDataError (DataFile *file, char *format, ...) PRINTF(2, 3);

extern int processDataStream (
DataFile *includer,
FILE *stream, const char *name,
DataOperandsProcessor *processLine, void *data
const DataFileParameters *parameters
);

extern int compareKeyword (const wchar_t *keyword, const wchar_t *characters, size_t count);
Expand All @@ -64,7 +74,8 @@ typedef struct {
} DataOperand;

extern int getDataOperand (DataFile *file, DataOperand *operand, const char *description);
extern int getDataText (DataFile *file, DataOperand *text, const char *description);
extern int getTextOperand (DataFile *file, DataOperand *text, const char *description);
extern void getTextRemaining (DataFile *file, DataOperand *text);

typedef struct {
unsigned char length;
Expand Down
3 changes: 1 addition & 2 deletions Headers/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ extern void resetOptions (const OptionsDescriptor *descriptor);
typedef struct {
void (*beginStream) (const char *name, void *data);
void (*endStream) (int incomplete, void *data);
DataOperandsProcessor *processLine;
void *data;
DataFileParameters dataFileParameters;
} InputFilesProcessingParameters;

extern ProgramExitStatus processInputFiles (
Expand Down
9 changes: 7 additions & 2 deletions Programs/atb_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static DATA_OPERANDS_PROCESSOR(processDotOperands) {
return 1;
}

static DATA_OPERANDS_PROCESSOR(processAttributesTableLine) {
static DATA_OPERANDS_PROCESSOR(processAttributesTableOperands) {
BEGIN_DATA_DIRECTIVE_TABLE
DATA_NESTING_DIRECTIVES,
{.name=WS_C("dot"), .processor=processDotOperands},
Expand All @@ -171,7 +171,12 @@ compileAttributesTable (const char *name) {

if ((atd.area = newDataArea())) {
if (allocateDataItem(atd.area, NULL, sizeof(AttributesTableHeader), __alignof__(AttributesTableHeader))) {
if (processDataFile(name, processAttributesTableLine, &atd)) {
const DataFileParameters parameters = {
.processOperands = processAttributesTableOperands,
.data = &atd
};

if (processDataFile(name, &parameters)) {
if (makeAttributesToDots(&atd)) {
if ((table = malloc(sizeof(*table)))) {
table->header.fields = getAttributesTableHeader(&atd);
Expand Down
78 changes: 29 additions & 49 deletions Programs/brltty-ctb.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ static DATA_OPERANDS_PROCESSOR(processContractsOperands) {
return 0;
}

static DATA_OPERANDS_PROCESSOR(processVerificationLine) {
static DATA_OPERANDS_PROCESSOR(processVerificationOperands) {
BEGIN_DATA_DIRECTIVE_TABLE
{.name=WS_C("contracts"), .processor=processContractsOperands},
END_DATA_DIRECTIVE_TABLE
Expand All @@ -407,42 +407,23 @@ static DATA_OPERANDS_PROCESSOR(processVerificationLine) {
static ProgramExitStatus
processVerificationTable (void) {
if (setTableDataVariables(VERIFICATION_TABLE_EXTENSION, VERIFICATION_SUBTABLE_EXTENSION)) {
if (processDataStream(NULL, verificationTableStream, verificationTablePath, processVerificationLine, NULL)) {
const DataFileParameters parameters = {
.processOperands = processVerificationOperands,
.data = NULL
};

if (processDataStream(NULL, verificationTableStream, verificationTablePath, &parameters)) {
return PROG_EXIT_SUCCESS;
}
}

return PROG_EXIT_FATAL;
}

static int
processInputLine (char *line, void *data) {
const char *string = line;
size_t length = strlen(string);
const char *byte = string;

size_t count = length + 1;
wchar_t characters[count];
wchar_t *character = characters;

convertUtf8ToWchars(&byte, &character, count);
length = character - characters;

return processInputCharacters(characters, length, data);
}

static ProgramExitStatus
processInputStream (FILE *stream) {
LineProcessingData lpd = {
.exitStatus = PROG_EXIT_SUCCESS
};
ProgramExitStatus exitStatus = processLines(stream, processInputLine, &lpd)? lpd.exitStatus: PROG_EXIT_FATAL;

if (exitStatus == PROG_EXIT_SUCCESS)
if (!(flushCharacters('\n', &lpd) && flushOutputStream(&lpd)))
exitStatus = lpd.exitStatus;

return exitStatus;
static DATA_OPERANDS_PROCESSOR(processInputLine) {
DataOperand line;
getTextRemaining(file, &line);
return processInputCharacters(line.characters, line.length, data);
}

int
Expand Down Expand Up @@ -521,27 +502,26 @@ main (int argc, char *argv[]) {
}

if (exitStatus == PROG_EXIT_SUCCESS) {
if (argc) {
do {
char *path = *argv;
if (strcmp(path, standardStreamArgument) == 0) {
exitStatus = processInputStream(stdin);
} else {
FILE *stream = fopen(path, "r");
if (stream) {
exitStatus = processInputStream(stream);
fclose(stream);
} else {
logMessage(LOG_ERR, "cannot open input file: %s: %s",
path, strerror(errno));
exitStatus = PROG_EXIT_FATAL;
}
}
} while ((exitStatus == PROG_EXIT_SUCCESS) && (++argv, --argc));
} else if (verificationTableStream) {
if (verificationTableStream && !argc) {
exitStatus = processVerificationTable();
} else {
exitStatus = processInputStream(stdin);
LineProcessingData lpd = {
.exitStatus = PROG_EXIT_SUCCESS
};

const InputFilesProcessingParameters parameters = {
.dataFileParameters = {
.options = DFO_NO_COMMENTS,
.processOperands = processInputLine,
.data = &lpd
}
};

if ((exitStatus = processInputFiles(argv, argc, &parameters)) == PROG_EXIT_SUCCESS) {
if (!(flushCharacters('\n', &lpd) && flushOutputStream(&lpd))) {
exitStatus = lpd.exitStatus;
}
}
}

if (textTable) destroyTextTable(textTable);
Expand Down
9 changes: 6 additions & 3 deletions Programs/brltty-tune.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static
DATA_OPERANDS_PROCESSOR(processTuneOperands) {
DataOperand line;

if (getDataText(file, &line, NULL)) {
if (getTextOperand(file, &line, NULL)) {
DataString text;

if (parseDataString(file, &text, line.characters, line.length, 0)) {
Expand Down Expand Up @@ -170,8 +170,11 @@ main (int argc, char *argv[]) {
const InputFilesProcessingParameters parameters = {
.beginStream = beginTuneStream,
.endStream = endTuneStream,
.processLine = processTuneLine,
.data = tb

.dataFileParameters = {
.processOperands = processTuneLine,
.data = tb
}
};

exitStatus = processInputFiles(argv, argc, &parameters);
Expand Down
9 changes: 7 additions & 2 deletions Programs/ctb_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static DATA_OPERANDS_PROCESSOR(processContractionTableDirective) {
}
}

static DATA_OPERANDS_PROCESSOR(processContractionTableLine) {
static DATA_OPERANDS_PROCESSOR(processContractionTableOperands) {
BEGIN_DATA_DIRECTIVE_TABLE
DATA_NESTING_DIRECTIVES,
{.name=NULL, .processor=processContractionTableDirective},
Expand Down Expand Up @@ -633,7 +633,12 @@ compileContractionTable (const char *fileName) {
if ((ctd.area = newDataArea())) {
if (allocateDataItem(ctd.area, NULL, sizeof(ContractionTableHeader), __alignof__(ContractionTableHeader))) {
if (allocateCharacterClasses(&ctd)) {
if (processDataFile(fileName, processContractionTableLine, &ctd)) {
const DataFileParameters parameters = {
.processOperands = processContractionTableOperands,
.data = &ctd
};

if (processDataFile(fileName, &parameters)) {
if (saveCharacterTable(&ctd)) {
if ((table = malloc(sizeof(*table)))) {
initializeCommonFields(table);
Expand Down
79 changes: 43 additions & 36 deletions Programs/datafile.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@
#include "brl_dots.h"

struct DataFileStruct {
const char *name;
DataFile *includer;
const char *const name;
const DataFileParameters *const parameters;
DataFile *const includer;
int line;

struct {
dev_t device;
ino_t file;
} identity;

DataOperandsProcessor *processLine;
void *data;

Queue *conditions;
VariableNestingLevel *variables;

Expand Down Expand Up @@ -268,26 +266,17 @@ ungetDataCharacters (DataFile *file, unsigned int count) {
return 1;
}

int
getDataOperand (DataFile *file, DataOperand *operand, const char *description) {
if (!findDataOperand(file, description)) return 0;

do {
file->end += 1;
} while (file->end[0] && !iswspace(file->end[0]));

operand->characters = file->start;
operand->length = file->end - file->start;
return 1;
void
getTextRemaining (DataFile *file, DataOperand *text) {
file->end = file->start + wcslen(file->start);
text->characters = file->start;
text->length = file->end - file->start;
}

int
getDataText (DataFile *file, DataOperand *text, const char *description) {
getTextOperand (DataFile *file, DataOperand *text, const char *description) {
if (!findDataOperand(file, description)) return 0;
file->end = file->start + wcslen(file->start);

text->characters = file->start;
text->length = file->end - file->start;
getTextRemaining(file, text);

while (text->length) {
unsigned int newLength = text->length - 1;
Expand All @@ -298,6 +287,19 @@ getDataText (DataFile *file, DataOperand *text, const char *description) {
return 1;
}

int
getDataOperand (DataFile *file, DataOperand *operand, const char *description) {
if (!findDataOperand(file, description)) return 0;

do {
file->end += 1;
} while (file->end[0] && !iswspace(file->end[0]));

operand->characters = file->start;
operand->length = file->end - file->start;
return 1;
}

int
parseDataString (DataFile *file, DataString *string, const wchar_t *characters, int length, int noUnicode) {
int index = 0;
Expand Down Expand Up @@ -802,7 +804,7 @@ pushDataCondition (
.length = name->length
};

condition->isIncluding = testCondition(file, &identifier, file->data);
condition->isIncluding = testCondition(file, &identifier, file->parameters->data);
if (negateCondition) condition->isIncluding = !condition->isIncluding;
}

Expand Down Expand Up @@ -946,21 +948,28 @@ processDirectiveOperand (DataFile *file, DataDirectives *directives, const char
}

static int
processDataOperands (DataFile *file, const wchar_t *line) {
processDataOperands (DataFile *file) {
return file->parameters->processOperands(file, file->parameters->data);
}

static int
processDataCharacters (DataFile *file, const wchar_t *line) {
file->end = file->start = line;

if (!findDataOperand(file, NULL)) return 1; /*blank line */
if (file->start[0] == WC_C('#')) return 1;
return file->processLine(file, file->data);
if (!(file->parameters->options & DFO_NO_COMMENTS)) {
if (!findDataOperand(file, NULL)) return 1;
if (file->start[0] == WC_C('#')) return 1;
}

return processDataOperands(file);
}

static int
processConditionSubdirective (DataFile *file, Element *element) {
int identifier = getElementIdentifier(element);

if (findDataOperand(file, NULL)) {
int result = file->processLine(file, file->data);

int result = processDataOperands(file);
removeDataCondition(file, element, identifier);
return result;
}
Expand Down Expand Up @@ -1204,7 +1213,7 @@ includeDataFile (DataFile *file, const wchar_t *name, int length) {
(int)suffixLength, suffixAddress);

if ((stream = openIncludedDataFile(file, path, "r", 0))) {
if (processDataStream(file, stream, path, file->processLine, file->data)) ok = 1;
if (processDataStream(file, stream, path, file->parameters)) ok = 1;
fclose(stream);
}
}
Expand Down Expand Up @@ -1244,25 +1253,23 @@ processDataLine (char *line, void *dataAddress) {
return 1;
}

return processDataOperands(file, characters);
return processDataCharacters(file, characters);
}

int
processDataStream (
DataFile *includer,
FILE *stream, const char *name,
DataOperandsProcessor *processLine, void *data
const DataFileParameters *parameters
) {
logMessage(LOG_DEBUG, "including data file: %s", name);
int ok = 0;

DataFile file = {
.name = name,
.parameters = parameters,
.includer = includer,
.line = 0,

.processLine = processLine,
.data = data
};

{
Expand Down Expand Up @@ -1297,12 +1304,12 @@ processDataStream (
}

int
processDataFile (const char *name, DataOperandsProcessor *processLine, void *data) {
processDataFile (const char *name, const DataFileParameters *parameters) {
int ok = 0;
FILE *stream;

if ((stream = openDataFile(name, "r", 0))) {
if (processDataStream(NULL, stream, name, processLine, data)) ok = 1;
if (processDataStream(NULL, stream, name, parameters)) ok = 1;
fclose(stream);
}

Expand Down
Loading

0 comments on commit 60ca9ca

Please sign in to comment.