Skip to content

Database integration #1

Merged
merged 3 commits into from Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,6 @@ bin/

# ignore vscode
.vscode/

# ignore cmake tools
build/
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -12,7 +12,7 @@ file(GLOB headers "lib/*.hpp")
file(GLOB sources "lib/*.cpp")

add_library(grandmaze SHARED ${sources} ${headers})
target_link_libraries(grandmaze ncurses)
target_link_libraries(grandmaze ncurses sqlite3)

install(TARGETS grandmaze
LIBRARY DESTINATION "lib")
Expand Down
2 changes: 2 additions & 0 deletions lib/colour.hpp
Expand Up @@ -3,6 +3,7 @@

namespace GrandMaze
{
// All colours
enum Colour
{
None,
Expand All @@ -27,6 +28,7 @@ namespace GrandMaze
WhiteInvert = Invert,
};

// Initialise colours
void initColour(void);
};

Expand Down
16 changes: 16 additions & 0 deletions lib/exception.cpp
@@ -0,0 +1,16 @@
#include <sqlite3.h>

#include "exception.hpp"

using namespace GrandMaze;

DatabaseError::DatabaseError(int errorCode) : errorCode(errorCode)
{

}

const char* DatabaseError::what() const noexcept
{
// get sqlite3 error from error code
return sqlite3_errstr(errorCode);
}
21 changes: 21 additions & 0 deletions lib/exception.hpp
@@ -0,0 +1,21 @@
#ifndef EXCEPTION_H
#define EXCEPTION_H

#include <exception>

namespace GrandMaze
{
class DatabaseError : public std::exception
{
private:
int errorCode;

public:
// Construct a DatabaseError with a SQLite error code
DatabaseError(int errorCode);

const char* what() const noexcept;
};
}

#endif
32 changes: 30 additions & 2 deletions lib/form.cpp
Expand Up @@ -16,10 +16,13 @@ Form::Form(WINDOW *win, std::string title)
void Form::draw(void)
{
wclear(win);
// get max column
int maxcol = getmaxx(win);
box(win, '|', '-');
// insert title
mvwaddstr(win, 1, (maxcol/2)-(title.size()/2), title.c_str());
for (int i = 1; i < maxcol-1; i++)
// add title underline
mvwaddch(win, 2, i, '-');

drawFields();
Expand All @@ -40,15 +43,17 @@ void Form::drawField(Field *field)
int x = field->x + 1;
int y = field->y + 3;

// if showName, display the name next to the field
if (field->showName)
{
boxSize -= field->name.size() + 1;
mvwaddstr(win, y+1, x, field->name.c_str());
waddch(win, ' ');
}

x = getcurx(win);
x = getcurx(win);

// draw field box
for (int i = 0; i < boxSize; i++)
{
char mid = i == 0 || i == boxSize-1 ? '|' : ' ';
Expand All @@ -61,12 +66,15 @@ void Form::drawField(Field *field)
}
}

// draw text if not empty
if (field->value.size() > 0)
{
int textSize = boxSize - 3;
char *textToDiplay = new char[textSize+1];
// if text is bigger than the field
if (textSize < field->value.size())
{
// tail text so that the it fits in the field
field->value.copy(
textToDiplay,
textSize,
Expand All @@ -76,11 +84,16 @@ void Form::drawField(Field *field)
}
else
{
// the whole string fits, so copy direct
field->value.copy(textToDiplay, field->value.size());
textToDiplay[field->value.size()] = '\0';
}
// insert tailed text
mvwaddstr(win, y+1, x+1, textToDiplay);

// free memory
delete textToDiplay;

// if field being draw is the current focused one, then add cursor
if (*currentField == field)
waddch(win, '_' | A_BLINK);
}
Expand All @@ -98,54 +111,69 @@ void Form::addField(Field *field)

void Form::show(FormCallback callback)
{
// set current field as the first field
currentField = fields.begin();
draw();

do
{
// get user input
int c = wgetch(win);
// pass user input to form callback
FormOp op = callback(c);

// process returned operation
if (op == NoOp)
{
// do nothing
continue;
}
else if (op == Push)
{
// add last inputted char to value of the current field
(*currentField)->value.push_back(c);
drawField(*currentField);
}
else if (op == Pop)
{
// if there is a value to delete from
if ((*currentField)->value.size() > 0)
{
// delete last char from value
(*currentField)->value.pop_back();
drawField(*currentField);
}
}
else if (op == NextField)
{
// if the next field in the iterator isn't the end
if (currentField+1 != fields.end())
{
// increment to next field
currentField++;
drawField(*(currentField-1));
drawField(*currentField);
}
}
else if (op == PrevField)
{
// if we aren't at the first field
if (currentField != fields.begin())
{
// decrement to previous field
currentField--;
drawField(*(currentField+1));
drawField(*currentField);
}
}
else if (op == SubmitForm)
{
// return value to calling function
break;
}
else if (op == CancelForm)
{
// erase all values and return to calling function
for (auto field : fields)
{
field->value.erase();
Expand Down
8 changes: 8 additions & 0 deletions lib/form.hpp
Expand Up @@ -10,6 +10,7 @@

namespace GrandMaze
{
// Form operations
enum FormOp
{
NoOp,
Expand All @@ -21,6 +22,8 @@ namespace GrandMaze
CancelForm,
};

// FormCallback is a callback function that takes an int
// and returns a FormOp
typedef std::function<FormOp (int)> FormCallback;

class Field
Expand Down Expand Up @@ -50,14 +53,19 @@ namespace GrandMaze
std::string title;
std::vector<Field*>::iterator currentField;

// Draw the form
void draw(void);
// Draw fields in the form
void drawFields(void);
// Draw a single field in the form
void drawField(Field *field);

public:
Form(WINDOW *win, std::string title);

// Add a new field to the form
void addField(Field *field);
// Display and get input from the form
void show(FormCallback callback);
};
};
Expand Down
1 change: 1 addition & 0 deletions lib/keys.hpp
Expand Up @@ -5,6 +5,7 @@

namespace GrandMaze
{
// User-friendly key names
enum Keys
{
Enter = 10,
Expand Down
3 changes: 3 additions & 0 deletions lib/map.cpp
Expand Up @@ -29,10 +29,12 @@ void Map::addRooms(std::initializer_list<Room*> rooms)

void Map::delRoom(Room *room)
{
// find room
auto elem = std::find(rooms.begin(), rooms.end(), room);
if (elem == rooms.end())
return;

// delete room
rooms.erase(elem);
}

Expand All @@ -46,6 +48,7 @@ void Map::delRooms(std::initializer_list<Room*> rooms)

void Map::draw(WINDOW *win)
{
// draw all rooms in map
for (Room *room : rooms)
{
room->draw(win);
Expand Down