Add serialization.cpp to base lib.

This commit is contained in:
David Capello 2010-11-08 20:14:53 -03:00
parent 67e67022fa
commit 2e0c961bef
3 changed files with 132 additions and 0 deletions

View File

@ -7,6 +7,7 @@ add_library(base-lib
mem_utils.cpp
mutex.cpp
path.cpp
serialization.cpp
split_string.cpp
string.cpp
thread.cpp)

View File

@ -0,0 +1,90 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#include "config.h"
#include "base/serialization.h"
using namespace base::serialization;
std::ostream& write8(std::ostream& os, uint8_t byte)
{
os.put(byte);
return os;
}
uint8_t read8(std::istream& is)
{
return (uint8_t)is.get();
}
std::ostream& little_endian::write16(std::ostream& os, uint16_t word)
{
os.put((int)((word & 0x00ff)));
os.put((int)((word & 0xff00) >> 8));
return os;
}
std::ostream& little_endian::write32(std::ostream& os, uint32_t dword)
{
os.put((int)((dword & 0x000000ffl)));
os.put((int)((dword & 0x0000ff00l) >> 8));
os.put((int)((dword & 0x00ff0000l) >> 16));
os.put((int)((dword & 0xff000000l) >> 24));
return os;
}
uint16_t little_endian::read16(std::istream& is)
{
int b1, b2;
b1 = is.get();
b2 = is.get();
return ((b2 << 8) | b1);
}
uint32_t little_endian::read32(std::istream& is)
{
int b1, b2, b3, b4;
b1 = is.get();
b2 = is.get();
b3 = is.get();
b4 = is.get();
return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}
std::ostream& big_endian::write16(std::ostream& os, uint16_t word)
{
os.put((int)((word & 0xff00) >> 8));
os.put((int)((word & 0x00ff)));
return os;
}
std::ostream& big_endian::write32(std::ostream& os, uint32_t dword)
{
os.put((int)((dword & 0xff000000l) >> 24));
os.put((int)((dword & 0x00ff0000l) >> 16));
os.put((int)((dword & 0x0000ff00l) >> 8));
os.put((int)((dword & 0x000000ffl)));
return os;
}
uint16_t big_endian::read16(std::istream& is)
{
int b1, b2;
b2 = is.get();
b1 = is.get();
return ((b2 << 8) | b1);
}
uint32_t big_endian::read32(std::istream& is)
{
int b1, b2, b3, b4;
b4 = is.get();
b3 = is.get();
b2 = is.get();
b1 = is.get();
return ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}

41
src/base/serialization.h Normal file
View File

@ -0,0 +1,41 @@
// ASE base library
// Copyright (C) 2001-2010 David Capello
//
// This source file is ditributed under a BSD-like license, please
// read LICENSE.txt for more information.
#ifndef BASE_SERIALIZATION_H_INCLUDED
#define BASE_SERIALIZATION_H_INCLUDED
#include <iostream>
namespace base {
namespace serialization {
std::ostream& write8(std::ostream& os, uint8_t byte);
uint8_t read8(std::istream& is);
namespace little_endian {
std::ostream& write16(std::ostream& os, uint16_t word);
std::ostream& write32(std::ostream& os, uint32_t dword);
uint16_t read16(std::istream& is);
uint32_t read32(std::istream& is);
} // little_endian namespace
namespace big_endian {
std::ostream& write16(std::ostream& os, uint16_t word);
std::ostream& write32(std::ostream& os, uint32_t dword);
uint16_t read16(std::istream& is);
uint32_t read32(std::istream& is);
} // big_endian namespace
} // serialization namespace
} // base namespace
#endif