void create(std::string filename){ cerr << "Compiling " << filename << endl; TiObj objs; objs.loadFile(filename); if ( objs.has("template") ){ TiVar& var_tmpl = objs["template"]; if ( var_tmpl.isObj() ){ this->db = var_tmpl.atObj(); } else { db.loadFile( objs.atStr("template") ); if ( db.is("Error") ){ cerr << objs; return; } } } else { if ( objs.is("Error") ){ cerr << objs; } else cerr << "Nao existe template configurado\n"; return; } this->root = objs.atStr("root","."); // Reopen cout int pos = filename.find_last_of('.'); string outfile = Join("%s.html").at(filename.substr(0,pos)).ok; freopen (outfile.c_str(),"w",stdout); // Create the response cout << "<html><head>\n"; cout << "\t<meta charset=\"utf-8\">\n"; for (int i=0; i<objs.size(); i++){ TiObj& node = objs.box[i]; if ( node.is("Css") ){ cout << Join("\t<link href=\"%s\" rel=\"stylesheet\">\n").at(node.atStr("url")).ok; }/* else if ( node.is("Js") ){ cout << Join("\t<link href=\"%s\" rel=\"stylesheet\">\n").at(node.atStr("url")).ok(); }*/ } if ( objs.has("title") ){ cout << Join("\t<title>%s</title>").at(objs.atStr("title")).ok; } cout << "</head><body>\n"; for (int i=0; i<objs.size(); i++){ this->process( objs.box[i] ); } cout << "</body></html>\n"; // Close the file fclose(stdout); }
void process(TiObj& obj){ bool error = false; if ( obj.classe == "" ){ if ( this->stack.size() == 0 ){ if ( obj.has("html") ){ cout << obj.atStr("html"); return; } } else { TiObj& parent = *this->stack[ this->stack.size()-1 ]; if ( parent.hasnt("default") ){ cerr << "Default do item nao encontrado\n"; error = true; } } } else if ( obj.classe != "Css" ){ if ( this->db.hasnt( obj.classe ) ){ cerr << Join("Item nao encontrado %s\n").at(obj.classe).ok; error = true; } } if ( error ) return; // Get Template TiObj* tempalte; if ( obj.classe == "" ){ if ( this->stack.size() > 0 ){ TiObj* parent = this->stack.back(); if ( parent->has("default") ){ tempalte = this->getObject(parent->at("default")); } } else cerr << "Pilha desbalanceada\n"; } else tempalte = &getTemplate(obj); assert(tempalte!=nullptr); // Process the template with the variable if ( tempalte->has("ini") || tempalte->has("end") ){ this->stack.push_back( &this->db.atObj(obj.classe) ); std::string ini = tempalte->atStr("ini"); std::string end = tempalte->atStr("end"); cout << this->parseTmpl(ini,obj); for (int i=0; i<obj.size(); i++){ this->process( obj.box[i] ); } cout << this->parseTmpl(end,obj); this->stack.pop_back(); } else { cout << this->parseTmpl(tempalte->atStr("txt"),obj); } }
int getVar_state1(string& out_string, TiObj& database, char c){ if ( isalnum(c) || c == '_' ){ this->buffer += c; } else { if ( database.has(this->buffer) ){ out_string += database.atStr(this->buffer); } else { this->error = "Field "+this->buffer+" not found in the Object"; return false; } out_string += c; this->state = 0; this->buffer = ""; } return true; }
TiObj Filesystem::ls (std::string url) { TiObj out; this->log( __FUNCTION__ ); string real_url = this->path_set(url); DIR *dp; struct stat buf; struct dirent *ep; dp = opendir (real_url.c_str()); if (dp != NULL){ string item, item_url; while ( ep = readdir(dp) ){ if ( strcmp(ep->d_name,".")==0 || strcmp(ep->d_name,"..")==0 ) continue; item = path_add(url, ep->d_name); item_url = path_add(real_url, ep->d_name); if ( stat(item_url.c_str(), &buf) != -1 ){ if (S_ISREG (buf.st_mode) ){ string tmp = ep->d_name; TiObj novo; novo.set("class", this->file_type(ep->d_name) ); novo.set("name", tmp); novo.set( "url", item); out.box() += novo; } else if (S_ISDIR (buf.st_mode) ){ TiObj novo; this->folder_sysobj( novo, item_url ); novo.set( "url", item); if ( !novo.has("name") ) novo.set("name", ep->d_name); out.box() += novo; } } } closedir(dp); } else { return this->error( strerror(errno), url ); } return out; }
bool parse(std::string& out, TiObj& database, std::string data){ this->error = ""; this->state = 0; for (int i=0; i<data.size(); i++){ if ( this->state == 0 ){ getVar_state0(out, database, data[i]); } else if (this->state == 1) { bool ok = getVar_state1(out, database, data[i]); if ( !ok ) return false; } } if ( this->buffer.size()>0 ){ if ( database.has(this->buffer) ){ out += database.atStr(this->buffer); this->buffer = ""; } else { this->error = "Field "+this->buffer+" not found in the Object"; return false; } } return true; }