-
Notifications
You must be signed in to change notification settings - Fork 4
/
adparser.js
65 lines (62 loc) · 2.2 KB
/
adparser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
exports.adparser = {
//parse something like
//JOB_GLIDEIN_ClusterId = "$$(GLIDEIN_ClusterId:Unknown)"
parse_value: function(v) {
if(v === "true") {
return true;
} else if(v === "false") {
return false;
} else if(v.indexOf("\"") == 0) {
//remove double quote from value if it's quoted
return v.substring(1, v.length-1);
}
//tryi converting to int if its int
var i = parseInt(v);
if(i == v) {
return i;
}
//console.log("not sure how to parse:"+v);
return v;
},
parse: function(lines) {
//console.log("dumping lines...............................");
//console.dir(lines);
//parse class ad key/value
var props = {};
var cont = null;
var cont_key = null;
lines.forEach(function(line) {
if(line == "") return;
if(line == ".") return; //what is this?
if(cont) {
if(line.indexOf('"') == -1) {
//continue on..
cont = cont+"\n"+line;
} else {
//ended
props[cont_key] = exports.adparser.parse_value(cont);
console.log("parsed multline value:"+cont);
cont = null;
}
} else {
var dpos = line.indexOf(" = ");
if(dpos == -1) {
console.log("malformed classad .. ignoring");
console.log(line);
} else {
var key = line.substring(0, dpos);
var value = line.substring(dpos+3);
if(value[0] == '"' && value.indexOf('"', 1) == -1) {
//found quoted string not delimited by " - probably continuing to the next line
cont = value.substring(0, value.length);
} else {
props[key] = exports.adparser.parse_value(value);
}
}
}
});
//console.log("persed too.....................");
//console.dir(props);
return props;
}
}