Skip to content

Commit

Permalink
Add transmission handling + small fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
skapix committed Mar 28, 2022
1 parent 06db924 commit 627ec69
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 17 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(BUILD_GUI_APP "Build GUI Qt application" ON)
option(GUI_APP "Try to build GUI Qt application" ON)

if(MSVC)
# Force to always compile with W4
Expand All @@ -24,8 +24,8 @@ endif()
enable_testing()
add_subdirectory(lib)
add_subdirectory(cohbcalc)
if (BUILD_GUI_APP)
add_subdirectory(gohbcalc)
if(GUI_APP)
add_subdirectory(gohbcalc)
endif()

# uninstall target
Expand Down
8 changes: 8 additions & 0 deletions cohbcalc/ConsoleHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ void handleSpecialKey(const SpecialKey key)
case SpecialKey::Undefined:
// do nothing
break;
case SpecialKey::EndOfTransmission:
if (!g_currentExpression.empty()) {
// do nothing
break;
}
g_currentExpression = "exit"; // Should be one of exit sequence.
handleSpecialKey(SpecialKey::EndLine);
break;
}
}

Expand Down
5 changes: 3 additions & 2 deletions cohbcalc/ConsoleHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ enum class SpecialKey : char
End,
Delete,
BackSpace,
EndLine
EndLine,
EndOfTransmission,
};

using Character = std::variant<char, SpecialKey>;
Expand All @@ -32,4 +33,4 @@ void handleSpecialKey(const SpecialKey key);
void handleChar(char c);
// function must be called after readChar return '\n'
std::string getExpression();
void markError(size_t pos);
void markError(size_t pos);
6 changes: 4 additions & 2 deletions cohbcalc/UnixKeyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ char getch() { return getchar(); }
#define CHKEY_BACKSPACE 127
#define CHKEY_DELETE 27, 91, 51, 126
#define CHKEY_ENDLINE 10
#define CHKEY_END_OF_TRANSMISSION 4

constexpr const int ch_left[] = {CHKEY_LEFT};
constexpr const int ch_right[] = {CHKEY_RIGHT};
Expand All @@ -63,12 +64,13 @@ constexpr const int ch_end[] = {CHKEY_END};
constexpr const int ch_backspace[] = {CHKEY_BACKSPACE};
constexpr const int ch_delete[] = {CHKEY_DELETE};
constexpr const int ch_endline[] = {CHKEY_ENDLINE};
constexpr const int ch_endoftrans[] = {CHKEY_END_OF_TRANSMISSION};

#define PTRSIZE(constv) make_pair<const int *, int>(&*constv, sizeof(constv) / sizeof(constv[0]))
constexpr const pair<const int *, int> g_allSpecial[] = {
PTRSIZE(ch_left), PTRSIZE(ch_right), PTRSIZE(ch_up), PTRSIZE(ch_down), PTRSIZE(ch_ctrlleft),
PTRSIZE(ch_ctrlright), PTRSIZE(ch_pgup), PTRSIZE(ch_pgdown), PTRSIZE(ch_home), PTRSIZE(ch_end),
PTRSIZE(ch_backspace), PTRSIZE(ch_delete), PTRSIZE(ch_endline)};
PTRSIZE(ch_backspace), PTRSIZE(ch_delete), PTRSIZE(ch_endline), PTRSIZE(ch_endoftrans)};


struct CharComparator
Expand Down Expand Up @@ -96,7 +98,7 @@ const map<pair<const int *, int>, SpecialKey, CharComparator> g_sequence = {
MAPVALUE(ch_down, Down), MAPVALUE(ch_ctrlleft, CtrlLeft), MAPVALUE(ch_ctrlright, CtrlRight),
MAPVALUE(ch_pgup, PageUp), MAPVALUE(ch_pgdown, PageDown), MAPVALUE(ch_home, Home),
MAPVALUE(ch_end, End), MAPVALUE(ch_backspace, BackSpace), MAPVALUE(ch_delete, Delete),
MAPVALUE(ch_endline, EndLine)};
MAPVALUE(ch_endline, EndLine), MAPVALUE(ch_endoftrans, EndOfTransmission)};

#undef PTRSIZE
#undef MAPVALUE
Expand Down
4 changes: 2 additions & 2 deletions cohbcalc/cohbcalc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ string getLine()
{
SpecialKey key = get<SpecialKey>(c);
handleSpecialKey(key);
if (key == SpecialKey::EndLine)
if (key == SpecialKey::EndLine || key == SpecialKey::EndOfTransmission)
{
return getExpression();
}
Expand Down Expand Up @@ -121,4 +121,4 @@ int main(int argc, const char *argv[])
}

return 0;
}
}
9 changes: 7 additions & 2 deletions gohbcalc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ cmake_minimum_required(VERSION 3.0)

project(gohbcalc)

find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
find_package(Qt5 5.12.0 COMPONENTS Core Widgets)

if (NOT Qt5_FOUND)
message(WARNING "Qt5 not found. Skip building GUI application")
return()
endif()

set(CMAKE_AUTOMOC ON)

Expand All @@ -24,4 +29,4 @@ target_include_directories(gohbcalc PRIVATE ${CMAKE_SOURCE_DIR}/common)
target_link_libraries(gohbcalc ohbcalc)
target_link_libraries(gohbcalc Qt5::Core Qt5::Widgets)

install(TARGETS gohbcalc DESTINATION bin)
install(TARGETS gohbcalc DESTINATION bin)
3 changes: 1 addition & 2 deletions gohbcalc/CalculatorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ CalculatorWindow::CalculatorWindow()
layout->addWidget(m_binary);
layout->addStretch(-1);
setCentralWidget(centralWidget);

connect(m_editor, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::textActivated), this,
connect(m_editor, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated), this,
&CalculatorWindow::on_enterPressed);
m_decimal->setRepresentationFunction([](int64_t result) { return std::to_string(result); });
m_udecimal->setRepresentationFunction([](int64_t result) { return std::to_string(static_cast<uint64_t>(result)); });
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0)
project(ohbcalc)

set(public_include include/ohbTransform.h include/transformImpl/TransformImpl.h
include/ohbException.h include/ohbcalc.h)
include/ohbException.h include/ohbcalc.h)

set(sources source/OHBCalcLib.cpp source/OHBCalcImpl.cpp source/OHBCalcImpl.h
source/TokenOperation.cpp source/TokenOperation.h source/CommonDefines.h
Expand Down
5 changes: 3 additions & 2 deletions lib/include/ohbException.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <exception>

class OHBException : public std::exception
{
Expand All @@ -10,7 +11,7 @@ class OHBException : public std::exception
, message(message)
{}

virtual const char*what() const noexcept override
virtual const char* what() const noexcept override
{
return message.c_str();
}
Expand All @@ -28,4 +29,4 @@ class OHBException : public std::exception
private:
size_t pos;
std::string message;
};
};
2 changes: 1 addition & 1 deletion lib/source/OHBCalcImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ int64_t getValue(CStringView expression, size_t &pos)
return false;
};

auto isBin = [&localPos](CStringView &number)
auto isBin = [/*&localPos*/](CStringView &number)
{
if (number.length() > 1 && (lastIdentificator(number) == 'b' || lastIdentificator(number) == 'i'))
{
Expand Down

0 comments on commit 627ec69

Please sign in to comment.