Settings json loading/saving is working

This commit is contained in:
Vadim Lopatin 2015-02-06 12:25:42 +03:00
parent 589a02debd
commit 7bffc5ec90
1 changed files with 29 additions and 18 deletions

View File

@ -1,5 +1,6 @@
module dlangui.core.settings; module dlangui.core.settings;
import dlangui.core.logger;
import std.range; import std.range;
import std.algorithm : equal; import std.algorithm : equal;
import std.conv : to; import std.conv : to;
@ -920,7 +921,6 @@ final class Setting {
break; break;
} }
} }
append(s);
append('\"'); append('\"');
} }
} }
@ -1078,7 +1078,7 @@ final class Setting {
return (ch >= '0' && ch <= '9'); return (ch >= '0' && ch <= '9');
} }
@property char skipSpaces() { @property char skipSpaces() {
for(;pos < json.length;) { for(;pos < json.length;pos++) {
char ch = json[pos]; char ch = json[pos];
if (!isSpace(ch)) if (!isSpace(ch))
break; break;
@ -1110,11 +1110,10 @@ final class Setting {
} }
@property string parseString() { @property string parseString() {
char ch = peek;
if (ch != '\"') {
error("cannot parse string");
}
char[] res; char[] res;
char ch = peek;
if (ch != '\"')
error("cannot parse string");
for (;;) { for (;;) {
ch = nextChar; ch = nextChar;
if (!ch) if (!ch)
@ -1156,7 +1155,7 @@ final class Setting {
res ~= ch; res ~= ch;
} }
} }
return cast(string)res; error("cannot parse string");
} }
@property string parseIdent() { @property string parseIdent() {
char ch = peek; char ch = peek;
@ -1201,7 +1200,7 @@ final class Setting {
int sign = 1; int sign = 1;
if (ch == '-') { if (ch == '-') {
sign = -1; sign = -1;
nextChar; ch = nextChar;
} }
if (!isDigit(ch)) if (!isDigit(ch))
error("cannot parse number"); error("cannot parse number");
@ -1238,16 +1237,21 @@ final class Setting {
shift = shift * 10 + (ch - '0'); shift = shift * 10 + (ch - '0');
ch = nextChar; ch = nextChar;
} }
if (shiftSign < 0)
shift = -shift;
} }
if (isAlpha(ch)) if (isAlpha(ch))
error("error while parsing number"); error("error while parsing number");
double v = sign > 0 ? n : -n; double v = cast(double)n;
if (n2) // part after period if (n2) // part after period
v += cast(double)n2 / n2_div; v += cast(double)n2 / n2_div;
if (shift) // E part - pow10 if (sign < 0)
v *= pow(10.0, shift); v = -v;
if (shift) { // E part - pow10
double p = pow(10.0, shift);
if (shiftSign > 0)
v *= p;
else
v /= p;
}
res.floating = v; res.floating = v;
} else { } else {
// integer // integer
@ -1330,17 +1334,24 @@ final class Setting {
return this; return this;
} }
bool parseJSON(string s) { void parseJSON(string s) {
clear(SettingType.NULL); clear(SettingType.NULL);
JsonParser parser; JsonParser parser;
parser.init(s); parser.init(s);
return true; parseJSON(parser);
} }
bool load(string filename) { bool load(string filename) {
import std.file; try {
string s = readText(filename); import std.file;
return parseJSON(s); string s = readText(filename);
parseJSON(s);
return true;
} catch (Exception e) {
// Failed
Log.e("exception while parsing json: ", e);
return false;
}
} }
} }