CfgList* CfgValue::LoadFile (std::string name) { InputBuffer buf; FILE *f = fopen (name.c_str(), "rb"); if (!f) throw ContentException(SPrintf("Failed to open file %s\n", name.c_str())); fseek (f, 0, SEEK_END); buf.len = ftell(f); buf.data = new char [buf.len]; fseek (f, 0, SEEK_SET); if (!fread (buf.data, buf.len, 1, f)) { fclose (f); delete[] buf.data; throw ContentException(SPrintf("Failed to read file %s\n", name.c_str())); } buf.filename = name.c_str(); fclose (f); CfgList *nlist = new CfgList; try { nlist->Parse (buf, true); } catch (const ContentException &e) { delete[] buf.data; delete nlist; throw e; } delete[] buf.data; return nlist; }
CfgValue* CfgValue::ParseValue (InputBuffer& buf) { CfgValue *v = 0; if (buf.skipWhitespace ()) buf.expecting ("Value"); // parse value types: int token = nextToken (buf); switch (token) { case CT_LIST: v = new CfgList(); break; case CT_NUMERIC: v = new CfgNumeric(); break; case CT_LITERAL: v = new CfgLiteral(); break; case CT_COMMAND: { ++buf; std::string cmd = buf.parseIdent(); d_trace("Cmd: %s\n", cmd.c_str()); if (cmd == "include") { return LoadNestedFile(buf); } else { throw ContentException(SPrintf("%s. Unknown command: %s", buf.location().c_str() ,cmd.c_str())); } } case CT_IDENT: throw ContentException(buf.location() + SPrintf("Unexpected identifier: '%s'", buf.parseIdent()) ); } if (v) { try { v->Parse(buf); v->cfgFilePath = buf.filename; } catch(const ContentException& e) { delete v; throw e; } } return v; }
XnbReader* ContentManager::load(const char* name) { std::string fullName = m_rootDirectory + name + ".xnb"; #if defined NXNA_PLATFORM_ANDROID FileStream* fs = AndroidFileSystem::Open(fullName.c_str()); #else FileStream* fs = new FileStream(fullName.c_str()); #endif if (fs == nullptr || fs->IsOpen() == false) throw ContentException(std::string("Unable to open file: ") + fullName); return new XnbReader(fs, name, fullName.c_str(), this); }
std::string InputBuffer::parseIdent () { std::string name; if(isalnum (get())) { name += get(); for (next(); isalnum (get()) || get() == '_' || get() == '-'; next()) name += get(); return name; } else throw ContentException(SPrintf("%s: Expecting an identifier instead of '%c'\n", location().c_str(), get())); }
void InputBuffer::skipKeyword (const char *kw) { skipWhitespace(); int a=0; while (!end() && kw[a]) { if (get() != kw[a]) break; next(), ++a; } if (kw[a]) throw ContentException(SPrintf("%s: Expecting keyword %s\n", location().c_str(), kw)); }
static int nextToken (InputBuffer& buf) { buf.skipWhitespace(); char r = *buf; if (r == '@') // special command return CT_COMMAND; else if(isalpha (r)) return CT_IDENT; else if(isdigit (r) || r == '.' || r == '-') return CT_NUMERIC; else if(r == '"') return CT_LITERAL; else if(r == '{') return CT_LIST; else if(r == ',') return CT_COMMA; throw ContentException(SPrintf("Unexpected character '%c'. ", r) + buf.location()); }
void CfgListElem::Parse (InputBuffer& buf) { if (buf.skipWhitespace ()) throw ContentException("Unexpected end of file in list element: " + buf.location()); // parse name int token = nextToken(buf); if (token == CT_COMMA) { ++buf; token = nextToken(buf); } if (token == CT_IDENT) { name = buf.parseIdent(); buf.skipWhitespace (); if (*buf != '=') buf.expecting("'=' character"); ++buf; } value = CfgValue::ParseValue (buf); }
void InputBuffer::expecting (const char *s) { throw ContentException (location() + "Expecting " + std::string(s) + "\n"); }