You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.8 KiB
C++

// ***************************************************************** -*- C++ -*-
/*
* Copyright (C) 2004 Andreas Huggel <ahuggel@gmx.net>
*
* This program is part of the Exiv2 distribution.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
File: value.cpp
Version: $Name: $ $Revision: 1.1 $
Author(s): Andreas Huggel (ahu) <ahuggel@gmx.net>
History: 26-Jan-04, ahu: created
11-Feb-04, ahu: isolated as a component
*/
// *****************************************************************************
#include "rcsid.hpp"
EXIV2_RCSID("@(#) $Name: $ $Revision: 1.1 $ $RCSfile: value.cpp,v $")
// *****************************************************************************
// included header files
#include "value.hpp"
#include "types.hpp"
// + standard includes
#include <iostream>
#include <sstream>
// *****************************************************************************
// class member definitions
namespace Exif {
Value* Value::create(TypeId typeId)
{
Value* value = 0;
switch (typeId) {
case invalid:
value = new DataValue(invalid);
break;
case unsignedByte:
value = new DataValue(unsignedByte);
break;
case asciiString:
value = new AsciiValue;
break;
case unsignedShort:
value = new ValueType<uint16>;
break;
case unsignedLong:
value = new ValueType<uint32>;
break;
case unsignedRational:
value = new ValueType<URational>;
break;
case invalid6:
value = new DataValue(invalid6);
break;
case undefined:
value = new DataValue;
break;
case signedShort:
value = new ValueType<int16>;
break;
case signedLong:
value = new ValueType<int32>;
break;
case signedRational:
value = new ValueType<Rational>;
break;
default:
value = new DataValue(typeId);
break;
}
return value;
} // Value::create
std::string Value::toString() const
{
std::ostringstream os;
write(os);
return os.str();
}
void DataValue::read(const char* buf, long len, ByteOrder byteOrder)
{
// byteOrder not needed
value_ = std::string(buf, len);
}
void DataValue::read(const std::string& buf)
{
std::istringstream is(buf);
int tmp;
value_.clear();
while (is >> tmp) {
value_ += (char)tmp;
}
}
long DataValue::copy(char* buf, ByteOrder byteOrder) const
{
// byteOrder not needed
return value_.copy(buf, value_.size());
}
long DataValue::size() const
{
return value_.size();
}
Value* DataValue::clone() const
{
return new DataValue(*this);
}
std::ostream& DataValue::write(std::ostream& os) const
{
std::string::size_type end = value_.size();
for (std::string::size_type i = 0; i != end; ++i) {
os << (int)(unsigned char)value_[i] << " ";
}
return os;
}
void AsciiValue::read(const char* buf, long len, ByteOrder byteOrder)
{
// byteOrder not needed
value_ = std::string(buf, len);
}
void AsciiValue::read(const std::string& buf)
{
value_ = buf;
if (value_[value_.size()-1] != '\0') value_ += '\0';
}
long AsciiValue::copy(char* buf, ByteOrder byteOrder) const
{
// byteOrder not needed
return value_.copy(buf, value_.size());
}
long AsciiValue::size() const
{
return value_.size();
}
Value* AsciiValue::clone() const
{
return new AsciiValue(*this);
}
std::ostream& AsciiValue::write(std::ostream& os) const
{
// Strip trailing '\0', if any
if (value_.size() > 0 && value_[value_.size() - 1] == '\0') {
os << value_.substr(0, value_.size() - 1);
}
else {
os << value_;
}
return os;
}
} // namespace Exif