From f55ff50dd5bf050e1e0b25410b7aa912ea3842f3 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 29 Aug 2013 19:19:52 +0100 Subject: [PATCH] Skip the UTF-8 BOM of including files. For avoiding illegal token error when parsing include files which have the UTF-8 BOM. Closes #75. --- CHANGES.current | 4 ++++ Examples/test-suite/bom_utf8.i | 9 +++++++++ Examples/test-suite/common.mk | 3 ++- Source/Swig/include.c | 11 ++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 Examples/test-suite/bom_utf8.i diff --git a/CHANGES.current b/CHANGES.current index dfbe651f0..0a8d722ff 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release. Version 2.0.11 (in progress) ============================ +2013-08-29: wsfulton + Pull Git patch #75: Handle UTF-8 files with BOM at beginning of file. Was giving an + 'Illegal token' syntax error. + 2013-08-29: wsfulton [C#] Pull Git patch #77: Allow exporting std::map using non-default comparison function. diff --git a/Examples/test-suite/bom_utf8.i b/Examples/test-suite/bom_utf8.i new file mode 100644 index 000000000..010774937 --- /dev/null +++ b/Examples/test-suite/bom_utf8.i @@ -0,0 +1,9 @@ +%module bom_utf8 + +/* Test for UTF8 BOM at start of file */ +%inline %{ +struct NotALotHere { + int n; +}; +%} + diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 9a335b46e..b1e5fc3b8 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -552,7 +552,8 @@ C_TEST_CASES += \ typedef_struct \ typemap_subst \ union_parameter \ - unions + unions \ + utf8_bom # Multi-module C++ test cases . (Can be run individually using make testcase.multicpptest) diff --git a/Source/Swig/include.c b/Source/Swig/include.c index 13afb21ae..7e80172ba 100644 --- a/Source/Swig/include.c +++ b/Source/Swig/include.c @@ -163,7 +163,8 @@ static FILE *Swig_open_file(const_String_or_char_ptr name, int sysfile, int use_ String *filename; List *spath = 0; char *cname; - int i, ilen; + int i, ilen, nbytes; + char bom[3]; if (!directories) directories = NewList(); @@ -191,6 +192,14 @@ static FILE *Swig_open_file(const_String_or_char_ptr name, int sysfile, int use_ if (f) { Delete(lastpath); lastpath = filename; + + /* Skip the UTF-8 BOM if it's present */ + nbytes = fread(bom, 1, 3, f); + if (nbytes == 3 && bom[0] == (char)0xEF && bom[1] == (char)0xBB && bom[2] == (char)0xBF) { + /* skip */ + } else { + fseek(f, 0, SEEK_SET); + } } return f; }