diff --git a/src/xml/repr-io.cpp b/src/xml/repr-io.cpp
index 884bb3360847a0a54eb91d0302fc4cd3f2ecf446..10f6101b656df3c10039b7c6707291921824a8a7 100644 (file)
--- a/src/xml/repr-io.cpp
+++ b/src/xml/repr-io.cpp
public:
XmlSource()
: filename(0),
public:
XmlSource()
: filename(0),
+ encoding(0),
fp(0),
fp(0),
- first(false),
+ firstFewLen(0),
dummy("x"),
instr(0),
gzin(0)
dummy("x"),
instr(0),
gzin(0)
virtual ~XmlSource()
{
close();
virtual ~XmlSource()
{
close();
+ if ( encoding ) {
+ g_free(encoding);
+ encoding = 0;
+ }
}
}
- void setFile( char const * filename );
-
- static int readCb( void * context, char * buffer, int len);
- static int closeCb(void * context);
+ int setFile( char const * filename );
+ static int readCb( void * context, char * buffer, int len );
+ static int closeCb( void * context );
+ char const* getEncoding() const { return encoding; }
int read( char * buffer, int len );
int close();
private:
const char* filename;
int read( char * buffer, int len );
int close();
private:
const char* filename;
+ char* encoding;
FILE* fp;
FILE* fp;
- bool first;
+ unsigned char firstFew[4];
+ int firstFewLen;
Inkscape::URI dummy;
Inkscape::IO::UriInputStream* instr;
Inkscape::IO::GzipInputStream* gzin;
};
Inkscape::URI dummy;
Inkscape::IO::UriInputStream* instr;
Inkscape::IO::GzipInputStream* gzin;
};
-void XmlSource::setFile(char const *filename)
+int XmlSource::setFile(char const *filename)
{
{
+ int retVal = -1;
+
this->filename = filename;
this->filename = filename;
+
fp = Inkscape::IO::fopen_utf8name(filename, "r");
fp = Inkscape::IO::fopen_utf8name(filename, "r");
- if (fp == NULL) {
- throw std::runtime_error("Could not open file for reading");
+ if ( fp ) {
+ // First peek in the file to see what it is
+ memset( firstFew, 0, sizeof(firstFew) );
+
+ size_t some = fread( firstFew, 1, 4, fp );
+ if ( fp ) {
+ // first check for compression
+ if ( (some >= 2) && (firstFew[0] == 0x1f) && (firstFew[1] == 0x8b) ) {
+ //g_message(" the file being read is gzip'd. extract it");
+ fclose(fp);
+ fp = 0;
+ fp = Inkscape::IO::fopen_utf8name(filename, "r");
+ instr = new Inkscape::IO::UriInputStream(fp, dummy);
+ gzin = new Inkscape::IO::GzipInputStream(*instr);
+
+ memset( firstFew, 0, sizeof(firstFew) );
+ some = 0;
+ int single = 0;
+ while ( some < 4 && single >= 0 )
+ {
+ single = gzin->get();
+ if ( single >= 0 ) {
+ firstFew[some++] = 0x0ff & single;
+ } else {
+ break;
+ }
+ }
+ }
+
+ int encSkip = 0;
+ if ( (some >= 2) &&(firstFew[0] == 0xfe) && (firstFew[1] == 0xff) ) {
+ encoding = g_strdup("UTF-16BE");
+ encSkip = 2;
+ } else if ( (some >= 2) && (firstFew[0] == 0xff) && (firstFew[1] == 0xfe) ) {
+ encoding = g_strdup("UTF-16LE");
+ encSkip = 2;
+ } else if ( (some >= 3) && (firstFew[0] == 0xef) && (firstFew[1] == 0xbb) && (firstFew[2] == 0xbf) ) {
+ encoding = g_strdup("UTF-8");
+ encSkip = 3;
+ }
+
+ if ( encSkip ) {
+ memmove( firstFew, firstFew + encSkip, (some - encSkip) );
+ some -= encSkip;
+ }
+
+ firstFewLen = some;
+ retVal = 0; // no error
+ }
}
}
- first = true;
+
+ return retVal;
}
}
int retVal = 0;
size_t got = 0;
int retVal = 0;
size_t got = 0;
- if ( first ) {
- first = false;
- char tmp[] = {0,0};
- size_t some = fread( tmp, 1, 2, fp );
-
- if ( (some >= 2) && (tmp[0] == 0x1f) && ((unsigned char)(tmp[1]) == 0x8b) ) {
- //g_message(" the file being read is gzip'd. extract it");
- fclose(fp);
- fp = 0;
- fp = Inkscape::IO::fopen_utf8name(filename, "r");
- instr = new Inkscape::IO::UriInputStream(fp, dummy);
- gzin = new Inkscape::IO::GzipInputStream(*instr);
- int single = 0;
- while ( (int)got < len && single >= 0 )
- {
- single = gzin->get();
- if ( single >= 0 ) {
- buffer[got++] = 0x0ff & single;
- } else {
- break;
- }
- }
- //g_message(" extracted %d bytes this pass", got );
- } else {
- memcpy( buffer, tmp, some );
- got = some;
+ if ( firstFewLen > 0 ) {
+ int some = (len < firstFewLen) ? len : firstFewLen;
+ memcpy( buffer, firstFew, some );
+ if ( len < firstFewLen ) {
+ memmove( firstFew, firstFew + some, (firstFewLen - some) );
}
}
+ firstFewLen -= some;
+ got = some;
} else if ( gzin ) {
int single = 0;
while ( (int)got < len && single >= 0 )
} else if ( gzin ) {
int single = 0;
while ( (int)got < len && single >= 0 )
break;
}
}
break;
}
}
- //g_message(" extracted %d bytes this pass b", got );
} else {
got = fread( buffer, 1, len, fp );
}
if ( feof(fp) ) {
retVal = got;
} else {
got = fread( buffer, 1, len, fp );
}
if ( feof(fp) ) {
retVal = got;
- }
- else if ( ferror(fp) ) {
+ } else if ( ferror(fp) ) {
retVal = -1;
retVal = -1;
- }
- else {
+ } else {
retVal = got;
}
retVal = got;
}
Inkscape::IO::dump_fopen_call( filename, "N" );
Inkscape::IO::dump_fopen_call( filename, "N" );
- XmlSource src;
- try
- {
- src.setFile(filename);
- }
- catch (...)
- {
- return NULL;
- }
-
- xmlDocPtr doubleDoc = xmlReadIO( XmlSource::readCb,
- XmlSource::closeCb,
- &src,
- localFilename,
- NULL, //"UTF-8",
- XML_PARSE_NOENT );
-
-
#ifdef HAVE_LIBWMF
if (strlen (localFilename) > 4) {
if ( (strcmp (localFilename + strlen (localFilename) - 4,".wmf") == 0)
#ifdef HAVE_LIBWMF
if (strlen (localFilename) > 4) {
if ( (strcmp (localFilename + strlen (localFilename) - 4,".wmf") == 0)
- || (strcmp (localFilename + strlen (localFilename) - 4,".WMF") == 0))
+ || (strcmp (localFilename + strlen (localFilename) - 4,".WMF") == 0)) {
doc = sp_wmf_convert (localFilename);
doc = sp_wmf_convert (localFilename);
- else
- doc = xmlParseFile (localFilename);
- }
- else {
- doc = xmlParseFile (localFilename);
+ }
}
}
-#else /* !HAVE_LIBWMF */
- //doc = xmlParseFile (localFilename);
-#endif /* !HAVE_LIBWMF */
+#endif // !HAVE_LIBWMF
- //rdoc = sp_repr_do_read (doc, default_ns);
- rdoc = sp_repr_do_read (doubleDoc, default_ns);
- if (doc)
- xmlFreeDoc (doc);
+ if ( !doc ) {
+ XmlSource src;
- if ( localFilename != NULL )
- g_free (localFilename);
+ if ( (src.setFile(filename) == 0) ) {
+ doc = xmlReadIO( XmlSource::readCb,
+ XmlSource::closeCb,
+ &src,
+ localFilename,
+ src.getEncoding(),
+ XML_PARSE_NOENT );
+ }
+ }
- if ( doubleDoc != NULL )
- {
- xmlFreeDoc( doubleDoc );
+ rdoc = sp_repr_do_read( doc, default_ns );
+ if ( doc ) {
+ xmlFreeDoc( doc );
+ }
+
+ if ( localFilename ) {
+ g_free( localFilename );
}
return rdoc;
}
return rdoc;
namespace {
typedef std::map<Glib::QueryQuark, gchar const *, Inkscape::compare_quark_ids> LocalNameMap;
namespace {
typedef std::map<Glib::QueryQuark, gchar const *, Inkscape::compare_quark_ids> LocalNameMap;
-typedef std::map<Glib::QueryQuark, Inkscape::Util::shared_ptr<char>, Inkscape::compare_quark_ids> NSMap;
+typedef std::map<Glib::QueryQuark, Inkscape::Util::ptr_shared<char>, Inkscape::compare_quark_ids> NSMap;
gchar const *qname_local_name(Glib::QueryQuark qname) {
static LocalNameMap local_name_map;
gchar const *qname_local_name(Glib::QueryQuark qname) {
static LocalNameMap local_name_map;
}
void add_ns_map_entry(NSMap &ns_map, Glib::QueryQuark prefix) {
}
void add_ns_map_entry(NSMap &ns_map, Glib::QueryQuark prefix) {
- using Inkscape::Util::shared_ptr;
+ using Inkscape::Util::ptr_shared;
using Inkscape::Util::share_unsafe;
static const Glib::QueryQuark xml_prefix("xml");
using Inkscape::Util::share_unsafe;
static const Glib::QueryQuark xml_prefix("xml");
g_warning("No namespace known for normalized prefix %s", g_quark_to_string(prefix));
}
} else {
g_warning("No namespace known for normalized prefix %s", g_quark_to_string(prefix));
}
} else {
- ns_map.insert(NSMap::value_type(prefix, shared_ptr<char>()));
+ ns_map.insert(NSMap::value_type(prefix, ptr_shared<char>()));
}
}
}
}
}
}
void
sp_repr_write_stream_root_element (Node *repr, Writer &out, gboolean add_whitespace, gchar const *default_ns)
{
void
sp_repr_write_stream_root_element (Node *repr, Writer &out, gboolean add_whitespace, gchar const *default_ns)
{
- using Inkscape::Util::shared_ptr;
+ using Inkscape::Util::ptr_shared;
g_assert(repr != NULL);
Glib::QueryQuark xml_prefix=g_quark_from_static_string("xml");
g_assert(repr != NULL);
Glib::QueryQuark xml_prefix=g_quark_from_static_string("xml");
@@ -664,7 +685,7 @@ sp_repr_write_stream_root_element (Node *repr, Writer &out, gboolean add_whitesp
for ( NSMap::iterator iter=ns_map.begin() ; iter != ns_map.end() ; ++iter )
{
Glib::QueryQuark prefix=(*iter).first;
for ( NSMap::iterator iter=ns_map.begin() ; iter != ns_map.end() ; ++iter )
{
Glib::QueryQuark prefix=(*iter).first;
- shared_ptr<char> ns_uri=(*iter).second;
+ ptr_shared<char> ns_uri=(*iter).second;
if (prefix.id()) {
if ( prefix != xml_prefix ) {
if (prefix.id()) {
if ( prefix != xml_prefix ) {