19 using System.Collections.Generic;
23 namespace Deveel.Data.Util {
40 this.defaults = defaults;
57 return this[key] = value;
97 public void Load(Stream inStream) {
102 StreamReader reader =
new StreamReader(inStream, Encoding.GetEncoding(
"ISO-8859-1"));
105 while ((line = reader.ReadLine()) != null) {
109 while (pos < line.Length
110 && Char.IsWhiteSpace(c = line[pos]))
114 if ((line.Length - pos) == 0
115 || line[pos] ==
'#' || line[pos] ==
'!')
122 bool needsEscape = line.IndexOf(
'\\', pos) != -1;
123 StringBuilder key = needsEscape ?
new StringBuilder() : null;
124 while (pos < line.Length
125 && !Char.IsWhiteSpace(c = line[pos++])
126 && c !=
'=' && c !=
':') {
127 if (needsEscape && c ==
'\\') {
128 if (pos == line.Length) {
132 line = reader.ReadLine();
136 while (pos < line.Length
137 && Char.IsWhiteSpace(c = line[pos]))
152 if (pos + 4 <= line.Length) {
153 char uni = (char)Convert.ToInt32(line.Substring(pos, 4), 16);
163 }
else if (needsEscape)
167 bool isDelim = (c ==
':' || c ==
'=');
171 keyString = key.ToString();
172 else if (isDelim ||
Char.IsWhiteSpace(c))
173 keyString = line.Substring(start, (pos - 1) - start);
175 keyString = line.Substring(start, pos - start);
177 while (pos < line.Length
178 &&
Char.IsWhiteSpace(c = line[pos]))
181 if (!isDelim && (c ==
':' || c ==
'=')) {
183 while (pos < line.Length
184 &&
Char.IsWhiteSpace(c = line[pos]))
190 this[keyString] = line.Substring(pos);
195 StringBuilder element =
new StringBuilder(line.Length - pos);
196 while (pos < line.Length) {
199 if (pos == line.Length) {
201 line = reader.ReadLine();
210 while (pos < line.Length
211 &&
Char.IsWhiteSpace(c = line[pos]))
213 element.EnsureCapacity(line.Length - pos + element.Length);
218 element.Append(
'\n');
221 element.Append(
'\t');
224 element.Append(
'\r');
227 if (pos + 4 <= line.Length) {
228 char uni = (char)Convert.ToInt32(line.Substring(pos, 4), 16);
241 this[keyString] = element.ToString();
254 [Obsolete(
"Use Store(Stream, string) method instead.")]
255 public void Save(Stream output, String header) {
257 Store(output, header);
258 }
catch (IOException) {
296 public void Store(Stream output, String header) {
298 StreamWriter writer =
new StreamWriter(output, Encoding.GetEncoding(
"ISO-8859-1"));
300 writer.WriteLine(
"#" + header);
301 writer.WriteLine(
"#" + DateTime.Now);
303 StringBuilder s =
new StringBuilder();
304 foreach (var entry
in this) {
305 FormatForOutput((String)entry.Key, s,
true);
307 FormatForOutput((String)entry.Value, s,
false);
332 String value = (String)prop[key];
337 while (prop != null);
357 String prop = GetProperty(key);
363 public ICollection PropertyNames {
369 var s =
new List<string>();
372 s.AddRange(prop.Keys);
374 }
while (prop != null);
390 public void List(Stream output) {
391 StreamWriter writer =
new StreamWriter(output);
395 public void List(StreamWriter output) {
396 output.WriteLine(
"-- listing properties --");
398 foreach (var entry
in this) {
399 output.Write((String)entry.Key +
"=");
401 String s = (String)entry.Value;
402 if (s != null && s.Length > 40)
403 output.WriteLine(s.Substring(0, 37) +
"...");
423 buffer.EnsureCapacity(str.Length);
425 buffer.EnsureCapacity(buffer.Length + str.Length);
427 int size = str.Length;
428 for (
int i = 0; i < size; i++) {
432 buffer.Append(
"\\n");
435 buffer.Append(
"\\r");
438 buffer.Append(
"\\t");
441 buffer.Append(head ?
"\\ " :
" ");
448 buffer.Append(
'\\').Append(c);
451 if (c < ' ' || c >
'~') {
452 String hex = ((int)c).ToString(
"{0:x4}");
453 buffer.Append(
"\\u0000".Substring(0, 6 - hex.Length));
A long string in the system.
Object SetProperty(String key, String value)
Properties()
Creates a new empty property list with no default values.
static void FormatForOutput(String str, StringBuilder buffer, bool key)
Formats a key or value for output in a properties file.
String GetProperty(String key, String defaultValue)
void Store(Stream output, String header)
Properties(Properties defaults)
void List(StreamWriter output)
String GetProperty(String key)
void Save(Stream output, String header)