Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c9f806d
Can use ":" but chat isn't good
BrandonMFong Jan 8, 2025
d2667a4
making a utils function that determines if the input is ready
BrandonMFong Jan 8, 2025
4b811e8
update todo
BrandonMFong Jan 8, 2025
8ba976d
update todo
BrandonMFong Jan 8, 2025
780a05b
clean up
BrandonMFong Jan 9, 2025
3a162d3
init operand that will handle the short and long version of a command's
BrandonMFong Jan 9, 2025
fa9540b
adding notes
BrandonMFong Jan 9, 2025
5ea71cd
adding simple constructors and compare ops
BrandonMFong Jan 20, 2025
2b14a81
stable implementation of Operand
BrandonMFong Jan 20, 2025
450d2e2
making sure we do not compare empty var
BrandonMFong Jan 20, 2025
72b08dc
testing utils
BrandonMFong Jan 22, 2025
5e4d7a2
adjusting makefile
BrandonMFong Jan 22, 2025
aa9924e
saving libs
BrandonMFong Jan 23, 2025
9bac2be
stable build but fails tests
BrandonMFong Jan 23, 2025
6c26aaa
stable but buggy
BrandonMFong Jan 24, 2025
273321a
can compare operands (test passed)
BrandonMFong Jan 24, 2025
c3228cc
adding todo
BrandonMFong Jan 24, 2025
be1d077
can use 'i' to draft a message
BrandonMFong Jan 24, 2025
aecb474
removing dereferencing
BrandonMFong Jan 24, 2025
4d84973
update todo
BrandonMFong Jan 24, 2025
44cba8f
adding footer for '?' for help
BrandonMFong Jan 24, 2025
88d15bd
update todo
BrandonMFong Jan 24, 2025
bcd57b3
updating libs ref
BrandonMFong Jan 24, 2025
c2bf474
update yaml
BrandonMFong Jan 24, 2025
166ae66
updating libs
BrandonMFong Jan 24, 2025
69b90a2
Update test.yml
BrandonMFong Jan 24, 2025
26d87fd
ping
BrandonMFong Jan 24, 2025
f714385
updating libs
BrandonMFong Jan 24, 2025
238a551
update todo
BrandonMFong Jan 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ jobs:
with:
submodules: recursive
token: ${{ secrets.PAT_TOKEN }}
- name: Install uuid-dev
run: |
sudo apt -y update;
sudo apt -y upgrade;
sudo apt -y install uuid-dev;
- name: building libs
run: |
cd external;
Expand Down
22 changes: 12 additions & 10 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ log user inputbuffer office \
chatroom message chatroomserver packet \
agent agentclient agentserver sealedpacket \
chatroomclient interfaceserver interfaceclient command \
permissions chat
permissions utils operand\
chat

UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
Expand All @@ -60,11 +61,16 @@ LIBRARIES = external/bin/openssl/libssl.a external/bin/openssl/libcrypto.a
OPENSSL_INCLUDE_PATH = -Iexternal/bin/openssl/include
endif

ifneq ($(CONFIG),test) # test
ifeq ($(CONFIG),release) # release
LIBRARIES += \
external/bin/libs/release/bflibc/libbfc.a \
external/bin/libs/release/bflibcpp/libbfcpp.a \
external/bin/libs/release/bfnet/libbfnet.a
else
LIBRARIES += \
external/bin/libs/$(CONFIG)/bflibc/libbfc.a \
external/bin/libs/$(CONFIG)/bflibcpp/libbfcpp.a \
external/bin/libs/$(CONFIG)/bfnet/libbfnet.a
external/bin/libs/debug/bflibc/libbfc-debug.a \
external/bin/libs/debug/bflibcpp/libbfcpp-debug.a \
external/bin/libs/debug/bfnet/libbfnet-debug.a
endif

LINKS = -lpthread -lncurses $(BF_LIB_C_FLAGS) -ldl
Expand All @@ -88,11 +94,7 @@ MAIN_FILE = testbench/tests.cpp
BIN_NAME = chat-test
#ADDR_SANITIZER = -fsanitize=address
FLAGS = $(CPPFLAGS) -DDEBUG -DTESTING -g -Isrc/ $(ADDR_SANITIZER) $(CPPSTD) -Iexternal/bin/libs/debug $(OPENSSL_INCLUDE_PATH)
LIBRARIES += \
external/bin/libs/debug/bflibc/libbfc-debug.a \
external/bin/libs/debug/bflibcpp/libbfcpp-debug.a \
external/bin/libs/debug/bfnet/libbfnet-debug.a \
external/bin/libs/debug/bftest/libbftest-debug.a
LIBRARIES += external/bin/libs/debug/bftest/libbftest-debug.a
endif # ($(CONFIG),...)

LIBS_MAKEFILES_PATH:=$(CURDIR)/external/libs/makefiles
Expand Down
10 changes: 8 additions & 2 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "command.hpp"
#include "inputbuffer.hpp"
#include "operand.hpp"
#include <bflibcpp/bflibcpp.hpp>

extern "C" {
Expand Down Expand Up @@ -32,8 +33,13 @@ Command::~Command() {

}

String Command::op() const {
return this->argumentAtIndex(0);
Operand Command::op() const {
String arg = this->argumentAtIndex(0);
if (arg.starts_with(":")) {
arg.remCharAtIndex(0);
}

return Operand({arg});
}

String Command::operator[](int i) const {
Expand Down
3 changes: 2 additions & 1 deletion src/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <bflibcpp/string.hpp>

class InputBuffer;
class Operand;

class Command : public BF::Object {
public:
Expand All @@ -22,7 +23,7 @@ class Command : public BF::Object {
*
* this is the first word in the buf
*/
BF::String op() const;
Operand op() const;

BF::String argumentAtIndex(int i) const;

Expand Down
14 changes: 6 additions & 8 deletions src/inputbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ using namespace BF;
InputBuffer::InputBuffer() : InputBuffer("") {}

InputBuffer::InputBuffer(const char * str) : String(str) {
this->_isready = false;
this->_enterPressed = false;
this->_cursorpos = 0;
}

InputBuffer::~InputBuffer() {

}
InputBuffer::~InputBuffer() { }

int InputBuffer::addChar(int ch) {
switch (ch) {
case '\n':
this->_isready = true;
this->_enterPressed = true;
break;
case KEY_BACKSPACE:
case 127:
Expand Down Expand Up @@ -55,15 +53,15 @@ int InputBuffer::addChar(int ch) {

int InputBuffer::reset() {
this->_cursorpos = 0;
this->_isready = false;
this->_enterPressed = false;
return this->String::clear();
}

size_t InputBuffer::cursorPosition() {
return this->_cursorpos;
}

bool InputBuffer::isready() {
return this->_isready;
bool InputBuffer::enterPressed() {
return this->_enterPressed;
}

4 changes: 2 additions & 2 deletions src/inputbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class InputBuffer : public BF::String {
/**
* when the buffer is ready to be sent
*/
bool isready();
bool enterPressed();

/**
* clears buffer and resets the cursor position
Expand All @@ -37,7 +37,7 @@ class InputBuffer : public BF::String {

private:

bool _isready;
bool _enterPressed;

/**
* current cursor position
Expand Down
35 changes: 20 additions & 15 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "agentclient.hpp"
#include "command.hpp"
#include "permissions.hpp"
#include "utils.hpp"
#include "operand.hpp"

extern "C" {
#include <bflibc/bflibc.h>
Expand Down Expand Up @@ -523,7 +525,11 @@ int Interface::windowUpdateInputWindowText(InputBuffer & userInput) {
BFLockLock(&this->_winlock);
werase(this->_inputWin);
if (this->_errorMessage.length() == 0) {
mvwprintw(this->_inputWin, 0, 0, userInput.cString());
if (userInput.length() == 0) {
mvwprintw(this->_inputWin, 0, 0, "-- '?' for help --");
} else {
mvwprintw(this->_inputWin, 0, 0, userInput.cString());
}
} else {
mvwprintw(this->_inputWin, 0, 0, this->_errorMessage.cString());
this->_errorMessage.clear();
Expand Down Expand Up @@ -604,15 +610,15 @@ Chatroom * _InterfaceGetChatroomAtIndex(int i) {
}

int Interface::processinputStateLobby(InputBuffer & userInput) {
if (userInput.isready()) {
if (Utils::inputReady(userInput, this->_state)) {
Command cmd(userInput);
if (!cmd.op().compareString(INTERFACE_COMMAND_QUIT)) { // quit
if (cmd.op() == OP_QUIT) { // quit
Office::quitApplication(this->_user.get());
this->_state = kInterfaceStateQuit;
} else if (!cmd.op().compareString(INTERFACE_COMMAND_HELP)) { // help
} else if (cmd.op() == OP_HELP) { // help
this->_returnfromhelpstate = this->_state;
this->_state = kInterfaceStateHelp;
} else if (!cmd.op().compareString(INTERFACE_COMMAND_CREATE)) { // create
} else if (cmd.op() == OP_CREATE) { // create
if (!Permissions::CanCreateChatroom()) {
String errmsg("not permitted: you are not allowd to create a chatroom");
this->setErrorMessage(errmsg);
Expand All @@ -632,7 +638,7 @@ int Interface::processinputStateLobby(InputBuffer & userInput) {
Chatroom * cr = ChatroomServer::create(chatroomname);
BFRelease(cr);
}
} else if (!cmd.op().compareString(INTERFACE_COMMAND_JOIN)) { // join
} else if (cmd.op() == OP_JOIN) { // join
int index = String::toi(cmd[1]) - 1;
if ((index >= 0) && (index < Chatroom::getChatroomsCount())) {
this->_chatroom = _InterfaceGetChatroomAtIndex(index);
Expand All @@ -646,7 +652,7 @@ int Interface::processinputStateLobby(InputBuffer & userInput) {
}
}
} else {
String errmsg("unknown command: %s", cmd.op().cString());
String errmsg("unknown command: '%s'", cmd.op().description().cString());
this->setErrorMessage(errmsg);
}
userInput.reset();
Expand All @@ -656,25 +662,25 @@ int Interface::processinputStateLobby(InputBuffer & userInput) {
}

int Interface::processinputStateChatroom(InputBuffer & userInput) {
if (userInput.isready()) {
if (Utils::inputReady(userInput, this->_state)) {
Command cmd(userInput);
if (!cmd.op().compareString(INTERFACE_COMMAND_LEAVE)) { // leave
if (cmd.op() == OP_LEAVE) { // leave
// tell chat room we are leaving
this->_chatroom.get()->resign(this->_user);

Object::release(this->_chatroom.get());
this->_chatroom = NULL;

this->_state = kInterfaceStateLobby;
} else if (!cmd.op().compareString(INTERFACE_COMMAND_HELP)) { // help
} else if (cmd.op() == OP_HELP) { // help
this->_returnfromhelpstate = this->_state;
this->_state = kInterfaceStateHelp;
} else if (!cmd.op().compareString(INTERFACE_COMMAND_DRAFT)) { // draft
} else if (cmd.op() == OP_DRAFT) { // draft
this->_state = kInterfaceStateDraft;
this->converstaionHasChanged();
} else {
String errmsg("unknown command: %s", cmd.op().cString());
this->setErrorMessage(*errmsg);
String errmsg("unknown command: '%s'", cmd.op().description().cString());
this->setErrorMessage(errmsg);
}

userInput.reset();
Expand All @@ -683,8 +689,7 @@ int Interface::processinputStateChatroom(InputBuffer & userInput) {
}

int Interface::processinputStateDraft(InputBuffer & userInput) {
if (userInput.isready()) {
// send buf
if (Utils::inputReady(userInput, this->_state)) { // send buf
this->_chatroom.get()->sendBuffer(userInput);

this->_state = kInterfaceStateChatroom;
Expand Down
67 changes: 67 additions & 0 deletions src/operand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* author: brando
* date: 1/9/25
*/

#include "operand.hpp"
#include <bflibcpp/bflibcpp.hpp>

extern "C" {
#include <bflibc/bflibc.h>
}

using namespace BF;

const Operand OP_HELP({"help", "?"});
const Operand OP_CREATE({"create"});
const Operand OP_JOIN({"join"});
const Operand OP_LEAVE({"leave"});
const Operand OP_DRAFT({"draft", "i"});
const Operand OP_QUIT({"quit", "q"});

void _OperandAcceptArgsRelease(char * a) {
BFFree(a);
}

int _OperandAcceptArgsCompare(char * a, char * b) {
return strcmp(a, b);
}

Operand::Operand(std::initializer_list<const char *> list) : Object() {
this->_acceptedArgs.setReleaseCallback(_OperandAcceptArgsRelease);
for (const char * arg : list) {
char * buf = BFStringCopyString(arg);
this->_acceptedArgs.add(buf);
}
this->_acceptedArgs.setComparator(_OperandAcceptArgsCompare);
}

Operand::~Operand() { }

bool Operand::compare(const Operand & op) {
// FIXME:
// this is a poor implementation. time efficiency will decrease
// as more accepted arguments are implmented. I suggested to use
// algorithms as presented here:https://stackoverflow.com/a/245521/12135693
//
// current implementation for BF::Array doesn't support the scope of those
// algorithms
for (int i = 0; i < this->_acceptedArgs.count(); i++) {
if (op._acceptedArgs.contains(this->_acceptedArgs[i]))
return true;
}
return false;
}

bool Operand::operator==(const Operand & op) {
return this->compare(op);
}

bool Operand::operator!=(const Operand & op) {
return !this->compare(op);
}

String Operand::description() const {
return String("%s", this->_acceptedArgs[0]);
}

41 changes: 41 additions & 0 deletions src/operand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* author: brando
* date: 1/9/25
*/

#ifndef OPERAND_HPP
#define OPERAND_HPP

#include <bflibcpp/object.hpp>
#include <bflibcpp/string.hpp>

/**
* I want the user to be able to pass '?' or ":help" in the
* command prompt to get the help menu
*/
class Operand : public BF::Object {
public:
Operand(std::initializer_list<const char *> list);
virtual ~Operand();

bool compare(const Operand & op);

BF::String description() const;

private:
BF::Array<char *> _acceptedArgs;

public:
bool operator==(const Operand & op);
bool operator!=(const Operand & op);
};

extern const Operand OP_HELP;
extern const Operand OP_CREATE;
extern const Operand OP_JOIN;
extern const Operand OP_LEAVE;
extern const Operand OP_DRAFT;
extern const Operand OP_QUIT;

#endif // OPERAND_HPP

19 changes: 19 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* author: brando
* date: 1/7/24
*/

#include "utils.hpp"
#include "inputbuffer.hpp"
#include "interface.hpp"

bool Utils::inputReady(InputBuffer & buf, InterfaceState state) {
return
buf.starts_with(":") && (buf.length() > 1) && buf.enterPressed()
||
!buf.starts_with(":") && (buf.length() == 1) && (state != kInterfaceStateDraft)
||
(state == kInterfaceStateDraft) && buf.enterPressed();
;
}

Loading
Loading