Skip to content
Snippets Groups Projects
main.cpp 3.48 KiB
Newer Older
#include "CommandLineParams.h"
gyroninja's avatar
gyroninja committed
#include "graphics/opengl/Window.h"
#include "html/HTMLParser.h"
#include "Log.h"
#include "URL.h"
#include "WebResource.h"

gyroninja's avatar
gyroninja committed
#include <ctime>
gyroninja's avatar
gyroninja committed
#include <iostream>
#include <sys/stat.h>
gyroninja's avatar
gyroninja committed
const std::unique_ptr<Window> window = std::make_unique<Window>();
Nubben's avatar
Nubben committed

bool setWindowContent(URL const& url) {
Odilitime's avatar
Odilitime committed
    logDebug() << "main::setWindowContent - " << url << std::endl;

    // download URL
Nubben's avatar
Nubben committed
    WebResource res = getWebResource(url);
    if (res.resourceType == ResourceType::INVALID) {
        logError() << "Invalid resource type: " << res.raw << std::endl;
Nubben's avatar
Nubben committed
        return false;
    // parse HTML
Nubben's avatar
Nubben committed
    HTMLParser parser;
    const std::clock_t begin = clock();
    std::shared_ptr<Node> rootNode = parser.parse(res.raw);
    const std::clock_t end = clock();
    logDebug() << "main::setWindowContent - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl;
    
    // send NodeTree to window
    window->setDOM(rootNode);
Nubben's avatar
Nubben committed
    return true;
bool isAbsolutePath(const std::string s);
bool isAbsolutePath(const std::string s) {
    return (s.length() > 0 && s[0] == '/');
}

bool fileExists(const std::string s);
bool fileExists(const std::string s) {
    struct stat buf;
    return stat(s.c_str(), &buf) != -1;
}

gyroninja's avatar
gyroninja committed
int main(int argc, char *argv[]) {
    // no longer require a URL
    /*
gyroninja's avatar
gyroninja committed
    if (argc == 1) {
        std::cout << "./netrunner [http://host.tld/|/path/to/file.html] [-log <error|warning|notice|info|debug>]" << std::endl;
gyroninja's avatar
gyroninja committed
        return 1;
    }
    std::cout << "/g/ntr - NetRunner build " << __DATE__ << std::endl;
Odilitime's avatar
Odilitime committed
    // we need to set up OGL before we can setDOM (because component can't be constructed (currently) without OGL)
    // but should be after CommandLineParams incase we need to change some type of window config
    window->windowWidth = 1024;
    window->windowHeight = 640;
Odilitime's avatar
Odilitime committed
    window->init();
    if (!window->window) {
        return 1;
    }
    //std::cout << "argc " << argc << std::endl;
    if (argc > 1) {
        initCLParams(argc, argv);
        // this isn't going to work
        std::string rawUrl = getCLParamByIndex(1);
        // if we do this here, shouldn't we do this in parseUri too?
        if (rawUrl.find("://") == rawUrl.npos) {
            // Path should always be absolute for file://
            if (isAbsolutePath(rawUrl)) {
                rawUrl = "file://" + rawUrl;
            } else {
                auto absolutePath = std::string(getenv("PWD")) + '/' + rawUrl;
                if (fileExists(absolutePath)) {
                    rawUrl = "file://" + absolutePath;
                } else {
                    // Default to http if the file wasn't found
                    rawUrl = "http://" + rawUrl;
                }
            }
        }
        //logDebug() << "pre URL parse [" << url << "]" << std::endl;
        window->currentURL = URL(rawUrl);
        logDebug() << "loading [" << window->currentURL << "]" << std::endl;
        if (!setWindowContent(window->currentURL)) {
            return 1;
        }
gyroninja's avatar
gyroninja committed
    while (!glfwWindowShouldClose(window->window)) {
Odilitime's avatar
Odilitime committed
        //const std::clock_t begin = clock();
gyroninja's avatar
gyroninja committed
        window->render();
        glfwWaitEvents(); // block until something changes
Odilitime's avatar
Odilitime committed
        //const std::clock_t end = clock();
        //std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f    " << std::flush;
gyroninja's avatar
gyroninja committed
    }
    return 0;
}