示例#1
0
static unsigned long long get_card(unsigned long long *deck){
        unsigned long long card;
        do{
                char buffer[8];
                printf("Input card: ");
                if(!fgets(buffer, sizeof(buffer), stdin)){
                        fprintf(stderr, "ERROR: Problem reading input.\n");
                        continue;
                }

                card = parse_card(buffer);
                if(!card){
                        fprintf(stderr, "ERROR: Unable to parse card.\n");
                        continue;
                }else if(!(card & *deck)){
                        printf("ATTENTION: This card has already been selected, please choose another.\n");
                        continue;
                }

                break;
        }while(1);

        *deck &= ~card;
        return card;
}
示例#2
0
static Hand_T parse_cards(const char *str) {
    if( str[0] != '[' ) {
        fprintf(stderr, "WARN: cannot parse cards string ``%s''\n", str);
        return emptyHand;
    }
    int len = strlen(str);
    Hand_T result = emptyHand;
    int i;
    for( i = 0; i+2 < len && str[i] != ']'; i += 3 ) {
        Hand_T card = parse_card(str+i+1);
        AddHandTo(result, card);
    }
    return result;
}
示例#3
0
static int
board_parse_lin (window_board_t *win, char *line, FILE *f)
{
	char *saveptr;
	char *tok;
	int card_nr = 0;

	setlocale (LC_NUMERIC, "C");

	board *b = board_new (win->n_boards + 1);
	int board_filled = 0;
	board_window_append_board (win, b);

	/* global list of names for vugraph files */
	char *name_arr[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	int name_n = 0;
	/* IMP/MP list */
	char *mp_str = NULL;
	char *mp_ptr = NULL;

	do {
	for (tok = sane_strtok_r(line, "|", &saveptr); tok; tok = STRTOK) {
		/* single hand */
		if (!strcmp(tok, "pn")) { /* SWNE */
			tok = STRTOK;
			char *nameptr;
			char *name = sane_strtok_r(tok, ",", &nameptr);
			int i = 0;
			do {
				if (i < 4)
					g_string_printf(b->hand_name[seat_mod (i++) - 1], "%s", name);
				if (name_n < 8)
					name_arr[name_n++] = strdup(name);
			} while ((name = sane_strtok_r(NULL, ",", &nameptr)));

		} else if (!strcmp(tok, "md")) { /* make deal */
			tok = STRTOK;
			if (*tok == '0') {
				printf ("md|0| (keep deal) mode not supported\n");
				continue;
			}
			if (board_filled) {
				/* start new board */
				card_nr = 0;
				board_filled = 0;

				b = board_new (win->n_boards + 1);
				board_window_append_board (win, b);
				/* initialize player names, required for vugraph files */
				int i;
				for (i = 0; i < 4; i++) {
					g_string_printf(b->hand_name[seat_mod (i) - 1],
						"%s", name_arr[i + (tok[0] == 'c' ? 4 : 0)]);
				}
			}
			char *c;
			seat se = south;
			suit su = spade;
			b->dealer = seat_mod(*tok - '0' - 1);
			for (c = tok + 1; *c; c++) {
				int i;
				if ((i = parse_suit(*c)) != -1) {
					su = i;
				} else if ((i = parse_rank_char(*c)) != -1) {
					if (add_card(b, se, (su * 13) + i) != 1)
						goto error;
				} else if (*c == ',') {
					se = seat_mod(se + 1);
				} else {
					printf("Parse error: %s", tok);
					goto error;
				}
			}
			// TODO: end positions
			deal_random(b); /* compute east hand */
			board_filled = 1; /* consider this board finished on next qx token */

		} else if (!strcmp(tok, "ah")) { /* board name */
			g_string_printf(b->name, "%s", STRTOK);

		} else if (!strcmp(tok, "qx")) { /* board number, o1, c1, o2, ... */
			tok = STRTOK;
			if (board_filled) { /* first token in new vugraph board */
				card_nr = 0;
				board_filled = 0;

				b = board_new (win->n_boards + 1);
				board_window_append_board (win, b);
				/* initialize player names, required for vugraph files */
				int i;
				for (i = 0; i < 4; i++) {
					g_string_printf(b->hand_name[seat_mod (i) - 1],
						"%s", name_arr[i + (tok[0] == 'c' ? 4 : 0)]);
				}
			}
			if (strlen (tok) >= 1)
				g_string_printf(b->name, "%s %s",
					tok[0] == 'c' ? _("Closed") :
						(tok[0] == 'o' ? _("Open") : _("Board")),
					tok + 1);

			/* for now assume qx|| is present in all lin files with mp|| */
			if (mp_str) {
				// FIXME: skip leading boards that were kibitzed but not played
				// (01-26-08-3.lin)
				char *score = mp_ptr ? sane_strtok_r (NULL, ",", &mp_ptr) :
						sane_strtok_r (mp_str, ",", &mp_ptr);
				b->mp[0] = score ? round (strtod (score, NULL) * 100.0) : 0;
				score = sane_strtok_r (NULL, ",", &mp_ptr);
				b->mp[1] = score ? round (strtod (score, NULL) * 100.0) : 0;
			}

		} else if (!strcmp(tok, "sv")) {
			tok = STRTOK;
			switch (*tok) {
				case '0':
				case 'o': b->vuln[0] = 0; b->vuln[1] = 0; break;
				case 'n': b->vuln[0] = 1; b->vuln[1] = 0; break;
				case 'e': b->vuln[0] = 0; b->vuln[1] = 1; break;
				case 'b': b->vuln[0] = 1; b->vuln[1] = 1; break;
				default: printf("Unknown vulnerability: sv|%s|\n", tok);
			}

		} else if (!strcmp(tok, "mb")) {
			/* mb|-ppp1Cp1Hp3Np4Dp4Hppp| */
			tok = STRTOK;
			char *bidp = tok;
			char *al = strchr (bidp, '!');
			if (al) {
				*al++ = '\0';
			}
			do {
				int bid = parse_bid(&bidp);
				if (bid == -1) {
					printf("Invalid bid %s/%s\n", tok, bidp);
					break;
				}
				board_append_bid(b, bid, 1);
				if (al) {
					board_set_alert (b, al);
					al = NULL;
				}
			} while (*bidp);

		} else if (!strcmp(tok, "an")) {
			tok = STRTOK;
			board_set_alert (b, !strcmp (tok, "!") ? "" : tok); /* filter uninteresting ! */

		} else if (!strcmp(tok, "pc")) {
			int c = parse_card(tok = STRTOK);
			if (c == -1) {
				printf("Invalid card %s\n", tok);
				continue;
			}
			if (card_nr < 52)
				b->played_cards[card_nr++] = c;

		} else if (!strcmp(tok, "mc")) {
			tok = STRTOK; // TODO: store number of (total) claimed tricks
			b->played_cards[card_nr] = claim_rest; // no card_nr increment here
			b->declarer_tricks = atoi (tok);

		/* vugraph file */
		} else if (!strcmp(tok, "vg")) { /* match title */
			tok = STRTOK;
			//printf ("Match title: %s\n", tok);
			if (win->title)
				free (win->title);
			if (win->subtitle)
				free (win->subtitle);
			if (win->team1)
				free (win->team1);
			if (win->team2)
				free (win->team2);

			char *t_ptr;
			char *title = sane_strtok_r (tok, ",", &t_ptr);
			if (title)
				win->title = strdup (title);
			char *subtitle = sane_strtok_r (NULL, ",", &t_ptr);
			if (subtitle)
				win->subtitle = strdup (subtitle);
			sane_strtok_r (NULL, ",", &t_ptr); /* scoring I IMPs P MPs B board-a-match */
			sane_strtok_r (NULL, ",", &t_ptr); /* first board nr */
			sane_strtok_r (NULL, ",", &t_ptr); /* last board nr */
			char *team1 = sane_strtok_r (NULL, ",", &t_ptr);
			if (team1)
				win->team1 = strdup (team1);
			sane_strtok_r (NULL, ",", &t_ptr); /* carry-over score team 1 */
			char *team2 = sane_strtok_r (NULL, ",", &t_ptr);
			if (team2)
				win->team2 = strdup (team2);
			/* carry-over score team 2 */

		} else if (!strcmp(tok, "pw")) { /* more player names */
			tok = STRTOK;
			//printf ("Players: %s\n", tok);
		} else if (!strcmp(tok, "bn")) { /* board numbers */
			tok = STRTOK;
			//printf ("Board numbers: %s\n", tok);
		} else if (!strcmp(tok, "rs")) { /* results */
			tok = STRTOK;
			//printf ("Results: %s\n", tok);
		} else if (!strcmp(tok, "mp")) { /* MP result */
			tok = STRTOK;
			//printf ("Scores: %s\n", tok);
			mp_str = strdup (tok);
		} else if (!strcmp(tok, "nt")) { /* comment (new text) */
			tok = STRTOK;
			//printf ("Comment: %s\n", tok);

		} else if (!strcmp(tok, "at")) { /* add text */
			STRTOK;
		} else if (!strcmp(tok, "cr") || !strcmp(tok, "cg") ||
			   !strcmp(tok, "cb")) { /* color */
			STRTOK;
		} else if (!strcmp(tok, "hc") || !strcmp(tok, "lc") ||
			   !strcmp(tok, "hs") || !strcmp(tok, "ls")) {
			STRTOK; /* hilight card, suit */
		} else if (!strcmp(tok, "pg")) {
			STRTOK; /* page break, e.g. after trick or comment */
		} else if (!strcmp(tok, "rh")) { /* reset heading */
			STRTOK;
		} else if (!strcmp(tok, "sk")) { /* set kibitzed */
			STRTOK;
		} else if (!strcmp(tok, "st")) { /* small text */
			STRTOK;
		} else if (!strcmp(tok, "up")) { /* undo play */
			tok = STRTOK;
		} else if (!*tok || *tok == '\n' || *tok == '\r') {
			/* empty token, hopefully end of line */
		} else {
			printf("Unknown token '%s|%s|'\n", tok, STRTOK);
		}
	}
	} while (fgets(line, 1023, f));

	int ret = 1;
	int i;
	goto ok;
error:
	ret = 0;
ok:
	for (i = 0; i < name_n; i++)
		free (name_arr[i]);
	if (mp_str)
		free (mp_str);

	setlocale (LC_NUMERIC, "");

	return ret;
}
示例#4
0
int
main(int argc, const char *argv[])
{
  if (argc < 2) {
    usage(); 
    exit(EXIT_FAILURE);   
  } 

  parseopts(argv[1]);  

  nfc_init(&context);
  if (context == NULL) {
    ERR("Unable to init libnfc (malloc)");
    exit(EXIT_FAILURE);
  }

// Try to open the NFC reader
  pnd = nfc_open(context, NULL);
  if (pnd == NULL) {
    ERR("Error opening NFC reader");
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  };

// Let the reader only try once to find a tag
  if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance.
  if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
  // Use raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));

// Try to find a MIFARE Classic tag
  if (select_target(pnd, &nt) <= 0) {
    printf("Error: no tag was found\n");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
// Test if we are dealing with a MIFARE compatible tag
  if ((nt.nti.nai.btSak & 0x08) == 0) {
    printf("Warning: tag is probably not a MFC!\n");
  }

  printf("Found MIFARE Classic card:\n");
  nt.nm = nmMifare;
  print_nfc_target(&nt, false);


// Guessing size
  if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02)
// 4K
    uiBlocks = 0xff;
  else if ((nt.nti.nai.btSak & 0x01) == 0x01)
// 320b
    uiBlocks = 0x13;
  else
// 1K/2K, checked through RATS
    uiBlocks = 0x3f;

    printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16);

  if (parse_card()) {
	printf("Done, %d blocks read.\n", uiBlocks + 1);
	fflush(stdout);
  }

  if(is_addv) if(!easy_add_value(0xff) || !parse_card()) printf("Failed Add Value!!\n");    

  printTag(&e); 

  nfc_close(pnd);
  nfc_exit(context);
  exit(EXIT_SUCCESS);
}