Config auswerten



  • Habe ein kleine funktion geschrieben:

    /*Written by Jan Koester realease under lgpl*/
    #include <iostream> 
    #include <fstream> 
    #include <string> 
    #include "easyc++.h"
    
    using namespace std; 
    
    string readconf(char* filename, string confkey)
    { 
    
            ifstream  file(filename); 
            string buf, key; 
            size_t n; 
    
            while(file.good()) { 
                    getline(file, buf, '\n'); 
                    n=buf.find(confkey); 
                    if(n != string::npos) { 
                            n += confkey.length();
                            key=buf.substr(n,buf.length()); 
                            break; 
                    } else { 
                            continue; 
                    } 
            } 
    
    file.close();
            if(key.length() > 0) { 
                    return key;
            } else { 
                    return "error key not found";
            }
    
    }
    

    Welche anhand eines Keywords Parameter auslesen kann nun möchte ich nicht jedes mal die Datei auslesen sondern in den Arbeitsspeicher auslagern und global von meinem Modulen drauf zugreifen können, die sich in verschiedenen Klassen sich befinden, die Module werden mit dlopen geladen.



  • Hallo

    Ja dann mach das doch.
    Oder hast du noch eine Frage?

    bis bald
    akari



  • Hauptprogramm:

    #include <iostream>
    #include <map>
    #include <fstream>
    #include "tuxcms.h"
    #include <vector>
    #include <html.h>
    #include <cgi.h>
    #include "modul/module.h"
    #include <easyc++.h>
    
    /*Under Construction*/
    
    using namespace std;
    
    string core::getconfig(string input)
    {
    string out;
    out = readconf("tuxcms.conf",input);
    return out;
    }
    
    void core::test()
    {
    cgi maincgi;
    	maincgi.request();
    	maincgi.urlencode();
    
    	string inhalt = maincgi.getcgidata("nav");
    	extern bool modul(string modulpath);
    	modul(inhalt);
    
    }
    
    void core::login()
    {
    html loginhtml;
    	loginhtml.createlayer("login","main");
    		loginhtml.startform("post", "loginform");
    			cout << "<p></p>" << endl;
    		loginhtml.input( "Username", "text");
    			cout << "<p></p>" << endl;
    		loginhtml.input( "Username", "password");
    			cout << "<p></p>" << endl;
    		loginhtml.input( "Login", "submit");
    			cout << "<p></p>" << endl;
    		loginhtml.link("Regristieren", "");
    	loginhtml.endform();
    loginhtml.endlayer();
    }
    
    void core::style()
    {
    	string style = "dark";
    	html stylehtml;
    
    	cout << "<link rel=\"icon\" type=\"image/png\" href=\"style/"<< style <<"/images/icon.png\" />" << endl,
    	cout << "<link rel=\"stylesheet\" type=\"text/css\" href=\"style/"<< style <<"/main.css\"/>" << endl;
    
    	stylehtml.bodystart();
    
    	stylehtml.createlayer("logo","main");
    		string imgtmp ="style/";
    		imgtmp += style;
    		imgtmp += "/images/logo.png";
    
    		stylehtml.img("logo1", imgtmp);
    		stylehtml.endlayer();
    }
    
    int main()
    {
    html mainhtml;
    core mainprog;
    
    sitename = mainprog.getconfig("sitename:");
    sqloptions = mainprog.getconfig("database:");
    
    mainhtml.starthtml();
    
    	mainprog.style();
    
    	mainprog.login();
    
    		mainhtml.createlayer("navbar","main");
    			mainhtml.link("News","?+nav=module/news.so");
    			mainhtml.link("Video","?+nav=video");
    			mainhtml.link("Projects","?+nav=module/projects.so");
    			mainhtml.link("Downloads","?+nav=module/filebrowser.so");
    			mainhtml.link("Sysinfo","?+nav=sysinfo");
    		mainhtml.endlayer();
    
    	mainprog.test();
    
    	mainhtml.bodyend();
    
    mainhtml.endhtml();
    }
    

    Header hauptprogramm:

    #include <vector>
    #include <string>
    
    static std::string sqloptions;
    static std::string sitename;
    
    class core
    {
    public:
    std::string getconfig(std::string input);
    void login();
    void test();
    void style();
    };
    

    Modulloader:

    /*
    Writen by Jan Koster releade under lgpl
    
    */
    #include <iostream>
    #include <dlfcn.h>
    #include <string>
    #include <easyc++.h>
    #include "module.h"
    
    using namespace std;
    
    bool modul(string modulpath){
    
    char* tmp = stringtochar(modulpath);
    
    using std::cout;
    using std::cerr;
    
    void* modul = dlopen(tmp, RTLD_LAZY);
    if (!modul) {
            cerr << "Cannot load library: " << dlerror() << '\n';
            return 1;
    }
    
    dlerror();
    
    create_t* create_modul = (create_t*) dlsym(modul, "create");
    const char* dlsym_error = dlerror();
    	if (dlsym_error) {
            	cerr << "Cannot load symbol create: " << dlsym_error << '\n';
    		return 1;
        	}
    
    destroy_t* destroy_modul = (destroy_t*) dlsym(modul, "destroy");
    dlsym_error = dlerror();
    	if (dlsym_error) {
            	cerr << "Cannot load symbol destroy: " << dlsym_error << '\n';
            	return 1;
    	}
    
        module* module = create_modul();
    
        module->htmlout();
    
        destroy_modul(module);
    
        dlclose(modul);
    
    	return 0;
    
    }
    

    Modulloader header:

    #ifndef module_H
    #define module_H
    
    class module {
    protected:
    
    public:
    virtual ~module() {}
    virtual void htmlout(){};
    };
    
    typedef module* create_t();
    typedef void destroy_t(module*);
    
    #endif
    
    bool modul(std::string modulpath);
    

    Ein modul:

    #include <iostream>
    #include <string>
    #include <pqxx/connection>
    #include <pqxx/transaction>
    #include <pqxx/result>
    #include "news.h"
    #include <html.h>
    #include <vector>
    #include <map>
    #include <easyc++.h>
    
    using namespace std;
    using namespace PGSTD;
    using namespace pqxx;
    
    void news::getnews(int row)
    {
    	News news;
    	result R;
    
            connection *conn = new connection(sqloptions);
    
            work T(*conn);
    
            R = T.exec("SELECT * FROM NEWS");
            T.commit();
    
            if(R.size() > 0)
            {
    		news.insert(NewsPair(R[row]["n_id"].as<int>(), R[row]["titel"].as<std::string>()));
    		news.insert(NewsPair(R[row]["n_id"].as<int>(), R[row]["autor"].as<std::string>()));
    		news.insert(NewsPair(R[row]["n_id"].as<int>(), R[row]["datum"].as<std::string>()));
    		news.insert(NewsPair(R[row]["n_id"].as<int>(), R[row]["zeit"].as<std::string>()));
    		news.insert(NewsPair(R[row]["n_id"].as<int>(), R[row]["inhalt"].as<std::string>()));
            }
            else
            {
                    cout << "Datenbank hat keine Daten" << endl;
            }
    
    	html newshtml;
    	News::iterator pos;
    
    	newshtml.createlayer("inhalt","main");
    
    		for (pos = news.begin(); pos != news.end(); ++pos) {
    			std::cout << pos->second <<"<p></p>" << endl;
    			}
    	newshtml.endlayer();	
    
            conn->disconnect();
            delete conn;
    }
    
    void news::newsrows()
    {
    	result R;
            connection *conn = new connection(sqloptions);
    
            work T(*conn);
    
            R = T.exec("select count(*) from news");
    	T.commit();
    	R[0][0].to(rows);
    	conn->disconnect();
            delete conn;
    
    	do
    	{
    	rows--;
    	getnews(rows);
    	}
    	while(rows>=0);
    
    }
    
    void news::writenews()
    {
    
    };
    

    modul header:

    #include <map>
    #include "../../modul/module.h"
    #include "../../tuxcms.h"
    
    class news : public module
    {
    private:
    int rows;
    typedef std::multimap<int, std::string> News;
    typedef std::pair<int, std::string> NewsPair;
    void getnews(int row);
    void newsrows();
    void writenews();
    
    public:
    
    void htmlout()
    {
    	newsrows();
    }
    
    };
    
    extern "C" module* create() {
        return new news;
    }
    
    extern "C" void destroy(module* p) {
        delete p;
    }
    

    variable daten werden nicht übertragen habe es mit cout ausprobiert



  • Hallo

    Glaubst du wirklich das sich jemand deine 100e Zeilen Code anschaut ohne auch nur zu wissen was das Problem ist? "Klappt nicht" ist keine ausreichende Fehlerbeschreibung! 🙄

    bis bald
    akari


Anmelden zum Antworten