bsnes/ananke/nall/string/markup/bml.hpp

148 lines
3.4 KiB
C++
Raw Normal View History

Update to v091r11 release. byuu says: This release refines HSU1 support as a bidirectional protocol, nests SFC manifests as "release/cartridge" and "release/information" (but release/ is not guaranteed to be finalized just yet), removes the database integration, and adds support for ananke. ananke represents inevitability. It's a library that, when installed, higan can use to load files from the command-line, and also from a new File -> Load Game menu option. I need to change the build rules a bit for it to work on Windows (need to make phoenix a DLL, basically), but it works now on Linux. Right now, it only takes *.sfc file names, looks them up in the included database, converts them to game folders, and returns the game folder path for higan to load. The idea is to continue expanding it to support everything we can that I don't want in the higan core: - load *.sfc, *.smc, *.swc, *.fig files - remove SNES copier headers - split apart merged firmware files - pull in external firmware files (eg dsp1b.rom - these are staying merged, just as SPC7110 prg+dat are merged) - load *.zip and *.7z archives - prompt for selection on multi-file archives - generate manifest files based on heuristics - apply BPS patches The "Load" menu option has been renamed to "Library", to represent games in your library. I'm going to add some sort of suffix to indicate unverified games, and use a different folder icon for those (eg manifests built on heuristics rather than from the database.) So basically, to future end users: File -> Load Game will be how they play games. Library -> (specific system) can be thought of as an infinitely-sized recent games list. purify will likely become a simple stub that invokes ananke's functions. No reason to duplicate all that code.
2012-11-05 08:22:50 +00:00
#ifdef NALL_STRING_INTERNAL_HPP
//BML v1.0 parser
//revision 0.02
namespace nall {
namespace BML {
struct Node : Markup::Node {
protected:
//test to verify if a valid character for a node name
bool valid(char p) const { //A-Z, a-z, 0-9, -./
return p - 'A' < 26u || p - 'a' < 26u || p - '0' < 10u || p - '-' < 3u;
}
//determine indentation level, without incrementing pointer
unsigned readDepth(const char *p) {
unsigned depth = 0;
while(p[depth] == '\t' || p[depth] == ' ') depth++;
return depth;
}
//determine indentation level
unsigned parseDepth(const char *&p) {
unsigned depth = readDepth(p);
p += depth;
return depth;
}
//read name
void parseName(const char *&p) {
unsigned length = 0;
while(valid(p[length])) length++;
if(length == 0) throw "Invalid node name";
name = substr(p, 0, length);
p += length;
}
void parseData(const char *&p) {
if(*p == '=' && *(p + 1) == '\"') {
unsigned length = 2;
while(p[length] && p[length] != '\n' && p[length] != '\"') length++;
if(p[length] != '\"') throw "Unescaped value";
data = substr(p, 2, length - 2);
p += length + 1;
} else if(*p == '=') {
unsigned length = 1;
while(p[length] && p[length] != '\n' && p[length] != '\"' && p[length] != ' ') length++;
if(p[length] == '\"') throw "Illegal character in value";
data = substr(p, 1, length - 1);
p += length;
} else if(*p == ':') {
unsigned length = 1;
while(p[length] && p[length] != '\n') length++;
data = {substr(p, 1, length - 1), "\n"};
p += length;
}
}
//read all attributes for a node
void parseAttributes(const char *&p) {
while(*p && *p != '\n') {
if(*p != ' ') throw "Invalid node name";
while(*p == ' ') p++; //skip excess spaces
Node node;
node.attribute = true;
unsigned length = 0;
while(valid(p[length])) length++;
if(length == 0) throw "Invalid attribute name";
node.name = substr(p, 0, length);
node.parseData(p += length);
children.append(node);
}
}
//read a node and all of its children nodes
void parseNode(const char *&p) {
level = parseDepth(p);
parseName(p);
parseData(p);
parseAttributes(p);
if(*p++ != '\n') throw "Missing line feed";
while(*p) {
if(*p == '\n') { p++; continue; }
unsigned depth = readDepth(p);
if(depth <= level) break;
if(p[depth] == ':') {
p += depth;
unsigned length = 0;
while(p[length] && p[length] != '\n') length++;
data.append(substr(p, 1, length - 1), "\n");
p += length;
continue;
}
Node node;
node.parseNode(p);
children.append(node);
}
data.rtrim<1>("\n");
}
//read top-level nodes
void parse(const char *p) {
while(*p) {
Node node;
node.parseNode(p);
if(node.level > 0) throw "Root nodes cannot be indented";
children.append(node);
}
}
friend class Document;
};
struct Document : Node {
string error;
bool load(string document) {
name = "{root}", data = "";
try {
document.replace("\r", "");
while(document.position("\n\n")) document.replace("\n\n", "\n");
parse(document);
} catch(const char *perror) {
error = perror;
children.reset();
return false;
}
return true;
}
Document(const string &document = "") {
load(document);
}
};
}
}
#endif