/*
 * Copyright (c) 1999 Johnny C. Lam.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 */

#include "marker.h"

#include <iostream>

unsigned int
Marker::diff(Marker m, Marker n)
{
    if (m.IsNA() || n.IsNA())
	return 0;
    if (m._marker > n._marker)
	return m._marker - n._marker;
    return n._marker - m._marker;
}

Marker &
Marker::operator+=(unsigned int i)
{
    _marker += (IsNA() ? 0 : i);
    return *this;
}

Marker &
Marker::operator-=(unsigned int i)
{
    _marker -= (IsNA() ? 0 : i);
    if (_marker < 0)
	_marker = 0;
    return *this;
}

/*
 * Read "NA" into the Marker if the istream contains "NA", otherwise, read
 * an integer into the Marker.
 */
void
Marker::Read( istream & istr )
{
    char c;
    
    istr >> c;
    if (c == 'N') {
	istr >> c;	// read past the 'A'
	_marker = NA;
    } else {
	istr.putback(c);
	istr >> _marker;
    }
}

/*
 * Print "NA" if the Marker is NA, otherwise, print the value of the Marker.
 */

ostream&
operator<<(ostream& ostr, Marker m)
{
    if (m.IsNA()) {
	ostr << "NA";
    } else {
	ostr << m.Value();
    }
    return ostr;
}
