From fae4b432e1dc646c2d88d125615e9aee686e0d23 Mon Sep 17 00:00:00 2001 From: Evgeniy Dushistov Date: Sun, 7 Jul 2013 17:04:51 +0000 Subject: [PATCH] - not requeire exact version number for stardict ifo file - ignore utf-8 BOM during magic string check --- src/lib/lib.cpp | 22 ++++++---------------- src/lib/lib.hpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/lib/lib.cpp b/src/lib/lib.cpp index c7d3b2f..e7e2b3a 100644 --- a/src/lib/lib.cpp +++ b/src/lib/lib.cpp @@ -43,18 +43,6 @@ namespace { return true; } - static inline guint32 get_uint32(const gchar *addr) - { - guint32 result; - memcpy(&result, addr, sizeof(guint32)); - return result; - } - - static inline void set_uint32(gchar *addr, guint32 val) - { - memcpy(addr, &val, sizeof(guint32)); - } - static inline gint stardict_strcmp(const gchar *s1, const gchar *s2) { gint a=g_ascii_strcasecmp(s1, s2); @@ -82,11 +70,14 @@ bool DictInfo::load_from_ifo_file(const std::string& ifofilename, if (!g_file_get_contents(ifofilename.c_str(), &buffer, nullptr, nullptr)) return false; -#define TREEDICT_MAGIC_DATA "StarDict's treedict ifo file\nversion=2.4.2\n" -#define DICT_MAGIC_DATA "StarDict's dict ifo file\nversion=2.4.2\n" + static const char TREEDICT_MAGIC_DATA[] = "StarDict's treedict ifo file"; + static const char DICT_MAGIC_DATA[] = "StarDict's dict ifo file"; const gchar *magic_data = istreedict ? TREEDICT_MAGIC_DATA : DICT_MAGIC_DATA; - if (!g_str_has_prefix(buffer, magic_data)) { + static const unsigned char utf8_bom[] = { 0xEF, 0xBB, 0xBF, '\0'}; + if (!g_str_has_prefix( + g_str_has_prefix(buffer, (const gchar *)(utf8_bom)) ? buffer + 3 : buffer, + magic_data)) { g_free(buffer); return false; } @@ -808,7 +799,6 @@ namespace { } } -//=================================================================== bool Dict::load(const std::string& ifofilename) { gulong idxfilesize; diff --git a/src/lib/lib.hpp b/src/lib/lib.hpp index 6ce4292..1f5fbea 100644 --- a/src/lib/lib.hpp +++ b/src/lib/lib.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -12,6 +13,19 @@ const int MAX_MATCH_ITEM_PER_LIB=100; const int MAX_FUZZY_DISTANCE= 3; // at most MAX_FUZZY_DISTANCE-1 differences allowed when find similar words +inline guint32 get_uint32(const gchar *addr) +{ + guint32 result; + memcpy(&result, addr, sizeof(guint32)); + return result; +} + +inline void set_uint32(gchar *addr, guint32 val) +{ + memcpy(addr, &val, sizeof(guint32)); +} + + struct cacheItem { guint32 offset; gchar *data;