# HG changeset patch # User Teemu Piippo # Date 1437605524 -10800 # Node ID 08ccaf26cffdbec6ae7b1bba5a604d97ca273e78 # Parent 53c2aecb9704c46b4bb7a9c5603497ed87c481bc Now works with MSVC 2010/pdcurses-win32a diff -r 53c2aecb9704 -r 08ccaf26cffd CMakeLists.txt --- a/CMakeLists.txt Thu Jul 23 00:16:47 2015 +0300 +++ b/CMakeLists.txt Thu Jul 23 01:52:04 2015 +0300 @@ -54,14 +54,19 @@ target_link_libraries (${TARGET_NAME} huffman) if (WIN32) - add_definitions ("/Za /D_CRT_SEURE_NO_WARNINGS") + add_definitions ("-D_CRT_SEURE_NO_WARNINGS") + target_link_libraries (${TARGET_NAME} wsock32 ws2_32) + + if (PDCURSES_WIN32A_PATH) + include_directories (${PDCURSES_WIN32A_PATH}/include) - if (NOT PDCURSES_WIN32A_PATH) + if (MINGW) + target_link_libraries (${TARGET_NAME} ${PDCURSES_WIN32A_PATH}/lib/pdcurses.a) + else() + target_link_libraries (${TARGET_NAME} ${PDCURSES_WIN32A_PATH}/lib/pdcurses.lib) + endif() + else() message (SEND_ERROR "Must give PDCURSES_WIN32A_PATH on Windows") - else() - include_directories (${PDCURSES_WIN32A_PATH}/include) - target_link_libraries (${TARGET_NAME} ${PDCURSES_WIN32A_PATH}/lib/pdcurses.a) - target_link_libraries (${TARGET_NAME} wsock32 ws2_32) endif() else() include_directories (${CURSES_INCUDE_DIRS}) # sic diff -r 53c2aecb9704 -r 08ccaf26cffd sources/basics.h --- a/sources/basics.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/basics.h Thu Jul 23 01:52:04 2015 +0300 @@ -29,13 +29,6 @@ */ #pragma once -#include -#include -#include -#include -#include -#include - #if !defined(_MSC_VER) && !defined(__cdecl) # define __cdecl #endif @@ -43,10 +36,12 @@ #define MACRO_TO_STRING_2(A) #A #define MACRO_TO_STRING(A) MACRO_TO_STRING_2(A) +#define BEGIN_ZFC_NAMESPACE namespace zfc { +#define END_ZFC_NAMESPACE } + +BEGIN_ZFC_NAMESPACE + class String; -using std::swap; -using std::min; -using std::max; // ------------------------------------------------------------------------------------------------- // @@ -65,6 +60,14 @@ NUM_COLORS }; +// Goddamnit, MSVC +#ifdef _MSC_VER +# define and && +# define or || +# define not ! +# define _CRT_SECURE_NO_WARNINGS +#endif + #define TEXTCOLOR_Escape "\x1C" #define TEXTCOLOR_Black TEXTCOLOR_Escape "M" #define TEXTCOLOR_Gray TEXTCOLOR_Escape "U" @@ -82,14 +85,23 @@ #define TEXTCOLOR_BrightCyan TEXTCOLOR_Escape "V" #define TEXTCOLOR_Reset TEXTCOLOR_Escape "-" -// ------------------------------------------------------------------------------------------------- -// +template +T min (T a, T b) +{ + return (a < b) ? b : a; +} + template -inline T clamp (T a, T b, T c) +T max (T a, T b) +{ + return (a > b) ? b : a; +} + +template +T clamp (T a, T b, T c) { return (a < b) ? b : (a > c) ? c : a; } -// ------------------------------------------------------------------------------------------------- -// struct Exitception {}; +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/coloredline.cpp --- a/sources/coloredline.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/coloredline.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -29,6 +29,7 @@ */ #include "coloredline.h" +BEGIN_ZFC_NAMESPACE struct ColorCodeInfo { @@ -37,6 +38,13 @@ bool bold; }; +ColoredLine::ColoredLine() : + m_length (0), + m_final (false), + m_activeColor (DEFAULT), + m_boldActive (false), + m_colorCodeStage (0) {} + // ------------------------------------------------------------------------------------------------- // void ColoredLine::finalize() @@ -156,3 +164,5 @@ return max (rows, 1); } + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/coloredline.h --- a/sources/coloredline.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/coloredline.h Thu Jul 23 01:52:04 2015 +0300 @@ -30,6 +30,7 @@ #pragma once #include "main.h" +BEGIN_ZFC_NAMESPACE // ------------------------------------------------------------------------------------------------- // @@ -60,7 +61,7 @@ class ColoredLine { public: - ColoredLine() {} + ColoredLine(); const Vector& data() const { return m_data; } int length() const { return m_length; } @@ -72,10 +73,12 @@ void set_color (Color a, bool on); Vector m_data; - int m_length = 0; - bool m_final = false; - Color m_activeColor = DEFAULT; - bool m_boldActive = false; - int m_colorCodeStage = 0; + int m_length; + bool m_final; + Color m_activeColor; + bool m_boldActive; + int m_colorCodeStage; String m_string; }; + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/geometry.h --- a/sources/geometry.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/geometry.h Thu Jul 23 01:52:04 2015 +0300 @@ -29,6 +29,8 @@ */ #pragma once +#include "basics.h" +BEGIN_ZFC_NAMESPACE struct Position { @@ -113,3 +115,5 @@ Position(), Size() {} }; + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/interface.cpp --- a/sources/interface.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/interface.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -28,12 +28,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include "interface.h" #include "network/rconsession.h" #include "network/ipaddress.h" #include "coloredline.h" +BEGIN_ZFC_NAMESPACE static const int g_pageSize = 10; @@ -127,7 +129,7 @@ { case INPUTSTATE_ADDRESS: if (CurrentAddress.host != 0) - mutable_current_input() = CurrentAddress.to_string (IP_WITH_PORT); + mutable_current_input() = CurrentAddress.to_string (IPAddress::WITH_PORT); break; default: @@ -141,7 +143,18 @@ // ------------------------------------------------------------------------------------------------- // Interface::Interface() : - Session (this) + Session (this), + InputCursor (0), + CursorPosition (0), + InputPanning (0), + NeedRefresh (false), + NeedStatusBarRender (false), + NeedInputRender (false), + NeedOutputRender (false), + NeedNicklistRender (false), + OutputScroll (0), + CurrentInputState (INPUTSTATE_NORMAL), + DisconnectConfirmFunction (NULL) { #ifdef XCURSES ::Xinitscr(argc, argv); @@ -219,7 +232,7 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::safe_disconnect (Function afterwards) +void Interface::safe_disconnect (std::function afterwards) { if (Session.is_active()) { @@ -252,8 +265,10 @@ { int x = x0; - for (int byte : line.data()) + for (int i = 0; i < line.length(); ++i) { + int byte = line.data()[i]; + if (x == x0 + width) { if (not allowWrap) @@ -443,8 +458,8 @@ // If we're inputting a password, replace it with asterisks if (CurrentInputState == INPUTSTATE_PASSWORD) { - for (char& ch : displayString) - ch = '*'; + for (int i = 0; i < displayString.length(); ++i) + displayString[i] = '*'; } // Ensure the cursor is within bounds @@ -506,7 +521,7 @@ case RCON_CONNECTING: case RCON_AUTHENTICATING: - text = "Connecting to " + Session.address().to_string (IP_WITH_PORT) + "..."; + text = "Connecting to " + Session.address().to_string (IPAddress::WITH_PORT) + "..."; break; case RCON_CONNECTED: @@ -523,8 +538,10 @@ Session.num_admins() != 1 ? "s" : ""); } - text.sprintf ("%s | %s | %s", Session.address().to_string (IP_WITH_PORT).chars(), - Session.level().chars(), adminText.chars()); + text.sprintf ("%s | %s | %s", + Session.address().to_string (IPAddress::WITH_PORT).chars(), + Session.level().chars(), + adminText.chars()); } break; } @@ -950,14 +967,16 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::print_to_console (String a) +void Interface::print_to_console (String message) { // Zandronum sometimes sends color codes as "\\c" and sometimes as "\x1C". // Let's correct that on our end and hope this won't cause conflicts. - a.replace ("\\c", "\x1C"); + message.replace ("\\c", "\x1C"); - for (char ch : a) + for (int i = 0; i < message.length(); ++i) { + char ch = message[i]; + if (ch == '\n') { OutputLines.last().finalize(); @@ -1028,3 +1047,5 @@ NeedInputRender = true; } } + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/interface.h --- a/sources/interface.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/interface.h Thu Jul 23 01:52:04 2015 +0300 @@ -33,6 +33,7 @@ #include "network/ipaddress.h" #include "coloredline.h" #include "network/rconsession.h" +BEGIN_ZFC_NAMESPACE class Interface { @@ -64,20 +65,20 @@ private: StringList InputHistory; - int InputCursor = 0; - int CursorPosition = 0; - int InputPanning = 0; - bool NeedRefresh = false; - bool NeedStatusBarRender = false; - bool NeedInputRender = false; - bool NeedOutputRender = false; - bool NeedNicklistRender = false; + int InputCursor; + int CursorPosition; + int InputPanning; + bool NeedRefresh; + bool NeedStatusBarRender; + bool NeedInputRender; + bool NeedOutputRender; + bool NeedNicklistRender; struct { char ch; int x; } CursorCharacter; Vector OutputLines; - int OutputScroll = 0; + int OutputScroll; String Title; - InputState CurrentInputState = INPUTSTATE_NORMAL; - Function DisconnectConfirmFunction = nullptr; + InputState CurrentInputState; + std::function DisconnectConfirmFunction; IPAddress CurrentAddress; String StatusBarText; StringList PlayerNames; @@ -85,7 +86,7 @@ RCONSession Session; void render_titlebar(); - void safe_disconnect (Function afterwards); + void safe_disconnect (std::function afterwards); int render_colorline (int y, int x0, int width, const ColoredLine& line, bool allowWrap); int nicklist_width(); void render_output(); @@ -100,8 +101,10 @@ void move_input_cursor (int delta); String prompt_string(); void set_input_state (InputState newstate); - void print_to_console (String a); + void print_to_console (String message); void yank (int a, int b); int find_previous_word(); int find_next_word(); }; + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/list.h --- a/sources/list.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/list.h Thu Jul 23 01:52:04 2015 +0300 @@ -36,6 +36,7 @@ #include #include #include "range.h" +BEGIN_ZFC_NAMESPACE // ------------------------------------------------------------------------------------------------- // @@ -43,11 +44,11 @@ class Container { public: - using Iterator = typename C::iterator; - using ConstIterator = typename C::const_iterator; - using ReverseIterator = typename C::reverse_iterator; - using ConstReverseIterator = typename C::const_reverse_iterator; - using Self = Container; + typedef typename C::iterator Iterator; + typedef typename C::const_iterator ConstIterator; + typedef typename C::reverse_iterator ReverseIterator; + typedef typename C::const_reverse_iterator ConstReverseIterator; + typedef Container Self; Container(){} @@ -70,7 +71,7 @@ ConstIterator begin() const { - return m_container.cbegin(); + return m_container.begin(); } void clear() @@ -80,17 +81,17 @@ bool contains (const T& a) const { - return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end(); + return std::find (m_container.begin(), m_container.end(), a) != m_container.end(); } ConstReverseIterator crbegin() const { - return m_container.crbegin(); + return m_container.rbegin(); } ConstReverseIterator crend() const { - return m_container.crbegin(); + return m_container.rend(); } const C& container() const @@ -105,7 +106,7 @@ ConstIterator end() const { - return m_container.cend(); + return m_container.end(); } Iterator find (const T& needle) @@ -120,15 +121,15 @@ ConstIterator find (const T& needle) const { - auto it = std::find (m_container.cbegin(), m_container.cend(), needle); + auto it = std::find (m_container.begin(), m_container.end(), needle); - if (it == m_container.cend()) + if (it == m_container.end()) return end(); return it; } - Iterator find (Function func) + Iterator find (bool (*func)(T const&)) { for (Iterator it = begin(); it != end(); ++it) { @@ -139,7 +140,7 @@ return end(); } - ConstIterator find (Function func) const + ConstIterator find (bool (*func)(T const&)) const { for (ConstIterator it = begin(); it != end(); ++it) { @@ -369,3 +370,5 @@ return data(); } }; + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/main.cpp --- a/sources/main.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/main.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -43,8 +43,7 @@ // ------------------------------------------------------------------------------------------------- // -FUNCTION -main (int argc, char* argv[]) -> int +int main (int argc, char* argv[]) { #ifdef _WIN32 FreeConsole(); @@ -67,7 +66,7 @@ return EXIT_FAILURE; } - Interface iface; + zfc::Interface iface; if (argc == 3) iface.connect (argv[1], argv[2]); @@ -100,7 +99,7 @@ iface.render(); } } - catch (const Exitception&) {} + catch (const zfc::Exitception&) {} iface.get_session()->disconnect(); return EXIT_SUCCESS; diff -r 53c2aecb9704 -r 08ccaf26cffd sources/main.h --- a/sources/main.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/main.h Thu Jul 23 01:52:04 2015 +0300 @@ -29,8 +29,14 @@ */ #pragma once -#include +#include +#include +#include +#include +#include +#include +#include #include "basics.h" #include "mystring.h" #include "geometry.h" -#include "version.h" +#include "version.h" \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/md5.cpp --- a/sources/md5.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/md5.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -1,6 +1,7 @@ #include "md5.h" #include #include +BEGIN_ZFC_NAMESPACE /* * This code implements the MD5 message-digest algorithm. @@ -305,3 +306,4 @@ } } +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/md5.h --- a/sources/md5.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/md5.h Thu Jul 23 01:52:04 2015 +0300 @@ -1,2 +1,7 @@ #pragma once +#include "basics.h" +BEGIN_ZFC_NAMESPACE + void CalculateMD5 (unsigned char const *buffer, int length, char *checksum); + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/mystring.cpp --- a/sources/mystring.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/mystring.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -33,6 +33,8 @@ #include "mystring.h" #include "md5.h" +BEGIN_ZFC_NAMESPACE + // ------------------------------------------------------------------------------------------------- // int String::compare (const String& other) const @@ -54,45 +56,47 @@ // String String::strip (const List& unwanted) { - String copy (m_string); + String result (m_string); - for (char c : unwanted) + for (int i = 0; i < unwanted.size(); ++i) { - for (int pos = 0; (pos = copy.find (String (c))) != -1;) - copy.remove_at (pos--); + String c = unwanted[i]; + + for (int pos = 0; (pos = result.find (c)) != -1;) + result.remove_at (pos--); } - return copy; + return result; } // ------------------------------------------------------------------------------------------------- // String String::to_uppercase() const { - String newstr (m_string); + String result (m_string); - for (char& c : newstr) + for (int i = 0; i < result.length(); ++i) { - if (c >= 'a' and c <= 'z') - c -= 'a' - 'A'; + if (islower (result[i])) + result[i] -= 'a' - 'A'; } - return newstr; + return result; } // ------------------------------------------------------------------------------------------------- // String String::to_lowercase() const { - String newstr (m_string); + String result (m_string); - for (char& c : newstr) + for (int i = 0; i < result.length(); ++i) { - if (c >= 'A' and c <= 'Z') - c += 'a' - 'A'; + if (isupper (result[i])) + result[i] += 'a' - 'A'; } - return newstr; + return result; } // ------------------------------------------------------------------------------------------------- @@ -144,13 +148,15 @@ // int String::count (char needle) const { - int needles = 0; + int result = 0; - for (const char & c : m_string) - if (c == needle) - needles++; + for (int i = 0; i < length(); ++i) + { + if (m_string[i] == needle) + result++; + } - return needles; + return result; } // ------------------------------------------------------------------------------------------------- @@ -164,7 +170,7 @@ return ""; if (b < a) - swap (a, b); + std::swap (a, b); if (a == 0 and b == length()) return *this; @@ -240,7 +246,7 @@ { errno = 0; char* endptr; - float i = strtof (chars(), &endptr); + float i = (float) strtod (chars(), &endptr); if (ok != nullptr) *ok = (errno == 0 and *endptr == '\0'); @@ -345,12 +351,12 @@ { String result; - for (const String& it : container()) + for (int i = 0; i < size(); ++i) { if (result.is_empty() == false) result += delim; - result += it; + result += container()[i]; } return result; @@ -495,3 +501,5 @@ else if (a != 0 or b != length() - 1) m_string = m_string.substr (a, b - a + 1); } + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/mystring.h --- a/sources/mystring.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/mystring.h Thu Jul 23 01:52:04 2015 +0300 @@ -34,6 +34,7 @@ #include #include "basics.h" #include "list.h" +BEGIN_ZFC_NAMESPACE class String; class StringList; @@ -45,8 +46,11 @@ public: String() {} - explicit String (char a) : - m_string ({ a, '\0' }) {} + String (char a) + { + char buffer[2] = { a, '0' }; + m_string = buffer; + } String (const char* data) : m_string (data) {} @@ -57,8 +61,8 @@ String (const Vector& data) : m_string (data.data(), data.size()) {} - using Iterator = std::string::iterator; - using ConstIterator = std::string::const_iterator; + typedef std::string::iterator Iterator; + typedef std::string::const_iterator ConstIterator; ConstIterator begin() const { return m_string.cbegin(); } int compare (const String &other) const; @@ -106,8 +110,7 @@ void __cdecl sprintf (const char* fmtstr, ...); void vsprintf (const char* fmtstr, va_list args); bool starts_with (const String &other); - String strip (char unwanted) { return strip ({unwanted}); } - String strip (const List &unwanted); + String strip (const List& unwanted); void trim (int n); static String from_number (short int a); @@ -125,6 +128,8 @@ String& operator+= (const char* data) { append (data); return *this; } String& operator+= (int num) { return operator+= (String::from_number (num)); } String& operator+= (char data) { append (data); return *this; } + char& operator[] (int i) { return m_string[i]; } + char operator[] (int i) const { return m_string[i]; } bool operator== (const String& other) const { return std_string() == other.std_string(); } bool operator== (const char* other) const { return m_string == other; } bool operator!= (const String& other) const { return std_string() != other.std_string(); } @@ -171,3 +176,5 @@ { return String (a) + b; } + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/bytestream.cpp --- a/sources/network/bytestream.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/bytestream.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -30,6 +30,7 @@ #include "bytestream.h" #include +BEGIN_ZFC_NAMESPACE // ------------------------------------------------------------------------------------------------- // @@ -72,8 +73,7 @@ } // ------------------------------------------------------------------------------------------------- -METHOD -Bytestream::operator= (const Bytestream& other) -> Bytestream& +Bytestream& Bytestream::operator= (const Bytestream& other) { init (other.data(), other.written_length()); return *this; @@ -81,8 +81,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::resize (unsigned long newsize) -> void +void Bytestream::resize (unsigned long newsize) { Vector olddata; unsigned long oldsize = 0L; @@ -104,8 +103,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::init (const unsigned char* data, unsigned long length) -> void +void Bytestream::init (const unsigned char* data, unsigned long length) { resize (length); memcpy (m_data, data, length); @@ -115,8 +113,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::clear() -> void +void Bytestream::clear() { m_cursor = &m_data[0]; m_writtenLength = 0; @@ -124,8 +121,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::ensure_read_space (unsigned int bytes) -> void +void Bytestream::ensure_read_space (unsigned int bytes) { if (bytes_left() < bytes) { @@ -140,8 +136,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::read_byte() -> char +char Bytestream::read_byte() { ensure_read_space (1); return *m_cursor++; @@ -149,8 +144,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::read_short() -> short int +short int Bytestream::read_short() { ensure_read_space (2); short int result = 0; @@ -164,8 +158,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::read_long() -> long int +long int Bytestream::read_long() { ensure_read_space (4); long int result = 0; @@ -179,8 +172,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::read_float() -> float +float Bytestream::read_float() { float value; int intvalue = read_long(); @@ -190,8 +182,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::read_string() -> String +String Bytestream::read_string() { // Zandronum sends strings of maximum 2048 characters, though it only // reads 2047-character long ones so I guess we can follow up and do @@ -224,8 +215,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::read (unsigned char* buffer, unsigned long length) -> void +void Bytestream::read (unsigned char* buffer, unsigned long length) { ensure_read_space (length); memcpy (buffer, m_cursor, length); @@ -234,8 +224,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write (unsigned char val) -> void +void Bytestream::write (unsigned char val) { *m_cursor++ = val; m_writtenLength++; @@ -243,8 +232,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write (const unsigned char* val, unsigned int length) -> void +void Bytestream::write (const unsigned char* val, unsigned int length) { grow_to_fit (length); memcpy (m_cursor, val, length); @@ -254,8 +242,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::grow_to_fit (unsigned long bytes) -> void +void Bytestream::grow_to_fit (unsigned long bytes) { if (space_left() < bytes) resize (allocated_size() + bytes + 128); @@ -263,8 +250,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write_byte (char val) -> void +void Bytestream::write_byte (char val) { grow_to_fit (1); write (val); @@ -272,8 +258,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write_short (short int val) -> void +void Bytestream::write_short (short int val) { grow_to_fit (2); @@ -283,8 +268,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write_long (long int val) -> void +void Bytestream::write_long (long int val) { grow_to_fit (4); @@ -294,8 +278,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write_float (float val) -> void +void Bytestream::write_float (float val) { // I know this is probably dangerous but this is what Zandronum does so yeah int intvalue; @@ -305,8 +288,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write_string (const String& val) -> void +void Bytestream::write_string (const String& val) { grow_to_fit (val.length() + 1); write (reinterpret_cast (val.chars()), val.length()); @@ -315,8 +297,9 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -Bytestream::write_buffer (const Bytestream& other) -> void +void Bytestream::write_buffer (const Bytestream& other) { write (other.data(), other.written_length()); } + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/bytestream.h --- a/sources/network/bytestream.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/bytestream.h Thu Jul 23 01:52:04 2015 +0300 @@ -31,10 +31,14 @@ #pragma once #include #include "../main.h" +BEGIN_ZFC_NAMESPACE class String; -enum { MAX_NETWORK_STRING = 0x800 }; +enum +{ + MAX_NETWORK_STRING = 0x800 +}; // TODO: Make able to handle big-endian too class Bytestream @@ -48,10 +52,9 @@ IOError (String message) : m_message (message) {} - inline METHOD - what() const throw() -> const char* + const char* what() const throw() { - return m_message.chars(); + return m_message; } }; @@ -61,37 +64,80 @@ Bytestream (const Bytestream& other); ~Bytestream(); - inline METHOD allocated_size() const -> unsigned long; - inline METHOD bytes_left() const -> unsigned long; - METHOD clear() -> void; - inline METHOD data() -> unsigned char*; - inline METHOD data() const -> const unsigned char*; - METHOD grow_to_fit (unsigned long bytes) -> void; - inline METHOD position() const -> unsigned long; - METHOD read (unsigned char* buffer, unsigned long length) -> void; - METHOD read_byte() -> char; - METHOD read_short() -> short int; - METHOD read_long() -> long int; - METHOD read_string() -> String; - METHOD read_float() -> float; - METHOD resize (unsigned long length) -> void; - inline METHOD rewind() -> void; - inline METHOD seek (unsigned long pos) -> void; - inline METHOD to_vector() const -> Vector; - METHOD write (const unsigned char* val, unsigned int length) -> void; - METHOD write_buffer (const Bytestream& other) -> void; - METHOD write_buffer (const Vector& other) -> void; - METHOD write_byte (char val) -> void; - METHOD write_double (double val) -> void; - METHOD write_float (float val) -> void; - METHOD write_long (long int val) -> void; - METHOD write_short (short int val) -> void; - METHOD write_string (const String& val) -> void; - inline METHOD written_length() const -> unsigned long; + void clear(); + void grow_to_fit (unsigned long bytes); + void read (unsigned char* buffer, unsigned long length); + char read_byte(); + short int read_short(); + long int read_long(); + String read_string(); + float read_float(); + void resize (unsigned long length); + void write (const unsigned char* val, unsigned int length); + void write_buffer (const Bytestream& other); + void write_buffer (const Vector& other); + void write_byte (char val); + void write_double (double val); + void write_float (float val); + void write_long (long int val); + void write_short (short int val); + void write_string (const String& val); + + Bytestream& operator= (const Bytestream& other); + + unsigned long allocated_size() const + { + return m_allocatedSize; + } + + unsigned long bytes_left() const + { + return m_writtenLength - (m_cursor - &m_data[0]); + } - inline METHOD operator[] (unsigned long idx) -> unsigned char&; - inline METHOD operator[] (unsigned long idx) const -> unsigned char; - METHOD operator= (const Bytestream& other) -> Bytestream&; + inline unsigned char* data() + { + return m_data; + } + inline const unsigned char* data() const + { + return m_data; + } + + unsigned long position() const + { + return m_cursor - m_data; + } + + void rewind() + { + m_cursor = m_data; + } + + void seek (unsigned long pos) + { + m_cursor = m_data + pos; + } + + Vector to_vector() const + { + return Vector (m_data, m_writtenLength); + } + + unsigned long written_length() const + { + return m_writtenLength; + } + + unsigned char& operator[] (unsigned long idx) + { + return m_data[idx]; + } + + unsigned char operator[] (unsigned long idx) const + { + return m_data[idx]; + } private: unsigned char* m_data; @@ -99,104 +145,14 @@ unsigned long m_allocatedSize; unsigned long m_writtenLength; - METHOD init (const unsigned char* data, unsigned long length) -> void; - METHOD write (unsigned char val) -> void; - METHOD ensure_read_space (unsigned int bytes) -> void; - inline METHOD space_left() const -> unsigned long; + void init (const unsigned char* data, unsigned long length); + void write (unsigned char val); + void ensure_read_space (unsigned int bytes); + + unsigned long space_left() const + { + return m_allocatedSize - m_writtenLength; + } }; -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::allocated_size() const -> unsigned long -{ - return m_allocatedSize; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::written_length() const -> unsigned long -{ - return m_writtenLength; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::operator[] (unsigned long idx) -> unsigned char& -{ - return m_data[idx]; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::operator[] (unsigned long idx) const -> unsigned char -{ - return m_data[idx]; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::position() const -> unsigned long -{ - return m_cursor - m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::seek (unsigned long pos) -> void -{ - m_cursor = m_data + pos; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::rewind() -> void -{ - m_cursor = m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::bytes_left() const -> unsigned long -{ - return (m_writtenLength - (m_cursor - &m_data[0])); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::space_left() const -> unsigned long -{ - return (m_allocatedSize - m_writtenLength); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::data() -> unsigned char* -{ - return m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::data() const -> const unsigned char* -{ - return m_data; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -Bytestream::to_vector() const -> Vector -{ - return Vector (m_data, m_writtenLength); -} +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/ipaddress.cpp --- a/sources/network/ipaddress.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/ipaddress.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -38,15 +38,12 @@ # include #endif -#include -#include -#include -#include +BEGIN_ZFC_NAMESPACE #ifdef _WIN32 -using AddrInfo = ADDRINFOA; +typedef ADDRINFOA AddrInfo; #else -using AddrInfo = struct addrinfo; +typedef struct addrinfo AddrInfo; #endif // ----------------------------------------------------------------------------- @@ -69,12 +66,11 @@ // ----------------------------------------------------------------------------- // -METHOD -IPAddress::to_string (WithPort withport) const -> String +String IPAddress::to_string (WithPort withport) const { String val; - if (withport == IP_WITH_PORT) + if (withport == WITH_PORT) val.sprintf ("%u.%u.%u.%u:%u", octet (0), octet (1), octet (2), octet (3), port); else val.sprintf ("%u.%u.%u.%u", octet (0), octet (1), octet (2), octet (3)); @@ -84,16 +80,14 @@ // ----------------------------------------------------------------------------- // -METHOD -IPAddress::octet (int n) const -> unsigned char +unsigned char IPAddress::octet (int n) const { return (host >> ((3 - n) * 8)) & 0xFF; } // ----------------------------------------------------------------------------- // -METHOD -IPAddress::set_octet (int n, unsigned char oct) -> void +void IPAddress::set_octet (int n, unsigned char oct) { // TODO: make a big-endian version host &= ~(0xFF << (3 - n) * 8); @@ -102,8 +96,7 @@ // ----------------------------------------------------------------------------- // -METHOD -IPAddress::compare (const IPAddress& other) const -> bool +bool IPAddress::compare (const IPAddress& other) const { for (int i = 0; i < 4; ++i) { @@ -123,8 +116,7 @@ // ----------------------------------------------------------------------------- // -METHOD -IPAddress::operator< (const IPAddress& other) const -> bool +bool IPAddress::operator< (const IPAddress& other) const { for (int i = 0; i < 4; ++i) { @@ -137,8 +129,7 @@ // ----------------------------------------------------------------------------- // -METHOD -IPAddress::to_sockaddr_in() const -> sockaddr_in +sockaddr_in IPAddress::to_sockaddr_in() const { sockaddr_in claddr; memset (&claddr, 0, sizeof claddr); @@ -150,8 +141,7 @@ // ----------------------------------------------------------------------------- // -STATIC METHOD -IPAddress::from_string (String input) -> IPAddress +IPAddress IPAddress::from_string (String input) { unsigned int parts[4]; int colonpos = input.find (":"); @@ -171,15 +161,14 @@ } if (colonpos != -1) - value.port = input.mid (colonpos + 1, -1).to_int(); + value.port = (unsigned short) input.mid (colonpos + 1, -1).to_int(); return value; } // ----------------------------------------------------------------------------- // -STATIC METHOD -IPAddress::resolve (String node) -> IPAddress +IPAddress IPAddress::resolve (String node) { AddrInfo hints; AddrInfo* lookup; @@ -197,3 +186,5 @@ freeaddrinfo (lookup); return result; } + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/ipaddress.h --- a/sources/network/ipaddress.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/ipaddress.h Thu Jul 23 01:52:04 2015 +0300 @@ -34,10 +34,21 @@ struct sockaddr; struct sockaddr_in; -enum WithPort { IP_WITH_PORT, IP_NO_PORT }; +BEGIN_ZFC_NAMESPACE struct IPAddress { + enum + { + LOCALHOST = 0x7f000001 + }; + + enum WithPort + { + WITH_PORT, + NO_PORT + }; + class StringParseError : public std::exception { String m_message; @@ -46,8 +57,7 @@ StringParseError (String message) : m_message (message) {} - inline METHOD - what() const throw() -> const char* + const char* what() const throw() { return m_message.chars(); } @@ -60,36 +70,18 @@ IPAddress (unsigned long host, unsigned short port); IPAddress (const IPAddress& other); - METHOD compare (const IPAddress& other) const -> bool; - METHOD octet (int n) const -> unsigned char; - METHOD set_octet (int n, unsigned char oct) -> void; - METHOD to_string (WithPort withport = IP_NO_PORT) const -> String; - METHOD to_sockaddr_in() const -> sockaddr_in; - METHOD operator< (const IPAddress& other) const -> bool; - inline METHOD operator== (const IPAddress& other) const -> bool; - inline METHOD operator!= (const IPAddress& other) const -> bool; - inline METHOD operator[] (int n) const -> unsigned char; + bool compare (const IPAddress& other) const; + unsigned char octet (int n) const; + void set_octet (int n, unsigned char oct); + String to_string (WithPort withport = NO_PORT) const; + sockaddr_in to_sockaddr_in() const; + bool operator< (const IPAddress& other) const; + bool operator== (const IPAddress& other) const { return compare (other); } + bool operator!= (const IPAddress& other) const { return not compare (other); } + unsigned char operator[] (int n) const { return octet (n); } - static METHOD from_string (String input) -> IPAddress; - static METHOD resolve (String node) -> IPAddress; + static IPAddress from_string (String input); + static IPAddress resolve (String node); }; -static const unsigned long localhost = 0x7F000001; - -inline METHOD -IPAddress::operator[] (int n) const -> unsigned char -{ - return octet (n); -} - -inline METHOD -IPAddress::operator== (const IPAddress& other) const -> bool -{ - return compare (other); -} - -inline METHOD -IPAddress::operator!= (const IPAddress& other) const -> bool -{ - return not operator== (other); -} +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/rconsession.cpp --- a/sources/network/rconsession.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/rconsession.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -31,6 +31,7 @@ #include #include "rconsession.h" #include "../interface.h" +BEGIN_ZFC_NAMESPACE // ------------------------------------------------------------------------------------------------- // @@ -72,7 +73,8 @@ Bytestream packet; packet.write_byte (CLRC_DISCONNECT); this->send (packet); - m_interface->print ("Disconnected from %s\n", m_address.to_string (IP_WITH_PORT).chars()); + m_interface->print ("Disconnected from %s\n", + m_address.to_string (IPAddress::WITH_PORT).chars()); m_interface->update_statusbar(); } @@ -274,7 +276,8 @@ // void RCONSession::send_hello() { - m_interface->print ("Connecting to %s...\n", m_address.to_string (IP_WITH_PORT).chars()); + m_interface->print ("Connecting to %s...\n", + m_address.to_string (IPAddress::WITH_PORT).chars()); Bytestream packet; packet.write_byte (CLRC_BEGINCONNECTION); packet.write_byte (RCON_PROTOCOL_VERSION); @@ -378,4 +381,6 @@ { m_interface->print ("This server does not support tab-completion\n", m_serverProtocol); } -} \ No newline at end of file +} + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/rconsession.h --- a/sources/network/rconsession.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/rconsession.h Thu Jul 23 01:52:04 2015 +0300 @@ -32,6 +32,7 @@ #include "ipaddress.h" #include "udpsocket.h" #include "bytestream.h" +BEGIN_ZFC_NAMESPACE // ------------------------------------------------------------------------------------------------- // @@ -129,3 +130,5 @@ String m_lastTabComplete; class Interface* m_interface; }; + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/udpsocket.cpp --- a/sources/network/udpsocket.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/udpsocket.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -33,19 +33,21 @@ #ifndef _WIN32 # include # include +# include +# include #else # include # include #endif #include -#include #include #include -#include #include "../huffman/huffman.h" -static char g_huffmanBuffer[131072]; +BEGIN_ZFC_NAMESPACE + +char UDPSocket::HuffmanBuffer[131072]; // ----------------------------------------------------------------------------- // @@ -56,7 +58,11 @@ // UDPSocket::~UDPSocket() { +#ifdef _WIN32 + closesocket (m_socket); +#else close (m_socket); +#endif } // ------------------------------------------------------------------------------------------------- @@ -89,8 +95,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -UDPSocket::bind (unsigned short port) -> bool +bool UDPSocket::bind (unsigned short port) { sockaddr_in svaddr; memset (&svaddr, 0, sizeof svaddr); @@ -113,7 +118,7 @@ { sockaddr_in claddr; socklen_t socklen = sizeof claddr; - int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof g_huffmanBuffer, 0, + int length = ::recvfrom (m_socket, HuffmanBuffer, sizeof HuffmanBuffer, 0, reinterpret_cast (&claddr), &socklen); if (length == -1) @@ -126,7 +131,7 @@ unsigned char decodedPacket[MAX_DATAGRAM_LENGTH]; int decodedLength = sizeof decodedPacket; - HUFFMAN_Decode (reinterpret_cast (g_huffmanBuffer), + HUFFMAN_Decode (reinterpret_cast (HuffmanBuffer), decodedPacket, length, &decodedLength); datagram.from.host = ntohl (claddr.sin_addr.s_addr); datagram.from.port = ntohs (claddr.sin_port); @@ -136,14 +141,13 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool +bool UDPSocket::send (const IPAddress& address, const Bytestream& data) { - int encodedlength = sizeof g_huffmanBuffer; - HUFFMAN_Encode (data.data(), reinterpret_cast (g_huffmanBuffer), + int encodedlength = sizeof HuffmanBuffer; + HUFFMAN_Encode (data.data(), reinterpret_cast (HuffmanBuffer), data.written_length(), &encodedlength); sockaddr_in claddr = address.to_sockaddr_in(); - int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0, + int res = ::sendto (m_socket, HuffmanBuffer, encodedlength, 0, reinterpret_cast (&claddr), sizeof claddr); if (res == -1) @@ -154,3 +158,5 @@ return true; } + +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/network/udpsocket.h --- a/sources/network/udpsocket.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/network/udpsocket.h Thu Jul 23 01:52:04 2015 +0300 @@ -32,6 +32,7 @@ #include "../main.h" #include "ipaddress.h" #include "bytestream.h" +BEGIN_ZFC_NAMESPACE enum { MAX_DATAGRAM_LENGTH = 5120 }; @@ -49,31 +50,19 @@ UDPSocket(); virtual ~UDPSocket(); - METHOD bind (unsigned short port) -> bool; - METHOD read (Datagram& datagram) -> bool; - METHOD send (const IPAddress& address, const Bytestream& data) -> bool; - METHOD set_blocking (bool a) -> bool; - inline METHOD error_string() -> const String&; - inline METHOD file_descriptor() -> int; + bool bind (unsigned short port); + bool read (Datagram& datagram); + bool send (const IPAddress& address, const Bytestream& data); + bool set_blocking (bool a); + const String& error_string() const { return m_error; } + int file_descriptor() const { return m_socket; } private: + static char HuffmanBuffer[131072]; + IPAddress m_addr; String m_error; int m_socket; }; -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -UDPSocket::file_descriptor() -> int -{ - return m_socket; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -UDPSocket::error_string() -> const String& -{ - return m_error; -} +END_ZFC_NAMESPACE \ No newline at end of file diff -r 53c2aecb9704 -r 08ccaf26cffd sources/range.h --- a/sources/range.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/range.h Thu Jul 23 01:52:04 2015 +0300 @@ -31,6 +31,8 @@ #pragma once #include #include +#include "basics.h" +BEGIN_ZFC_NAMESPACE // // ------------------------------------------------------------------------------------------------- @@ -146,4 +148,6 @@ { return not operator== (other); } -}; \ No newline at end of file +}; + +END_ZFC_NAMESPACE diff -r 53c2aecb9704 -r 08ccaf26cffd sources/version.cpp --- a/sources/version.cpp Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/version.cpp Thu Jul 23 01:52:04 2015 +0300 @@ -45,14 +45,14 @@ #ifdef HG_TAG return HG_TAG; #else - static char buffer[128] = {0}; + static char buffer[256] = {0}; if (buffer[0] == '\0') { if (strcmp (HG_BRANCH, "default") != 0) - snprintf (buffer, sizeof buffer, "%s-%s (%s)", version_string(), HG_NODE, HG_BRANCH); + sprintf (buffer, "%s-%s (%s)", version_string(), HG_NODE, HG_BRANCH); else - snprintf (buffer, sizeof buffer, "%s-%s", version_string(), HG_NODE); + sprintf (buffer, "%s-%s", version_string(), HG_NODE); } return buffer; diff -r 53c2aecb9704 -r 08ccaf26cffd sources/version.h --- a/sources/version.h Thu Jul 23 00:16:47 2015 +0300 +++ b/sources/version.h Thu Jul 23 01:52:04 2015 +0300 @@ -30,12 +30,10 @@ #pragma once #include "basics.h" - #define APPNAME "zfc9000" #define VERSION_MAJOR 1 #define VERSION_MINOR 1 #define VERSION_PATCH 0 - // #define IS_RELEASE // -------------------------------------------------------------------------------------------------