gtk: Fix Builder xml definition loading

Gtk.Builder crashes when AddFromString(string) is called with a string
that contains a BOM. Hence, if the buffer contains a BOM, just skip it.

This fixes the crash with the Gtk# 3 project template in MonoDevelop
(robpvn/MonoDevelopGtkSharp3Template).
This commit is contained in:
Antonius Riha 2014-09-07 19:08:12 +02:00 committed by Bertrand Lorentz
parent a281a85f9d
commit b75e7c82b6

View File

@ -26,8 +26,10 @@
namespace Gtk {
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
public partial class Builder {
@ -159,13 +161,7 @@ namespace Gtk {
if (s == null)
throw new ArgumentNullException ("s");
int size = (int) s.Length;
byte[] buffer = new byte[size];
s.Read (buffer, 0, size);
s.Close ();
AddFromString(System.Text.Encoding.UTF8.GetString (buffer));
AddFromStream (s);
TranslationDomain = translation_domain;
}
@ -191,13 +187,7 @@ namespace Gtk {
throw new ArgumentException ("Cannot get resource file '" + resource_name + "'",
"resource_name");
int size = (int) s.Length;
byte[] buffer = new byte[size];
s.Read (buffer, 0, size);
s.Close ();
AddFromString(System.Text.Encoding.UTF8.GetString (buffer));
AddFromStream (s);
TranslationDomain = translation_domain;
}
@ -368,6 +358,22 @@ namespace Gtk {
}
void AddFromStream (Stream stream)
{
var size = (int)stream.Length;
var buffer = new byte[size];
stream.Read (buffer, 0, size);
stream.Close ();
// If buffer contains a BOM, omit it while reading, otherwise AddFromString(text) crashes
var offset = 0;
if (size >= 3 && buffer [0] == 0xEF && buffer [1] == 0xBB && buffer [2] == 0xBF) {
offset = 3;
}
var text = Encoding.UTF8.GetString (buffer, offset, size - offset);
AddFromString (text);
}
void BindFields (object target)
{