diff --git a/Makefile b/Makefile index 229869a..3010bdf 100644 --- a/Makefile +++ b/Makefile @@ -1,40 +1,28 @@ +CXXFLAGS=-std=c++11 -Wall -pedantic ifeq "$(shell uname)" "Darwin" - LDFLAGS= -lglut -framework OpenGL - CXX=g++ + LDFLAGS := -lglut -framework OpenGL + CXX := g++ else - LDFLAGS= -lglut -lGL -lGLU -lX11 - CXX=clang++-3.9 + LDFLAGS := -lglut -lGL -lGLU -lX11 -lc++ -lc++abi + CXX := clang++ + CXXFLAGS += -stdlib=libc++ endif -all: http.o Node.o Parser.o LayoutEngine.o main.o render.o box.o text.o loadimage.o - $(CXX) $(LDFLAGS) render.o box.o text.o loadimage.o http.o Node.o Parser.o LayoutEngine.o main.o -o main - -render.o: - $(CXX) -c -std=c++11 render/render.cpp - -box.o: - $(CXX) -c -std=c++11 render/box.cpp - -text.o: - $(CXX) -c -std=c++11 render/text.cpp - -loadimage.o: - $(CXX) -c -std=c++11 images/loadimage.cpp - -main.o: - $(CXX) -c -std=c++11 main.cpp - -LayoutEngine.o: - $(CXX) -c -std=c++11 layout/LayoutEngine.cpp - -Parser.o: - $(CXX) -c -std=c++11 dom/Parser.cpp - -Node.o: - $(CXX) -c -std=c++11 dom/Node.cpp - -http.o: - $(CXX) -c -std=c++11 http/http.cpp +OBJS := \ + main.o \ + http/http.o \ + images/loadimage.o \ + dom/Node.o \ + dom/Parser.o \ + layout/LayoutEngine.o \ + render/render.o \ + render/box.o \ + render/text.o + +all: main + +main: $(OBJS) + $(CXX) $(LDFLAGS) $^ -o $@ clean: - rm *.o main + rm -f $(OBJS) main diff --git a/dom/Parser.cpp b/dom/Parser.cpp index 5cf2573..d12b9b6 100644 --- a/dom/Parser.cpp +++ b/dom/Parser.cpp @@ -129,6 +129,9 @@ Node * Parser::findNode(Node *n, std::string name) { return n; } else { for (uint i = 0; i < n->children.size(); i++) { + // FIXME + // why is this a loop? this will just call + // findNode(n->children[0], name) and return that return findNode(n->children[i], name); } } @@ -155,6 +158,8 @@ std::string Parser::findCSS(Node *r) { return r->textData; } else if (r->children.size() > 1) { for (uint i = 1; i < r->children.size(); i++) { + // FIXME again, I'm not seeing why this is in a loop since it + // returns right away return findCSS(r->children[i-1]) + findCSS(r->children[i]); } } else if (r->children.size() == 1) { diff --git a/dom/Parser.h b/dom/Parser.h index a2b80ea..fc24946 100644 --- a/dom/Parser.h +++ b/dom/Parser.h @@ -1,10 +1,10 @@ +#ifndef PARSER_H +#define PARSER_H + #include "Node.h" #include #include -#ifndef PARSER_H -#define PARSER_H - class Parser { public: Node *root; diff --git a/http/http.cpp b/http/http.cpp index f481cc1..fd97d67 100644 --- a/http/http.cpp +++ b/http/http.cpp @@ -8,17 +8,15 @@ extern "C" { } #include #include +#include #include -#include #include -#define BUFFER_SIZE 10000 - void Http::connect(std::string url) { extern int h_errno; const char *nameC = url.c_str(); struct hostent host = *gethostbyname(nameC); - if (h_errno != 0) std::cout<<"gethostbyname failure\n"; + if (h_errno != 0) std::cerr << "gethostbyname failure\n"; struct sockaddr_in addr; addr.sin_family = AF_INET; @@ -26,24 +24,19 @@ void Http::connect(std::string url) { bcopy((char*)host.h_addr, (char*)&addr.sin_addr.s_addr, host.h_length); Http::sck = socket(AF_INET, SOCK_STREAM, 0); - if (Http::sck == -1) std::cout<<"socket creation failure\n"; + if (Http::sck == -1) std::cerr << "socket creation failure\n"; if (::connect(Http::sck, (const struct sockaddr*)&addr, sizeof(addr)) != 0) { - std::cout<<"connection failure\n"; - } else { - //std::cout<<"successful connection\n"; + std::cerr << "connection failure\n"; } } std::string Http::makeRequest(const char *req) { + constexpr auto BUFFER_SIZE = 10000; send(Http::sck, req, std::string(req).length(), 0); - char* buffer = (char *)malloc(BUFFER_SIZE); - memset(buffer, 0, BUFFER_SIZE); - std::string response; sleep(1); - read(Http::sck, buffer, BUFFER_SIZE-1); - response = std::string(buffer); - //std::cout< buffer(BUFFER_SIZE); + read(Http::sck, buffer.data(), buffer.size()); + return {buffer.data()}; } std::string Http::dropHeaders(std::string doc) { diff --git a/layout/LayoutData.h b/layout/LayoutData.h index be50429..2c11c25 100644 --- a/layout/LayoutData.h +++ b/layout/LayoutData.h @@ -5,6 +5,8 @@ * Frame Tree data encapsulation class */ +#include + class LayoutData { public: @@ -16,8 +18,8 @@ class LayoutData { int width = 0; int height = 0; - int topColor[3]; - int bottomColor[3]; + std::array topColor; + std::array bottomColor; const char* text = "wut"; int textsize = 2; diff --git a/layout/LayoutEngine.cpp b/layout/LayoutEngine.cpp index 946b61d..ef24ff0 100644 --- a/layout/LayoutEngine.cpp +++ b/layout/LayoutEngine.cpp @@ -21,8 +21,11 @@ std::vector LayoutEngine::DFS() { Node* n = stack.top(); stack.pop(); result.push_back(n); - for (int i = n->children.size() - 1; i >=0; i--) - stack.push(n->children[i]); + for (auto it = n->children.rbegin(), end = n->children.rend(); + it != end; + ++it) { + stack.push(*it); + } } return result; } @@ -38,15 +41,15 @@ std::vector LayoutEngine::toLayoutData() { {"pink", 6} }; - int colorVals[7][3] = { - {255,255,0}, - {0,0,255}, - {255,0,0}, - {0,0,0}, - {0,255,255}, - {0,128,0}, - {255,192,203} - }; + std::array, 7> colorVals = {{ + {{255,255,0}}, + {{0,0,255}}, + {{255,0,0}}, + {{0,0,0}}, + {{0,255,255}}, + {{0,128,0}}, + {{255,192,203}} + }}; std::vector nodes = DFS(); std::vector result; @@ -54,8 +57,7 @@ std::vector LayoutEngine::toLayoutData() { std::string bgcolor = style[n->attributes["class"]]["background"]; if (bgcolor.empty()) { bgcolor = style[n->attributes["class"]]["background-color"]; - bcopy(colorVals[colorNames[bgcolor]], n->ld.topColor, 3*sizeof(int)); - bcopy(colorVals[colorNames[bgcolor]], n->ld.bottomColor, 3*sizeof(int)); + n->ld.bottomColor = n->ld.topColor = colorVals[colorNames[bgcolor]]; } else { debug(1); uint first = bgcolor.find("("); //first color @@ -66,8 +68,8 @@ std::vector LayoutEngine::toLayoutData() { debug(bgcolor); debug(color1); debug(color2); - bcopy(colorVals[colorNames[color1]], n->ld.topColor, 3*sizeof(int)); - bcopy(colorVals[colorNames[color2]], n->ld.bottomColor, 3*sizeof(int)); + n->ld.topColor = colorVals[colorNames[color1]]; + n->ld.bottomColor = colorVals[colorNames[color2]]; } n->ld.text = n->textData.c_str(); result.push_back(n->ld); @@ -189,7 +191,6 @@ void LayoutEngine::layout(Node* n, int voff_, int hoff_) { } LayoutData LayoutEngine::layoutText(std::string in) { - in.length(); LayoutData d; // d.width = 100; d.width = glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (unsigned char *)d.text); diff --git a/layout/LayoutEngine.h b/layout/LayoutEngine.h index 5165770..fc423a6 100644 --- a/layout/LayoutEngine.h +++ b/layout/LayoutEngine.h @@ -11,9 +11,8 @@ * Layout Engine */ -typedef std::map > css; - class LayoutEngine { + using css = std::map>; public: const int BROWSER_WIDTH = 960; Node* root; diff --git a/render/box.cpp b/render/box.cpp index 160ba38..4b418f4 100644 --- a/render/box.cpp +++ b/render/box.cpp @@ -10,7 +10,8 @@ using std::vector; Box::Box(const char *text, bool image, int voff, int hoff, int width, int height, - int size, int *topcolori, int *botcolori) + int size, const std::array& topcolori, + const std::array& botcolori) { // debug(111); this->text = text; diff --git a/render/box.h b/render/box.h index 28b83b9..830decd 100644 --- a/render/box.h +++ b/render/box.h @@ -4,6 +4,7 @@ #include "render.h" #include "../images/loadimage.h" #include +#include struct Point { @@ -23,8 +24,9 @@ class Box bool image; GLuint texture; public: - Box(const char *text, bool image, int voff, int hoff, int width, int height, - int size, int *boxtop, int *boxbottom); + Box(const char *text, bool image, int voff, int hoff, int width, + int height, int size, const std::array& boxtop, + const std::array& boxbottom); ~Box(); void draw(void); };