void View_Board::paintEvent(QPaintEvent *event) { QPainter paint(this); // draw coordinate lines paint.setPen(QPen(Qt::white, 1)); paint.setBrush(QBrush(QColor(0x55, 0x55, 0x55))); for (int i = 1; i <= 8; i++) { paint.drawRect(QRect(i * 44, 0, 44, 44)); } for (int j = 1; j <= 8; j++) { paint.drawRect(QRect(0, j * 44, 44, 44)); } paint.drawText(60 + 44*0, 30, "1"); paint.drawText(60 + 44*1, 30, "2"); paint.drawText(60 + 44*2, 30, "3"); paint.drawText(60 + 44*3, 30, "4"); paint.drawText(60 + 44*4, 30, "5"); paint.drawText(60 + 44*5, 30, "6"); paint.drawText(60 + 44*6, 30, "7"); paint.drawText(60 + 44*7, 30, "8"); paint.drawText(15, 75 + 44*0, "1"); paint.drawText(15, 75 + 44*1, "2"); paint.drawText(15, 75 + 44*2, "3"); paint.drawText(15, 75 + 44*3, "4"); paint.drawText(15, 75 + 44*4, "5"); paint.drawText(15, 75 + 44*5, "6"); paint.drawText(15, 75 + 44*6, "7"); paint.drawText(15, 75 + 44*7, "8"); // draw cells paint.setPen(QPen(Qt::white, 1)); for (int i = 0, x0 = 44; i < 8; i++) { for (int j = 0, y0 = 44; j < 8; j++) { paint.setBrush(QBrush(( (i + j) % 2 ? QColor(0x77, 0xdd, 0x77) : QColor(0xba, 0xdb, 0xad) ))); paint.drawRect(QRect(x0 + i * 44, y0 + j * 44, 44, 44)); } } // draw figures for (int i = 0, x0 = 44; i < 8; i++) { for (int j = 0, y0 = 44; j < 8; j++) { FIGURE f = board->gcell(i, j); if (IS_EMPTY(f)) continue; paint.setBrush(QBrush(IS_WHITE(f) ? Qt::white : Qt::black)); paint.setPen(QPen(IS_WHITE(f) ? Qt::black : Qt::white, 1)); paint.drawEllipse(QRect(x0 + i * 44 + 4, y0 + j * 44 + 4, 36, 36)); if (IS_KNG(f)) { paint.setPen(QPen(IS_WHITE(f) ? Qt::black : Qt::white, 3)); paint.drawEllipse(QRect(x0 + i * 44 + 10, y0 + j * 44 + 10, 24, 24)); } } } // move indication if (!end_flag) { paint.setBrush(QBrush(Qt::yellow)); paint.setPen(QPen(Qt::white, 2)); if (board->getCurrentColor() != WHITE) { paint.drawEllipse(QRect(410, 60, 20, 20)); } else { paint.drawEllipse(QRect(410, 360, 20, 20)); } } }
int Utils::countWordLetters( const std::string &str, int position, bool toRight ) { int len = static_cast<int>(str.size()); if (len == 0 || position < 0 || position > len) { return 0; } int pos = position; int whiteCount = 0; int notWhiteCount = 0; if (toRight && pos < len) { char ch = str[++pos]; while (pos < len) { if (IS_WHITE(ch)) { if (notWhiteCount > 0 && whiteCount == 0) { break; } whiteCount++; } else { if (whiteCount > 0 && notWhiteCount == 0) { break; } notWhiteCount++; } ch = str[++pos]; } } else if (!toRight && pos > 0) { char ch = str[--pos]; while (pos > 0) { if (IS_WHITE(ch)) { if (notWhiteCount > 0 && whiteCount == 0) { break; } whiteCount++; } else { if (whiteCount > 0 && notWhiteCount == 0) { break; } notWhiteCount++; } ch = str[--pos]; } } return pos - position; }
*/ static void trim_lines(REBSER *ser, REBCNT index, REBCNT tail) /* ** Remove all newlines and extra space. ** ***********************************************************************/ { REBINT pad = 1; // used to allow a single space REBUNI uc; REBCNT out = index; for (; index < tail; index++) { uc = GET_ANY_CHAR(ser, index); if (IS_WHITE(uc)) { uc = ' '; if (!pad) { SET_ANY_CHAR(ser, out, uc); out++; pad = 2; } } else { SET_ANY_CHAR(ser, out, uc); out++; pad = 0; } } // Remove extra end pad if found: if (pad == 2) out--; SET_ANY_CHAR(ser, out, 0); SERIES_TAIL(ser) = out; }
CFGItem *parse_line(char *str) { int argc = 0; char *cp, *arg; char *argv[MAXARGS]; CFGItem *item; int i; char *argstr = NULL; printf("PARSE: '%s'\n", str); if (*str == EOS) { return NULL; } str = strdup(str); argstr = arg = cp = skip_blanks(str); while (*argstr && !IS_WHITE(*argstr)) { argstr++; } argstr = skip_blanks(argstr); argstr = strdup(argstr); while (*arg != EOS) { while (*cp && !(IS_WHITE(*cp))) { cp++; } *cp++ = EOS; argv[argc] = arg; argc++; arg = cp = skip_blanks(cp); } item = malloc(sizeof(CFGItem)); item->next = NULL; item->name = argv[0]; item->argstr = argstr; item->argv = malloc(sizeof(char *) * (argc-1)); for (i=1; i<argc; i++) { item->argv[i-1] = argv[i]; } item->argc = argc-1; return item; }
*/ static void trim_auto(REBSER *ser, REBCNT index, REBCNT tail) /* ** Skip any blank lines and then determine indent of ** first line and make the rest align with it. ** ** BUG!!! If the indentation uses TABS, then it could ** fill past the source pointer! ** ***********************************************************************/ { REBCNT out = index; REBCNT line; REBCNT len; REBCNT indent; REBUNI uc = 0; // Skip whitespace, remember start of last line: for (line = index; index < tail; index++) { uc = GET_ANY_CHAR(ser, index); if (!IS_WHITE(uc)) break; if (uc == LF) line = index+1; } // Count the indentation used: for (indent = 0; line < index; line++) { if (GET_ANY_CHAR(ser, line) == ' ') indent++; else indent = (indent + TAB_SIZE) & ~3; } // For each line, pad with necessary indentation: while (index < tail) { // Skip to next content, track indentation: for (len = 0; index < tail; index++) { uc = GET_ANY_CHAR(ser, index); if (!IS_SPACE(uc) || len >= indent) break; if (uc == ' ') len++; else len = (len + TAB_SIZE) & ~3; } // Indent the line: for (; len > indent; len--) { SET_ANY_CHAR(ser, out, ' '); out++; } // Copy line contents: while (index < tail) { uc = GET_ANY_CHAR(ser, index); SET_ANY_CHAR(ser, out, uc); out++; index++; if (uc == LF) break; } } SET_ANY_CHAR(ser, out, 0); SERIES_TAIL(ser) = out; }
bool View_Board::eventFilter(QObject *target, QEvent *event) { if (event->type() == QEvent::MouseButtonPress && read_flag) { QMouseEvent *mouseEvent = (QMouseEvent*)event; int x = (mouseEvent->pos().x() - 44) / 44, y = (mouseEvent->pos().y() - 44) / 44; FIGURE f = board->gcell(x, y); if (ready == 0) { qDebug() << "if (ready == 0) ..."; result.from.x = x; result.from.y = y; if (0 || (color == WHITE && !IS_WHITE(f)) || (color == BLACK && !IS_BLACK(f)) ) return true; ready = 1; qDebug() << "ready = 1;"; return true; } else if (ready == 1) { qDebug() << "if (ready == 1) ..."; result.to.x = x; result.to.y = y; if (!board->canMove(result)) { result.from.x = x; result.from.y = y; if (0 || (color == WHITE && !IS_WHITE(f)) || (color == BLACK && !IS_BLACK(f)) ) { ready = 0; qDebug() << "ready = 0;"; return true; } ready = 1; qDebug() << "ready = 1;"; return true; } ready = 2; qDebug() << "ready = 2;"; read_flag = false; return true; } return true; } return false; };
char * lxmldbc_reatwhite(char * ptr) { int i; if (ptr==NULL) return NULL; i = strlen(ptr)-1; while (i >= 0 && IS_WHITE(ptr[i])) ptr[i--] = '\0'; return ptr; }
static EJSBool is_white (GCObjectPtr ptr) { uint32_t cell_idx; PageInfo *page = find_page_and_cell(ptr, &cell_idx); if (!page) return EJS_FALSE; return IS_WHITE(page->page_bitmap[cell_idx]); }
char * lxmldbc_eatwhite(char * string) { if (string==NULL) return NULL; while (*string) { if (!IS_WHITE(*string)) break; string++; } return string; }
static char *print_big_board( CHAR_DATA * ch, GAME_BOARD_DATA * board ) { static char retbuf[MAX_STRING_LENGTH * 2]; char buf[MAX_STRING_LENGTH], buf2[MAX_STRING_LENGTH]; char s1[16], s2[16]; int x, y; sprintf( s1, "&Y&W" ); sprintf( s2, "&z&z" ); sprintf( retbuf, WHITE_FOREGROUND "\n\r&g 1 2 3 4 5 6 7 8\n\r" ); for( x = 0; x < 8; x++ ) { strcat( retbuf, " " ); for( y = 0; y < 8; y++ ) { sprintf( buf, "%s%s", x % 2 == 0 ? ( y % 2 == 0 ? BLACK_BACKGROUND : WHITE_BACKGROUND ) : ( y % 2 == 0 ? WHITE_BACKGROUND : BLACK_BACKGROUND ), big_pieces[board->board[x][y]][0] ); sprintf( buf2, buf, IS_WHITE( board->board[x][y] ) ? s1 : s2 ); strcat( retbuf, buf2 ); } strcat( retbuf, BLACK_BACKGROUND "\n\r" ); sprintf( buf, WHITE_FOREGROUND "&g%c ", 'A' + x ); strcat( retbuf, buf ); for( y = 0; y < 8; y++ ) { sprintf( buf, "%s%s", x % 2 == 0 ? ( y % 2 == 0 ? BLACK_BACKGROUND : WHITE_BACKGROUND ) : ( y % 2 == 0 ? WHITE_BACKGROUND : BLACK_BACKGROUND ), big_pieces[board->board[x][y]][1] ); sprintf( buf2, buf, IS_WHITE( board->board[x][y] ) ? s1 : s2 ); strcat( retbuf, buf2 ); } strcat( retbuf, BLACK_BACKGROUND "\n\r" ); } return ( retbuf ); }
char *print_big_board( game_board_data * board ) { static char retbuf[MSL * 2]; char buf[MSL], buf2[MSL]; char s1[16], s2[16]; int x, y; snprintf( s1, 16, "&Y&W" ); snprintf( s2, 16, "&z&z" ); snprintf( retbuf, MSL * 2, WHITE_FOREGROUND "\r\n&g 1 2 3 4 5 6 7 8\r\n" ); for( x = 0; x < 8; ++x ) { mudstrlcat( retbuf, " ", MSL * 2 ); for( y = 0; y < 8; ++y ) { snprintf( buf, MSL, "%s%s", x % 2 == 0 ? ( y % 2 == 0 ? BLACK_BACKGROUND : WHITE_BACKGROUND ) : ( y % 2 == 0 ? WHITE_BACKGROUND : BLACK_BACKGROUND ), big_pieces[board->board[x][y]][0] ); snprintf( buf2, MSL, buf, IS_WHITE( board->board[x][y] ) ? s1 : s2 ); strcat( retbuf, buf2 ); } mudstrlcat( retbuf, BLACK_BACKGROUND "\r\n", MSL * 2 ); snprintf( buf, MSL, WHITE_FOREGROUND "&g%c ", 'A' + x ); mudstrlcat( retbuf, buf, MSL * 2 ); for( y = 0; y < 8; ++y ) { snprintf( buf, MSL, "%s%s", x % 2 == 0 ? ( y % 2 == 0 ? BLACK_BACKGROUND : WHITE_BACKGROUND ) : ( y % 2 == 0 ? WHITE_BACKGROUND : BLACK_BACKGROUND ), big_pieces[board->board[x][y]][1] ); snprintf( buf2, MSL, buf, IS_WHITE( board->board[x][y] ) ? s1 : s2 ); mudstrlcat( retbuf, buf2, MSL * 2 ); } mudstrlcat( retbuf, BLACK_BACKGROUND "\r\n", MSL * 2 ); } return retbuf; }
const char *StatTokeniser::nextToken() { if (mInput[0] == '\0') { return nullptr; } int foundChars = 0; int pos = 0; int outPos = 0; char ch = mInput[0]; while (ch != '\0') { bool isWhite = IS_WHITE(ch); if (isWhite) { if (foundChars > 0) { break; } ch = mInput[++pos]; continue; } int currentChar = isDelim(ch) ? 2 : 1; if (foundChars == 0) { foundChars = currentChar; } else { if (foundChars != currentChar) { break; } } mBuff[outPos++] = ch; ch = mInput[++pos]; } mBuff[outPos] = '\0'; mInput += pos; return mBuff; }
void System::printErrorLine() { if (_programSrc) { int line = 1; char *errLine = _programSrc; char *ch = _programSrc; while (line < gsb_last_line) { while (*ch && *ch != '\n') { ch++; } if (*ch) { errLine = ++ch; } line++; } while (*ch && *ch != '\n') { ch++; } char end; if (*ch == '\n') { ch++; end = *ch; *ch = '\0'; } else { end = *ch; } while (*errLine && (IS_WHITE(*errLine))) { errLine++; } int prevScreenId = _output->getScreenId(true); _output->selectBackScreen(CONSOLE_SCREEN); _output->print("\033[4mError line:\033[0m\n"); _output->print(errLine); *ch = end; _output->selectBackScreen(prevScreenId); } }
wx28HtmlTag::wx28HtmlTag(wx28HtmlTag *parent, const wxString& source, int pos, int end_pos, wx28HtmlTagsCache *cache, wx28HtmlEntitiesParser *entParser) : wxObject() { /* Setup DOM relations */ m_Next = NULL; m_FirstChild = m_LastChild = NULL; m_Parent = parent; if (parent) { m_Prev = m_Parent->m_LastChild; if (m_Prev == NULL) m_Parent->m_FirstChild = this; else m_Prev->m_Next = this; m_Parent->m_LastChild = this; } else m_Prev = NULL; /* Find parameters and their values: */ int i; wxChar c; // fill-in name, params and begin pos: i = pos+1; // find tag's name and convert it to uppercase: while ((i < end_pos) && ((c = source[i++]) != wxT(' ') && c != wxT('\r') && c != wxT('\n') && c != wxT('\t') && c != wxT('>'))) { if ((c >= wxT('a')) && (c <= wxT('z'))) c -= (wxT('a') - wxT('A')); m_Name << c; } // if the tag has parameters, read them and "normalize" them, // i.e. convert to uppercase, replace whitespaces by spaces and // remove whitespaces around '=': if (source[i-1] != wxT('>')) { #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \ c == wxT('\n') || c == wxT('\t')) wxString pname, pvalue; wxChar quote; enum { ST_BEFORE_NAME = 1, ST_NAME, ST_BEFORE_EQ, ST_BEFORE_VALUE, ST_VALUE } state; quote = 0; state = ST_BEFORE_NAME; while (i < end_pos) { c = source[i++]; if (c == wxT('>') && !(state == ST_VALUE && quote != 0)) { if (state == ST_BEFORE_EQ || state == ST_NAME) { m_ParamNames.Add(pname); m_ParamValues.Add(wxEmptyString); } else if (state == ST_VALUE && quote == 0) { m_ParamNames.Add(pname); if (entParser) m_ParamValues.Add(entParser->Parse(pvalue)); else m_ParamValues.Add(pvalue); } break; } switch (state) { case ST_BEFORE_NAME: if (!IS_WHITE(c)) { pname = c; state = ST_NAME; } break; case ST_NAME: if (IS_WHITE(c)) state = ST_BEFORE_EQ; else if (c == wxT('=')) state = ST_BEFORE_VALUE; else pname << c; break; case ST_BEFORE_EQ: if (c == wxT('=')) state = ST_BEFORE_VALUE; else if (!IS_WHITE(c)) { m_ParamNames.Add(pname); m_ParamValues.Add(wxEmptyString); pname = c; state = ST_NAME; } break; case ST_BEFORE_VALUE: if (!IS_WHITE(c)) { if (c == wxT('"') || c == wxT('\'')) quote = c, pvalue = wxEmptyString; else quote = 0, pvalue = c; state = ST_VALUE; } break; case ST_VALUE: if ((quote != 0 && c == quote) || (quote == 0 && IS_WHITE(c))) { m_ParamNames.Add(pname); if (quote == 0) { // VS: backward compatibility, no real reason, // but wxHTML code relies on this... :( pvalue.MakeUpper(); } if (entParser) m_ParamValues.Add(entParser->Parse(pvalue)); else m_ParamValues.Add(pvalue); state = ST_BEFORE_NAME; } else pvalue << c; break; } } #undef IS_WHITE } m_Begin = i; cache->QueryTag(pos, &m_End1, &m_End2); if (m_End1 > end_pos) m_End1 = end_pos; if (m_End2 > end_pos) m_End2 = end_pos; }
*/ REBYTE *Scan_Item(REBYTE *src, REBYTE *end, REBUNI term, REBYTE *invalid) /* ** Scan as UTF8 an item like a file or URL. ** ** Returns continuation point or zero for error. ** ** Put result into the MOLD_BUF as uni-chars. ** ***********************************************************************/ { REBUNI c; REBSER *buf; buf = BUF_MOLD; RESET_TAIL(buf); while (src < end && *src != term) { c = *src; // End of stream? if (c == 0) break; // If no term, then any white will terminate: if (!term && IS_WHITE(c)) break; // Ctrl chars are invalid: if (c < ' ') return 0; // invalid char if (c == '\\') c = '/'; // Accept %xx encoded char: else if (c == '%') { if (!Scan_Hex2(src+1, &c, FALSE)) return 0; src += 2; } // Accept ^X encoded char: else if (c == '^') { if (src+1 == end) return 0; // nothing follows ^ c = Scan_Char(&src); if (!term && IS_WHITE(c)) break; src--; } // Accept UTF8 encoded char: else if (c >= 0x80) { c = Decode_UTF8_Char(&src, 0); // zero on error if (c == 0) return 0; } // Is char as literal valid? (e.g. () [] etc.) else if (invalid && strchr(invalid, c)) return 0; src++; *UNI_SKIP(buf, buf->tail) = c; // not affected by Extend_Series if (++(buf->tail) >= SERIES_REST(buf)) Extend_Series(buf, 1); } if (*src && *src == term) src++; UNI_TERM(buf); return src; }
*/ REBSER *Parse_String(REBSER *series, REBCNT index, REBVAL *rules, REBCNT flags) /* ***********************************************************************/ { REBCNT tail = series->tail; REBSER *blk; REBSER *set; REBCNT begin; REBCNT end; REBOOL skip_spaces = !(flags & PF_ALL); REBUNI uc; blk = BUF_EMIT; // shared series RESET_SERIES(blk); // String of delimiters or single character: if (IS_STRING(rules) || IS_CHAR(rules)) { begin = Find_Max_Bit(rules); if (begin <= ' ') begin = ' ' + 1; set = Make_Bitset(begin); Set_Bits(set, rules, TRUE); } // None, so use defaults ",;": else { set = Make_Bitset(1+MAX(',',';')); Set_Bit(set, ',', TRUE); Set_Bit(set, ';', TRUE); } SAVE_SERIES(set); // If required, make space delimiters too: if (skip_spaces) { for (uc = 1; uc <= ' '; uc++) Set_Bit(set, uc, TRUE); } while (index < tail) { if (--Eval_Count <= 0 || Eval_Signals) Do_Signals(); // Skip whitespace if not /all refinement: if (skip_spaces) { uc = 0; for (; index < tail; index++) { uc = GET_ANY_CHAR(series, index); if (!IS_WHITE(uc)) break; } } else uc = GET_ANY_CHAR(series, index); // prefetch if (index < tail) { // Handle quoted strings (in a simple way): if (uc == '"') { begin = ++index; // eat quote for (; index < tail; index++) { uc = GET_ANY_CHAR(series, index); if (uc == '"') break; } end = index; if (index < tail) index++; } // All other tokens: else { begin = index; for (; index < tail; index++) { if (Check_Bit(set, GET_ANY_CHAR(series, index), !(flags & PF_CASE))) break; } end = index; } // Skip trailing spaces: if (skip_spaces) for (; index < tail; index++) { uc = GET_ANY_CHAR(series, index); if (!IS_WHITE(uc)) break; } // Check for and remove separator: if (Check_Bit(set, GET_ANY_CHAR(series, index), !(flags & PF_CASE))) index++; // Append new string: Set_String(Append_Value(blk), Copy_String(series, begin, end - begin)); } } UNSAVE_SERIES(set); return Copy_Block(blk, 0); }
void do_chess( CHAR_DATA * ch, const char *argument ) { char arg[MAX_INPUT_LENGTH]; argument = one_argument( argument, arg ); if ( IS_NPC( ch ) ) { send_to_char( "NPC's can't be in chess games.\r\n", ch ); return; } if ( !str_cmp( arg, "begin" ) ) { GAME_BOARD_DATA *board; if ( ch->pcdata->game_board ) { send_to_char( "You are already in a chess match.\r\n", ch ); return; } CREATE( board, GAME_BOARD_DATA, 1 ); init_board( board ); ch->pcdata->game_board = board; ch->pcdata->game_board->player1 = QUICKLINK( ch->name ); send_to_char( "You have started a game of chess.\r\n", ch ); return; } if ( !str_cmp( arg, "join" ) ) { GAME_BOARD_DATA *board = NULL; CHAR_DATA *vch; char arg2[MAX_INPUT_LENGTH]; if ( ch->pcdata->game_board ) { send_to_char( "You are already in a game of chess.\r\n", ch ); return; } argument = one_argument( argument, arg2 ); if ( arg2[0] == '\0' ) { send_to_char( "Join whom in a chess match?\r\n", ch ); return; } #ifdef IMC if ( strstr( arg2, "@" ) ) { if ( !str_cmp( imc_mudof( arg2 ), this_imcmud->localname ) ) { send_to_char( "You cannot join IMC chess on the local mud!\r\n", ch ); return; } if ( !str_cmp( imc_mudof( arg2 ), "*" ) ) { send_to_char( "* is not a valid mud name.\r\n", ch ); return; } if ( !str_cmp( imc_nameof( arg2 ), "*" ) ) { send_to_char( "* is not a valid player name.\r\n", ch ); return; } send_to_char( "Attempting to initiate IMC chess game...\r\n", ch ); CREATE( board, GAME_BOARD_DATA, 1 ); init_board( board ); board->type = TYPE_IMC; board->player1 = QUICKLINK( ch->name ); board->player2 = STRALLOC( arg2 ); board->turn = -1; ch->pcdata->game_board = board; imc_send_chess( ch->name, arg2, "start" ); return; } #endif if ( !( vch = get_char_world( ch, arg2 ) ) ) { send_to_char( "Cannot find that player.\r\n", ch ); return; } if ( IS_NPC( vch ) ) { send_to_char( "That player is an NPC, and cannot play games.\r\n", ch ); return; } board = vch->pcdata->game_board; if ( !board ) { send_to_char( "That player is not playing a game.\r\n", ch ); return; } if ( board->player2 ) { send_to_char( "That game already has two players.\r\n", ch ); return; } board->player2 = QUICKLINK( ch->name ); ch->pcdata->game_board = board; send_to_char( "You have joined a game of chess.\r\n", ch ); vch = get_char_world( ch, board->player1 ); ch_printf( vch, "%s has joined your game.\r\n", ch->name ); return; } if ( !ch->pcdata->game_board ) { send_to_char( "Usage: chess <begin|cease|status|board|move|join>\r\n", ch ); return; } if ( !str_cmp( arg, "cease" ) ) { free_game( ch->pcdata->game_board ); return; } if ( !str_cmp( arg, "status" ) ) { GAME_BOARD_DATA *board = ch->pcdata->game_board; if ( !board->player1 ) send_to_char( "There is no black player.\r\n", ch ); else if ( !str_cmp( board->player1, ch->name ) ) send_to_char( "You are black.\r\n", ch ); else ch_printf( ch, "%s is black.\r\n", board->player1 ); if ( king_in_checkmate( board, BLACK_KING ) ) send_to_char( "The black king is in checkmate!\r\n", ch ); else if ( king_in_check( board, BLACK_KING ) ) send_to_char( "The black king is in check.\r\n", ch ); if ( !board->player2 ) send_to_char( "There is no white player.\r\n", ch ); else if ( !str_cmp( board->player2, ch->name ) ) send_to_char( "You are white.\r\n", ch ); else ch_printf( ch, "%s is white.\r\n", board->player2 ); if ( king_in_checkmate( board, WHITE_KING ) ) send_to_char( "The white king is in checkmate!\r\n", ch ); else if ( king_in_check( board, WHITE_KING ) ) send_to_char( "The white king is in check.\r\n", ch ); if ( !board->player2 || !board->player1 ) return; ch_printf( ch, "%d turns.\r\n", board->turn ); if ( board->turn % 2 == 1 && !str_cmp( board->player1, ch->name ) ) { ch_printf( ch, "It is %s's turn.\r\n", board->player2 ); return; } else if ( board->turn % 2 == 0 && !str_cmp( board->player2, ch->name ) ) { ch_printf( ch, "It is %s's turn.\r\n", board->player1 ); return; } else { send_to_char( "It is your turn.\r\n", ch ); return; } return; } if ( !str_prefix( arg, "board" ) ) { send_to_char( print_big_board( ch, ch->pcdata->game_board ), ch ); return; } if ( !str_prefix( arg, "move" ) ) { CHAR_DATA *opp; char opp_name[MAX_INPUT_LENGTH]; char a, b; int x, y, dx, dy, ret; if ( !ch->pcdata->game_board->player1 || !ch->pcdata->game_board->player2 ) { send_to_char( "There is only 1 player.\r\n", ch ); return; } if ( ch->pcdata->game_board->turn < 0 ) { send_to_char( "The game hasn't started yet.\r\n", ch ); return; } if ( king_in_checkmate( ch->pcdata->game_board, BLACK_KING ) ) { send_to_char( "The black king has been checkmated, the game is over.\r\n", ch ); return; } if ( king_in_checkmate( ch->pcdata->game_board, WHITE_KING ) ) { send_to_char( "The white king has been checkmated, the game is over.\r\n", ch ); return; } if ( !*argument ) { send_to_char( "Usage: chess move [piece to move] [where to move]\r\n", ch ); return; } if ( ch->pcdata->game_board->turn % 2 == 1 && !str_cmp( ch->pcdata->game_board->player1, ch->name ) ) { send_to_char( "It is not your turn.\r\n", ch ); return; } if ( ch->pcdata->game_board->turn % 2 == 0 && !str_cmp( ch->pcdata->game_board->player2, ch->name ) ) { send_to_char( "It is not your turn.\r\n", ch ); return; } if ( sscanf( argument, "%c%d %c%d", &a, &y, &b, &dy ) != 4 ) { send_to_char( "Usage: chess move [dest] [source]\r\n", ch ); return; } if ( a < 'a' || a > 'h' || b < 'a' || b > 'h' || y < 1 || y > 8 || dy < 1 || dy > 8 ) { send_to_char( "Invalid move, use a-h, 1-8.\r\n", ch ); return; } x = a - 'a'; dx = b - 'a'; --y; --dy; ret = is_valid_move( ch, ch->pcdata->game_board, x, y, dx, dy ); if ( ret == MOVE_OK || ret == MOVE_TAKEN ) { GAME_BOARD_DATA *board; int piece, destpiece; board = ch->pcdata->game_board; piece = board->board[x][y]; destpiece = board->board[dx][dy]; board->board[dx][dy] = piece; board->board[x][y] = NO_PIECE; if ( king_in_check( board, IS_WHITE( board->board[dx][dy] ) ? WHITE_KING : BLACK_KING ) && ( board->board[dx][dy] != WHITE_KING && board->board[dx][dy] != BLACK_KING ) ) { board->board[dx][dy] = destpiece; board->board[x][y] = piece; ret = MOVE_INCHECK; } else { ++board->turn; #ifdef IMC if ( ch->pcdata->game_board->type == TYPE_IMC ) { snprintf( arg, LGST, "move %d%d %d%d", x, y, dx, dy ); imc_send_chess( ch->pcdata->game_board->player1, ch->pcdata->game_board->player2, arg ); } #endif } } if ( !str_cmp( ch->name, ch->pcdata->game_board->player1 ) ) { opp = get_char_world( ch, ch->pcdata->game_board->player2 ); if ( !opp ) mudstrlcpy( opp_name, ch->pcdata->game_board->player2, MAX_INPUT_LENGTH ); } else { opp = get_char_world( ch, ch->pcdata->game_board->player1 ); if ( !opp ) mudstrlcpy( opp_name, ch->pcdata->game_board->player1, MAX_INPUT_LENGTH ); } #ifdef IMC # define SEND_TO_OPP(arg,opp) \ if( opp ) \ { \ if( ch->pcdata->game_board->type == TYPE_LOCAL ) \ ch_printf( (opp), "%s\r\n", (arg) ); \ } \ else \ { \ if( ch->pcdata->game_board->type == TYPE_IMC ) \ imc_send_tell( ch->name, opp_name, (arg), 1 ); \ } #else # define SEND_TO_OPP(arg,opp) \ if( opp ) \ { \ if( ch->pcdata->game_board->type == TYPE_LOCAL ) \ ch_printf( (opp), "%s\r\n", (arg) ); \ } #endif switch ( ret ) { case MOVE_OK: send_to_char( "Ok.\r\n", ch ); snprintf( arg, MAX_INPUT_LENGTH, "%s has moved.\r\n", ch->name ); SEND_TO_OPP( arg, opp ); break; case MOVE_INVALID: send_to_char( "Invalid move.\r\n", ch ); break; case MOVE_BLOCKED: send_to_char( "You are blocked in that direction.\r\n", ch ); break; case MOVE_TAKEN: send_to_char( "You take the enemy's piece.\r\n", ch ); snprintf( arg, MAX_INPUT_LENGTH, "%s has taken one of your pieces!", ch->name ); SEND_TO_OPP( arg, opp ); break; case MOVE_CHECKMATE: send_to_char( "That move would result in a checkmate.\r\n", ch ); snprintf( arg, MAX_INPUT_LENGTH, "%s has attempted a move that would result in checkmate.", ch->name ); SEND_TO_OPP( arg, opp ); break; case MOVE_OFFBOARD: send_to_char( "That move would be off the board.\r\n", ch ); break; case MOVE_SAMECOLOR: send_to_char( "Your own piece blocks the way.\r\n", ch ); break; case MOVE_CHECK: send_to_char( "That move would result in a check.\r\n", ch ); snprintf( arg, MAX_INPUT_LENGTH, "%s has made a move that would result in a check.", ch->name ); SEND_TO_OPP( arg, opp ); break; case MOVE_WRONGCOLOR: send_to_char( "That is not your piece.\r\n", ch ); break; case MOVE_INCHECK: send_to_char( "You are in check, you must save your king.\r\n", ch ); break; default: bug( "%s: Unknown return value", __FUNCTION__ ); break; } #undef SEND_TO_OPP return; } send_to_char( "Usage: chess <begin|cease|status|board|move|join>\r\n", ch ); }
static int is_valid_move( CHAR_DATA * ch, GAME_BOARD_DATA * board, int x, int y, int dx, int dy ) { if ( dx < 0 || dy < 0 || dx > 7 || dy > 7 ) return MOVE_OFFBOARD; if ( board->board[x][y] == NO_PIECE ) return MOVE_INVALID; if ( x == dx && y == dy ) return MOVE_INVALID; if ( IS_WHITE( board->board[x][y] ) && !str_cmp( board->player1, ch->name ) ) return MOVE_WRONGCOLOR; if ( IS_BLACK( board->board[x][y] ) && ( !str_cmp( board->player2, ch->name ) || !ch ) ) return MOVE_WRONGCOLOR; switch ( board->board[x][y] ) { case WHITE_PAWN: case BLACK_PAWN: if ( IS_WHITE( board->board[x][y] ) && dx == x + 2 && x == 1 && dy == y && board->board[dx][dy] == NO_PIECE && board->board[x + 1][dy] == NO_PIECE ) return MOVE_OK; else if ( IS_BLACK( board->board[x][y] ) && dx == x - 2 && x == 6 && dy == y && board->board[dx][dy] == NO_PIECE && board->board[x - 1][dy] == NO_PIECE ) return MOVE_OK; if ( IS_WHITE( board->board[x][y] ) && dx != x + 1 ) return MOVE_INVALID; else if ( IS_BLACK( board->board[x][y] ) && dx != x - 1 ) return MOVE_INVALID; if ( dy != y && dy != y - 1 && dy != y + 1 ) return MOVE_INVALID; if ( dy == y ) { if ( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; else if ( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; else return MOVE_BLOCKED; } else { if ( board->board[dx][dy] == NO_PIECE ) return MOVE_INVALID; else if ( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; else if ( board->board[dx][dy] != BLACK_KING && board->board[dx][dy] != WHITE_KING ) return MOVE_TAKEN; else return MOVE_INVALID; } break; case WHITE_ROOK: case BLACK_ROOK: { int cnt; if ( dx != x && dy != y ) return MOVE_INVALID; if ( dx == x ) { for ( cnt = y; cnt != dy; ) { if ( cnt != y && board->board[x][cnt] != NO_PIECE ) return MOVE_BLOCKED; if ( dy > y ) cnt++; else cnt--; } } else if ( dy == y ) { for ( cnt = x; cnt != dx; ) { if ( cnt != x && board->board[cnt][y] != NO_PIECE ) return MOVE_BLOCKED; if ( dx > x ) cnt++; else cnt--; } } if ( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if ( !SAME_COLOR( x, y, dx, dy ) ) return MOVE_TAKEN; return MOVE_SAMECOLOR; } break; case WHITE_KNIGHT: case BLACK_KNIGHT: if ( ( dx == x - 2 && dy == y - 1 ) || ( dx == x - 2 && dy == y + 1 ) || ( dx == x - 1 && dy == y - 2 ) || ( dx == x - 1 && dy == y + 2 ) || ( dx == x + 1 && dy == y - 2 ) || ( dx == x + 1 && dy == y + 2 ) || ( dx == x + 2 && dy == y - 1 ) || ( dx == x + 2 && dy == y + 1 ) ) { if ( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if ( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; return MOVE_TAKEN; } return MOVE_INVALID; break; case WHITE_BISHOP: case BLACK_BISHOP: { int l, m, blocked = FALSE; if ( dx == x || dy == y ) return MOVE_INVALID; l = x; m = y; while ( 1 ) { if ( dx > x ) l++; else l--; if ( dy > y ) m++; else m--; if ( l > 7 || m > 7 || l < 0 || m < 0 ) return MOVE_INVALID; if ( l == dx && m == dy ) break; if ( board->board[l][m] != NO_PIECE ) blocked = TRUE; } if ( l != dx || m != dy ) return MOVE_INVALID; if ( blocked ) return MOVE_BLOCKED; if ( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if ( !SAME_COLOR( x, y, dx, dy ) ) return MOVE_TAKEN; return MOVE_SAMECOLOR; } break; case WHITE_QUEEN: case BLACK_QUEEN: { int l, m, blocked = FALSE; l = x; m = y; while ( 1 ) { if ( dx > x ) l++; else if ( dx < x ) l--; if ( dy > y ) m++; else if ( dy < y ) m--; if ( l > 7 || m > 7 || l < 0 || m < 0 ) return MOVE_INVALID; if ( l == dx && m == dy ) break; if ( board->board[l][m] != NO_PIECE ) blocked = TRUE; } if ( l != dx || m != dy ) return MOVE_INVALID; if ( blocked ) return MOVE_BLOCKED; if ( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if ( !SAME_COLOR( x, y, dx, dy ) ) return MOVE_TAKEN; return MOVE_SAMECOLOR; } break; case WHITE_KING: case BLACK_KING: { int sp, sk; if ( dx > x + 1 || dx < x - 1 || dy > y + 1 || dy < y - 1 ) return MOVE_INVALID; sk = board->board[x][y]; sp = board->board[dx][dy]; board->board[x][y] = sp; board->board[dx][dy] = sk; if ( king_in_check( board, sk ) ) { board->board[x][y] = sk; board->board[dx][dy] = sp; return MOVE_CHECK; } board->board[x][y] = sk; board->board[dx][dy] = sp; if ( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if ( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; return MOVE_TAKEN; } break; default: bug( "Invaild piece: %d", board->board[x][y] ); return MOVE_INVALID; } if ( ( IS_WHITE( board->board[x][y] ) && IS_WHITE( board->board[dx][dy] ) ) || ( IS_BLACK( board->board[x][y] ) && IS_BLACK( board->board[dx][dy] ) ) ) return MOVE_SAMECOLOR; return MOVE_OK; }
static bool king_in_check( GAME_BOARD_DATA * board, int piece ) { int x = 0, y = 0, l, m; if ( piece != WHITE_KING && piece != BLACK_KING ) return FALSE; if ( !find_piece( board, &x, &y, piece ) ) return FALSE; if ( x < 0 || y < 0 || x > 7 || y > 7 ) return FALSE; /* * pawns */ if ( IS_WHITE( piece ) && x < 7 && ( ( y > 0 && IS_BLACK( board->board[x + 1][y - 1] ) ) || ( y < 7 && IS_BLACK( board->board[x + 1][y + 1] ) ) ) ) return TRUE; else if ( IS_BLACK( piece ) && x > 0 && ( ( y > 0 && IS_WHITE( board->board[x - 1][y - 1] ) ) || ( y < 7 && IS_WHITE( board->board[x - 1][y + 1] ) ) ) ) return TRUE; /* * knights */ if ( x - 2 >= 0 && y - 1 >= 0 && ( ( board->board[x - 2][y - 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 2][y - 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x - 2 >= 0 && y + 1 < 8 && ( ( board->board[x - 2][y + 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 2][y + 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x - 1 >= 0 && y - 2 >= 0 && ( ( board->board[x - 1][y - 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 1][y - 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x - 1 >= 0 && y + 2 < 8 && ( ( board->board[x - 1][y + 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 1][y + 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x + 1 < 8 && y - 2 >= 0 && ( ( board->board[x + 1][y - 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 1][y - 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x + 1 < 8 && y + 2 < 8 && ( ( board->board[x + 1][y + 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 1][y + 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x + 2 < 8 && y - 1 >= 0 && ( ( board->board[x + 2][y - 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 2][y - 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; if ( x + 2 < 8 && y + 1 < 8 && ( ( board->board[x + 2][y + 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 2][y + 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return TRUE; /* * horizontal/vertical long distance */ for ( l = x + 1; l < 8; l++ ) if ( board->board[l][y] != NO_PIECE ) { if ( SAME_COLOR( x, y, l, y ) ) break; if ( board->board[l][y] == BLACK_QUEEN || board->board[l][y] == WHITE_QUEEN || board->board[l][y] == BLACK_ROOK || board->board[l][y] == WHITE_ROOK ) return TRUE; break; } for ( l = x - 1; l >= 0; l-- ) if ( board->board[l][y] != NO_PIECE ) { if ( SAME_COLOR( x, y, l, y ) ) break; if ( board->board[l][y] == BLACK_QUEEN || board->board[l][y] == WHITE_QUEEN || board->board[l][y] == BLACK_ROOK || board->board[l][y] == WHITE_ROOK ) return TRUE; break; } for ( m = y + 1; m < 8; m++ ) if ( board->board[x][m] != NO_PIECE ) { if ( SAME_COLOR( x, y, x, m ) ) break; if ( board->board[x][m] == BLACK_QUEEN || board->board[x][m] == WHITE_QUEEN || board->board[x][m] == BLACK_ROOK || board->board[x][m] == WHITE_ROOK ) return TRUE; break; } for ( m = y - 1; m >= 0; m-- ) if ( board->board[x][m] != NO_PIECE ) { if ( SAME_COLOR( x, y, x, m ) ) break; if ( board->board[x][m] == BLACK_QUEEN || board->board[x][m] == WHITE_QUEEN || board->board[x][m] == BLACK_ROOK || board->board[x][m] == WHITE_ROOK ) return TRUE; break; } /* * diagonal long distance */ for ( l = x + 1, m = y + 1; l < 8 && m < 8; l++, m++ ) if ( board->board[l][m] != NO_PIECE ) { if ( SAME_COLOR( x, y, l, m ) ) break; if ( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return TRUE; break; } for ( l = x - 1, m = y + 1; l >= 0 && m < 8; l--, m++ ) if ( board->board[l][m] != NO_PIECE ) { if ( SAME_COLOR( x, y, l, m ) ) break; if ( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return TRUE; break; } for ( l = x + 1, m = y - 1; l < 8 && m >= 0; l++, m-- ) if ( board->board[l][m] != NO_PIECE ) { if ( SAME_COLOR( x, y, l, m ) ) break; if ( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return TRUE; break; } for ( l = x - 1, m = y - 1; l >= 0 && m >= 0; l--, m-- ) if ( board->board[l][m] != NO_PIECE ) { if ( SAME_COLOR( x, y, l, m ) ) break; if ( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return TRUE; break; } return FALSE; }
int is_valid_move( char_data * ch, game_board_data * board, int x, int y, int dx, int dy ) { if( !ch ) { bug( "%s: nullptr ch!", __func__ ); return MOVE_INVALID; } if( !board ) { bug( "%s: nullptr board!", __func__ ); return MOVE_INVALID; } if( dx < 0 || dy < 0 || dx > 7 || dy > 7 ) return MOVE_OFFBOARD; if( board->board[x][y] == NO_PIECE ) return MOVE_INVALID; if( x == dx && y == dy ) return MOVE_INVALID; if( IS_WHITE( board->board[x][y] ) && !str_cmp( board->player1, ch->name ) ) return MOVE_WRONGCOLOR; if( IS_BLACK( board->board[x][y] ) && ( !ch || !str_cmp( board->player2, ch->name ) ) ) return MOVE_WRONGCOLOR; switch ( board->board[x][y] ) { case WHITE_PAWN: case BLACK_PAWN: if( IS_WHITE( board->board[x][y] ) && dx == x + 2 && x == 1 && dy == y && board->board[dx][dy] == NO_PIECE && board->board[x + 1][dy] == NO_PIECE ) return MOVE_OK; else if( IS_BLACK( board->board[x][y] ) && dx == x - 2 && x == 6 && dy == y && board->board[dx][dy] == NO_PIECE && board->board[x - 1][dy] == NO_PIECE ) return MOVE_OK; if( IS_WHITE( board->board[x][y] ) && dx != x + 1 ) return MOVE_INVALID; else if( IS_BLACK( board->board[x][y] ) && dx != x - 1 ) return MOVE_INVALID; if( dy != y && dy != y - 1 && dy != y + 1 ) return MOVE_INVALID; if( dy == y ) { if( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; else if( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; else return MOVE_BLOCKED; } else { if( board->board[dx][dy] == NO_PIECE ) return MOVE_INVALID; else if( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; else if( board->board[dx][dy] != BLACK_KING && board->board[dx][dy] != WHITE_KING ) return MOVE_TAKEN; else return MOVE_INVALID; } break; case WHITE_ROOK: case BLACK_ROOK: { int cnt; if( dx != x && dy != y ) return MOVE_INVALID; if( dx == x ) { for( cnt = y; cnt != dy; ) { if( cnt != y && board->board[x][cnt] != NO_PIECE ) return MOVE_BLOCKED; if( dy > y ) ++cnt; else --cnt; } } else if( dy == y ) { for( cnt = x; cnt != dx; ) { if( cnt != x && board->board[cnt][y] != NO_PIECE ) return MOVE_BLOCKED; if( dx > x ) ++cnt; else --cnt; } } if( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if( !SAME_COLOR( x, y, dx, dy ) ) return MOVE_TAKEN; return MOVE_SAMECOLOR; } break; case WHITE_KNIGHT: case BLACK_KNIGHT: if( ( dx == x - 2 && dy == y - 1 ) || ( dx == x - 2 && dy == y + 1 ) || ( dx == x - 1 && dy == y - 2 ) || ( dx == x - 1 && dy == y + 2 ) || ( dx == x + 1 && dy == y - 2 ) || ( dx == x + 1 && dy == y + 2 ) || ( dx == x + 2 && dy == y - 1 ) || ( dx == x + 2 && dy == y + 1 ) ) { if( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; return MOVE_TAKEN; } return MOVE_INVALID; break; case WHITE_BISHOP: case BLACK_BISHOP: { int l, m, blocked = false; if( dx == x || dy == y ) return MOVE_INVALID; l = x; m = y; while( 1 ) { if( dx > x ) ++l; else --l; if( dy > y ) ++m; else --m; if( l > 7 || m > 7 || l < 0 || m < 0 ) return MOVE_INVALID; if( l == dx && m == dy ) break; if( board->board[l][m] != NO_PIECE ) blocked = true; } if( l != dx || m != dy ) return MOVE_INVALID; if( blocked ) return MOVE_BLOCKED; if( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if( !SAME_COLOR( x, y, dx, dy ) ) return MOVE_TAKEN; return MOVE_SAMECOLOR; } break; case WHITE_QUEEN: case BLACK_QUEEN: { int l, m, blocked = false; l = x; m = y; while( 1 ) { if( dx > x ) ++l; else if( dx < x ) --l; if( dy > y ) ++m; else if( dy < y ) --m; if( l > 7 || m > 7 || l < 0 || m < 0 ) return MOVE_INVALID; if( l == dx && m == dy ) break; if( board->board[l][m] != NO_PIECE ) blocked = true; } if( l != dx || m != dy ) return MOVE_INVALID; if( blocked ) return MOVE_BLOCKED; if( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if( !SAME_COLOR( x, y, dx, dy ) ) return MOVE_TAKEN; return MOVE_SAMECOLOR; } break; case WHITE_KING: case BLACK_KING: { int sp, sk; if( dx > x + 1 || dx < x - 1 || dy > y + 1 || dy < y - 1 ) return MOVE_INVALID; sk = board->board[x][y]; sp = board->board[dx][dy]; board->board[x][y] = sp; board->board[dx][dy] = sk; if( king_in_check( board, sk ) ) { board->board[x][y] = sk; board->board[dx][dy] = sp; return MOVE_CHECK; } board->board[x][y] = sk; board->board[dx][dy] = sp; if( board->board[dx][dy] == NO_PIECE ) return MOVE_OK; if( SAME_COLOR( x, y, dx, dy ) ) return MOVE_SAMECOLOR; return MOVE_TAKEN; } break; default: bug( "Invaild piece: %d", board->board[x][y] ); return MOVE_INVALID; } if( ( IS_WHITE( board->board[x][y] ) && IS_WHITE( board->board[dx][dy] ) ) || ( IS_BLACK( board->board[x][y] ) && IS_BLACK( board->board[dx][dy] ) ) ) return MOVE_SAMECOLOR; return MOVE_OK; }
*/ static void trim_head_tail(REBSER *ser, REBCNT index, REBCNT tail, REBFLG h, REBFLG t) /* ** Trim from head and tail of each line, trim any leading or ** trailing lines as well, leaving one at the end if present ** ***********************************************************************/ { REBCNT start = index; REBCNT out = index; REBUNI uc; // Skip head lines if required: if (h || !t) { for (; index < tail; index++) { uc = GET_ANY_CHAR(ser, index); if (!IS_WHITE(uc)) break; } } // Trim the head and tail parts of a line: if (!h && !t) { REBINT hf = 1; // head space flag REBINT tf = 0; // tail space flag and index // Trim lines: for (; index < tail; index++) { uc = GET_ANY_CHAR(ser, index); if (IS_SPACE(uc)) { if (hf) continue; // trim from head tf = index; // tailing spaces? } else if (uc == LF) { hf = 1; if (tf) out = tf; tf = 0; } else hf = tf = 0; SET_ANY_CHAR(ser, out, uc); out++; } } else { for (; index < tail; index++) { uc = GET_ANY_CHAR(ser, index); SET_ANY_CHAR(ser, out, uc); out++; } } // Trim tail lines if required: if (t || !h) { REBOOL flag = FALSE; // found newline for (out--; out >= start; out--) { uc = GET_ANY_CHAR(ser, out); if (!IS_WHITE(uc)) break; if (uc == LF) flag = TRUE; } out++; if (!t && flag) { SET_ANY_CHAR(ser, out, LF); out++; } } SET_ANY_CHAR(ser, out, 0); SERIES_TAIL(ser) = out; }
int main (int argc, char * argv[]) { parse_opts(argc,argv); if(!doInit()) { printf("Error Initializing Stuff.\n"); exit(EXIT_FAILURE); } else { //o calibrar colores o seguir la linea //bool rotate = true; double pval; bool stop = true; int left, right; while(!ag_psh_is_pushed(&push, &pval)); UDELAY(750000); printf("pval is: %.2f\n", pval); ag_lgt_set_led(&lright, true); ag_lgt_set_led(&lleft, true); if (mode) { //calibrar colores int i = 0; double acumr [calib], minr, maxr, upr, lowr; double acuml [calib], minl, maxl, upl, lowl; for (;i<calib;i++) { acumr[i] = (double)ag_read_int(&lright); acuml[i] = (double)ag_read_int(&lleft); UDELAY(100); } gsl_stats_minmax(&minr, &maxr, acumr, 1, calib); gsl_stats_minmax(&minl, &maxl, acuml, 1, calib); gsl_sort (acumr,1,calib); gsl_sort (acuml,1,calib); upl = gsl_stats_quantile_from_sorted_data (acuml,1,calib,0.95);//uq lowl = gsl_stats_quantile_from_sorted_data (acuml,1,calib,0.05);//lq upr = gsl_stats_quantile_from_sorted_data (acumr,1,calib,0.95);//uq lowr = gsl_stats_quantile_from_sorted_data (acumr,1,calib,0.05);//lq for (i = 0 ; i < 2; i++){ printf("COLOR: %s, SENSOR: %s\n", white ? "WHITE" : "BLACK", i == 0 ? "LEFT" : "RIGHT"); printf("min_v: %d, max_v: %d\n", i == 0 ? (int)minl : (int)minr, i == 0 ? (int)maxl : (int)maxr); printf("low_q: %d, up_q :%d\n", i == 0 ? (int)lowl : (int)lowr, i == 0 ? (int)upl : (int)upr); printf("\n"); } } else { //seguir linea while(!ag_psh_is_pushed(&push, &pval)) { //printf("pval is: %.2f\n", pval); if (stop){ stop = false; move_all(MY_FWD,vel); } right = ag_read_int(&lright); left = ag_read_int(&lleft); if(! IS_WHITE(right) || ! IS_WHITE(left)){ stop_all(); stop = true; if (IS_BLACK(left)){ rotate_robot(vel, true); while(IS_BLACK(ag_read_int(&lleft))); UDELAY(TDELAY); stop_all(); } else if (IS_BLACK(right)) { rotate_robot(vel, false); while(IS_BLACK(ag_read_int(&lright))); UDELAY(TDELAY); stop_all(); } } } } } lego_shutdown(); exit(EXIT_SUCCESS); }
int main(void) { gcinit(); stackinit(); while(1) { char buffer[80]; ssize_t bytes_read = raw_input("calc> ", buffer, sizeof(buffer)); if(bytes_read == EOF) { return 0; } else { Buffer p; buffer_init(&p, buffer, bytes_read); size_t i = 0; while(p.pos != p.end) { if(IS_DIGIT(*p.pos)) { int num = *p.pos - '0'; stackpush(newLong(num)); printf("PUSH %d\n", num); } else if(IS_OPERATOR(*p.pos)) { if(pstack.stacksize < 2) { fprintf(stderr, "operator '%c' takes 2 args\n", *p.pos); goto finally; } else { Object* op2 = stackpop(); Object* op1 = stackpop(); if(*p.pos == '+') { printf("POP %ld\n", O_LVAL(op2)); printf("POP %ld\n", O_LVAL(op1)); long result; result = O_LVAL(op1) + O_LVAL(op2); Object *retval = newLong(result); printf("ADD\n"); stackpush(retval); printf("PUSH %ld\n", result); } else if(*p.pos == '-') { long result; result = O_LVAL(op1) - O_LVAL(op2); Object *retval = newLong(result); stackpush(retval); } } } else if (IS_WHITE(*p.pos)) { goto out; } else { fprintf(stderr, "Invalid token %c\n", *p.pos); goto finally; } out: p.pos++; i++; } if(pstack.stacksize == 1) { Object *top = stackpop(); objectEcho(top); } else { printf("To many values: %zu", pstack.stacksize); } finally: gcterm(); gcinit(); stackinit(); printf("\n"); } } return 0; }
const char *TextTokeniser::nextToken() { if (ctok == nullptr || ctok[0] == '\0') { return nullptr; } char ch = *ctok++; int j = 0; if (mInFormat == 0) { while (ch != '<' && ch != '\0') { tokBuff[j++] = ch; ch = *ctok++; } if (ch == '\0') { ctok--; } else if (ch == '<') { mInFormat = 1; } } else if (mInFormat == 1) { tokBuff[j++] = '<'; ctok--; mInFormat = 2; } else if (mInFormat == 2) { while (IS_WHITE(ch)) { ch = *ctok++; } if (ch == '/') { tokBuff[j++] = ch; } else { while (ch != '>' && ch != '\0' && !(IS_WHITE(ch)) && ch != '=' && ch != '\'' && ch != '"') { tokBuff[j++] = ch; ch = *ctok++; } if (ch == '\0') { ctok--; } else if (ch == '\'' || ch == '"') { ch = *ctok++; while (ch != '\0' && ch != '\'' && ch != '"') { tokBuff[j++] = ch; ch = *ctok++; } } else if (ch == '>') { mInFormat = 3; } else if (ch == '=') { mInFormat = 4; ctok--; } } } else if (mInFormat == 4) { tokBuff[j++] = '='; //ctok--; mInFormat = 2; } else { tokBuff[j++] = '>'; ctok--; mInFormat = 0; } tokBuff[j++] = '\0'; if (tokBuff[0] == '\0') { return nextToken(); } return tokBuff; }
static GLboolean expand (expand_state *e, pp_symbols *symbols) { while (!IS_NULL(*e->input)) { if (IS_FIRST_ID_CHAR(*e->input)) { slang_string buffer; const char *id; /* Parse the identifier. */ slang_string_init (&buffer); slang_string_pushc (&buffer, *e->input++); while (IS_NEXT_ID_CHAR(*e->input)) slang_string_pushc (&buffer, *e->input++); id = slang_string_cstr (&buffer); /* Now check if the identifier is special in some way. The "defined" identifier is * actually an operator that we must handle here and expand it either to " 0 " or " 1 ". * The other identifiers start with "__" and we expand it to appropriate values * taken from the preprocessor state. */ if (_mesa_strcmp (id, "defined") == 0) { if (!expand_defined (e, &buffer)) return GL_FALSE; } else if (_mesa_strcmp (id, "__LINE__") == 0) { slang_string_pushc (e->output, ' '); slang_string_pushi (e->output, e->state->line); slang_string_pushc (e->output, ' '); } else if (_mesa_strcmp (id, "__FILE__") == 0) { slang_string_pushc (e->output, ' '); slang_string_pushi (e->output, e->state->file); slang_string_pushc (e->output, ' '); } else if (_mesa_strcmp (id, "__VERSION__") == 0) { slang_string_pushc (e->output, ' '); slang_string_pushi (e->output, e->state->version); slang_string_pushc (e->output, ' '); } #if FEATURE_es2_glsl else if (_mesa_strcmp (id, "GL_ES") == 0 || _mesa_strcmp (id, "GL_FRAGMENT_PRECISION_HIGH") == 0) { slang_string_pushc (e->output, ' '); slang_string_pushi (e->output, '1'); slang_string_pushc (e->output, ' '); } #endif else { pp_symbol *symbol; /* The list of symbols from <symbols> take precedence over the list from <state>. * Note that in some cases this is the same list so avoid double look-up. */ symbol = pp_symbols_find (symbols, id); if (symbol == NULL && symbols != &e->state->symbols) symbol = pp_symbols_find (&e->state->symbols, id); /* If the symbol was found, recursively expand its definition. */ if (symbol != NULL) { if (!expand_symbol (e, symbol)) { slang_string_free (&buffer); return GL_FALSE; } } else { slang_string_push (e->output, &buffer); } } slang_string_free (&buffer); } else if (IS_WHITE(*e->input)) { slang_string_pushc (e->output, *e->input++); } else { while (!IS_WHITE(*e->input) && !IS_NULL(*e->input) && !IS_FIRST_ID_CHAR(*e->input)) slang_string_pushc (e->output, *e->input++); } } return GL_TRUE; }
wxHtmlTag::wxHtmlTag(wxHtmlTag *parent, const wxString *source, const wxString::const_iterator& pos, const wxString::const_iterator& end_pos, wxHtmlTagsCache *cache, wxHtmlEntitiesParser *entParser) { /* Setup DOM relations */ m_Next = NULL; m_FirstChild = m_LastChild = NULL; m_Parent = parent; if (parent) { m_Prev = m_Parent->m_LastChild; if (m_Prev == NULL) m_Parent->m_FirstChild = this; else m_Prev->m_Next = this; m_Parent->m_LastChild = this; } else m_Prev = NULL; /* Find parameters and their values: */ wxChar c wxDUMMY_INITIALIZE(0); // fill-in name, params and begin pos: wxString::const_iterator i(pos+1); // find tag's name and convert it to uppercase: while ((i < end_pos) && ((c = *(i++)) != wxT(' ') && c != wxT('\r') && c != wxT('\n') && c != wxT('\t') && c != wxT('>') && c != wxT('/'))) { if ((c >= wxT('a')) && (c <= wxT('z'))) c -= (wxT('a') - wxT('A')); m_Name << c; } // if the tag has parameters, read them and "normalize" them, // i.e. convert to uppercase, replace whitespaces by spaces and // remove whitespaces around '=': if (*(i-1) != wxT('>')) { #define IS_WHITE(c) (c == wxT(' ') || c == wxT('\r') || \ c == wxT('\n') || c == wxT('\t')) wxString pname, pvalue; wxChar quote; enum { ST_BEFORE_NAME = 1, ST_NAME, ST_BEFORE_EQ, ST_BEFORE_VALUE, ST_VALUE } state; quote = 0; state = ST_BEFORE_NAME; while (i < end_pos) { c = *(i++); if (c == wxT('>') && !(state == ST_VALUE && quote != 0)) { if (state == ST_BEFORE_EQ || state == ST_NAME) { m_ParamNames.Add(pname); m_ParamValues.Add(wxGetEmptyString()); } else if (state == ST_VALUE && quote == 0) { m_ParamNames.Add(pname); if (entParser) m_ParamValues.Add(entParser->Parse(pvalue)); else m_ParamValues.Add(pvalue); } break; } switch (state) { case ST_BEFORE_NAME: if (!IS_WHITE(c)) { pname = c; state = ST_NAME; } break; case ST_NAME: if (IS_WHITE(c)) state = ST_BEFORE_EQ; else if (c == wxT('=')) state = ST_BEFORE_VALUE; else pname << c; break; case ST_BEFORE_EQ: if (c == wxT('=')) state = ST_BEFORE_VALUE; else if (!IS_WHITE(c)) { m_ParamNames.Add(pname); m_ParamValues.Add(wxGetEmptyString()); pname = c; state = ST_NAME; } break; case ST_BEFORE_VALUE: if (!IS_WHITE(c)) { if (c == wxT('"') || c == wxT('\'')) quote = c, pvalue = wxGetEmptyString(); else quote = 0, pvalue = c; state = ST_VALUE; } break; case ST_VALUE: if ((quote != 0 && c == quote) || (quote == 0 && IS_WHITE(c))) { m_ParamNames.Add(pname); if (quote == 0) { // VS: backward compatibility, no real reason, // but wxHTML code relies on this... :( pvalue.MakeUpper(); } if (entParser) m_ParamValues.Add(entParser->Parse(pvalue)); else m_ParamValues.Add(pvalue); state = ST_BEFORE_NAME; } else pvalue << c; break; } } #undef IS_WHITE } m_Begin = i; cache->QueryTag(pos, source->end(), &m_End1, &m_End2, &m_hasEnding); if (m_End1 > end_pos) m_End1 = end_pos; if (m_End2 > end_pos) m_End2 = end_pos; #if WXWIN_COMPATIBILITY_2_8 m_sourceStart = source->begin(); #endif // Try to parse any style parameters that can be handled simply by // converting them to the equivalent HTML 3 attributes: this is a far cry // from perfect but better than nothing. static const struct EquivAttr { const char *style; const char *attr; } equivAttrs[] = { { "text-align", "ALIGN" }, { "width", "WIDTH" }, { "vertical-align", "VALIGN" }, { "background", "BGCOLOR" }, }; wxHtmlStyleParams styleParams(*this); for ( unsigned n = 0; n < WXSIZEOF(equivAttrs); n++ ) { const EquivAttr& ea = equivAttrs[n]; if ( styleParams.HasParam(ea.style) && !HasParam(ea.attr) ) { m_ParamNames.Add(ea.attr); m_ParamValues.Add(styleParams.GetParam(ea.style)); } } }
bool king_in_check( game_board_data * board, int piece ) { int x = 0, y = 0, l, m; if( piece != WHITE_KING && piece != BLACK_KING ) return false; if( !find_piece( board, &x, &y, piece ) ) return false; if( x < 0 || y < 0 || x > 7 || y > 7 ) return false; /* * pawns */ if( IS_WHITE( piece ) && x < 7 && ( ( y > 0 && IS_BLACK( board->board[x + 1][y - 1] ) ) || ( y < 7 && IS_BLACK( board->board[x + 1][y + 1] ) ) ) ) return true; else if( IS_BLACK( piece ) && x > 0 && ( ( y > 0 && IS_WHITE( board->board[x - 1][y - 1] ) ) || ( y < 7 && IS_WHITE( board->board[x - 1][y + 1] ) ) ) ) return true; /* * knights */ if( x - 2 >= 0 && y - 1 >= 0 && ( ( board->board[x - 2][y - 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 2][y - 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x - 2 >= 0 && y + 1 < 8 && ( ( board->board[x - 2][y + 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 2][y + 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x - 1 >= 0 && y - 2 >= 0 && ( ( board->board[x - 1][y - 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 1][y - 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x - 1 >= 0 && y + 2 < 8 && ( ( board->board[x - 1][y + 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x - 1][y + 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x + 1 < 8 && y - 2 >= 0 && ( ( board->board[x + 1][y - 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 1][y - 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x + 1 < 8 && y + 2 < 8 && ( ( board->board[x + 1][y + 2] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 1][y + 2] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x + 2 < 8 && y - 1 >= 0 && ( ( board->board[x + 2][y - 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 2][y - 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; if( x + 2 < 8 && y + 1 < 8 && ( ( board->board[x + 2][y + 1] == BLACK_KNIGHT && IS_WHITE( board->board[x][y] ) ) || ( board->board[x + 2][y + 1] == WHITE_KNIGHT && IS_BLACK( board->board[x][y] ) ) ) ) return true; /* * horizontal/vertical long distance */ for( l = x + 1; l < 8; ++l ) if( board->board[l][y] != NO_PIECE ) { if( SAME_COLOR( x, y, l, y ) ) break; if( board->board[l][y] == BLACK_QUEEN || board->board[l][y] == WHITE_QUEEN || board->board[l][y] == BLACK_ROOK || board->board[l][y] == WHITE_ROOK ) return true; break; } for( l = x - 1; l >= 0; --l ) if( board->board[l][y] != NO_PIECE ) { if( SAME_COLOR( x, y, l, y ) ) break; if( board->board[l][y] == BLACK_QUEEN || board->board[l][y] == WHITE_QUEEN || board->board[l][y] == BLACK_ROOK || board->board[l][y] == WHITE_ROOK ) return true; break; } for( m = y + 1; m < 8; ++m ) if( board->board[x][m] != NO_PIECE ) { if( SAME_COLOR( x, y, x, m ) ) break; if( board->board[x][m] == BLACK_QUEEN || board->board[x][m] == WHITE_QUEEN || board->board[x][m] == BLACK_ROOK || board->board[x][m] == WHITE_ROOK ) return true; break; } for( m = y - 1; m >= 0; --m ) if( board->board[x][m] != NO_PIECE ) { if( SAME_COLOR( x, y, x, m ) ) break; if( board->board[x][m] == BLACK_QUEEN || board->board[x][m] == WHITE_QUEEN || board->board[x][m] == BLACK_ROOK || board->board[x][m] == WHITE_ROOK ) return true; break; } /* * diagonal long distance */ for( l = x + 1, m = y + 1; l < 8 && m < 8; ++l, ++m ) if( board->board[l][m] != NO_PIECE ) { if( SAME_COLOR( x, y, l, m ) ) break; if( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return true; break; } for( l = x - 1, m = y + 1; l >= 0 && m < 8; --l, ++m ) if( board->board[l][m] != NO_PIECE ) { if( SAME_COLOR( x, y, l, m ) ) break; if( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return true; break; } for( l = x + 1, m = y - 1; l < 8 && m >= 0; ++l, --m ) if( board->board[l][m] != NO_PIECE ) { if( SAME_COLOR( x, y, l, m ) ) break; if( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return true; break; } for( l = x - 1, m = y - 1; l >= 0 && m >= 0; --l, --m ) if( board->board[l][m] != NO_PIECE ) { if( SAME_COLOR( x, y, l, m ) ) break; if( board->board[l][m] == BLACK_QUEEN || board->board[l][m] == WHITE_QUEEN || board->board[l][m] == BLACK_BISHOP || board->board[l][m] == WHITE_BISHOP ) return true; break; } return false; }
int _scannert( int (*__Gett) (void *srceP), void (*__UnGett) (int ch, void *srceP), void *srceP, const _TCHAR *formP, va_list varPP ) { flagBits flags; int count = 0; int charCt = 0; int status; int width; int base; bits_t bitSet; /* for scan sets */ int exclude; _TCHAR a, b; int c; __int64 lRes; unsigned short ldRes[5]; /* actually a long double */ int nscanned; charClass curClass; int paramIsWide; union { char *PtrA; wchar_t *PtrW; } DualPtr; #define cPA (DualPtr.PtrA) #define cPW (DualPtr.PtrW) #ifdef _UNICODE # define cP (DualPtr.PtrW) #else # define cP (DualPtr.PtrA) #endif ssNEXT: if (_TEXT('\0') == (b = *(formP++))) return count; /* the normal end */ if ((b != _TEXT('%')) || (_TEXT('%') == (b = *formP++))) { charCt ++; a = c = __Gett (srceP); if (c == _TEOF) goto ssEOF; if (IS_WHITE(b)) /* white space ? */ { while (IS_WHITE(a)) { charCt ++; a = c = __Gett (srceP); if (c == _TEOF) goto ssEOF; } __UnGett (a, srceP); charCt --; } else /* literal match required */ { if (a != b) { __UnGett (a, srceP); goto ssEND; } #if defined(_MBCS) if (_istleadbyte (a) && *formP) { b = c = __Gett (srceP); if (c == _TEOF) goto ssEOF; if (b != *(formP++)) { __UnGett (b, srceP); __UnGett (a, srceP); goto ssEND; } charCt ++; } #endif } goto ssNEXT; } /* If fall through to here then begin a conversion specification. */ flags = isFarPtr; width = -1; paramIsWide = DEFWIDTH; ssSwitch: curClass = (b & 0x80) ? _dc : scanCtype [b & 0x7F]; switch (curClass) { case (_su) : flags |= isSuppressed; b = *(formP++); goto ssSwitch; case (_ha) : flags |= isHalf; b = *(formP++); goto ssSwitch; case (_lo) : flags |= isLong; b = *(formP++); goto ssSwitch; case (_ld) : flags |= isLongDouble; b = *(formP++); goto ssSwitch; case (_nu) : width = (width < 0) ? b - _TEXT('0') : 10 * width + b - _TEXT('0'); b = *(formP++); goto ssSwitch; case (_ne) : flags &= ~isFarPtr; b = *(formP++); goto ssSwitch; case (_fa) : flags |= isFarPtr; b = *(formP++); goto ssSwitch; case (_wi) : if (formP[0] == _TEXT('6') && formP[1] == _TEXT('4')) { flags = isLongDouble | (flags & ~(isLong | isHalf)); formP += 2; } else if (formP[0] == _TEXT('3') && formP[1] == _TEXT('2')) { flags = isLong | (flags & ~(isLongDouble | isHalf)); formP += 2; } else if (formP[0] == _TEXT('1') && formP[1] == _TEXT('6')) { flags = isHalf | (flags & ~(isLongDouble | isLong)); formP += 2; } else if (formP[0] == _TEXT('8')) { flags &= ~(isLongDouble | isLong | isHalf); formP += 1; } b = *(formP++); goto ssSwitch; case (_pt) : goto ssPTR; case (_un) : case (_de) : base = 10; goto ssINT; case (_oc) : base = 8; goto ssINT; case (_he) : base = 16; goto ssINT; case (_in) : base = 0; goto ssINT; case (_ct) : lRes = charCt; goto ssPUTINT; case (_fl) : goto ssFLOAT; case (_St) : case (_st) : goto ssTOKEN; case (_Ch) : case (_ch) : goto ssCHAR; case (_sc) : goto ssSCANSET; case (_dc) : goto ssEND; default: /* never occurs. */; } ssINT: lRes = __scanttoint64 (__Gett, __UnGett, srceP, base, width & 0x7FFF, &charCt, &status); if (status < 0) goto ssEOF; else if (status == 0) goto ssEND; ssPUTINT: if ((_TEXT('A') <= b) && (b <= _TEXT('Z')) && (b != _TEXT('X'))) flags |= isLong; if ((flags & isSuppressed) == 0) { if (flags & isLongDouble) *va_arg(varPP,__int64 *) = lRes; else if (flags & isLong) *va_arg(varPP,long *) = lRes; else if (flags & isHalf) *va_arg(varPP,short *) = (short)lRes; else *va_arg(varPP,int *) = (int)lRes; if (b != _TEXT('n')) count ++; } goto ssNEXT; ssPTR: lRes = __scanttoint64 (__Gett, __UnGett, srceP, 16, 8, &charCt, &status); if (status <= 0) goto ssEND; if ((flags & isSuppressed) == 0) { *va_arg(varPP,long *) = lRes; count ++; } goto ssNEXT; ssFLOAT: /* We don't actually reference any floating point values in this * code, so as to avoid linking in the floating point conversion * routines in integer-only programs. We also assume that a * pointer to a floating point number is the same size as a pointer * to a void. */ _scanttod (ldRes,__Gett, __UnGett, srceP, width & 0x7FFF, &charCt, &status); if (status < 0) goto ssEOF; else if (status == 0) goto ssEND; if ((flags & isSuppressed) == 0) { _scanrslt(ldRes, va_arg(varPP, void *), flags); count ++; } goto ssNEXT; ssTOKEN: /* Skip leading whitespace. */ if (flags & isLong) paramIsWide = 1; /* string width forced to wide */ else if (flags & isHalf) /* string width forced to narrow */ paramIsWide = 0; else if (curClass == _St) /* opposite width string */ paramIsWide = !paramIsWide; do { charCt++; a = c = __Gett (srceP); if (c == _TEOF) goto ssEOF; } while (IS_WHITE(a)); if ((flags & isSuppressed) == 0) { if (paramIsWide) cPW = va_arg(varPP, wchar_t *); else cPA = va_arg(varPP, char *); count ++; }
bool settings_read_i(const char *path, const char *section, settings_callback_t *callback, settings_section_callback_t *sect_callback, void *context, const char **error_r) { /* pretty horrible code, but v2.0 will have this rewritten anyway.. */ struct input_stack root, *input; const char *errormsg, *next_section, *name, *last_section_path = NULL; char *line, *key, *p, quote; string_t *full_line; size_t len; int fd, last_section_line = 0, skip, sections, root_section; fd = open(path, O_RDONLY); if (fd < 0) { *error_r = t_strdup_printf( "Can't open configuration file %s: %m", path); return FALSE; } if (section == NULL) { skip = 0; next_section = NULL; } else { skip = 1; next_section = t_strcut(section, '/'); } memset(&root, 0, sizeof(root)); root.path = path; input = &root; full_line = t_str_new(512); sections = 0; root_section = 0; errormsg = NULL; input->input = i_stream_create_fd_autoclose(&fd, (size_t)-1); i_stream_set_return_partial_line(input->input, TRUE); prevfile: while ((line = i_stream_read_next_line(input->input)) != NULL) { input->linenum++; /* @UNSAFE: line is modified */ /* skip whitespace */ while (IS_WHITE(*line)) line++; /* ignore comments or empty lines */ if (*line == '#' || *line == '\0') continue; /* strip away comments. pretty kludgy way really.. */ for (p = line; *p != '\0'; p++) { if (*p == '\'' || *p == '"') { quote = *p; for (p++; *p != quote && *p != '\0'; p++) { if (*p == '\\' && p[1] != '\0') p++; } if (*p == '\0') break; } else if (*p == '#') { if (!IS_WHITE(p[-1])) { i_warning("Configuration file %s line %u: " "Ambiguous '#' character in line, treating it as comment. " "Add a space before it to remove this warning.", input->path, input->linenum); } *p = '\0'; break; } } /* remove whitespace from end of line */ len = strlen(line); while (IS_WHITE(line[len-1])) len--; line[len] = '\0'; if (len > 0 && line[len-1] == '\\') { /* continues in next line */ len--; while (IS_WHITE(line[len-1])) len--; str_append_n(full_line, line, len); str_append_c(full_line, ' '); continue; } if (str_len(full_line) > 0) { str_append(full_line, line); line = str_c_modifiable(full_line); } /* a) key = value b) section_type [section_name] { c) } */ key = line; while (!IS_WHITE(*line) && *line != '\0' && *line != '=') line++; if (IS_WHITE(*line)) { *line++ = '\0'; while (IS_WHITE(*line)) line++; } if (strcmp(key, "!include_try") == 0 || strcmp(key, "!include") == 0) { if (settings_include(fix_relative_path(line, input), &input, strcmp(key, "!include_try") == 0, &errormsg) == 0) goto prevfile; } else if (*line == '=') { /* a) */ *line++ = '\0'; while (IS_WHITE(*line)) line++; len = strlen(line); if (len > 0 && ((*line == '"' && line[len-1] == '"') || (*line == '\'' && line[len-1] == '\''))) { line[len-1] = '\0'; line = str_unescape(line+1); } errormsg = skip ? NULL : callback(key, line, context); } else if (strcmp(key, "}") != 0 || *line != '\0') { /* b) + errors */ line[-1] = '\0'; if (*line == '{') name = ""; else { name = line; while (!IS_WHITE(*line) && *line != '\0') line++; if (*line != '\0') { *line++ = '\0'; while (IS_WHITE(*line)) line++; } } if (*line != '{') errormsg = "Expecting '='"; else { sections++; if (next_section != NULL && strcmp(next_section, name) == 0) { section += strlen(next_section); if (*section == '\0') { skip = 0; next_section = NULL; root_section = sections; } else { i_assert(*section == '/'); section++; next_section = t_strcut(section, '/'); } } if (skip > 0) skip++; else { skip = sect_callback == NULL ? 1 : !sect_callback(key, name, context, &errormsg); if (errormsg != NULL && last_section_line != 0) { errormsg = t_strdup_printf( SECTION_ERRORMSG, errormsg, last_section_path, last_section_line); } } last_section_path = input->path; last_section_line = input->linenum; } } else { /* c) */ if (sections == 0) errormsg = "Unexpected '}'"; else { if (skip > 0) skip--; else { i_assert(sect_callback != NULL); sect_callback(NULL, NULL, context, &errormsg); if (root_section == sections && errormsg == NULL) { /* we found the section, now quit */ break; } } last_section_path = input->path; last_section_line = input->linenum; sections--; } } if (errormsg != NULL) { *error_r = t_strdup_printf( "Error in configuration file %s line %d: %s", input->path, input->linenum, errormsg); break; } str_truncate(full_line, 0); } i_stream_destroy(&input->input); input = input->prev; if (line == NULL && input != NULL) goto prevfile; return errormsg == NULL; }