From 00be091a4221c0798b10db908eae2355f367494f Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 17:38:58 -0700 Subject: [PATCH 01/10] Replaces malloc()'d buffer (leak) with std::vector --- http/http.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/http/http.cpp b/http/http.cpp index f481cc1..6a83070 100644 --- a/http/http.cpp +++ b/http/http.cpp @@ -8,12 +8,10 @@ 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(); @@ -35,15 +33,12 @@ void Http::connect(std::string url) { } 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) { From def9db129c7675f271156cc8e661d2dc5496260f Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 17:53:45 -0700 Subject: [PATCH 02/10] Uses makefile macros to avoid repetiton. --- Makefile | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 229869a..b1c45d1 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +CXXFLAGS=-std=c++11 -Wall -pedantic ifeq "$(shell uname)" "Darwin" LDFLAGS= -lglut -framework OpenGL CXX=g++ @@ -7,34 +8,34 @@ else 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 + $(CXX) $(LDFLAGS) $^ -render.o: - $(CXX) -c -std=c++11 render/render.cpp +render.o: render/render.cpp + $(CXX) $(CXXFLAGS) -c $^ -box.o: - $(CXX) -c -std=c++11 render/box.cpp +box.o: render/box.cpp + $(CXX) $(CXXFLAGS) -c $^ -text.o: - $(CXX) -c -std=c++11 render/text.cpp +text.o: render/text.cpp + $(CXX) $(CXXFLAGS) -c $^ -loadimage.o: - $(CXX) -c -std=c++11 images/loadimage.cpp +loadimage.o: images/loadimage.cpp + $(CXX) $(CXXFLAGS) -c $^ -main.o: - $(CXX) -c -std=c++11 main.cpp +main.o: main.cpp + $(CXX) $(CXXFLAGS) -c $^ -LayoutEngine.o: - $(CXX) -c -std=c++11 layout/LayoutEngine.cpp +LayoutEngine.o: layout/LayoutEngine.cpp + $(CXX) $(CXXFLAGS) -c $^ -Parser.o: - $(CXX) -c -std=c++11 dom/Parser.cpp +Parser.o: dom/Parser.cpp + $(CXX) $(CXXFLAGS) -c $^ -Node.o: - $(CXX) -c -std=c++11 dom/Node.cpp +Node.o: dom/Node.cpp + $(CXX) $(CXXFLAGS) -c $^ -http.o: - $(CXX) -c -std=c++11 http/http.cpp +http.o: http/http.cpp + $(CXX) $(CXXFLAGS) -c $^ clean: rm *.o main From 592b360907a6e88bc06c8511cecac1165490be5e Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 17:59:48 -0700 Subject: [PATCH 03/10] Adds makefile flags for clang on Linux Removes explicit clang version number. --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b1c45d1..5383d72 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,9 @@ ifeq "$(shell uname)" "Darwin" 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 From cfd2017c4329ecde6e5b60221600b24077426211 Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 22:16:19 -0700 Subject: [PATCH 04/10] Uses implicit make rules --- Makefile | 56 +++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index 5383d72..3010bdf 100644 --- a/Makefile +++ b/Makefile @@ -1,42 +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 -lc++ -lc++abi - CXX=clang++ - CXXFLAGS+=-stdlib=libc++ + 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: render/render.cpp - $(CXX) $(CXXFLAGS) -c $^ - -box.o: render/box.cpp - $(CXX) $(CXXFLAGS) -c $^ - -text.o: render/text.cpp - $(CXX) $(CXXFLAGS) -c $^ - -loadimage.o: images/loadimage.cpp - $(CXX) $(CXXFLAGS) -c $^ - -main.o: main.cpp - $(CXX) $(CXXFLAGS) -c $^ - -LayoutEngine.o: layout/LayoutEngine.cpp - $(CXX) $(CXXFLAGS) -c $^ - -Parser.o: dom/Parser.cpp - $(CXX) $(CXXFLAGS) -c $^ - -Node.o: dom/Node.cpp - $(CXX) $(CXXFLAGS) -c $^ - -http.o: http/http.cpp - $(CXX) $(CXXFLAGS) -c $^ +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 From ed8436564f820f7eb907c9d9c397eb8a025a0256 Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 22:17:57 -0700 Subject: [PATCH 05/10] logs errors with cerr instead of cout --- http/http.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/http/http.cpp b/http/http.cpp index 6a83070..fd97d67 100644 --- a/http/http.cpp +++ b/http/http.cpp @@ -16,7 +16,7 @@ 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; @@ -24,11 +24,9 @@ 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"; } } From 6a2408befdecec12f96e7c1d15e499be74c2a365 Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 22:18:53 -0700 Subject: [PATCH 06/10] moves header guard to top --- dom/Parser.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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; From 8b0655d3b43b853203e96a711266e15585c30edd Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 22:38:32 -0700 Subject: [PATCH 07/10] Moves "css" type alias into class definition --- layout/LayoutEngine.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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; From cc805ed9df7c9ffca30109ad48cda80986957f14 Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 22:52:30 -0700 Subject: [PATCH 08/10] Uses std::array for colors. --- layout/LayoutData.h | 6 ++++-- layout/LayoutEngine.cpp | 33 +++++++++++++++++---------------- 2 files changed, 21 insertions(+), 18 deletions(-) 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); From 43f35c9b06962a992c765c441c87b036e0ca29ef Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 22:53:13 -0700 Subject: [PATCH 09/10] Adds FIXMEs about once-run loops --- dom/Parser.cpp | 5 +++++ 1 file changed, 5 insertions(+) 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) { From 8248ff1cb9a20b5414f7674c7e818a57976f18f8 Mon Sep 17 00:00:00 2001 From: Ryan Haining Date: Mon, 26 Sep 2016 23:05:42 -0700 Subject: [PATCH 10/10] Box ctor changed to take std::array& --- render/box.cpp | 3 ++- render/box.h | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) 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); };