comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /**
2 * Authors: Frank Benoit <keinfarbton@googlemail.com>
3 */
4 module dwt.dwthelper.ResourceBundle;
5
6 import tango.text.Util;
7 import tango.io.Stdout;
8
9 import dwt.DWT;
10 import dwt.dwthelper.utils;
11 import dwt.dwthelper.utils : IOException;
12 import tango.io.File;
13
14 class ResourceBundle {
15
16 char[][ char[] ] map;
17
18 public this( char[] data ){
19 char[] line;
20 int dataIndex;
21
22 //tango.io.Stdout.Stdout.formatln( "properties put ..." );
23 void readLine(){
24 line.length = 0;
25 char i = data[ dataIndex++ ];
26 while( dataIndex < data.length && i !is '\n' && i !is '\r' ){
27 line ~= i;
28 i = data[ dataIndex++ ];
29 }
30 }
31
32 //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ );
33 bool linecontinue = false;
34 bool iskeypart = true;
35 char[] key;
36 char[] value;
37 nextline:
38 while( dataIndex < data.length ){
39 //tango.io.Stdout.Stdout.formatln( "properties put {} startline", __LINE__ );
40 readLine();
41 line = dwt.dwthelper.utils.trim(line);
42 if( line.length is 0 ){
43 //tango.io.Stdout.Stdout.formatln( "properties put {} was 0 length", __LINE__ );
44 continue;
45 }
46 if( line[0] is '#' ){
47 //tango.io.Stdout.Stdout.formatln( "properties put {} was comment", __LINE__ );
48 continue;
49 }
50 int pos = 0;
51 bool esc = false;
52 if( !linecontinue ){
53 iskeypart = true;
54 key = null;
55 value = null;
56 }
57 else{
58 linecontinue = false;
59 }
60 while( pos < line.length ){
61 char c = line[pos];
62 if( esc ){
63 esc = false;
64 switch( c ){
65 case 't' : c = '\t'; break;
66 case 'n' : c = '\n'; break;
67 case '\\': c = '\\'; break;
68 case '\"': c = '\"'; break;
69 //case ':' : c = ':' ; break;
70 default: break;
71 }
72 }
73 else{
74 if( c is '\\' ){
75 if( pos is line.length -1 ){
76 linecontinue = true;
77 goto nextline;
78 }
79 esc = true;
80 pos++;
81 continue;
82 }
83 else if( iskeypart && c is '=' ){
84 pos++;
85 iskeypart = false;
86 continue;
87 }
88 }
89 pos++;
90 if( iskeypart ){
91 key ~= c;
92 }
93 else{
94 value ~= c;
95 }
96 }
97 if( iskeypart ){
98 // Cannot find '=' in record
99 DWT.error( __FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT );
100 continue;
101 }
102 key = dwt.dwthelper.utils.trim(key);
103 value = dwt.dwthelper.utils.trim(value);
104 //tango.io.Stdout.Stdout.formatln( "properties put {}=>{}", key, value );
105
106 map[ key.dup ] = value.dup;
107 //tango.io.Stdout.Stdout.formatln( "properties put {}", __LINE__ );
108 }
109 }
110
111 public bool hasString( char[] key ){
112 return ( key in map ) !is null;
113 }
114
115 public char[] getString( char[] key ){
116 if( auto v = key in map ){
117 return (*v).dup;
118 }
119 throw new MissingResourceException( "key not found", this.classinfo.name, key );
120 }
121
122 public char[][] getKeys(){
123 return map.keys;
124 }
125
126 public static ResourceBundle getBundle( ImportData data ){
127 return new ResourceBundle( cast(char[]) data.data );
128 }
129 public static ResourceBundle getBundle( char[] name ){
130 try{
131 scope f = new File(name);
132 return new ResourceBundle( cast(char[]) f.read() );
133 }
134 catch( IOException e){
135 e.msg ~= " file:" ~ name;
136 throw e;
137 }
138 }
139 public static ResourceBundle getBundleFromData( char[] data ){
140 return new ResourceBundle( data );
141 }
142 }
143
144