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