# HG changeset patch # User Teemu Piippo # Date 1469016217 -10800 # Node ID d9073c13dc98d1d91a5576452042a7c4eb1e3751 # Parent 8fb1c657e0b019a40e4ddb4e2750e5817dffa884# Parent e49aa4aa98c0a2279b279168dded5fda683f8637 Merged with default diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/coloredline.cpp --- a/sources/coloredline.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/coloredline.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -129,10 +129,8 @@ { String color = m_incomingColorName.to_lowercase(); - for (size_t i = 0; i < countof(colorCodes); ++i) + for (const ColorCodeInfo &colorInfo : colorCodes) { - const ColorCodeInfo& colorInfo = colorCodes[i]; - if (String(colorInfo.name).to_lowercase() == color) { activate_color(colorInfo.color, colorInfo.bold); diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/interface.cpp --- a/sources/interface.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/interface.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -41,30 +41,30 @@ // ------------------------------------------------------------------------------------------------- // -chtype Interface::color_pair (Color fg, Color bg) +chtype Interface::getColorPair(Color fg, Color bg) { if (fg == DEFAULT && bg == DEFAULT) return 0; else - return COLOR_PAIR (1 + (int (fg) * NUM_COLORS) + int (bg)); + return COLOR_PAIR(1 + (int(fg) * NUM_COLORS) + int(bg)); } // ------------------------------------------------------------------------------------------------- // -const String& Interface::current_input() +const String& Interface::getCurrentInput() { return m_inputHistory[m_inputCursor]; } // ------------------------------------------------------------------------------------------------- // -// Makes current_input() the lastmost input (so that we won't modify history) +// Makes current_input() the lastmost input(so that we won't modify history) // -void Interface::detach_input() +void Interface::detachInput() { if (m_inputCursor > 0) { - m_inputHistory[0] = current_input(); + m_inputHistory[0] = getCurrentInput(); m_inputCursor = 0; } } @@ -72,15 +72,15 @@ // ------------------------------------------------------------------------------------------------- // A version of current_input() that allows changing the contents of it. // -String& Interface::mutable_current_input() +String& Interface::getEditableInput() { - detach_input(); + detachInput(); return m_inputHistory[m_inputCursor]; } // ------------------------------------------------------------------------------------------------- // -void Interface::move_input_cursor (int delta) +void Interface::moveInputCursor(int delta) { // No input history when inputting addresses or passwords if (m_inputState != INPUTSTATE_NORMAL) @@ -90,18 +90,18 @@ } int oldcursor = m_inputCursor; - m_inputCursor = clamp (m_inputCursor + delta, 0, m_inputHistory.size() - 1); + m_inputCursor = clamp(m_inputCursor + delta, 0, m_inputHistory.size() - 1); if (m_inputCursor != oldcursor) { - m_cursorPosition = current_input().length(); + m_cursorPosition = getCurrentInput().length(); m_needInputRender = true; } } // ------------------------------------------------------------------------------------------------- // -String Interface::prompt_string() +String Interface::getPromptString() { String prompt; @@ -118,21 +118,21 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::set_input_state (InputState newstate) +void Interface::setInputState(InputState newstate) { - // Clear the input row (unless going to or from confirm state) + // Clear the input row(unless going to or from confirm state) if (newstate != INPUTSTATE_CONFIRM_DISCONNECTION and m_inputState != INPUTSTATE_CONFIRM_DISCONNECTION) { m_inputCursor = 0; - mutable_current_input().clear(); + getEditableInput().clear(); } switch (newstate) { case INPUTSTATE_ADDRESS: if (m_remoteAddress.host != 0) - mutable_current_input() = m_remoteAddress.to_string (IPAddress::WITH_PORT); + getEditableInput() = m_remoteAddress.to_string(IPAddress::WITH_PORT); break; default: @@ -146,73 +146,73 @@ // ------------------------------------------------------------------------------------------------- // Interface::Interface() : - m_inputCursor (0), - m_cursorPosition (0), - m_inputPanning (0), - m_needRefresh (false), - m_needStatusBarRender (false), - m_needInputRender (false), - m_needOutputRender (false), - m_needNicklistRender (false), - m_outputScroll (0), - m_inputState (INPUTSTATE_NORMAL), - m_disconnectCallback (nullptr) + m_inputCursor(0), + m_cursorPosition(0), + m_inputPanning(0), + m_needRefresh(false), + m_needStatusBarRender(false), + m_needInputRender(false), + m_needOutputRender(false), + m_needNicklistRender(false), + m_outputScroll(0), + m_inputState(INPUTSTATE_NORMAL), + m_disconnectCallback(nullptr) { ::initscr(); ::raw(); - ::keypad (stdscr, true); + ::keypad(stdscr, true); ::noecho(); ::refresh(); - ::timeout (0); + ::timeout(0); m_inputHistory.clear(); m_inputHistory << ""; m_outputLines.clear(); m_outputLines << ColoredLine(); - m_session.set_interface (this); - reset_title(); + m_session.set_interface(this); + resetTitle(); if (::has_colors()) { ::start_color(); - bool hasDefaultColors = (::use_default_colors() == OK); + bool hasDefaultColors =(::use_default_colors() == OK); int defaultFg = hasDefaultColors ? -1 : COLOR_WHITE; int defaultBg = hasDefaultColors ? -1 : COLOR_BLACK; // Initialize color pairs - for (int i = 0; i < NUM_COLORS; ++i) - for (int j = 0; j < NUM_COLORS; ++j) + for (int i : range(NUM_COLORS)) + for (int j : range(NUM_COLORS)) { int pairnum = 1 + (i * NUM_COLORS + j); - int fg = (i == DEFAULT) ? defaultFg : i; - int bg = (j == DEFAULT) ? defaultBg : j; + int fg =(i == DEFAULT) ? defaultFg : i; + int bg =(j == DEFAULT) ? defaultBg : j; if (fg != -1 || bg != -1) { - if (::init_pair (pairnum, fg, bg) == ERR) - print_warning ("Unable to initialize color pair %d (%d, %d)\n", pairnum, fg, bg); + if (::init_pair(pairnum, fg, bg) == ERR) + printWarning("Unable to initialize color pair %d(%d, %d)\n", pairnum, fg, bg); } } } - render_full(); + renderFull(); refresh(); m_needRefresh = false; } // ------------------------------------------------------------------------------------------------- // -void Interface::render_titlebar() +void Interface::renderTitlebar() { if (m_title.length() <= COLS) { - chtype pair = color_pair (WHITE, BLUE); - int startx = (COLS - m_title.length()) / 2; + chtype pair = getColorPair(WHITE, BLUE); + int startx =(COLS - m_title.length()) / 2; int endx = startx + m_title.length(); - attron (pair); - mvprintw (0, startx, "%s", m_title.chars()); - mvhline (0, 0, ' ', startx); - mvhline (0, endx, ' ', COLS - endx); - attroff (pair); + attron(pair); + mvprintw(0, startx, "%s", m_title.chars()); + mvhline(0, 0, ' ', startx); + mvhline(0, endx, ' ', COLS - endx); + attroff(pair); } m_needRefresh = true; @@ -220,20 +220,20 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::set_title (const String& title) +void Interface::setTitle(const String& title) { m_title = title; - render_titlebar(); + renderTitlebar(); } // ------------------------------------------------------------------------------------------------- // -void Interface::safe_disconnect (std::function afterwards) +void Interface::safeDisconnect(std::function afterwards) { if (m_session.is_active()) { m_disconnectCallback = afterwards; - set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION); + setInputState(INPUTSTATE_CONFIRM_DISCONNECTION); } else afterwards(false); @@ -241,30 +241,28 @@ // ------------------------------------------------------------------------------------------------- // -int Interface::nicklist_width() +int Interface::nicklistWidth() { // Allocate at least 12 characters, at most 24 characters, for the nicklist. If we cannot - // afford that (o_O) then we probably shouldn't draw the nicklist at all I think. + // afford that(o_O) then we probably shouldn't draw the nicklist at all I think. int nicklistWidth = COLS / 4; if (nicklistWidth < 12) return 0; - return min (nicklistWidth, 24); + return min(nicklistWidth, 24); } // ------------------------------------------------------------------------------------------------- // Renders the given colored line onto the screen. Will wrap if allowWrap is true. Returns the // 'y' value for the next line. // -int Interface::render_colorline (int y, int x0, int width, const ColoredLine& line, bool allowWrap) +int Interface::renderColorline(int y, int x0, int width, const ColoredLine& line, bool allowWrap) { int x = x0; - for (int i = 0; i < line.data().size(); ++i) + for (int byte : line.data()) { - int byte = line.data()[i]; - if (x == x0 + width) { if (not allowWrap) @@ -274,24 +272,24 @@ ++y; } - if (byte < 256 && isprint (byte)) + if (byte < 256 && isprint(byte)) { - mvaddch (y, x, char (byte)); + mvaddch(y, x, char(byte)); ++x; } - else if (byte >= RLINE_ON_COLOR and byte < (RLINE_ON_COLOR + 16)) + else if (byte >= RLINE_ON_COLOR and byte <(RLINE_ON_COLOR + 16)) { - auto attrfunction = (byte < RLINE_OFF_COLOR ? &attron : &attroff); - (*attrfunction) (color_pair (Color ((byte - RLINE_ON_COLOR) & 7), DEFAULT)); + auto attrfunction =(byte < RLINE_OFF_COLOR ? &attron : &attroff); + (*attrfunction)(getColorPair(Color((byte - RLINE_ON_COLOR) & 7), DEFAULT)); } else switch (byte) { case RLINE_ON_BOLD: - attron (A_BOLD); + attron(A_BOLD); break; case RLINE_OFF_BOLD: - attroff (A_BOLD); + attroff(A_BOLD); break; } } @@ -301,15 +299,15 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::render_output() +void Interface::renderOutput() { if (m_outputLines.size() == 1) return; - m_outputScroll = clamp (m_outputScroll, 0, m_outputLines.size() - 1); + m_outputScroll = clamp(m_outputScroll, 0, m_outputLines.size() - 1); int height = LINES - 3; - int width = COLS - nicklist_width(); + int width = COLS - nicklistWidth(); int printOffset = 0; int end = m_outputLines.size() - 1 - m_outputScroll; int start = end; @@ -320,7 +318,7 @@ // Where to start? while (start > 0) { - int rows = m_outputLines[start - 1].rows (width); + int rows = m_outputLines[start - 1].rows(width); if (usedHeight + rows > height) { @@ -333,12 +331,12 @@ usedHeight += rows; } - // See if there's any more rows to use (end may be too small) + // See if there's any more rows to use(end may be too small) if (not tightFit) { while (end < m_outputLines.size()) { - int rows = m_outputLines[end].rows (width); + int rows = m_outputLines[end].rows(width); if (usedHeight + rows > height) { @@ -359,17 +357,17 @@ if (start < 0 or start == end or printOffset >= height) return; - assert (start <= end and start - end <= height); + assert(start <= end and start - end <= height); // Clear the display - for (int i = y; i < y + height; ++i) - mvhline (i, 0, ' ', width); + for (int i : range(height)) + mvhline(y + i, 0, ' ', width); // Print the lines y += printOffset; - for (int i = start; i < end; ++i) - y = render_colorline (y, 0, width, m_outputLines[i], true); + for (int i : range(start, end)) + y = renderColorline(y, 0, width, m_outputLines[i], true); m_needOutputRender = false; m_needRefresh = true; @@ -377,22 +375,22 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::render_nicklist() +void Interface::renderNicklist() { - int width = nicklist_width(); + int width = nicklistWidth(); int height = LINES- 3; int y = 1; int x = COLS - width; - if (width == 0) + if (width > 0) return; - for (int i = 0; i < height; ++i) + for (int i : range(height)) { - mvhline (y, x, ' ', width); + mvhline(y, x, ' ', width); if (i < m_playerNames.size()) - render_colorline (y, x, width, m_playerNames[i], false); + renderColorline(y, x, width, m_playerNames[i], false); y++; } @@ -403,36 +401,36 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::render_input() +void Interface::renderInput() { - chtype promptColor = color_pair (WHITE, BLUE); + chtype promptColor = getColorPair(WHITE, BLUE); // If we're asking the user if they want to disconnect, we don't render any input strings, // just the confirmation message. if (m_inputState == 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); + attron(promptColor); + mvhline(LINES - 2, 0, ' ', COLS); + mvprintw(LINES - 2, 0, "Are you sure you want to disconnect? y/n"); + attroff(promptColor); m_needRefresh = true; return; } - String prompt = prompt_string(); + String prompt = getPromptString(); int displayLength = COLS - prompt.length() - 2; - String displayString = current_input(); + String displayString = getCurrentInput(); int y = LINES - 2; // If we're inputting a password, replace it with asterisks if (m_inputState == INPUTSTATE_PASSWORD) { - for (int i = 0; i < displayString.length(); ++i) - displayString[i] = '*'; + for (char &ch : displayString) + ch = '*'; } // Ensure the cursor is within bounds - m_cursorPosition = clamp (m_cursorPosition, 0, displayString.length()); + m_cursorPosition = clamp(m_cursorPosition, 0, displayString.length()); // Ensure that the cursor is always in view, adjust panning if this is not the case if (m_cursorPosition > m_inputPanning + displayLength) @@ -442,19 +440,19 @@ // What part of the string to draw? int start = m_inputPanning; - int end = min (displayString.length(), start + displayLength); - assert (m_cursorPosition >= start and m_cursorPosition <= end); + int end = min(displayString.length(), start + displayLength); + assert(m_cursorPosition >= start and m_cursorPosition <= end); // Render the input string - mvhline (LINES - 2, 0, ' ', COLS); - mvprintw (y, prompt.length() + 1, "%s", displayString.mid (start, end).chars()); + mvhline(LINES - 2, 0, ' ', COLS); + mvprintw(y, prompt.length() + 1, "%s", displayString.mid(start, end).chars()); // Render the prompt - attron (promptColor); - mvprintw (y, 0, "%s", prompt.chars()); - attroff (promptColor); + attron(promptColor); + mvprintw(y, 0, "%s", prompt.chars()); + attroff(promptColor); - // Store in memory where the cursor is now (so that we can re-draw it to position the terminal + // Store in memory where the cursor is now(so that we can re-draw it to position the terminal // cursor). m_cursorCharacter.ch = m_cursorPosition != 0 ? displayString[m_cursorPosition - 1] : '\0'; m_cursorCharacter.x = prompt.length() + (m_cursorPosition - m_inputPanning); @@ -464,21 +462,21 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::render_statusbar() +void Interface::renderStatusBar() { - chtype color = color_pair (WHITE, BLUE); + chtype color = getColorPair(WHITE, BLUE); int y = LINES - 1; - attron (color); - mvhline (y, 0, ' ', COLS); - mvprintw (y, 0, "%s", m_statusBarText.chars()); - attroff (color); + attron(color); + mvhline(y, 0, ' ', COLS); + mvprintw(y, 0, "%s", m_statusBarText.chars()); + attroff(color); m_needRefresh = true; m_needStatusBarRender = false; } // ------------------------------------------------------------------------------------------------- // -void Interface::update_statusbar() +void Interface::updateStatusBar() { String text; @@ -490,7 +488,7 @@ case RCON_CONNECTING: case RCON_AUTHENTICATING: - text = "Connecting to " + m_session.address().to_string (IPAddress::WITH_PORT) + "..."; + text = "Connecting to " + m_session.address().to_string(IPAddress::WITH_PORT) + "..."; break; case RCON_CONNECTED: @@ -503,12 +501,12 @@ } else { - adminText.sprintf ("%d other admin%s", m_session.num_admins(), + adminText.sprintf("%d other admin%s", m_session.num_admins(), m_session.num_admins() != 1 ? "s" : ""); } - text.sprintf ("%s | %s | %s", - m_session.address().to_string (IPAddress::WITH_PORT).chars(), + text.sprintf("%s | %s | %s", + m_session.address().to_string(IPAddress::WITH_PORT).chars(), m_session.level().chars(), adminText.chars()); } @@ -519,7 +517,7 @@ text += " | "; text += "Ctrl+N to connect, Ctrl+Q to "; - text += (m_session.state() == RCON_DISCONNECTED) ? "quit" : "disconnect"; + text +=(m_session.state() == RCON_DISCONNECTED) ? "quit" : "disconnect"; if (text != m_statusBarText) { @@ -530,19 +528,19 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::render_full() +void Interface::renderFull() { - update_statusbar(); - render_titlebar(); - render_output(); - render_statusbar(); - render_input(); - render_nicklist(); + updateStatusBar(); + renderTitlebar(); + renderOutput(); + renderStatusBar(); + renderInput(); + renderNicklist(); } // ------------------------------------------------------------------------------------------------- // -void Interface::position_cursor() +void Interface::positionCursor() { // This is only relevant if the input string is being drawn if (m_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) @@ -551,24 +549,24 @@ int y = LINES - 2; if (m_cursorCharacter.ch != '\0') - mvprintw (y, m_cursorCharacter.x, "%c", m_cursorCharacter.ch); + mvprintw(y, m_cursorCharacter.x, "%c", m_cursorCharacter.ch); else - mvprintw (y, prompt_string().length(), " "); + mvprintw(y, getPromptString().length(), " "); } // ------------------------------------------------------------------------------------------------- // -int Interface::find_previous_word() +int Interface::findPreviousWord() { - const String& input = current_input(); + const String& input = getCurrentInput(); int pos = m_cursorPosition; // Move past whitespace - while (pos > 0 and isspace (input[pos - 1])) + while (pos > 0 and isspace(input[pos - 1])) pos--; // Move past the word - while (pos > 0 and not isspace (input[pos - 1])) + while (pos > 0 and not isspace(input[pos - 1])) pos--; return pos; @@ -576,17 +574,17 @@ // ------------------------------------------------------------------------------------------------- // -int Interface::find_next_word() +int Interface::findNextWord() { - const String& input = current_input(); + const String& input = getCurrentInput(); int pos = m_cursorPosition; // Move past current whitespace - while (pos < input.length() and isspace (input[pos])) + while (pos < input.length() and isspace(input[pos])) pos++; // Move past the word - while (input[pos] != '\0' and not isspace (input[pos])) + while (input[pos] != '\0' and not isspace(input[pos])) pos++; return pos; @@ -594,7 +592,7 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::yank (int a, int b) +void Interface::yank(int a, int b) { if (a >= b) return; @@ -602,15 +600,15 @@ if (m_cursorPosition > a and m_cursorPosition <= b) m_cursorPosition = a; - String& input = mutable_current_input(); - m_pasteBuffer = input.mid (a, b); - input.remove (a, b - a); + String& input = getEditableInput(); + m_pasteBuffer = input.mid(a, b); + input.remove(a, b - a); m_needInputRender = true; } // ------------------------------------------------------------------------------------------------- // -void Interface::handle_input() +void Interface::handleInput() { int ch = ::getch(); @@ -620,7 +618,7 @@ if (ch == KEY_RESIZE) { ::clear(); - render_full(); + renderFull(); return; } @@ -632,14 +630,14 @@ m_disconnectCallback(true); } else if (ch == 'n' or ch == 'N') - set_input_state (INPUTSTATE_NORMAL); + setInputState(INPUTSTATE_NORMAL); return; } if (ch >= 0x20 and ch <= 0x7E) { - mutable_current_input().insert (m_cursorPosition++, char (ch)); + getEditableInput().insert(m_cursorPosition++, char(ch)); m_needInputRender = true; } else switch (ch) @@ -651,11 +649,11 @@ break; case INPUTSTATE_NORMAL: - safe_disconnect ([&](bool hadsession) + safeDisconnect([&](bool hadsession) { if (hadsession) { - set_input_state (INPUTSTATE_NORMAL); + setInputState(INPUTSTATE_NORMAL); } else { @@ -666,11 +664,11 @@ break; case INPUTSTATE_PASSWORD: - set_input_state (INPUTSTATE_ADDRESS); + setInputState(INPUTSTATE_ADDRESS); break; case INPUTSTATE_ADDRESS: - set_input_state (INPUTSTATE_NORMAL); + setInputState(INPUTSTATE_NORMAL); } break; @@ -685,7 +683,7 @@ case KEY_RIGHT: case 'F' - 'A' + 1: // readline ^F - if (m_cursorPosition < current_input().length()) + if (m_cursorPosition < getCurrentInput().length()) { m_cursorPosition++; m_needInputRender = true; @@ -694,7 +692,7 @@ case KEY_DOWN: case KEY_UP: - move_input_cursor (ch == KEY_DOWN ? -1 : 1); + moveInputCursor(ch == KEY_DOWN ? -1 : 1); break; case KEY_HOME: @@ -708,9 +706,9 @@ case KEY_END: case 'E' - 'A' + 1: // readline ^E - if (m_cursorPosition != current_input().length()) + if (m_cursorPosition != getCurrentInput().length()) { - m_cursorPosition = current_input().length(); + m_cursorPosition = getCurrentInput().length(); m_needInputRender = true; } break; @@ -719,50 +717,50 @@ case '\b': if (m_cursorPosition > 0) { - mutable_current_input().remove_at (--m_cursorPosition); + getEditableInput().remove_at(--m_cursorPosition); m_needInputRender = true; } break; case KEY_DC: case 'D' - 'A' + 1: // readline ^D - if (m_cursorPosition < current_input().length()) + if (m_cursorPosition < getCurrentInput().length()) { - mutable_current_input().remove_at (m_cursorPosition); + getEditableInput().remove_at(m_cursorPosition); m_needInputRender = true; } break; case KEY_PPAGE: - m_outputScroll += min (PAGE_SIZE, LINES / 2); + m_outputScroll += min(PAGE_SIZE, LINES / 2); m_needOutputRender = true; break; case KEY_NPAGE: - m_outputScroll -= min (PAGE_SIZE, LINES / 2); + m_outputScroll -= min(PAGE_SIZE, LINES / 2); m_needOutputRender = true; break; case 'U' - 'A' + 1: // readline ^U - delete from start to cursor if (m_cursorPosition > 0) { - yank (0, m_cursorPosition); + yank(0, m_cursorPosition); m_cursorPosition = 0; } break; case 'K' - 'A' + 1: // readline ^K - delete from cursor to end - yank (m_cursorPosition, mutable_current_input().length()); + yank(m_cursorPosition, getEditableInput().length()); break; case 'W' - 'A' + 1: // readline ^W - delete from previous word bounary to current - yank (find_previous_word(), m_cursorPosition); + yank(findPreviousWord(), m_cursorPosition); break; case 'Y' - 'A' + 1: // readline ^Y - paste previously deleted text if (not m_pasteBuffer.is_empty()) { - mutable_current_input().insert (m_cursorPosition, m_pasteBuffer); + getEditableInput().insert(m_cursorPosition, m_pasteBuffer); m_cursorPosition += m_pasteBuffer.length(); m_needInputRender = true; } @@ -770,14 +768,14 @@ case '\t': { - int space = current_input().find (" "); + int space = getCurrentInput().find(" "); if (m_inputState == INPUTSTATE_NORMAL and m_cursorPosition > 0 - and (space == -1 or space >= m_cursorPosition)) + and(space == -1 or space >= m_cursorPosition)) { - String start = current_input().mid (0, m_cursorPosition); - m_session.request_tab_complete (start); + String start = getCurrentInput().mid(0, m_cursorPosition); + m_session.request_tab_complete(start); } } break; @@ -793,39 +791,39 @@ case INPUTSTATE_ADDRESS: try { - m_remoteAddress = IPAddress::from_string (current_input()); + m_remoteAddress = IPAddress::from_string(getCurrentInput()); } catch (std::exception& e) { - print ("%s\n", e.what()); + print("%s\n", e.what()); return; } if (m_remoteAddress.port == 0) m_remoteAddress.port = 10666; - set_input_state (INPUTSTATE_PASSWORD); + setInputState(INPUTSTATE_PASSWORD); break; case INPUTSTATE_PASSWORD: - if (m_inputState == INPUTSTATE_PASSWORD and not current_input().is_empty()) + if (m_inputState == INPUTSTATE_PASSWORD and not getCurrentInput().is_empty()) { m_session.disconnect(); - m_session.set_password (current_input()); - m_session.connect (m_remoteAddress); - set_input_state (INPUTSTATE_NORMAL); + m_session.set_password(getCurrentInput()); + m_session.connect(m_remoteAddress); + setInputState(INPUTSTATE_NORMAL); } break; case INPUTSTATE_NORMAL: - if (current_input()[0] == '/') + if (getCurrentInput()[0] == '/') { - handle_command(current_input()); - flush_input(); + handleCommand(getCurrentInput()); + flushInput(); } - else if (m_session.send_command (current_input())) + else if (m_session.send_command(getCurrentInput())) { - flush_input(); + flushInput(); } break; } @@ -833,7 +831,7 @@ case 'N' - 'A' + 1: // ^N if (m_inputState == INPUTSTATE_NORMAL) - safe_disconnect ([&](bool){set_input_state (INPUTSTATE_ADDRESS);}); + safeDisconnect([&](bool){setInputState(INPUTSTATE_ADDRESS);}); break; case '\x1b': // Escape @@ -847,26 +845,26 @@ case 'b': case 'B': // readline alt-b - move one word to the left - m_cursorPosition = find_previous_word(); + m_cursorPosition = findPreviousWord(); m_needInputRender = true; break; case 'f': case 'F': // readline alt-f - move one word to the right - m_cursorPosition = find_next_word(); + m_cursorPosition = findNextWord(); m_needInputRender = true; break; case 'd': case 'D': // readline alt-d - delete from here till next word boundary - yank (m_cursorPosition, find_next_word()); + yank(m_cursorPosition, findNextWord()); break; case KEY_BACKSPACE: // alt+backspace, remove previous word case '\b': - yank (find_previous_word(), m_cursorPosition); + yank(findPreviousWord(), m_cursorPosition); break; } } @@ -874,9 +872,9 @@ { // No alt-key, handle pure escape if (m_inputState == INPUTSTATE_PASSWORD) - set_input_state (INPUTSTATE_ADDRESS); + setInputState(INPUTSTATE_ADDRESS); else if (m_inputState == INPUTSTATE_ADDRESS) - set_input_state (INPUTSTATE_NORMAL); + setInputState(INPUTSTATE_NORMAL); } break; } @@ -888,14 +886,14 @@ // void Interface::render() { - if (m_needStatusBarRender) render_statusbar(); - if (m_needInputRender) render_input(); - if (m_needOutputRender) render_output(); - if (m_needNicklistRender) render_nicklist(); + if (m_needStatusBarRender) renderStatusBar(); + if (m_needInputRender) renderInput(); + if (m_needOutputRender) renderOutput(); + if (m_needNicklistRender) renderNicklist(); if (m_needRefresh) { - position_cursor(); + positionCursor(); refresh(); m_needRefresh = false; } @@ -903,68 +901,66 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::vprint (const char* fmtstr, va_list args) +void Interface::vprint(const char* fmtstr, va_list args) { String message; - message.vsprintf (fmtstr, args); - print_to_console (message); + message.vsprintf(fmtstr, args); + printToConsole(message); } // ------------------------------------------------------------------------------------------------- // -void __cdecl Interface::print_text (const char* fmtstr, ...) +void __cdecl Interface::printText(const char* fmtstr, ...) { va_list args; - va_start (args, fmtstr); - vprint (fmtstr, args); - va_end (args); + va_start(args, fmtstr); + vprint(fmtstr, args); + va_end(args); } // ------------------------------------------------------------------------------------------------- // -void __cdecl Interface::print (const char* fmtstr, ...) +void __cdecl Interface::print(const char* fmtstr, ...) { va_list args; - va_start (args, fmtstr); - print_to_console (TEXTCOLOR_BrightBlue); - vprint (fmtstr, args); - va_end (args); + va_start(args, fmtstr); + printToConsole(TEXTCOLOR_BrightBlue); + vprint(fmtstr, args); + va_end(args); } // ------------------------------------------------------------------------------------------------- // -void __cdecl Interface::print_warning (const char* fmtstr, ...) +void __cdecl Interface::printWarning(const char* fmtstr, ...) { va_list args; - va_start (args, fmtstr); - print_to_console (TEXTCOLOR_BrightYellow "-!- "); - vprint (fmtstr, args); - va_end (args); + va_start(args, fmtstr); + printToConsole(TEXTCOLOR_BrightYellow "-!- "); + vprint(fmtstr, args); + va_end(args); } // ------------------------------------------------------------------------------------------------- // -void __cdecl Interface::print_error (const char* fmtstr, ...) +void __cdecl Interface::printError(const char* fmtstr, ...) { va_list args; - va_start (args, fmtstr); - print_to_console (TEXTCOLOR_BrightRed "!!! "); - vprint (fmtstr, args); - va_end (args); + va_start(args, fmtstr); + printToConsole(TEXTCOLOR_BrightRed "!!! "); + vprint(fmtstr, args); + va_end(args); } // ------------------------------------------------------------------------------------------------- // -void Interface::print_to_console (String message) +void Interface::printToConsole(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. - message.replace ("\\c", "\x1C"); + message.replace("\\c", "\x1C"); - for (int i = 0; i < message.length(); ++i) + for (char ch : message) { - char ch = message[i]; - if (ch == '\n') { m_outputLines.last().finalize(); @@ -975,19 +971,19 @@ if (m_outputLines.last().length() == 0) { time_t now; - time (&now); + time(&now); char timestamp[32]; - strftime (timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime (&now)); + strftime(timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime(&now)); - for (char* cp = timestamp; *cp != '\0'; ++cp) - m_outputLines.last().add_char (*cp); + for (char ch : String(timestamp)) + m_outputLines.last().add_char(ch); } // Remove some lines if there's too many of them. 20,000 should be enough, I hope. while (m_outputLines.size() > 20000) m_outputLines.remove_at(0); - m_outputLines.last().add_char (ch); + m_outputLines.last().add_char(ch); } m_needOutputRender = true; @@ -995,15 +991,15 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::connect (String address, String password) +void Interface::connect(String address, String password) { try { - m_remoteAddress = IPAddress::from_string (address); + m_remoteAddress = IPAddress::from_string(address); } catch (std::exception& e) { - print ("%s\n", e.what()); + print("%s\n", e.what()); return; } @@ -1011,22 +1007,22 @@ m_remoteAddress.port = 10666; m_session.disconnect(); - m_session.set_password (password); - m_session.connect (m_remoteAddress); + m_session.set_password(password); + m_session.connect(m_remoteAddress); } // ------------------------------------------------------------------------------------------------- // -void Interface::set_player_names (const StringList& names) +void Interface::setPlayerNames(const StringList& names) { m_playerNames.clear(); for (const String& name : names) { ColoredLine coloredname; - coloredname.add_string (name); + coloredname.add_string(name); coloredname.finalize(); - m_playerNames.append (coloredname); + m_playerNames.append(coloredname); } m_needNicklistRender = true; @@ -1034,16 +1030,16 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::tab_complete (const String& part, String complete) +void Interface::tabComplete(const String& part, String complete) { - String& input = mutable_current_input(); + String& input = getEditableInput(); - if (input.starts_with (part)) + if (input.starts_with(part)) { if (input[part.length()] != ' ') complete += ' '; - input.replace (0, part.length(), complete); + input.replace(0, part.length(), complete); m_cursorPosition = complete.length(); m_needInputRender = true; } @@ -1051,7 +1047,7 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::handle_command(const String& input) +void Interface::handleCommand(const String& input) { if (input[0] != '/') return; @@ -1064,7 +1060,7 @@ { if (args.size() != 2) { - print_error("Usage: /connect
\n"); + printError("Usage: /connect
\n"); } else { @@ -1076,7 +1072,7 @@ } catch (std::exception& e) { - print_error("%s\n", e.what()); + printError("%s\n", e.what()); return; } @@ -1106,7 +1102,7 @@ print_error("No CVars to watch.\n"); } else - print_error("Unknown command: %s\n", command.chars()); + printError("Unknown command: %s\n", command.chars()); } // ------------------------------------------------------------------------------------------------- @@ -1114,22 +1110,22 @@ void Interface::disconnected() { print("Disconnected from %s\n", m_session.address().to_string(IPAddress::WITH_PORT).chars()); - reset_title(); - render_full(); + resetTitle(); + renderFull(); } // ------------------------------------------------------------------------------------------------- // -void Interface::reset_title() +void Interface::resetTitle() { - m_title.sprintf ("%s %s (%s)", application_name(), full_version_string(), changeset_date_string()); + m_title.sprintf("%s %s (%s)", application_name(), full_version_string(), changeset_date_string()); } // ------------------------------------------------------------------------------------------------- // -void Interface::flush_input() +void Interface::flushInput() { - m_inputHistory.insert (0, ""); + m_inputHistory.insert(0, ""); m_inputCursor = 0; m_needInputRender = true; } diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/interface.h --- a/sources/interface.h Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/interface.h Wed Jul 20 15:03:37 2016 +0300 @@ -49,24 +49,23 @@ }; Interface(); - void handle_input(); + void connect(String address, String password); + void disconnected(); + RCONSession* getSession() { return &m_session; } + void handleCommand(const String& input); + void handleInput(); + void needRefresh(); + void __cdecl print(const char* fmtstr, ...); + void __cdecl printWarning(const char* fmtstr, ...); + void __cdecl printError(const char* fmtstr, ...); + void __cdecl printText(const char* fmtstr, ...); void render(); - void render_full(); - void set_title (const String& message); - void update_statusbar(); - void connect (String address, String password); - void set_player_names (const StringList& names); - void need_refresh(); - void tab_complete (const String& part, String complete); - RCONSession* get_session() { return &m_session; } - void handle_command(const String& input); - void disconnected(); - - void vprint (const char* fmtstr, va_list args); - void __cdecl print (const char* fmtstr, ...); - void __cdecl print_warning (const char* fmtstr, ...); - void __cdecl print_error (const char* fmtstr, ...); - void __cdecl print_text (const char* fmtstr, ...); + void renderFull(); + void setPlayerNames(const StringList& names); + void setTitle(const String& message); + void tabComplete(const String& part, String complete); + void updateStatusBar(); + void vprint(const char* fmtstr, va_list args); private: StringList m_inputHistory; @@ -90,28 +89,28 @@ String m_pasteBuffer; RCONSession m_session; - void render_titlebar(); - 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(); - void render_nicklist(); - void render_input(); - void render_statusbar(); - void position_cursor(); - chtype color_pair (Color fg, Color bg); - const String& current_input(); - void detach_input(); - String& mutable_current_input(); - void move_input_cursor (int delta); - String prompt_string(); - void set_input_state (InputState newstate); - void print_to_console (String message); - void yank (int a, int b); - int find_previous_word(); - int find_next_word(); - void reset_title(); - void flush_input(); + void detachInput(); + int findNextWord(); + int findPreviousWord(); + void flushInput(); + chtype getColorPair(Color fg, Color bg); + const String& getCurrentInput(); + String& getEditableInput(); + String getPromptString(); + void moveInputCursor(int delta); + int nicklistWidth(); + void positionCursor(); + void printToConsole(String message); + int renderColorline(int y, int x0, int width, const ColoredLine& line, bool allowWrap); + void renderInput(); + void renderNicklist(); + void renderOutput(); + void renderStatusBar(); + void renderTitlebar(); + void resetTitle(); + void safeDisconnect(std::function afterwards); + void setInputState(InputState newstate); + void yank(int a, int b); }; END_ZFC_NAMESPACE \ No newline at end of file diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/list.h --- a/sources/list.h Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/list.h Wed Jul 20 15:03:37 2016 +0300 @@ -263,7 +263,7 @@ Self result; - for (int i = a; i <= b; ++i) + for (int i = a; i < b; ++i) result << operator[] (i); return result; diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/main.cpp --- a/sources/main.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/main.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -84,7 +84,7 @@ FD_ZERO (&fdset); FD_SET (0, &fdset); - int fd = iface.get_session()->socket()->file_descriptor(); + int fd = iface.getSession()->socket()->file_descriptor(); highest = zfc::max (highest, fd); FD_SET (fd, &fdset); @@ -93,15 +93,15 @@ if (FD_ISSET (0, &fdset)) { // stdin is ready, what's incoming? - iface.handle_input(); + iface.handleInput(); } - iface.get_session()->tick(); + iface.getSession()->tick(); iface.render(); } } catch (const zfc::Exitception&) {} - iface.get_session()->disconnect(); + iface.getSession()->disconnect(); return EXIT_SUCCESS; } diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/mystring.cpp --- a/sources/mystring.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/mystring.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -70,13 +70,9 @@ { String result (m_string); - for (int i = 0; i < unwanted.size(); ++i) - { - String c = unwanted[i]; - - for (int pos = 0; (pos = result.find (c)) != -1;) - result.remove_at (pos--); - } + for (String c : unwanted) + for (int pos = 0; (pos = result.find (c)) != -1;) + result.remove_at (pos--); return result; } @@ -87,10 +83,10 @@ { String result (m_string); - for (int i = 0; i < result.length(); ++i) + for (char &ch : result) { - if (islower (result[i])) - result[i] -= 'a' - 'A'; + if (islower(ch)) + ch -= 'a' - 'A'; } return result; @@ -102,10 +98,10 @@ { String result (m_string); - for (int i = 0; i < result.length(); ++i) + for (char &ch : result) { - if (isupper (result[i])) - result[i] += 'a' - 'A'; + if (isupper(ch)) + ch += 'a' - 'A'; } return result; @@ -162,9 +158,9 @@ { int result = 0; - for (int i = 0; i < length(); ++i) + for (char ch : *this) { - if (m_string[i] == needle) + if (ch == needle) result++; } @@ -205,12 +201,10 @@ { int count = 0; - for (int i = 0; i < length(); ++i) + for (char ch : *this) { - if (not isspace (m_string[i]) or ++count < n) + if (not isspace(ch) or ++count < n) continue; - - return i; } return -1; @@ -379,12 +373,12 @@ { String result; - for (int i = 0; i < size(); ++i) + for (const String &item : container()) { if (result.is_empty() == false) result += delim; - result += container()[i]; + result += item; } return result; diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/network/bytestream.cpp --- a/sources/network/bytestream.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/network/bytestream.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -149,7 +149,7 @@ ensure_read_space (2); short int result = 0; - for (int i = 0; i < 2; ++i) + for (int i : range(2)) result |= m_cursor[i] << (i * 8); m_cursor += 2; @@ -163,7 +163,7 @@ ensure_read_space (4); long int result = 0; - for (int i = 0; i < 4; ++i) + for (int i : range(4)) result |= m_cursor[i] << (i * 8); m_cursor += 4; @@ -258,7 +258,7 @@ { grow_to_fit (2); - for (int i = 0; i < 2; ++i) + for (int i : range(2)) write ((val >> (i * 8)) & 0xFF); } @@ -268,7 +268,7 @@ { grow_to_fit (4); - for (int i = 0; i < 4; ++i) + for (int i : range(4)) write ((val >> (i * 8)) & 0xFF); } diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/network/ipaddress.cpp --- a/sources/network/ipaddress.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/network/ipaddress.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -98,7 +98,7 @@ // bool IPAddress::compare (const IPAddress& other) const { - for (int i = 0; i < 4; ++i) + for (int i : range(4)) { if (octet (i) != other.octet (i)) return false; @@ -118,7 +118,7 @@ // bool IPAddress::operator< (const IPAddress& other) const { - for (int i = 0; i < 4; ++i) + for (int i : range(4)) { if (octet (i) != other.octet (i)) return octet (i) < other.octet (i); @@ -151,7 +151,7 @@ // Try scanf the IPv4 host first if (sscanf (addressString, "%u.%u.%u.%u", &parts[0], &parts[1], &parts[2], &parts[3])) { - for (short i = 0; i < 4; ++i) + for (int i : range(4)) value.set_octet (i, parts[i]); } else diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/network/rconsession.cpp --- a/sources/network/rconsession.cpp Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/network/rconsession.cpp Wed Jul 20 15:03:37 2016 +0300 @@ -59,7 +59,7 @@ { m_address = address; m_state = RCON_CONNECTING; - m_interface->update_statusbar(); + m_interface->updateStatusBar(); send_hello(); } @@ -140,12 +140,12 @@ switch (ServerResponse (header)) { case SVRC_OLDPROTOCOL: - m_interface->print_error ("Your RCON client is using outdated protocol.\n"); + m_interface->printError ("Your RCON client is using outdated protocol.\n"); m_state = RCON_DISCONNECTED; break; case SVRC_BANNED: - m_interface->print_error ("You have been banned from the server.\n"); + m_interface->printError ("You have been banned from the server.\n"); m_state = RCON_DISCONNECTED; break; @@ -156,7 +156,7 @@ break; case SVRC_INVALIDPASSWORD: - m_interface->print_error ("Login failed.\n"); + m_interface->printError ("Login failed.\n"); m_state = RCON_DISCONNECTED; break; @@ -164,7 +164,7 @@ { String message = packet.read_string(); message.normalize(); - m_interface->print_text ("%s\n", message.chars()); + m_interface->printText ("%s\n", message.chars()); } break; @@ -172,7 +172,7 @@ m_interface->print ("Login successful!\n"); m_serverProtocol = packet.read_byte(); m_hostname = packet.read_string(); - m_interface->set_title (m_hostname); + m_interface->setTitle (m_hostname); m_state = RCON_CONNECTED; for (int i = packet.read_byte(); i > 0; --i) @@ -184,7 +184,7 @@ { String message = packet.read_string(); message.normalize(); - m_interface->print_text ("--- %s\n", message.chars()); + m_interface->printText ("--- %s\n", message.chars()); } m_interface->print ("End of previous messages.\n"); @@ -209,21 +209,22 @@ case SVRC_TABCOMPLETE: { StringList completes; + completes.resize(packet.read_byte()); - for (signed int i = packet.read_byte(); i > 0; --i) - completes << packet.read_string(); + for (String& completion : completes) + completion = packet.read_string(); if (completes.size() == 1) { - m_interface->tab_complete (m_lastTabComplete, completes[0]); + m_interface->tabComplete (m_lastTabComplete, completes[0]); } else if (not completes.is_empty()) { m_interface->print ("Completions for '%s':\n", m_lastTabComplete.chars()); - for (int i = 0; i < completes.size(); i += 8) + for (int i : range(0, completes.size(), 8)) { - Range spliceRange (i, min (i + 8, completes.size() - 1)); + Range spliceRange (i, min (i + 8, completes.size())); StringList splice (completes.splice (spliceRange)); m_interface->print ("- %s\n", splice.join (", ").chars()); } @@ -269,7 +270,7 @@ } catch (std::exception& e) { - m_interface->print_warning ("Couldn't process packet: %s\n", e.what()); + m_interface->printWarning ("Couldn't process packet: %s\n", e.what()); } } @@ -286,22 +287,22 @@ for (int i = packet.read_byte(); i > 0; --i) players.append (packet.read_string()); - m_interface->set_player_names (players); + m_interface->setPlayerNames (players); } break; case SVRCU_ADMINCOUNT: m_numAdmins = packet.read_byte(); - m_interface->update_statusbar(); + m_interface->updateStatusBar(); break; case SVRCU_MAP: m_level = packet.read_string(); - m_interface->update_statusbar(); + m_interface->updateStatusBar(); break; default: - m_interface->print_warning ("Unknown server update type: %d\n", header); + m_interface->printWarning ("Unknown server update type: %d\n", header); break; } } diff -r 8fb1c657e0b0 -r d9073c13dc98 sources/range.h --- a/sources/range.h Wed Jul 20 13:29:03 2016 +0300 +++ b/sources/range.h Wed Jul 20 15:03:37 2016 +0300 @@ -67,7 +67,7 @@ bool operator!= (const Iterator& other) const { - return value != other.value; + return value < other.value; } Iterator& operator++() @@ -90,18 +90,12 @@ Iterator begin() const { - Iterator it; - it.value = min(); - it.step = m_step; - return it; + return Iterator(min(), m_step); } Iterator end() const { - Iterator it; - it.value = max() + 1; - it.step = m_step; - return it; + return Iterator(max(), m_step); } T min() const @@ -150,4 +144,16 @@ } }; +template +Range range(T a, T b, T step = 1) +{ + return Range(a, b, step); +} + +template +Range range(T b) +{ + return Range(T(), b); +} + END_ZFC_NAMESPACE