Replace fopen() in JSON ride file reader

.. since it fails to open files with foreign characters in the
path name. We now read via QFile into a buffer.
This commit is contained in:
Mark Liversedge
2013-03-12 08:32:53 +00:00
parent 7e424c88ad
commit b56e610744
2 changed files with 23 additions and 12 deletions

View File

@@ -79,3 +79,8 @@
#if !defined(FLEX_SCANNER) || (YY_FLEX_SUBMINOR_VERSION < 9)
int JsonRideFilelex_destroy(void) { return 0; }
#endif
void JsonRideFile_setString(QString p)
{
JsonRideFile_scan_string(p.toLatin1().data());
}

View File

@@ -52,11 +52,10 @@ static QMap <QString, QString> JsonOverrides;
// Lex scanner
extern int JsonRideFilelex(); // the lexer aka yylex()
extern void JsonRideFile_setString(QString);
extern int JsonRideFilelex_destroy(void); // the cleaner for lexer
// yacc parser
extern void JsonRideFilerestart (FILE *input_file); // the lexer file restart aka yyrestart()
extern FILE *JsonRideFilein; // used by the lexer aka yyin
extern char *JsonRideFiletext; // set by the lexer aka yytext
void JsonRideFileerror(const char *error) // used by parser aka yyerror()
{ JsonRideFileerrors << error; }
@@ -243,15 +242,25 @@ static int jsonFileReaderRegistered =
RideFile *
JsonFileReader::openRideFile(QFile &file, QStringList &errors, QList<RideFile*>*) const
{
// jsonRide is local and static, used in the parser
// JsonRideFilein is the FILE * used by the lexer
JsonRideFilein = fopen(file.fileName().toLatin1(), "r");
if (JsonRideFilein == NULL) {
errors << "unable to open file (locale?)" + file.fileName();
return NULL; // failed to open the file ?
// Read the entire file into a QString -- we avoid using fopen since it
// doesn't handle foreign characters well. Instead we use QFile and parse
// from a QString
QString contents;
if (file.exists() && file.open(QFile::ReadOnly | QFile::Text)) {
// read in the whole thing
QTextStream in(&file);
contents = in.readAll();
file.close();
} else {
errors << "unable to open file" + file.fileName();
return NULL;
}
// inform the parser/lexer we have a new file
JsonRideFilerestart(JsonRideFilein);
JsonRideFile_setString(contents);
// setup
JsonRide = new RideFile;
@@ -265,9 +274,6 @@ JsonFileReader::openRideFile(QFile &file, QStringList &errors, QList<RideFile*>*
// parse it
JsonRideFileparse();
// release the file handle
fclose(JsonRideFilein);
// clean up
JsonRideFilelex_destroy();