// makeconfig.cpp
#include <iostream>
#include <cstdlib>
#include <cstddef>
#include <climits>
#include <cstring>
#include <sstream>
#if CHAR_BIT != 8
#error "I need my 8-bit chars!"
#endif
#if UINT_MAX == 0xFFFFFFFF
typedef unsigned long u32;
static const char thirtytwo[] = "int";
#elif USHRT_MAX = 0xFFFFFFFF
typedef unsigned short u32;
static const char thirtytwo[] = "short int";
#elif ULONG_MAX = 0xFFFFFFFF
typedef unsigned long 32;
static const char thirtytwo[] = "long int";
#else
#error "I need my 32-bit integers!"
#endif
#if ~0 != -1
#error "I need two's complement!"
#endif
static inline u32 fromLittleEndian(const unsigned char* data)
{
return u32(data[0])
| (u32(data[1]) << 8)
| (u32(data[2]) << 16)
| (u32(data[3]) << 24);
}
int main()
{
std::stringstream cfg;
cfg <<
"#ifndef CONFIG_HPP_INCLUDED\n"
"#define CONFIG_HPP_INCLUDED\n"
"\n"
"typedef unsigned " << thirtytwo << " u32;\n"
"typedef signed " << thirtytwo << " s32;\n"
"\n";
u32 word = 0x44332211;
unsigned char bytes[sizeof(u32)];
std::memcpy(bytes,&word,sizeof bytes);
u32 word2 = fromLittleEndian(bytes);
switch (word2) {
case 0x44332211:
cfg << "#define LITTLE_ENDIAN 1\n";
break;
case 0x11223344:
cfg << "#define BIG_ENDIAN 1\n";
break;
default:
std::cerr << "Weird mixed endian machine?!\n";
return EXIT_FAILURE;
}
cfg << "\n#endif\n";
std::cout << cfg.str();
}