| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
 | /*******************************************************************************
   This file is part of LibTMCG.
 Copyright (C) 2004, 2005, 2006,
               2016, 2017, 2018  Heiko Stamer <[email protected]>
   LibTMCG is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   LibTMCG is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with LibTMCG; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*******************************************************************************/
// include headers
#ifdef HAVE_CONFIG_H
	#include "libTMCG_config.h"
#endif
#include "VTMF_Card.hh"
// additional headers
#include "mpz_helper.hh"
#include "parse_helper.hh"
VTMF_Card::VTMF_Card
	()
{
	mpz_init(c_1), mpz_init(c_2);
}
VTMF_Card::VTMF_Card
	(const VTMF_Card& that)
{
	mpz_init_set(c_1, that.c_1), mpz_init_set(c_2, that.c_2);
}
VTMF_Card& VTMF_Card::operator =
	(const VTMF_Card& that)
{
	mpz_set(c_1, that.c_1), mpz_set(c_2, that.c_2);
	return *this;
}
bool VTMF_Card::operator ==
	(const VTMF_Card& that) const
{
	if (mpz_cmp(c_1, that.c_1) || mpz_cmp(c_2, that.c_2))
		return false;
	return true;
}
bool VTMF_Card::operator !=
	(const VTMF_Card& that) const
{
	return !(*this == that);
}
bool VTMF_Card::import
	(std::string s)
{
	try
	{
		// check magic
		if (!TMCG_ParseHelper::cm(s, "crd", '|'))
			throw false;
		
		// card data
		std::string mpz_str;
		if (!TMCG_ParseHelper::gs(s, '|', mpz_str))
			throw false;
		if ((mpz_set_str(c_1, mpz_str.c_str(), TMCG_MPZ_IO_BASE) < 0) ||
			!TMCG_ParseHelper::nx(s, '|'))
		{
			throw false;
		}
		if (!TMCG_ParseHelper::gs(s, '|', mpz_str))
			throw false;
		if ((mpz_set_str(c_2, mpz_str.c_str(), TMCG_MPZ_IO_BASE) < 0) ||
			!TMCG_ParseHelper::nx(s, '|'))
		{
			throw false;
		}
		
		// finish
		throw true;
	}
	catch (bool return_value)
	{
		return return_value;
	}
}
VTMF_Card::~VTMF_Card()
{
	mpz_clear(c_1), mpz_clear(c_2);
}
std::ostream& operator <<
	(std::ostream& out, const VTMF_Card& card)
{
	out << "crd|" << card.c_1 << "|" << card.c_2 << "|";
	return out;
}
std::istream& operator >>
	(std::istream& in, VTMF_Card& card)
{
	char *tmp = new char[TMCG_MAX_CARD_CHARS];
	in.getline(tmp, TMCG_MAX_CARD_CHARS);
	if (!card.import(std::string(tmp)))
	{
		mpz_set_ui(card.c_1, 0L), mpz_set_ui(card.c_2, 0L);
		in.setstate(std::istream::iostate(std::istream::failbit));
	}
	delete [] tmp;
	return in;
}
 |