Mercurial > projects > dwt-mac
diff dwt/dwthelper/ResourceBundle.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | c74ba20de292 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwt/dwthelper/ResourceBundle.d Sat Aug 09 17:00:02 2008 +0200 @@ -0,0 +1,144 @@ +/** + * Authors: Frank Benoit <keinfarbton@googlemail.com> + */ +module dwt.dwthelper.ResourceBundle; + +import tango.text.Util; +import tango.io.Stdout; + +import dwt.DWT; +import dwt.dwthelper.utils; +import dwt.dwthelper.utils : IOException; +import tango.io.File; + +class ResourceBundle { + + 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 = dwt.dwthelper.utils.trim(line); + if( line.length is 0 ){ + //tango.io.Stdout.Stdout.formatln( "properties put {} was 0 length", __LINE__ ); + continue; + } + if( line[0] is '#' ){ + //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; + case '\"': c = '\"'; break; + //case ':' : c = ':' ; break; + default: break; + } + } + else{ + if( c is '\\' ){ + if( pos is line.length -1 ){ + linecontinue = true; + goto nextline; + } + esc = true; + pos++; + continue; + } + else if( iskeypart && c is '=' ){ + pos++; + iskeypart = false; + continue; + } + } + pos++; + if( iskeypart ){ + key ~= c; + } + else{ + value ~= c; + } + } + if( iskeypart ){ + // Cannot find '=' in record + DWT.error( __FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT ); + continue; + } + key = dwt.dwthelper.utils.trim(key); + value = dwt.dwthelper.utils.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 bool hasString( char[] key ){ + return ( key in map ) !is null; + } + + public char[] getString( char[] key ){ + if( auto v = key in map ){ + return (*v).dup; + } + throw new MissingResourceException( "key not found", this.classinfo.name, key ); + } + + public char[][] getKeys(){ + return map.keys; + } + + public static ResourceBundle getBundle( ImportData data ){ + return new ResourceBundle( cast(char[]) data.data ); + } + public static ResourceBundle getBundle( char[] name ){ + try{ + scope f = new File(name); + return new ResourceBundle( cast(char[]) f.read() ); + } + catch( IOException e){ + e.msg ~= " file:" ~ name; + throw e; + } + } + public static ResourceBundle getBundleFromData( char[] data ){ + return new ResourceBundle( data ); + } +} + +