X-Git-Url: http://git.chise.org/gitweb/?a=blobdiff_plain;f=MCharTable.cs;h=1d9adc73b76be76c474cbd2fee5484efb6ec6ee3;hb=d03a372c57dcd3011c23ffe6f433441d5638a3ed;hp=35685d717497f480c26ed0d88f1abf143cb85b96;hpb=9db8c225f7679dc803ac309aa6d8f24bbb626972;p=m17n%2Fm17n-lib-cs.git diff --git a/MCharTable.cs b/MCharTable.cs index 35685d7..1d9adc7 100644 --- a/MCharTable.cs +++ b/MCharTable.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.IO; using M17N; using M17N.Core; @@ -78,8 +79,11 @@ namespace M17N.Core public override string ToString () { - return String.Format ("[U+{0:X}..U+{1:X} {2}]", from, to, - value == null ? "null" : value); + return ((from == to) + ? String.Format ("[U+{0:X} {1}]", from, + value == null ? "null" : value) + : String.Format ("[U+{0:X}..U+{1:X} {2}]", from, to, + value == null ? "null" : value)); } } @@ -174,7 +178,17 @@ namespace M17N.Core public object this[int from, int to] { - set { set_range (from, to, value); } + set { + MCharRange.CheckChar (from); + + if (from == to) + Set (from, value); + else + { + MCharRange.CheckChar (to); + set_range (from, to, value); + } + } } private void set_range (int from, int to, object value) @@ -263,12 +277,9 @@ namespace M17N.Core MCharTable table; private MCharRange range; - public MCharTableEnum (MCharTable table) - { - this.table = table; - } - - public void Dispose () {} + public MCharTableEnum (MCharTable table) { + this.table = table; + } public bool MoveNext () { @@ -280,20 +291,117 @@ namespace M17N.Core return range.Next (); } - public void Reset () - { - range = null; - } + public void Reset () { range = null; } - public MCharRange Current + public MCharRange Current { get { return range; } } + + object IEnumerator.Current { get { return Current; } } + + public void Dispose () {} + } + } + + public class MCharProp : MCharTable + { + private static Dictionary char_prop + = new Dictionary (); + + public static void Define (MSymbol prop, MDatabase mdb) { - get { return range; } + char_prop[prop] = mdb; } - object IEnumerator.Current + public MCharProp (MSymbol prop) { - get { return Current; } + MDatabase mdb; + + if (! char_prop.TryGetValue (prop, out mdb)) + throw new Exception ("Undefined character property: " + prop); + mdb.Load (this); } + } + + public partial class MDatabase : IComparable + { + private bool read_range (MStreamReader mst, out int from, out int to) + { + if (! mst.ReadInteger (out from)) + { + to = from; + return false; + } + to = mst.Read (); + if (to < 0) + return false; + if (to != '-') + { + to = from; + return true; + } + return mst.ReadInteger (out to); + } + + private MCharTable load_char_table (MCharTable table) + { + MSymbol type = tag[1]; + + using (FileStream stream = FileInfo.OpenRead ()) + { + MStreamReader mst = new MStreamReader (stream, ';', true); + int c, from, to; + + while ((c = mst.Peek ()) >= 0) + { + if (c != '#' + && read_range (mst, out from, out to) + && mst.SkipSpace (out c)) + { + object value = null; + + if (type == MSymbol.integer) + { + int i; + if (mst.ReadInteger (out i)) + value = i; + } + else if (type == MSymbol.symbol) + { + MSymbol sym; + if (mst.ReadSymbol (out sym, -1)) + value = sym; + } + else if (type == MSymbol.mtext) + { + MText mt; + if (mst.ReadMText (out mt)) + value = mt; + } + else if (type == MSymbol.plist) + { + value = new MPlist (mst); + } + else if (type == MSymbol.mstring) + { + string str; + if (mst.ReadString (out str)) + value = str; + } + if (value != null) + table[from, to] = value; + } + mst.ForwardLine (); + } + } + return table; + } + + public object Load (MCharTable table) + { + if (loader != null || Info.Format != Mchar_table) + throw new ArgumentException ("Not a database of CharTable type"); + if (! update_status ()) + throw new Exception ("Database invalid"); + return load_char_table (table); } } }