2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** serial-util.h
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp 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.
|
|
|
|
**
|
|
|
|
** mkxp 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 mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SERIALUTIL_H
|
|
|
|
#define SERIALUTIL_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2013-10-14 10:57:30 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <SDL_endian.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
typedef unsigned uint;
|
|
|
|
|
2013-10-14 10:57:30 +00:00
|
|
|
#if SDL_BYTEORDER != SDL_LIL_ENDIAN
|
|
|
|
#error "Non little endian systems not supported"
|
|
|
|
#endif
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
static inline int16_t
|
|
|
|
read_int16(const char *data, uint &i)
|
|
|
|
{
|
2013-10-14 10:57:30 +00:00
|
|
|
int16_t result;
|
|
|
|
|
|
|
|
memcpy(&result, &data[i], 2);
|
2013-09-01 14:27:21 +00:00
|
|
|
i += 2;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t
|
|
|
|
read_int32(const char *data, uint &i)
|
|
|
|
{
|
2013-10-14 10:57:30 +00:00
|
|
|
int32_t result;
|
|
|
|
|
|
|
|
memcpy(&result, &data[i], 4);
|
2013-09-01 14:27:21 +00:00
|
|
|
i += 4;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-10-14 10:57:30 +00:00
|
|
|
static inline double
|
|
|
|
read_double(const char *data, uint &i)
|
|
|
|
{
|
|
|
|
double result;
|
|
|
|
|
|
|
|
memcpy(&result, &data[i], 8);
|
|
|
|
i += 8;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
static inline void
|
|
|
|
write_int16(char **data, int16_t value)
|
|
|
|
{
|
2013-10-14 10:57:30 +00:00
|
|
|
memcpy(*data, &value, 2);
|
|
|
|
|
2013-10-16 23:01:08 +00:00
|
|
|
*data += 2;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
write_int32(char **data, int32_t value)
|
|
|
|
{
|
2013-10-14 10:57:30 +00:00
|
|
|
memcpy(*data, &value, 4);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-16 23:01:08 +00:00
|
|
|
*data += 4;
|
2013-10-14 10:57:30 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
write_double(char **data, double value)
|
|
|
|
{
|
2013-10-14 10:57:30 +00:00
|
|
|
memcpy(*data, &value, 8);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-10-16 23:01:08 +00:00
|
|
|
*data += 8;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SERIALUTIL_H
|