summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Albright <eric_albright@sil.org>2007-11-17 04:51:32 +0000
committerEric Albright <eric_albright@sil.org>2007-11-17 04:51:32 +0000
commit891ccef5a6c4f71abd99c05d437c50aae3035ea1 (patch)
tree7204d7275923110c669399c73dc18cd503ab7c47
parent88c9db762ce5ca6820f9a05623058a361779b6b2 (diff)
downloadenchant-891ccef5a6c4f71abd99c05d437c50aae3035ea1.tar.gz
Ignore UTF-8 BOM at beginning of pwl
git-svn-id: svn+ssh://svn.abisource.com/svnroot/enchant/trunk@22310 bcba8976-2d24-0410-9c9c-aab3bd5fdfd6
-rw-r--r--msvc/unittest-enchant.vcproj8
-rw-r--r--src/pwl.c18
-rw-r--r--unittests/EnchantDictionaryTestFixture.h13
-rw-r--r--unittests/pwl/enchant_pwl_tests.cpp47
4 files changed, 81 insertions, 5 deletions
diff --git a/msvc/unittest-enchant.vcproj b/msvc/unittest-enchant.vcproj
index 04f4707..c9e669f 100644
--- a/msvc/unittest-enchant.vcproj
+++ b/msvc/unittest-enchant.vcproj
@@ -304,6 +304,14 @@
>
</File>
</Filter>
+ <Filter
+ Name="pwl"
+ >
+ <File
+ RelativePath="..\unittests\pwl\enchant_pwl_tests.cpp"
+ >
+ </File>
+ </Filter>
</Filter>
<Filter
Name="Header Files"
diff --git a/src/pwl.c b/src/pwl.c
index 6385743..ab88e2b 100644
--- a/src/pwl.c
+++ b/src/pwl.c
@@ -252,17 +252,27 @@ EnchantPWL* enchant_pwl_init_with_file(const char * file)
f = fdopen(fd, "r");
if (f)
{
- char line[BUFSIZ];
-
+ char buffer[BUFSIZ];
+ char* line;
+ size_t line_number = 1;
+
enchant_lock_file (f);
- while (NULL != (fgets (line, sizeof (line), f)))
+ while (NULL != (fgets (buffer, sizeof (buffer), f)))
{
- size_t l = strlen(line)-1;
+ const gunichar BOM = 0xfeff;
+ size_t l;
+
+ line = buffer;
+ if(line_number == 1 && BOM == g_utf8_get_char(line))
+ line = g_utf8_next_char(line);
+
+ l = strlen(line)-1;
if (line[l]=='\n')
line[l] = '\0';
enchant_pwl_add_to_trie(pwl, line, strlen(line), FALSE);
+ ++line_number;
}
enchant_unlock_file (f);
diff --git a/unittests/EnchantDictionaryTestFixture.h b/unittests/EnchantDictionaryTestFixture.h
index e380cbc..19d2dd9 100644
--- a/unittests/EnchantDictionaryTestFixture.h
+++ b/unittests/EnchantDictionaryTestFixture.h
@@ -160,7 +160,7 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture
bool PersonalWordListFileHasContents()
{
bool hasContents = false;
- int fd = open(GetPersonalDictFileName().c_str(), O_RDONLY);
+ int fd = g_open(GetPersonalDictFileName().c_str(), O_RDONLY, S_IREAD );
if(fd == -1){
return false;
}
@@ -198,6 +198,17 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture
bool IsWordInDictionary(const std::string& word){
return enchant_dict_check(_dict, word.c_str(), word.size())==0;
}
+
+ void ExternalAddWordToDictionary(const std::string& word)
+ {
+ FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "at");
+ if(f)
+ {
+ fputs(word.c_str(), f);
+ fputc('\n', f);
+ fclose(f);
+ }
+ }
};
#if defined(_MSC_VER)
#pragma warning(pop)
diff --git a/unittests/pwl/enchant_pwl_tests.cpp b/unittests/pwl/enchant_pwl_tests.cpp
new file mode 100644
index 0000000..48e59f9
--- /dev/null
+++ b/unittests/pwl/enchant_pwl_tests.cpp
@@ -0,0 +1,47 @@
+/* Copyright (c) 2007 Eric Scott Albright
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#define NOMINMAX //don't want windows to collide with std::min
+#include <UnitTest++.h>
+#include <enchant.h>
+#include <enchant-provider.h>
+
+#include "../EnchantDictionaryTestFixture.h"
+
+struct EnchantPwl_TestFixture : EnchantDictionaryTestFixture
+{
+ //Setup
+ EnchantPwl_TestFixture(const std::string& languageTag="qaa"):
+ EnchantDictionaryTestFixture(EmptyDictionary_ProviderConfiguration, languageTag)
+ { }
+};
+
+/////////////////////////////////////////////////////////////////////////////////////////////////
+// DictionaryBeginsWithBOM
+TEST_FIXTURE(EnchantPwl_TestFixture,
+ IsWordInDictionary_DictionaryBeginsWithBOM_Successful)
+{
+ std::string Utf8Bom ("\xef\xbb\xbf");
+ ExternalAddWordToDictionary(Utf8Bom + "cat");
+
+ ReloadTestDictionary();
+
+ CHECK( IsWordInDictionary("cat") );
+}