diff dwt/dwthelper/ResourceBundle.d @ 132:e22f9b443521

Working on ControlExample, png loading makes problems
author Frank Benoit <benoit@tionex.de>
date Mon, 21 Jan 2008 15:39:59 +0100
parents 0f12f6bb9739
children 2640d1618770
line wrap: on
line diff
--- a/dwt/dwthelper/ResourceBundle.d	Mon Jan 21 15:39:26 2008 +0100
+++ b/dwt/dwthelper/ResourceBundle.d	Mon Jan 21 15:39:59 2008 +0100
@@ -1,21 +1,119 @@
-/**
+/**
  * Authors: Frank Benoit <keinfarbton@googlemail.com>
  */
 module dwt.dwthelper.ResourceBundle;
 
+import tango.text.Util;
+import tango.io.Stdout;
+
+
 class ResourceBundle {
 
-    public this( char[] name ){
+    char[][ char[] ] map;
+
+    public this( char[] data ){
+        char[] line;
+        int dataIndex;
+
+        //tango.io.Stdout.Stdout.formatln( "properties put ..." );
+        void readLine(){
+            line.length = 0;
+            char i = data[ dataIndex++ ];
+            while( dataIndex < data.length && i !is '\n' && i !is '\r' ){
+                line ~= i;
+                i = data[ dataIndex++ ];
+            }
+        }
+
+        //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ );
+        bool linecontinue = false;
+        bool iskeypart = true;
+        char[] key;
+        char[] value;
+nextline:
+        while( dataIndex < data.length ){
+            //tango.io.Stdout.Stdout.formatln( "properties put {} startline", __LINE__ );
+            readLine();
+            line = tango.text.Util.trim( line );
+            if( line.length is 0 ){
+                //tango.io.Stdout.Stdout.formatln( "properties put {} was 0 length", __LINE__ );
+                continue;
+            }
+            if( line[0] == '#' ){
+                //tango.io.Stdout.Stdout.formatln( "properties put {} was comment", __LINE__ );
+                continue;
+            }
+            int pos = 0;
+            bool esc = false;
+            if( !linecontinue ){
+                iskeypart = true;
+                key = null;
+                value = null;
+            }
+            else{
+                linecontinue = false;
+            }
+            while( pos < line.length ){
+                char c = line[pos];
+                if( esc ){
+                    esc = false;
+                    switch( c ){
+                    case 't': c = '\t'; break;
+                    case 'n': c = '\n'; break;
+                    case '\\': c = '\\'; break;
+                    default:  c = '?'; break;
+                    }
+                }
+                else{
+                    if( c == '\\' ){
+                        if( pos == line.length -1 ){
+                            linecontinue = true;
+                            goto nextline;
+                        }
+                        esc = true;
+                        pos++;
+                        continue;
+                    }
+                    else if( iskeypart && c == '=' ){
+                        pos++;
+                        iskeypart = false;
+                        continue;
+                    }
+                }
+                pos++;
+                if( iskeypart ){
+                    key ~= c;
+                }
+                else{
+                    value ~= c;
+                }
+            }
+            if( iskeypart ){
+                tango.io.Stdout.Stdout.formatln( "dwt.dwthelper.ResourceBundle ctor cannot find '='." );
+                continue;
+            }
+            key = tango.text.Util.trim( key );
+            value = tango.text.Util.trim(value);
+            //tango.io.Stdout.Stdout.formatln( "properties put {}=>{}", key, value );
+
+            map[ key.dup ] = value.dup;
+            //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ );
+        }
     }
 
     public char[] getString( char[] key ){
+        if( auto v = key in map ){
+            return (*v).dup;
+        }
         return key;
     }
 
     public static ResourceBundle getBundle( char[] name ){
-        return new ResourceBundle( name );
+        return new ResourceBundle( null );
     }
-
+    public static ResourceBundle getBundleFromData( char[] data ){
+        return new ResourceBundle( data );
+    }
 }