|
| 1 | +/** |
| 2 | + * Copyright (C) 2013 Andi Drebes <andi.drebes@lip6.fr> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License version 2 as published |
| 6 | + * by the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program; if not, write to the Free Software |
| 15 | + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifndef ANSI_EXTRAS_H |
| 19 | +#define ANSI_EXTRAS_H |
| 20 | + |
| 21 | +#include <endian.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <string.h> |
| 24 | +#include <sys/types.h> |
| 25 | +#include <sys/stat.h> |
| 26 | +#include <unistd.h> |
| 27 | +#include <malloc.h> |
| 28 | + |
| 29 | +#define SWAP_BITS(val, ret, bits) \ |
| 30 | + do { \ |
| 31 | + char* cval = (char*)&val; \ |
| 32 | + for(unsigned int i = 0; i < (bits) / 8; ++i) \ |
| 33 | + ret |= cval[i] << (((bits)-8) - 8*i); \ |
| 34 | + } while(0) |
| 35 | + |
| 36 | +inline int64_t int64_swap(int64_t val) |
| 37 | +{ |
| 38 | + int64_t ret; |
| 39 | + SWAP_BITS(val, ret, sizeof(int64_t)*8); |
| 40 | + |
| 41 | + return ret; |
| 42 | +} |
| 43 | + |
| 44 | +inline int32_t int32_swap(int32_t val) |
| 45 | +{ |
| 46 | + int32_t ret; |
| 47 | + SWAP_BITS(val, ret, sizeof(int32_t)*8); |
| 48 | + |
| 49 | + return ret; |
| 50 | +} |
| 51 | + |
| 52 | +inline int16_t int16_swap(int16_t val) |
| 53 | +{ |
| 54 | + int16_t ret; |
| 55 | + SWAP_BITS(val, ret, sizeof(int16_t)*8); |
| 56 | + |
| 57 | + return ret; |
| 58 | +} |
| 59 | + |
| 60 | +#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 61 | + #define int16_htole(val) val |
| 62 | + #define int32_htole(val) val |
| 63 | + #define int64_htole(val) val |
| 64 | + #define int16_letoh(val) val |
| 65 | + #define int32_letoh(val) val |
| 66 | + #define int64_letoh(val) val |
| 67 | +#elif __BYTE_ORDER == __BIG_ENDIAN |
| 68 | + #define int16_htole(val) int16_swap(val) |
| 69 | + #define int32_htole(val) int32_swap(val) |
| 70 | + #define int64_htole(val) int64_swap(val) |
| 71 | + #define int16_letoh(val) int16_swap(val) |
| 72 | + #define int32_letoh(val) int32_swap(val) |
| 73 | + #define int64_letoh(val) int64_swap(val) |
| 74 | +#else |
| 75 | + #error "Could not determine your system's endianness" |
| 76 | +#endif |
| 77 | + |
| 78 | +/* Very simple string replacement function. |
| 79 | + * haystack must contain enough space for all |
| 80 | + * replacements*/ |
| 81 | +static inline void strreplace(char* haystack, const char* needle, const char* replacement) |
| 82 | +{ |
| 83 | + char* pos; |
| 84 | + int needle_len = strlen(needle); |
| 85 | + int replacement_len = strlen(replacement); |
| 86 | + |
| 87 | + while((pos = strstr(haystack, needle))) { |
| 88 | + memmove(pos+(replacement_len-needle_len), pos, strlen(pos)+1); |
| 89 | + memcpy(pos, replacement, replacement_len); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +static inline off_t file_size(const char* filename) |
| 94 | +{ |
| 95 | + struct stat stat_buf; |
| 96 | + |
| 97 | + if(stat(filename, &stat_buf) == -1) |
| 98 | + return -1; |
| 99 | + |
| 100 | + return stat_buf.st_size; |
| 101 | +} |
| 102 | + |
| 103 | +#endif |
0 commit comments