Mon, 04 May 2015 15:51:03 +0300
Removed a lot of boilerplate code
sources/coloredline.cpp | file | annotate | diff | comparison | revisions | |
sources/coloredline.h | file | annotate | diff | comparison | revisions | |
sources/format.h | file | annotate | diff | comparison | revisions | |
sources/geometry.h | file | annotate | diff | comparison | revisions | |
sources/interface.cpp | file | annotate | diff | comparison | revisions | |
sources/list.h | file | annotate | diff | comparison | revisions | |
sources/mystring.cpp | file | annotate | diff | comparison | revisions | |
sources/mystring.h | file | annotate | diff | comparison | revisions | |
sources/range.h | file | annotate | diff | comparison | revisions | |
sources/version.cpp | file | annotate | diff | comparison | revisions | |
sources/version.h | file | annotate | diff | comparison | revisions |
--- a/sources/coloredline.cpp Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/coloredline.cpp Mon May 04 15:51:03 2015 +0300 @@ -63,8 +63,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -ColoredLine::finalize() -> void +void ColoredLine::finalize() { if (m_activeColor != DEFAULT) set_color (m_activeColor, false); @@ -77,8 +76,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -ColoredLine::add_char (char ch) -> void +void ColoredLine::add_char (char ch) { if (m_final) return; // Don't touch finalized lines. @@ -127,8 +125,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -ColoredLine::set_color (Color a, bool on) -> void +void ColoredLine::set_color (Color a, bool on) { switch (a) { @@ -148,8 +145,7 @@ // ------------------------------------------------------------------------------------------------- // How many rows does this line take up? // -METHOD -ColoredLine::rows (int cols) const -> int +int ColoredLine::rows (int cols) const { int rows = length() / cols;
--- a/sources/coloredline.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/coloredline.h Mon May 04 15:51:03 2015 +0300 @@ -62,14 +62,14 @@ public: ColoredLine() {} - METHOD data() const -> const Vector<int>& { return m_data; } - METHOD length() const -> int { return m_length; } - METHOD add_char (char ch) -> void; - METHOD finalize() -> void; - METHOD rows (int cols) const -> int; + const Vector<int>& data() const { return m_data; } + int length() const { return m_length; } + void add_char (char ch); + void finalize(); + int rows (int cols) const; private: - METHOD set_color (Color a, bool on) -> void; + void set_color (Color a, bool on); Vector<int> m_data; int m_length = 0;
--- a/sources/format.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/format.h Mon May 04 15:51:03 2015 +0300 @@ -34,7 +34,7 @@ #include "geometry.h" #define FORMAT_OVERLOAD(...) \ - inline FUNCTION make_format_argument (__VA_ARGS__ a) -> String + inline String make_format_argument (__VA_ARGS__ a) // ------------------------------------------------------------------------------------------------- // @@ -97,8 +97,15 @@ return "???"; } -FORMAT_OVERLOAD (Position) { return String ("(") + a.x + ", " + a.y + ")"; } -FORMAT_OVERLOAD (Size) { return String ("(") + a.width + "x" + a.height + ")"; } +FORMAT_OVERLOAD (Position) +{ + return String ("(") + a.x + ", " + a.y + ")"; +} + +FORMAT_OVERLOAD (Size) +{ + return String ("(") + a.width + "x" + a.height + ")"; +} FORMAT_OVERLOAD (Rectangle) { @@ -117,8 +124,8 @@ // // Expands the given arguments into a vector of strings. // -template<typename T, typename... RestTypes> FUNCTION -expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) -> void +template<typename T, typename... RestTypes> +void expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) { data.append (make_format_argument (arg)); expand_format_arguments (data, rest...); @@ -152,8 +159,8 @@ // argument did not expand into a number in the first place, 0 is used // and 0x0 is printed. // -template<typename... argtypes> FUNCTION -format (const String& fmtstr, const argtypes&... raw_args) -> String +template<typename... argtypes> +String format (const String& fmtstr, const argtypes&... raw_args) { Vector<String> args; expand_format_arguments (args, raw_args...); @@ -166,8 +173,7 @@ // It returns the formatter string as-is. // static String format (const String& fmtstr) __attribute__ ((unused)); -static String // FUNCTION -format (const String& fmtstr) // -> String +static String format (const String& fmtstr) // -> String { return fmtstr; } @@ -176,8 +182,8 @@ // // Prints the formatting result to the given file handle // -template<typename... Args> FUNCTION -print_to (FILE* fp, const String& fmtstr, Args const& ...args) -> void +template<typename... Args> +void print_to (FILE* fp, const String& fmtstr, Args const& ...args) { std::fprintf (fp, "%s", format (fmtstr, args...).chars()); }
--- a/sources/geometry.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/geometry.h Mon May 04 15:51:03 2015 +0300 @@ -43,12 +43,41 @@ x (0), y (0) {} - inline METHOD operator< (const Position& other) const -> bool; - inline METHOD operator> (const Position& other) const -> bool; - inline METHOD operator== (const Position& other) const -> bool; - inline METHOD operator<= (const Position& other) const -> bool; - inline METHOD operator>= (const Position& other) const -> bool; - inline METHOD operator!= (const Position& other) const -> bool; + bool operator< (const Position& other) const + { + if (y != other.y) + return y < other.y; + + return x < other.x; + } + + bool operator> (const Position& other) const + { + if (y != other.y) + return y > other.y; + + return x > other.x; + } + + bool operator== (const Position& other) const + { + return y == other.y and x == other.x; + } + + bool operator!= (const Position& other) const + { + return not operator== (other); + } + + bool operator<= (const Position& other) const + { + return not operator> (other); + } + + bool operator>= (const Position& other) const + { + return not operator< (other); + } }; // ------------------------------------------------------------------------------------------------- @@ -84,57 +113,3 @@ Position(), Size() {} }; - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -Position::operator< (const Position& other) const -> bool -{ - if (y != other.y) - return y < other.y; - - return x < other.x; -} - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -Position::operator> (const Position& other) const -> bool -{ - if (y != other.y) - return y > other.y; - - return x > other.x; -} - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -Position::operator== (const Position& other) const -> bool -{ - return y == other.y and x == other.x; -} - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -Position::operator<= (const Position& other) const -> bool -{ - return not operator> (other); -} - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -Position::operator>= (const Position& other) const -> bool -{ - return not operator< (other); -} - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -Position::operator!= (const Position& other) const -> bool -{ - return not operator== (other); -}
--- a/sources/interface.cpp Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/interface.cpp Mon May 04 15:51:03 2015 +0300 @@ -45,25 +45,25 @@ INPUTSTATE_CONFIRM_DISCONNECTION, }; -static StringList g_input; -static int g_inputCursor = 0; -static int g_cursor = 0; -static int g_pan = 0; -static bool g_needRefresh = false; -static bool g_needStatusBarRender = false; -static bool g_needInputRender = false; -static bool g_needOutputRender = false; -static bool g_needNicklistRender = false; -static struct { char ch; int x; } g_cursorChar; -static Vector<ColoredLine> g_output;; -static int g_outputScroll = 0; -static String g_title; -static InputState g_inputState = INPUTSTATE_NORMAL; -static Function<void (void)> g_disconnectConfirmFunction = nullptr; -static IPAddress g_address; -static String g_statusBarText; -static StringList g_playerNames; -static String g_pasteBuffer; +static StringList InputHistory; +static int InputCursor = 0; +static int CursorPosition = 0; +static int InputPanning = 0; +static bool NeedRefresh = false; +static bool NeedStatusBarRender = false; +static bool NeedInputRender = false; +static bool NeedOutputRender = false; +static bool NeedNicklistRender = false; +static struct { char ch; int x; } CursorCharacter; +static Vector<ColoredLine> OutputLines; +static int OutputScroll = 0; +static String Title; +static InputState CurrentInputState = INPUTSTATE_NORMAL; +static Function<void (void)> DisconnectConfirmFunction = nullptr; +static IPAddress CurrentAddress; +static String StatusBarText; +static StringList PlayerNames; +static String PasteBuffer; // ------------------------------------------------------------------------------------------------- // @@ -78,7 +78,7 @@ static FUNCTION current_input() -> const String& { - return g_input[g_inputCursor]; + return InputHistory[InputCursor]; } // ------------------------------------------------------------------------------------------------- @@ -88,10 +88,10 @@ static FUNCTION detach_input() -> void { - if (g_inputCursor > 0) + if (InputCursor > 0) { - g_input[0] = current_input(); - g_inputCursor = 0; + InputHistory[0] = current_input(); + InputCursor = 0; } } @@ -102,7 +102,7 @@ mutable_current_input() -> String& { detach_input(); - return g_input[g_inputCursor]; + return InputHistory[InputCursor]; } // ------------------------------------------------------------------------------------------------- @@ -111,19 +111,19 @@ move_input_cursor (int delta) -> void { // No input history when inputting addresses or passwords - if (g_inputState != INPUTSTATE_NORMAL) + if (CurrentInputState != INPUTSTATE_NORMAL) { - g_inputCursor = 0; + InputCursor = 0; return; } - int oldcursor = g_inputCursor; - g_inputCursor = clamp (g_inputCursor + delta, 0, g_input.size() - 1); + int oldcursor = InputCursor; + InputCursor = clamp (InputCursor + delta, 0, InputHistory.size() - 1); - if (g_inputCursor != oldcursor) + if (InputCursor != oldcursor) { - g_cursor = current_input().length(); - g_needInputRender = true; + CursorPosition = current_input().length(); + NeedInputRender = true; } } @@ -134,7 +134,7 @@ { String prompt; - switch (g_inputState) + switch (CurrentInputState) { case INPUTSTATE_NORMAL: prompt = ">"; break; case INPUTSTATE_ADDRESS: prompt = "address:"; break; @@ -152,25 +152,25 @@ { // Clear the input row (unless going to or from confirm state) if (newstate != INPUTSTATE_CONFIRM_DISCONNECTION - and g_inputState != INPUTSTATE_CONFIRM_DISCONNECTION) + and CurrentInputState != INPUTSTATE_CONFIRM_DISCONNECTION) { - g_inputCursor = 0; + InputCursor = 0; mutable_current_input().clear(); } switch (newstate) { case INPUTSTATE_ADDRESS: - if (g_address.host != 0) - mutable_current_input() = g_address.to_string (IP_WITH_PORT); + if (CurrentAddress.host != 0) + mutable_current_input() = CurrentAddress.to_string (IP_WITH_PORT); break; default: break; } - g_inputState = newstate; - g_needInputRender = true; + CurrentInputState = newstate; + NeedInputRender = true; } // ------------------------------------------------------------------------------------------------- @@ -186,11 +186,11 @@ ::refresh(); ::timeout (0); ::use_default_colors(); - g_input.clear(); - g_input << ""; - g_output.clear(); - g_output << ColoredLine(); - g_title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string()); + InputHistory.clear(); + InputHistory << ""; + OutputLines.clear(); + OutputLines << ColoredLine(); + Title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string()); for (int i = 0; i < NUM_COLORS; ++i) for (int j = 0; j < NUM_COLORS; ++j) @@ -202,7 +202,7 @@ render_full(); refresh(); - g_needRefresh = false; + NeedRefresh = false; } // ------------------------------------------------------------------------------------------------- @@ -210,19 +210,19 @@ static FUNCTION interface_render_titlebar() -> void { - if (g_title.length() <= COLS) + if (Title.length() <= COLS) { int pair = interface_color_pair (WHITE, BLUE); - int startx = (COLS - g_title.length()) / 2; - int endx = startx + g_title.length(); + int startx = (COLS - Title.length()) / 2; + int endx = startx + Title.length(); attron (pair); - mvprintw (0, startx, "%s", g_title.chars()); + mvprintw (0, startx, "%s", Title.chars()); mvhline (0, 0, ' ', startx); mvhline (0, endx, ' ', COLS - endx); attroff (pair); } - g_needRefresh = true; + NeedRefresh = true; } // ------------------------------------------------------------------------------------------------- @@ -230,7 +230,7 @@ FUNCTION Interface::set_title (const String& title) -> void { - g_title = title; + Title = title; interface_render_titlebar(); } @@ -241,7 +241,7 @@ { if (RCONSession::get_session()->is_active()) { - g_disconnectConfirmFunction = afterwards; + DisconnectConfirmFunction = afterwards; set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION); } else @@ -331,15 +331,15 @@ static FUNCTION interface_render_output() -> void { - if (g_output.size() == 1) + if (OutputLines.size() == 1) return; - g_outputScroll = clamp (g_outputScroll, 0, g_output.size() - 1); + OutputScroll = clamp (OutputScroll, 0, OutputLines.size() - 1); int height = LINES - 3; int width = COLS - interface_nicklist_width(); int printOffset = 0; - int end = g_output.size() - 1 - g_outputScroll; + int end = OutputLines.size() - 1 - OutputScroll; int start = end; int usedHeight = 0; int y = 1; @@ -348,7 +348,7 @@ // Where to start? while (start > 0) { - int rows = g_output[start - 1].rows (width); + int rows = OutputLines[start - 1].rows (width); if (usedHeight + rows > height) { @@ -364,9 +364,9 @@ // See if there's any more rows to use (end may be too small) if (not tightFit) { - while (end < g_output.size()) + while (end < OutputLines.size()) { - int rows = g_output[end].rows (width); + int rows = OutputLines[end].rows (width); if (usedHeight + rows > height) { @@ -382,7 +382,7 @@ if (start > 0) printOffset = height - usedHeight; - g_outputScroll = g_output.size() - 1 - end; + OutputScroll = OutputLines.size() - 1 - end; if (start < 0 or start == end or printOffset >= height) return; @@ -397,10 +397,10 @@ y += printOffset; for (int i = start; i < end; ++i) - y = interface_render_colorline (y, 0, width, g_output[i], true); + y = interface_render_colorline (y, 0, width, OutputLines[i], true); - g_needOutputRender = false; - g_needRefresh = true; + NeedOutputRender = false; + NeedRefresh = true; } // ------------------------------------------------------------------------------------------------- @@ -420,9 +420,9 @@ { mvhline (y, x, ' ', width); - if (i < g_playerNames.size()) + if (i < PlayerNames.size()) { - String displaynick = g_playerNames[i]; + String displaynick = PlayerNames[i]; if (displaynick.length() > width) { @@ -436,8 +436,8 @@ y++; } - g_needNicklistRender = false; - g_needRefresh = true; + NeedNicklistRender = false; + NeedRefresh = true; } // ------------------------------------------------------------------------------------------------- @@ -449,13 +449,13 @@ // If we're asking the user if they want to disconnect, we don't render any input strings, // just the confirmation message. - if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) + if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION) { attron (promptColor); mvhline (LINES - 2, 0, ' ', COLS); mvprintw (LINES - 2, 0, "Are you sure you want to disconnect? y/n"); attroff (promptColor); - g_needRefresh = true; + NeedRefresh = true; return; } @@ -465,25 +465,25 @@ int y = LINES - 2; // If we're inputting a password, replace it with asterisks - if (g_inputState == INPUTSTATE_PASSWORD) + if (CurrentInputState == INPUTSTATE_PASSWORD) { for (char& ch : displayString) ch = '*'; } // Ensure the cursor is within bounds - g_cursor = clamp (g_cursor, 0, displayString.length()); + CursorPosition = clamp (CursorPosition, 0, displayString.length()); // Ensure that the cursor is always in view, adjust panning if this is not the case - if (g_cursor > g_pan + displayLength) - g_pan = g_cursor - displayLength; // cursor went too far right - else if (g_cursor < g_pan) - g_pan = g_cursor; // cursor went past the pan value to the left + if (CursorPosition > InputPanning + displayLength) + InputPanning = CursorPosition - displayLength; // cursor went too far right + else if (CursorPosition < InputPanning) + InputPanning = CursorPosition; // cursor went past the pan value to the left // What part of the string to draw? - int start = g_pan; + int start = InputPanning; int end = min<int> (displayString.length(), start + displayLength); - assert (g_cursor >= start and g_cursor <= end); + assert (CursorPosition >= start and CursorPosition <= end); // Render the input string mvhline (LINES - 2, 0, ' ', COLS); @@ -496,10 +496,10 @@ // Store in memory where the cursor is now (so that we can re-draw it to position the terminal // cursor). - g_cursorChar.ch = g_cursor != 0 ? displayString[g_cursor - 1] : '\0'; - g_cursorChar.x = prompt.length() + (g_cursor - g_pan); - g_needRefresh = true; - g_needInputRender = false; + CursorCharacter.ch = CursorPosition != 0 ? displayString[CursorPosition - 1] : '\0'; + CursorCharacter.x = prompt.length() + (CursorPosition - InputPanning); + NeedRefresh = true; + NeedInputRender = false; } // ------------------------------------------------------------------------------------------------- @@ -511,10 +511,10 @@ int y = LINES - 1; attron (color); mvhline (y, 0, ' ', COLS); - mvprintw (y, 0, "%s", g_statusBarText.chars()); + mvprintw (y, 0, "%s", StatusBarText.chars()); attroff (color); - g_needRefresh = true; - g_needStatusBarRender = false; + NeedRefresh = true; + NeedStatusBarRender = false; } // ------------------------------------------------------------------------------------------------- @@ -551,10 +551,10 @@ text += "^N to connect, ^Q to quit"; - if (text != g_statusBarText) + if (text != StatusBarText) { - g_statusBarText = text; - g_needStatusBarRender = true; + StatusBarText = text; + NeedStatusBarRender = true; } } @@ -577,13 +577,13 @@ interface_position_cursor() -> void { // This is only relevant if the input string is being drawn - if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) + if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION) return; int y = LINES - 2; - if (g_cursorChar.ch != '\0') - mvprintw (y, g_cursorChar.x, "%c", g_cursorChar.ch); + if (CursorCharacter.ch != '\0') + mvprintw (y, CursorCharacter.x, "%c", CursorCharacter.ch); else mvprintw (y, interface_prompt_string().length(), " "); } @@ -594,7 +594,7 @@ interface_find_previous_word() -> int { const String& input = current_input(); - int pos = g_cursor; + int pos = CursorPosition; // Move past whitespace while (pos > 0 and isspace (input[pos - 1])) @@ -613,7 +613,7 @@ interface_find_next_word() -> int { const String& input = current_input(); - int pos = g_cursor; + int pos = CursorPosition; // Move past current whitespace while (pos < input.length() and isspace (input[pos])) @@ -634,13 +634,13 @@ if (a >= b) return; - if (g_cursor > a and g_cursor <= b) - g_cursor = a; + if (CursorPosition > a and CursorPosition <= b) + CursorPosition = a; String& input = mutable_current_input(); - g_pasteBuffer = input.mid (a, b); + PasteBuffer = input.mid (a, b); input.remove (a, b - a); - g_needInputRender = true; + NeedInputRender = true; } // ------------------------------------------------------------------------------------------------- @@ -657,12 +657,12 @@ return; } - if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) + if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION) { if (ch == 'y' or ch == 'Y') { RCONSession::get_session()->disconnect(); - g_disconnectConfirmFunction(); + DisconnectConfirmFunction(); } else if (ch == 'n' or ch == 'N') set_input_state (INPUTSTATE_NORMAL); @@ -672,13 +672,13 @@ if (ch >= 0x20 and ch <= 0x7E) { - mutable_current_input().insert (g_cursor++, char (ch)); - g_needInputRender = true; + mutable_current_input().insert (CursorPosition++, char (ch)); + NeedInputRender = true; } else switch (ch) { case 'Q' - 'A' + 1: // ^Q - switch (g_inputState) + switch (CurrentInputState) { case INPUTSTATE_CONFIRM_DISCONNECTION: break; @@ -712,19 +712,19 @@ case KEY_LEFT: case 'B' - 'A' + 1: // readline ^B - if (g_cursor > 0) + if (CursorPosition > 0) { - g_cursor--; - g_needInputRender = true; + CursorPosition--; + NeedInputRender = true; } break; case KEY_RIGHT: case 'F' - 'A' + 1: // readline ^F - if (g_cursor < current_input().length()) + if (CursorPosition < current_input().length()) { - g_cursor++; - g_needInputRender = true; + CursorPosition++; + NeedInputRender = true; } break; @@ -735,77 +735,77 @@ case KEY_HOME: case 'A' - 'A' + 1: // readline ^A - if (g_cursor != 0) + if (CursorPosition != 0) { - g_cursor = 0; - g_needInputRender = true; + CursorPosition = 0; + NeedInputRender = true; } break; case KEY_END: case 'E' - 'A' + 1: // readline ^E - if (g_cursor != current_input().length()) + if (CursorPosition != current_input().length()) { - g_cursor = current_input().length(); - g_needInputRender = true; + CursorPosition = current_input().length(); + NeedInputRender = true; } break; case KEY_BACKSPACE: - if (g_cursor > 0) + if (CursorPosition > 0) { - mutable_current_input().remove_at (--g_cursor); - g_needInputRender = true; + mutable_current_input().remove_at (--CursorPosition); + NeedInputRender = true; } break; case KEY_DC: case 'D' - 'A' + 1: // readline ^D - if (g_cursor < current_input().length()) + if (CursorPosition < current_input().length()) { - mutable_current_input().remove_at (g_cursor); - g_needInputRender = true; + mutable_current_input().remove_at (CursorPosition); + NeedInputRender = true; } break; case KEY_PPAGE: - g_outputScroll += min (g_pageSize, LINES / 2); - g_needOutputRender = true; + OutputScroll += min (g_pageSize, LINES / 2); + NeedOutputRender = true; break; case KEY_NPAGE: - g_outputScroll -= min (g_pageSize, LINES / 2); - g_needOutputRender = true; + OutputScroll -= min (g_pageSize, LINES / 2); + NeedOutputRender = true; break; case 'U' - 'A' + 1: // readline ^U - delete from start to cursor - if (g_cursor > 0) + if (CursorPosition > 0) { - yank (0, g_cursor); - g_cursor = 0; + yank (0, CursorPosition); + CursorPosition = 0; } break; case 'K' - 'A' + 1: // readline ^K - delete from cursor to end - yank (g_cursor, mutable_current_input().length()); + yank (CursorPosition, mutable_current_input().length()); break; case 'W' - 'A' + 1: // readline ^W - delete from previous word bounary to current - yank (interface_find_previous_word(), g_cursor); + yank (interface_find_previous_word(), CursorPosition); break; case 'Y' - 'A' + 1: // readline ^Y - paste previously deleted text - if (not g_pasteBuffer.is_empty()) + if (not PasteBuffer.is_empty()) { - mutable_current_input().insert (g_cursor, g_pasteBuffer); - g_cursor += g_pasteBuffer.length(); - g_needInputRender = true; + mutable_current_input().insert (CursorPosition, PasteBuffer); + CursorPosition += PasteBuffer.length(); + NeedInputRender = true; } break; case '\n': case KEY_ENTER: - switch (g_inputState) + switch (CurrentInputState) { case INPUTSTATE_CONFIRM_DISCONNECTION: break; // handled above @@ -813,7 +813,7 @@ case INPUTSTATE_ADDRESS: try { - g_address = IPAddress::from_string (current_input()); + CurrentAddress = IPAddress::from_string (current_input()); } catch (std::exception& e) { @@ -821,19 +821,19 @@ return; } - if (g_address.port == 0) - g_address.port = 10666; + if (CurrentAddress.port == 0) + CurrentAddress.port = 10666; set_input_state (INPUTSTATE_PASSWORD); break; case INPUTSTATE_PASSWORD: - if (g_inputState == INPUTSTATE_PASSWORD and not current_input().is_empty()) + if (CurrentInputState == INPUTSTATE_PASSWORD and not current_input().is_empty()) { RCONSession* session = RCONSession::get_session(); session->disconnect(); session->set_password (current_input()); - session->connect (g_address); + session->connect (CurrentAddress); set_input_state (INPUTSTATE_NORMAL); } break; @@ -841,15 +841,15 @@ case INPUTSTATE_NORMAL: if (RCONSession::get_session()->send_command (current_input())) { - g_input.insert (0, ""); - g_needInputRender = true; + InputHistory.insert (0, ""); + NeedInputRender = true; } break; } break; case 'N' - 'A' + 1: // ^N - if (g_inputState == INPUTSTATE_NORMAL) + if (CurrentInputState == INPUTSTATE_NORMAL) safe_disconnect ([]() {set_input_state (INPUTSTATE_ADDRESS);}); break; @@ -864,30 +864,30 @@ case 'b': case 'B': // readline alt-b - move one word to the left - g_cursor = interface_find_previous_word(); - g_needInputRender = true; + CursorPosition = interface_find_previous_word(); + NeedInputRender = true; break; case 'f': case 'F': // readline alt-f - move one word to the right - g_cursor = interface_find_next_word(); - g_needInputRender = true; + CursorPosition = interface_find_next_word(); + NeedInputRender = true; break; case 'd': case 'D': // readline alt-d - delete from here till next word boundary - yank (g_cursor, interface_find_next_word()); + yank (CursorPosition, interface_find_next_word()); break; } } else { // No alt-key, handle pure escape - if (g_inputState == INPUTSTATE_PASSWORD) + if (CurrentInputState == INPUTSTATE_PASSWORD) set_input_state (INPUTSTATE_ADDRESS); - else if (g_inputState == INPUTSTATE_ADDRESS) + else if (CurrentInputState == INPUTSTATE_ADDRESS) set_input_state (INPUTSTATE_NORMAL); } break; @@ -899,16 +899,16 @@ FUNCTION Interface::render() -> void { - if (g_needStatusBarRender) interface_render_statusbar(); - if (g_needInputRender) interface_render_input(); - if (g_needOutputRender) interface_render_output(); - if (g_needNicklistRender) interface_render_nicklist(); + if (NeedStatusBarRender) interface_render_statusbar(); + if (NeedInputRender) interface_render_input(); + if (NeedOutputRender) interface_render_output(); + if (NeedNicklistRender) interface_render_nicklist(); - if (g_needRefresh) + if (NeedRefresh) { interface_position_cursor(); refresh(); - g_needRefresh = false; + NeedRefresh = false; } } @@ -924,12 +924,12 @@ { if (ch == '\n') { - g_output.last().finalize(); - g_output << ColoredLine(); + OutputLines.last().finalize(); + OutputLines << ColoredLine(); continue; } - if (g_output.last().length() == 0) + if (OutputLines.last().length() == 0) { time_t now; time (&now); @@ -937,13 +937,13 @@ strftime (timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime (&now)); for (char* cp = timestamp; *cp != '\0'; ++cp) - g_output.last().add_char (*cp); + OutputLines.last().add_char (*cp); } - g_output.last().add_char (ch); + OutputLines.last().add_char (ch); } - g_needOutputRender = true; + NeedOutputRender = true; } // ------------------------------------------------------------------------------------------------- @@ -953,7 +953,7 @@ { try { - g_address = IPAddress::from_string (address); + CurrentAddress = IPAddress::from_string (address); } catch (std::exception& e) { @@ -961,13 +961,13 @@ return; } - if (g_address.port == 0) - g_address.port = 10666; + if (CurrentAddress.port == 0) + CurrentAddress.port = 10666; RCONSession* session = RCONSession::get_session(); session->disconnect(); session->set_password (password); - session->connect (g_address); + session->connect (CurrentAddress); } // ------------------------------------------------------------------------------------------------- @@ -975,6 +975,6 @@ FUNCTION Interface::set_player_names (const StringList& names) -> void { - g_playerNames = names; - g_needNicklistRender = true; + PlayerNames = names; + NeedNicklistRender = true; }
--- a/sources/list.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/list.h Mon May 04 15:51:03 2015 +0300 @@ -49,81 +49,309 @@ using ConstReverseIterator = typename C::const_reverse_iterator; using Self = Container<T, C>; - Container(); - Container (int numvalues); - Container (const C& a); - Container (std::initializer_list<T>&& a); + Container(){} + + Container (int numvalues) : + m_container (numvalues) {} + + Container (const C& other) : + m_container (other) {} + + Container (std::initializer_list<T>&& a) : + m_container (a) {} + + T& append (const T& value) + { + m_container.push_back (value); + return m_container[m_container.size() - 1]; + } + + Iterator begin() + { + return m_container.begin(); + } + + ConstIterator begin() const + { + return m_container.cbegin(); + } + + void clear() + { + m_container.clear(); + } + + bool contains (const T& a) const + { + return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end(); + } + + ConstReverseIterator crbegin() const + { + return m_container.crbegin(); + } + + ConstReverseIterator crend() const + { + return m_container.crbegin(); + } + + const C& container() const + { + return m_container; + } + + Iterator end() + { + return m_container.end(); + } + + ConstIterator end() const + { + return m_container.cend(); + } + + Iterator find (const T& needle) + { + auto it = std::find (m_container.begin(), m_container.end(), needle); + + if (it == m_container.end()) + return end(); + + return it; + } + + ConstIterator find (const T& needle) const + { + auto it = std::find (m_container.cbegin(), m_container.cend(), needle); + + if (it == m_container.cend()) + return end(); + + return it; + } + + Iterator find (Function<bool (T const&)> func) + { + for (Iterator it = begin(); it != end(); ++it) + { + if (func (*it)) + return it; + } + + return end(); + } + + ConstIterator find (Function<bool (T const&)> func) const + { + for (ConstIterator it = begin(); it != end(); ++it) + { + if (func (*it)) + return it; + } + + return end(); + } + + T& first() + { + return *begin(); + } + + const T& first() const + { + return *begin(); + } + + void insert (int pos, const T& value) + { + m_container.insert (m_container.begin() + pos, value); + } + + bool is_empty() const + { + return size() == 0; + } + + T& last() + { + return *(end() - 1); + } - auto append (const T& value) -> T&; - auto begin() -> Iterator; - auto begin() const -> ConstIterator; - auto clear() -> void; - auto contains (const T& a) const -> bool; - auto crbegin() const -> ConstReverseIterator; - auto crend() const -> ConstReverseIterator; - auto container() const -> const C&; - auto end() -> Iterator; - auto end() const -> ConstIterator; - auto find (const T& needle) -> Iterator; - auto find (const T& needle) const -> ConstIterator; - auto find (Function<bool (T const&)> func) -> Iterator; - auto find (Function<bool (T const&)> func) const -> ConstIterator; - auto first() -> T&; - auto first() const -> const T&; - auto insert (int pos, const T& value) -> void; - auto is_empty() const -> bool; - auto last() -> T&; - auto last() const -> const T&; - auto merge (const Self& other) -> void; - auto pop (T& val) -> bool; - auto prepend (const T& value) -> T&; - auto rbegin() -> ReverseIterator; - auto remove_at (int pos) -> void; - auto remove_duplicates() -> void; - auto remove_one (const T& it) -> void; - auto rend() -> ReverseIterator; - auto resize (int size) -> void; - auto reverse() const -> Self; - auto size() const -> int; - auto sort() -> void; - auto splice (int a, int b) const -> Self; - auto splice (const Range<int>& a) const -> Self; + const T& last() const + { + return *(end() - 1); + } + + void merge (const Self& other) + { + int oldsize = size(); + resize (size() + other.size()); + std::copy (other.begin(), other.end(), begin() + oldsize); + } + + bool pop (T& val) + { + if (is_empty()) + return false; + + val = m_container[size() - 1]; + m_container.erase (m_container.end() - 1); + return true; + } + + T& prepend (const T& value) + { + m_container.push_front (value); + return m_container[0]; + } + + ReverseIterator rbegin() + { + return m_container.rbegin(); + } + + void remove_at (int pos) + { + assert (pos < size()); + m_container.erase (m_container.begin() + pos); + } + + void remove_duplicates() + { + sort(); + resize (std::distance (begin(), std::unique (begin(), end()))); + } + + void remove_one (const T& valueToRemove) + { + auto it = std::find (m_container.begin(), m_container.end(), valueToRemove); + + if (it != m_container.end()) + m_container.erase (it); + } + + ReverseIterator rend() + { + return m_container.rend(); + } + + void resize (int size) + { + m_container.resize (size); + } - auto operator<< (const T& value) -> Self&; - auto operator<< (const Self& vals) -> Self&; - auto operator[] (int n) -> T&; - auto operator[] (int n) const -> const T&; - auto operator[] (Range<int> const& n) const -> Self; - auto operator+ (const Self& other) const -> Self; + Self reverse() const + { + Self rev; + std::copy (rbegin(), rend(), rev.begin()); + return rev; + } + + int size() const + { + return m_container.size(); + } + + void sort() + { + std::sort (begin(), end()); + } + + Self splice (int a, int b) const + { + if (a < 0 or b >= size() or b < a) + return Self(); + + Self result; + + for (int i = a; i <= b; ++i) + result << operator[] (i); + + return result; + } + + Self splice (const Range<int>& a) const + { + return splice (a.min(), a.max()); + } + + Self& operator<< (const T& value) + { + append (value); + return *this; + } + + Self& operator<< (const Self& vals) + { + merge (vals); + return *this; + } + + T& operator[] (int n) + { + assert (n < size()); + return m_container[n]; + } + + const T& operator[] (int n) const + { + assert (n < size()); + return m_container[n]; + } + + Self operator[] (Range<int> const& n) const + { + return splice (n); + } + + Self operator+ (const Self& other) const + { + Self out (*this); + out.merge (other); + return out; + } protected: C m_container; }; +// ------------------------------------------------------------------------------------------------- +// template<typename T, typename C> -Container<T, C>& operator>> (const T& value, Container<T, C>& haystack); +Container<T, C>& operator>> (const T& value, Container<T, C>& haystack) +{ + haystack.prepend (value); + return haystack; +} + +// ------------------------------------------------------------------------------------------------- +// template<typename T> using List = Container<T, std::deque<T>>; +// ------------------------------------------------------------------------------------------------- +// + template<typename T> class Vector : public Container<T, std::vector<T>> { public: using Super = Container<T, std::vector<T>>; + using Super::Container; - Vector() {} - Vector (int numvalues) : Super (numvalues) {} - Vector (const Vector<T>& a) : Super (a) {} - Vector (std::initializer_list<T>&& a) : Super (a) {} - Vector (T* data, size_t length) : Super (std::vector<T> (data, data + length)) {} + Vector(){} - auto data() -> T* + Vector (T* data, size_t length) : + Super (std::vector<T> (data, data + length)) {} + + T* data() { return Super::m_container.data(); } - auto data() const -> const T* + const T* data() const { return Super::m_container.data(); } @@ -132,412 +360,4 @@ { return data(); } -}; - -// -// ------------------------------------------------------------------------------------------------- -// -// IMPLEMENTATIONS -// - -template<typename T, typename C> -Container<T, C>::Container() {} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -Container<T, C>::Container (const C& other) : - m_container (other) {} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -Container<T, C>::Container (std::initializer_list<T> && a) : - m_container (a) {} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -Container<T, C>::Container (int numvalues) : - m_container (numvalues) {} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::begin() -> Iterator -{ - return m_container.begin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::begin() const -> ConstIterator -{ - return m_container.cbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::end() -> Iterator -{ - return m_container.end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::end() const -> ConstIterator -{ - return m_container.cend(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::rbegin() -> ReverseIterator -{ - return m_container.rbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::crbegin() const -> ConstReverseIterator -{ - return m_container.crbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::rend() -> ReverseIterator -{ - return m_container.rend(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::crend() const -> ConstReverseIterator -{ - return m_container.crend(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::remove_at (int pos) -> void -{ - assert (pos < size()); - m_container.erase (m_container.begin() + pos); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::prepend (const T& value) -> T& -{ - m_container.push_front (value); - return m_container[0]; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::append (const T& value) -> T& -{ - m_container.push_back (value); - return m_container[m_container.size() - 1]; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::merge (const Self& other) -> void -{ - int oldsize = size(); - resize (size() + other.size()); - std::copy (other.begin(), other.end(), begin() + oldsize); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::pop (T& val) -> bool -{ - if (is_empty()) - return false; - - val = m_container[size() - 1]; - m_container.erase (m_container.end() - 1); - return true; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::operator<< (const T& value) -> Self& -{ - append (value); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::operator<< (const Self& vals) -> Self& -{ - merge (vals); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::reverse() const -> Self -{ - Self rev; - std::copy (rbegin(), rend(), rev.begin()); - return rev; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::clear() -> void -{ - m_container.clear(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::insert (int pos, const T& value) -> void -{ - m_container.insert (m_container.begin() + pos, value); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::remove_duplicates() -> void -{ - sort(); - resize (std::distance (begin(), std::unique (begin(), end()))); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::size() const -> int -{ - return m_container.size(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::operator[] (int n) -> T& -{ - assert (n < size()); - return m_container[n]; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::operator[] (int n) const -> const T& -{ - assert (n < size()); - return m_container[n]; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::operator[] (const Range<int>& n) const -> Self -{ - return splice (n); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::resize (int size) -> void -{ - m_container.resize (size); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::sort() -> void -{ - std::sort (begin(), end()); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::find (const T& needle) -> Iterator -{ - auto it = std::find (m_container.begin(), m_container.end(), needle); - - if (it == m_container.end()) - return end(); - - return it; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::find (const T& needle) const -> ConstIterator -{ - auto it = std::find (m_container.cbegin(), m_container.cend(), needle); - - if (it == m_container.cend()) - return end(); - - return it; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::find (Function<bool (T const&)> func) -> Iterator -{ - for (Iterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::find (Function<bool (T const&)> func) const -> ConstIterator -{ - for (ConstIterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::remove_one (const T& a) -> void -{ - auto it = std::find (m_container.begin(), m_container.end(), a); - - if (it != m_container.end()) - m_container.erase (it); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::is_empty() const -> bool -{ - return size() == 0; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::splice (int a, int b) const -> Self -{ - if (a < 0 or b >= size() or b < a) - return Self(); - - Self result; - - for (int i = a; i <= b; ++i) - result << operator[] (i); - - return result; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::splice (const Range<int>& a) const -> Self -{ - return splice (a.min(), a.max()); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::container() const -> const C& -{ - return m_container; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::first() -> T& -{ - return *m_container.begin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::first() const -> const T& -{ - return *m_container.cbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::last() -> T& -{ - return *(m_container.end() - 1); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::last() const -> const T& -{ - return *(m_container.cend() - 1); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::contains (const T& a) const -> bool -{ - return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto Container<T, C>::operator+ (const Self& other) const -> Self -{ - Self out (*this); - out.merge (other); - return out; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -auto operator>> (const T& value, Container<T, C>& haystack) -> Container<T, C>& -{ - haystack.prepend (value); - return haystack; -} +}; \ No newline at end of file
--- a/sources/mystring.cpp Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/mystring.cpp Mon May 04 15:51:03 2015 +0300 @@ -36,16 +36,14 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::compare (const String& other) const -> int +int String::compare (const String& other) const { return m_string.compare (other.std_string()); } // ------------------------------------------------------------------------------------------------- // -METHOD -String::trim (int n) -> void +void String::trim (int n) { if (n > 0) m_string = mid (0, length() - n).std_string(); @@ -55,8 +53,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::strip (const List<char>& unwanted) -> String +String String::strip (const List<char>& unwanted) { String copy (m_string); @@ -71,8 +68,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::to_uppercase() const -> String +String String::to_uppercase() const { String newstr (m_string); @@ -87,8 +83,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::to_lowercase() const -> String +String String::to_lowercase() const { String newstr (m_string); @@ -103,8 +98,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::split (char del) const -> StringList +StringList String::split (char del) const { String delimstr; delimstr += del; @@ -113,8 +107,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::split (const String& del) const -> StringList +StringList String::split (const String& del) const { StringList res; int a = 0; @@ -140,8 +133,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::replace (const char* a, const char* b) -> void +void String::replace (const char* a, const char* b) { long pos; @@ -151,8 +143,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::count (char needle) const -> int +int String::count (char needle) const { int needles = 0; @@ -165,8 +156,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::mid (long a, long b) const -> String +String String::mid (long a, long b) const { if (b == -1 or b > length()) b = length(); @@ -190,8 +180,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::word_position (int n) const -> int +int String::word_position (int n) const { int count = 0; @@ -208,8 +197,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::find (const char* c, int a) const -> int +int String::find (const char* c, int a) const { int pos = m_string.find (c, a); @@ -221,8 +209,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::find_last (const char* c, int a) const -> int +int String::find_last (const char* c, int a) const { if (a == -1 or a >= length()) a = length() - 1; @@ -236,8 +223,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::to_int (bool* ok, int base) const -> long +long String::to_int (bool* ok, int base) const { errno = 0; char* endptr; @@ -251,8 +237,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::to_float (bool* ok) const -> float +float String::to_float (bool* ok) const { errno = 0; char* endptr; @@ -266,8 +251,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::to_double (bool* ok) const -> double +double String::to_double (bool* ok) const { errno = 0; char* endptr; @@ -281,8 +265,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::operator+ (const String& data) const -> String +String String::operator+ (const String& data) const { String newString = *this; newString.append (data); @@ -291,8 +274,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::operator+ (const char* data) const -> String +String String::operator+ (const char* data) const { String newstr = *this; newstr.append (data); @@ -301,39 +283,16 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::is_numeric() const -> bool +bool String::is_numeric() const { - bool gotDot = false; - - for (const char & c : m_string) - { - // Allow leading hyphen for negatives - if (&c == &m_string[0] and c == '-') - continue; - - // Check for decimal point - if (!gotDot and c == '.') - { - gotDot = true; - continue; - } - - if (c >= '0' and c <= '9') - continue; // Digit - - // If the above cases didn't catch this character, it was - // illegal and this is therefore not a number. - return false; - } - - return true; + char* endptr; + strtol (chars(), &endptr, 10); + return not (endptr && *endptr); } // ------------------------------------------------------------------------------------------------- // -METHOD -String::ends_with (const String& other) -> bool +bool String::ends_with (const String& other) { if (length() < other.length()) return false; @@ -344,8 +303,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::starts_with (const String& other) -> bool +bool String::starts_with (const String& other) { if (length() < other.length()) return false; @@ -355,16 +313,18 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::sprintf (const char* fmtstr, ...) -> void +void String::sprintf (const char* fmtstr, ...) { - char* buf; + char* buf = nullptr; int bufsize = 256; va_list va; va_start (va, fmtstr); do + { + delete[] buf; buf = new char[bufsize]; + } while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize); va_end (va); @@ -374,8 +334,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -StringList::join (const String& delim) -> String +String StringList::join (const String& delim) { String result; @@ -392,8 +351,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::mask_against (const String& pattern) const -> bool +bool String::mask_against (const String& pattern) const { // Elevate to uppercase for case-insensitive matching String pattern_upper = pattern.to_uppercase(); @@ -441,8 +399,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (short int a) -> String +String String::from_number (short int a) { char buf[32]; ::sprintf (buf, "%d", a); @@ -451,8 +408,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (int a) -> String +String String::from_number (int a) { char buf[32]; ::sprintf (buf, "%d", a); @@ -461,8 +417,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (long int a) -> String +String String::from_number (long int a) { char buf[32]; ::sprintf (buf, "%ld", a); @@ -471,8 +426,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (unsigned short int a) -> String +String String::from_number (unsigned short int a) { char buf[32]; ::sprintf (buf, "%u", a); @@ -481,8 +435,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (unsigned int a) -> String +String String::from_number (unsigned int a) { char buf[32]; ::sprintf (buf, "%u", a); @@ -491,8 +444,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (unsigned long int a) -> String +String String::from_number (unsigned long int a) { char buf[32]; ::sprintf (buf, "%lu", a); @@ -501,8 +453,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::from_number (double a) -> String +String String::from_number (double a) { char buf[64]; ::sprintf (buf, "%f", a); @@ -511,8 +462,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::md5() const -> String +String String::md5() const { char checksum[33]; CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum); @@ -522,8 +472,7 @@ // ------------------------------------------------------------------------------------------------- // -METHOD -String::normalize (int (*filter)(int)) -> void +void String::normalize (int (*filter)(int)) { int a = 0; int b = length() - 1;
--- a/sources/mystring.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/mystring.h Mon May 04 15:51:03 2015 +0300 @@ -57,77 +57,83 @@ String (const Vector<char>& data) : m_string (data.data(), data.size()) {} - inline METHOD append (const char* data) -> void; - inline METHOD append (char data) -> void; - inline METHOD append (const String& data) -> void; - inline METHOD begin() -> std::string::iterator; - inline METHOD begin() const -> std::string::const_iterator; - inline METHOD clear() -> void; - METHOD compare (const String& other) const -> int; - METHOD count (char needle) const -> int; - inline METHOD chars() const -> const char*; - inline METHOD end() -> std::string::iterator; - inline METHOD end() const -> std::string::const_iterator; - METHOD ends_with (const String& other) -> bool; - METHOD find (const char* c, int a = 0) const -> int; - METHOD to_lowercase() const -> String; - inline METHOD index_difference (int a, int b) -> int; - inline METHOD insert (int pos, char c) -> void; - inline METHOD insert (int pos, const char* c) -> void; - inline METHOD is_empty() const -> bool; - METHOD is_numeric() const -> bool; - METHOD find_last (const char* c, int a = -1) const -> int; - inline METHOD length() const -> int; - METHOD mask_against (const String& pattern) const -> bool; - METHOD md5() const -> String; - METHOD mid (long a, long b = -1) const -> String; - inline METHOD modify_index (int& a) -> void; - METHOD normalize (int (*filter)(int) = &std::isspace) -> void; - inline METHOD prepend (String a) -> void; - inline METHOD remove (int pos, int len) -> void; - inline METHOD remove_at (int pos) -> void; - inline METHOD remove_from_end (int len) -> void; - inline METHOD remove_from_start (int len) -> void; - METHOD replace (const char* a, const char* b) -> void; - inline METHOD replace (int pos, int n, const String& a) -> void; - inline METHOD shrink_to_fit() -> void; - METHOD split (const String& del) const -> StringList; - METHOD split (char del) const -> StringList; - METHOD sprintf (const char* fmtstr, ...) -> void; - METHOD starts_with (const String& other) -> bool; - inline METHOD std_string() const -> const std::string&; - inline METHOD strip (char unwanted) -> String; - METHOD strip (const List<char>& unwanted) -> String; - METHOD to_double (bool* ok = nullptr) const -> double; - METHOD to_float (bool* ok = nullptr) const -> float; - METHOD to_int (bool* ok = nullptr, int base = 10) const -> long; - METHOD trim (int n) -> void; - METHOD to_uppercase() const -> String; - METHOD word_position (int n) const -> int; + using Iterator = std::string::iterator; + using ConstIterator = std::string::const_iterator; + + ConstIterator begin() const { return m_string.cbegin(); } + int compare (const String &other) const; + int count (char needle) const; + const char* chars() const { return m_string.c_str(); } + ConstIterator end() const { return m_string.end(); } + int find (const char*c, int a = 0) const; + bool is_empty() const { return m_string[0] == '\0'; } + bool is_numeric() const; + int find_last (const char*c, int a) const; + int length() const { return m_string.length(); } + bool mask_against (const String &pattern) const; + String md5() const; + String mid (long a, long b) const; + StringList split (const String &del) const; + StringList split (char del) const; + const std::string& std_string() const { return m_string; } + double to_double (bool* ok = nullptr) const; + float to_float (bool* ok = nullptr) const; + long to_int (bool* ok = nullptr, int base = 10) const; + String to_lowercase() const; + String to_uppercase() const; + int word_position (int n) const; - static METHOD from_number (short int a) -> String; - static METHOD from_number (int a) -> String; - static METHOD from_number (long int a) -> String; - static METHOD from_number (unsigned short int a) -> String; - static METHOD from_number (unsigned int a) -> String; - static METHOD from_number (unsigned long int a) -> String; - static METHOD from_number (double a) -> String; + void append (const char* data) { m_string.append (data); } + void append (char data) { m_string.push_back (data); } + void append (const String& data) { m_string.append (data.chars()); } + Iterator begin() { return m_string.begin(); } + void clear() { m_string.clear(); } + Iterator end() { return m_string.end(); } + bool ends_with (const String &other); + int index_difference (int a, int b) { modify_index (a); modify_index (b); return b - a; } + void insert (int pos, char c) { m_string.insert (m_string.begin() + pos, c); } + void insert (int pos, const char*c) { m_string.insert (pos, c); } + void modify_index (int &a) { if (a < 0) { a = length() - a; } } + void normalize (int (*filter)(int) = &std::isspace); + void prepend (String a) { m_string = (a + m_string).std_string(); } + void remove (int pos, int len) { m_string.replace (pos, len, ""); } + void remove_at (int pos) { m_string.erase (m_string.begin() + pos); } + void remove_from_end (int len) { remove (length() - len, len); } + void remove_from_start (int len) { remove (0, len); } + void replace (const char* a, const char* b); + void replace (int pos, int n, const String &a) { m_string.replace (pos, n, a.chars()); } + void shrink_to_fit() { m_string.shrink_to_fit(); } + void sprintf (const char* fmtstr, ...); + bool starts_with (const String &other); + String strip (char unwanted) { return strip ({unwanted}); } + String strip (const List<char> &unwanted); + void trim (int n); - METHOD operator+ (const String& data) const -> String; - METHOD operator+ (const char* data) const -> String; - inline METHOD operator+ (int num) const -> String; - inline METHOD operator+= (const String& data) -> String&; - inline METHOD operator+= (const char* data) -> String&; - inline METHOD operator+= (int num) -> String&; - inline METHOD operator+= (char data) -> String&; - inline METHOD operator== (const String& other) const -> bool; - inline METHOD operator== (const char* other) const -> bool; - inline METHOD operator!= (const String& other) const -> bool; - inline METHOD operator!= (const char* other) const -> bool; - inline METHOD operator> (const String& other) const -> bool; - inline METHOD operator< (const String& other) const -> bool; - inline operator const char*() const; - inline operator const std::string&() const; + static String from_number (short int a); + static String from_number (int a); + static String from_number (long int a); + static String from_number (unsigned short int a); + static String from_number (unsigned int a); + static String from_number (unsigned long int a); + static String from_number (double a); + + String operator+ (const String& data) const; + String operator+ (const char* data) const; + String operator+ (int num) const { return *this + String::from_number (num); } + String& operator+= (const String& data) { append (data); return *this; } + 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; } + 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(); } + bool operator!= (const char* other) const { return m_string != other; } + bool operator> (const String& other) const { return std_string() > other.std_string(); } + bool operator< (const String& other) const { return std_string() < other.std_string(); } + bool operator>= (const String& other) const { return std_string() >= other.std_string(); } + bool operator<= (const String& other) const { return std_string() <= other.std_string(); } + operator const char*() const { return chars(); } + operator const std::string&() const { return std_string(); } private: std::string m_string; @@ -138,328 +144,27 @@ class StringList : public List<String> { public: - template<typename... Args> - StringList (Args... args) : - List<String> (args...) {} + using Super = List<String>; + using Super::Super; + + StringList() {} - METHOD join (const String& delim) -> String; + StringList (const Super& other) : + Super (other) {} + + String join (const String& delim); }; // ------------------------------------------------------------------------------------------------- // -inline FUNCTION operator== (const char* a, const String& b) -> bool; -inline FUNCTION operator+ (const char* a, const String& b) -> String; - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::is_empty() const -> bool -{ - return m_string[0] == '\0'; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::append (const char* data) -> void -{ - m_string.append (data); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::append (char data) -> void -{ - m_string.push_back (data); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::append (const String& data) -> void -{ - m_string.append (data.chars()); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::begin() -> std::string::iterator -{ - return m_string.begin(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::begin() const -> std::string::const_iterator -{ - return m_string.cbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline const char* String::chars() const -{ - return m_string.c_str(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::end() -> std::string::iterator -{ - return m_string.end(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::end() const -> std::string::const_iterator -{ - return m_string.end(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::clear() -> void -{ - m_string.clear(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::remove_at (int pos) -> void -{ - m_string.erase (m_string.begin() + pos); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::insert (int pos, char c) -> void -{ - m_string.insert (m_string.begin() + pos, c); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::insert (int pos, const char* c) -> void -{ - m_string.insert (pos, c); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::length() const -> int -{ - return m_string.length(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::remove (int pos, int len) -> void -{ - m_string.replace (pos, len, ""); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::remove_from_start (int len) -> void -{ - remove (0, len); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::remove_from_end (int len) -> void -{ - remove (length() - len, len); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::replace (int pos, int n, const String& a) -> void -{ - m_string.replace (pos, n, a.chars()); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::shrink_to_fit() -> void -{ - m_string.shrink_to_fit(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline const std::string& String::std_string() const -{ - return m_string; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::strip (char unwanted) -> String -{ - return strip ({unwanted}); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator+ (int num) const -> String -{ - return *this + String::from_number (num); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator+= (const String& data) -> String& -{ - append (data); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator+= (const char* data) -> String& -{ - append (data); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator+= (int num) -> String& -{ - return operator+= (String::from_number (num)); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::prepend (String a) -> void -{ - m_string = (a + m_string).std_string(); -} - -// ------------------------------------------------------------------------------------------------- - -inline METHOD -String::operator+= (char data) -> String& -{ - append (data); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator== (const String& other) const -> bool -{ - return std_string() == other.std_string(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator== (const char* other) const -> bool -{ - return operator== (String (other)); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator!= (const String& other) const -> bool -{ - return std_string() != other.std_string(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator!= (const char* other) const -> bool -{ - return operator!= (String (other)); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator> (const String& other) const -> bool -{ - return std_string() > other.std_string(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::operator< (const String& other) const -> bool -{ - return std_string() < other.std_string(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline String::operator const char*() const -{ - return chars(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline String::operator const std::string&() const -{ - return std_string(); -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::modify_index (int& a) -> void -{ - if (a < 0) - a = length() - a; -} - -// ------------------------------------------------------------------------------------------------- -// -inline METHOD -String::index_difference (int a, int b) -> int -{ - modify_index (a); - modify_index (b); - return b - a; -} - -// ------------------------------------------------------------------------------------------------- -// -inline FUNCTION -operator== (const char* a, const String& b) -> bool +inline bool operator== (const char* a, const String& b) { return b == a; } // ------------------------------------------------------------------------------------------------- // -inline FUNCTION -operator+ (const char* a, const String& b) -> String +inline String operator+ (const char* a, const String& b) { return String (a) + b; }
--- a/sources/range.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/range.h Mon May 04 15:51:03 2015 +0300 @@ -48,161 +48,102 @@ { T value; T step; - inline METHOD operator*() -> T&; - inline METHOD operator== (const Iterator& other) const -> bool; - inline METHOD operator!= (const Iterator& other) const -> bool; - inline METHOD operator++() -> Iterator&; + + Iterator (T value, T step) : + value (value), + step (step) {} + + T& operator*() + { + return value; + } + + bool operator== (const Iterator& other) const + { + return value == other.value; + } + + bool operator!= (const Iterator& other) const + { + return value != other.value; + } + + Iterator& operator++() + { + value += step; return *this; + } }; - Range (const T& a, const T& b, const T& step = 1); - Range(); - - METHOD begin() const -> Iterator; - METHOD end() const -> Iterator; - METHOD min() const -> T; - METHOD max() const -> T; - METHOD check_bounds() -> void; - METHOD contains (const T& c) const -> bool; - METHOD contains_exclusively (const T& c) const -> bool; - METHOD overlaps (Range<T> const& other) const -> bool; - METHOD operator== (Range<T> const& other) const -> bool; - METHOD operator!= (Range<T> const& other) const -> bool; -}; + Range (T a, T b, T step = 1) : + m_a (a), + m_b (b), + m_step (step) + { + check_bounds(); + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> -Range<T>::Range (const T& a, const T& b, const T& step) : - m_a (a), - m_b (b), - m_step (step) -{ - check_bounds(); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T> -Range<T>::Range() : - m_a (T()), - m_b (T()) {} + Range() : + m_a (T()), + m_b (T()) {} -// ------------------------------------------------------------------------------------------------- -// -template<typename T> inline METHOD -Range<T>::Iterator::operator*() -> T& -{ - return value; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T> inline METHOD -Range<T>::Iterator::operator== (const Iterator& other) const -> bool -{ - return value == other.value; -} + Iterator begin() const + { + Iterator it; + it.value = min(); + it.step = m_step; + return it; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> inline METHOD -Range<T>::Iterator::operator!= (const Iterator& other) const -> bool -{ - return value != other.value; -} + Iterator end() const + { + Iterator it; + it.value = max() + 1; + it.step = m_step; + return it; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> inline METHOD -Range<T>::Iterator::operator++() -> Iterator& -{ - value += step; - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::check_bounds() -> void -{ - if (m_b < m_a) - std::swap (m_a, m_b); -} + T min() const + { + return m_a; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::contains (const T& c) const -> bool -{ - return (c >= m_a) and (c <= m_b); -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::contains_exclusively (const T& c) const -> bool -{ - return (c > m_a) and (c < m_b); -} + T max() const + { + return m_b; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::overlaps (Range<T> const& other) const -> bool -{ - return contains (other.m_a) or contains (other.m_b); -} + void check_bounds() + { + if (m_b < m_a) + std::swap (m_a, m_b); + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::operator== (Range<T> const& other) const -> bool -{ - return m_a == other.m_a and m_b == other.m_b; -} + bool contains (T c) const + { + return c >= m_a + and c <= m_b; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::operator!= (Range<T> const& other) const -> bool -{ - return not operator== (other); -} + bool contains_exclusively (T c) const + { + return c > m_a + and c < m_b; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::min() const -> T -{ - return m_a; -} + bool overlaps (Range<T> const& other) const + { + return contains (other.m_a) + or contains (other.m_b); + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::max() const -> T -{ - return m_b; -} + bool operator== (Range<T> const& other) const + { + return m_a == other.m_a + and m_b == other.m_b; + } -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::begin() const -> Iterator -{ - Iterator it; - it.value = min(); - it.step = m_step; - return it; -} - -// ------------------------------------------------------------------------------------------------- -// -template<typename T> METHOD -Range<T>::end() const -> Iterator -{ - Iterator it; - it.value = max() + 1; - it.step = m_step; - return it; -} + bool operator!= (Range<T> const& other) const + { + return not operator== (other); + } +}; \ No newline at end of file
--- a/sources/version.cpp Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/version.cpp Mon May 04 15:51:03 2015 +0300 @@ -40,8 +40,7 @@ // ------------------------------------------------------------------------------------------------- // -FUNCTION -full_version_string() -> const char* +const char* full_version_string() { #ifdef IS_RELEASE return VERSION_STRING; @@ -62,8 +61,7 @@ // ------------------------------------------------------------------------------------------------- // -FUNCTION -changeset_date_string() -> const char* +const char* changeset_date_string() { return HG_DATE_STRING; }
--- a/sources/version.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/version.h Mon May 04 15:51:03 2015 +0300 @@ -55,14 +55,13 @@ // ------------------------------------------------------------------------------------------------- // Returns the bare version string (1.2.3) -inline FUNCTION -version_string() -> const char* +inline const char* version_string() { return VERSION_STRING; } // Returns full version string, with hash (1.2.3-abcd456) -FUNCTION full_version_string() -> const char*; +const char* full_version_string(); // Returns changeset date string -FUNCTION changeset_date_string() -> const char*; +const char* changeset_date_string(); \ No newline at end of file