monotone

monotone Mtn Source Tree

Root/botan/big_io.cpp

1/*************************************************
2* BigInt Input/Output Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/bigint.h>
7#include <iostream>
8
9namespace Botan {
10
11/*************************************************
12* Write the BigInt into a stream *
13*************************************************/
14std::ostream& operator<<(std::ostream& stream, const BigInt& n)
15 {
16 BigInt::Base base = BigInt::Decimal;
17 if(stream.flags() & std::ios::hex)
18 base = BigInt::Hexadecimal;
19 else if(stream.flags() & std::ios::oct)
20 base = BigInt::Octal;
21
22 if(n == 0)
23 stream.write("0", 1);
24 else
25 {
26 if(n < 0)
27 stream.write("-", 1);
28 SecureVector<byte> buffer = BigInt::encode(n, base);
29 u32bit skip = 0;
30 while(buffer[skip] == '0' && skip < buffer.size())
31 skip++;
32 stream.write((const char*)buffer.begin() + skip, buffer.size() - skip);
33 }
34 if(!stream.good())
35 throw Stream_IO_Error("BigInt output operator has failed");
36 return stream;
37 }
38
39/*************************************************
40* Read the BigInt from a stream *
41*************************************************/
42std::istream& operator>>(std::istream& stream, BigInt& n)
43 {
44 std::string str;
45 std::getline(stream, str);
46 if(stream.bad() || (stream.fail() && !stream.eof()))
47 throw Stream_IO_Error("BigInt input operator has failed");
48 n = BigInt(str);
49 return stream;
50 }
51
52}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status