src/mess/tools/pile.h
| r13273 | r13274 | |
| 1 | | /**************************************************************************** |
| 2 | | |
| 3 | | pile.h |
| 4 | | |
| 5 | | Utility code to implement "piles"; blocks of data that are built up |
| 6 | | and then detached |
| 7 | | |
| 8 | | ****************************************************************************/ |
| 9 | | |
| 10 | | #ifndef PILE_H |
| 11 | | #define PILE_H |
| 12 | | |
| 13 | | #include <stdlib.h> |
| 14 | | #include <stdarg.h> |
| 15 | | |
| 16 | | typedef struct |
| 17 | | { |
| 18 | | void *ptr; |
| 19 | | size_t physical_size; |
| 20 | | size_t logical_size; |
| 21 | | } mess_pile; |
| 22 | | |
| 23 | | void pile_init(mess_pile *pile); |
| 24 | | void pile_delete(mess_pile *pile); |
| 25 | | void pile_clear(mess_pile *pile); |
| 26 | | void *pile_detach(mess_pile *pile); |
| 27 | | int pile_write(mess_pile *pile, const void *ptr, size_t size); |
| 28 | | int pile_writebyte(mess_pile *pile, char byte, size_t size); |
| 29 | | |
| 30 | | INLINE void *pile_getptr(mess_pile *pile) |
| 31 | | { |
| 32 | | return pile->ptr; |
| 33 | | } |
| 34 | | |
| 35 | | INLINE size_t pile_size(mess_pile *pile) |
| 36 | | { |
| 37 | | return pile->logical_size; |
| 38 | | } |
| 39 | | |
| 40 | | int pile_vprintf(mess_pile *pile, const char *fmt, va_list args); |
| 41 | | #ifdef __GNUC__ |
| 42 | | int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/__attribute__((format(printf, 2, 3))); |
| 43 | | #else |
| 44 | | int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/; |
| 45 | | #endif |
| 46 | | int pile_putc(mess_pile *pile, char c); |
| 47 | | int pile_puts(mess_pile *pile, const char *s); |
| 48 | | |
| 49 | | #endif /* PILE_H */ |
| 50 | | |
| 51 | | |
src/mess/tools/pile.c
| r13273 | r13274 | |
| 1 | | /**************************************************************************** |
| 2 | | |
| 3 | | pile.h |
| 4 | | |
| 5 | | Utility code to implement "piles"; blocks of data that are built up |
| 6 | | and then detached |
| 7 | | |
| 8 | | ****************************************************************************/ |
| 9 | | |
| 10 | | #include <string.h> |
| 11 | | #include <stdio.h> |
| 12 | | |
| 13 | | #include "osdcomm.h" |
| 14 | | #include "pile.h" |
| 15 | | |
| 16 | | void pile_init(mess_pile *pile) |
| 17 | | { |
| 18 | | pile->ptr = NULL; |
| 19 | | pile->physical_size = 0; |
| 20 | | pile->logical_size = 0; |
| 21 | | } |
| 22 | | |
| 23 | | |
| 24 | | |
| 25 | | void pile_delete(mess_pile *pile) |
| 26 | | { |
| 27 | | if (pile->ptr) |
| 28 | | { |
| 29 | | free(pile->ptr); |
| 30 | | pile_init(pile); |
| 31 | | } |
| 32 | | } |
| 33 | | |
| 34 | | |
| 35 | | |
| 36 | | void pile_clear(mess_pile *pile) |
| 37 | | { |
| 38 | | pile_delete(pile); |
| 39 | | } |
| 40 | | |
| 41 | | |
| 42 | | |
| 43 | | void *pile_detach(mess_pile *pile) |
| 44 | | { |
| 45 | | void *ptr; |
| 46 | | |
| 47 | | ptr = realloc(pile->ptr, pile->logical_size); |
| 48 | | if (!ptr) |
| 49 | | ptr = pile->ptr; |
| 50 | | |
| 51 | | pile_init(pile); |
| 52 | | return ptr; |
| 53 | | } |
| 54 | | |
| 55 | | |
| 56 | | |
| 57 | | int pile_write(mess_pile *pile, const void *ptr, size_t size) |
| 58 | | { |
| 59 | | size_t new_logical_size; |
| 60 | | size_t new_physical_size; |
| 61 | | size_t blocksize = 4096; |
| 62 | | char *new_ptr; |
| 63 | | |
| 64 | | new_logical_size = pile->logical_size + size; |
| 65 | | if (new_logical_size > pile->physical_size) |
| 66 | | { |
| 67 | | new_physical_size = new_logical_size + blocksize; |
| 68 | | new_physical_size -= new_physical_size % blocksize; |
| 69 | | |
| 70 | | new_ptr = (char *) realloc(pile->ptr, new_physical_size); |
| 71 | | if (!new_ptr) |
| 72 | | return -1; |
| 73 | | |
| 74 | | pile->ptr = new_ptr; |
| 75 | | pile->physical_size = new_physical_size; |
| 76 | | } |
| 77 | | memcpy(((char *) pile->ptr) + pile->logical_size, ptr, size); |
| 78 | | pile->logical_size = new_logical_size; |
| 79 | | return 0; |
| 80 | | } |
| 81 | | |
| 82 | | |
| 83 | | |
| 84 | | int pile_writebyte(mess_pile *pile, char byte, size_t size) |
| 85 | | { |
| 86 | | int rc = 0; |
| 87 | | |
| 88 | | while(size--) |
| 89 | | { |
| 90 | | rc = pile_write(pile, &byte, 1); |
| 91 | | if (rc) |
| 92 | | return rc; |
| 93 | | } |
| 94 | | return rc; |
| 95 | | } |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | int pile_vprintf(mess_pile *pile, const char *fmt, va_list args) |
| 100 | | { |
| 101 | | int count; |
| 102 | | char buf[1024]; |
| 103 | | |
| 104 | | count = vsnprintf(buf, ARRAY_LENGTH(buf), fmt, args); |
| 105 | | if (pile_write(pile, buf, count * sizeof(*buf))) |
| 106 | | return 0; |
| 107 | | return count; |
| 108 | | } |
| 109 | | |
| 110 | | |
| 111 | | |
| 112 | | int pile_printf(mess_pile *pile, const char *fmt, ...) |
| 113 | | { |
| 114 | | int count; |
| 115 | | va_list va; |
| 116 | | |
| 117 | | va_start(va, fmt); |
| 118 | | count = pile_vprintf(pile, fmt, va); |
| 119 | | va_end(va); |
| 120 | | return count; |
| 121 | | } |
| 122 | | |
| 123 | | |
| 124 | | |
| 125 | | int pile_putc(mess_pile *pile, char c) |
| 126 | | { |
| 127 | | return pile_write(pile, &c, sizeof(c)); |
| 128 | | } |
| 129 | | |
| 130 | | |
| 131 | | |
| 132 | | int pile_puts(mess_pile *pile, const char *s) |
| 133 | | { |
| 134 | | size_t size; |
| 135 | | size = strlen(s); |
| 136 | | return pile_write(pile, s, size); |
| 137 | | } |
| 138 | | |
| 139 | | |
src/mess/tools/tools.mak
| r13273 | r13274 | |
| 27 | 27 | include $(MESSSRC)/tools/floptool/floptool.mak |
| 28 | 28 | TOOLS += $(FLOPTOOL) |
| 29 | 29 | |
| 30 | | #ifdef BUILD_MESSTEST |
| 31 | | #include $(MESSSRC)/tools/messtest/messtest.mak |
| 32 | | #TOOLS += $(MESSTEST) |
| 33 | | #endif |
| 34 | | |
| 35 | | ifdef BUILD_DAT2HTML |
| 36 | | include $(MESSSRC)/tools/dat2html/dat2html.mak |
| 37 | | TOOLS += $(DAT2HTML) |
| 38 | | endif |
| 39 | | |
| 40 | 30 | # include OS-specific MESS stuff |
| 41 | 31 | # the following expression is true if OSD is windows or winui |
| 42 | 32 | ifeq ($(OSD),$(filter $(OSD),windows winui)) |
| 43 | | include $(MESSSRC)/tools/messdocs/messdocs.mak |
| 44 | 33 | |
| 45 | 34 | ifdef BUILD_WIMGTOOL |
| 46 | 35 | include $(MESSSRC)/tools/imgtool/windows/wimgtool.mak |
src/mess/tools/imgtool/windows/pile.h
| r0 | r13274 | |
| 1 | /**************************************************************************** |
| 2 | |
| 3 | pile.h |
| 4 | |
| 5 | Utility code to implement "piles"; blocks of data that are built up |
| 6 | and then detached |
| 7 | |
| 8 | ****************************************************************************/ |
| 9 | |
| 10 | #ifndef PILE_H |
| 11 | #define PILE_H |
| 12 | |
| 13 | #include <stdlib.h> |
| 14 | #include <stdarg.h> |
| 15 | |
| 16 | typedef struct |
| 17 | { |
| 18 | void *ptr; |
| 19 | size_t physical_size; |
| 20 | size_t logical_size; |
| 21 | } mess_pile; |
| 22 | |
| 23 | void pile_init(mess_pile *pile); |
| 24 | void pile_delete(mess_pile *pile); |
| 25 | void pile_clear(mess_pile *pile); |
| 26 | void *pile_detach(mess_pile *pile); |
| 27 | int pile_write(mess_pile *pile, const void *ptr, size_t size); |
| 28 | int pile_writebyte(mess_pile *pile, char byte, size_t size); |
| 29 | |
| 30 | INLINE void *pile_getptr(mess_pile *pile) |
| 31 | { |
| 32 | return pile->ptr; |
| 33 | } |
| 34 | |
| 35 | INLINE size_t pile_size(mess_pile *pile) |
| 36 | { |
| 37 | return pile->logical_size; |
| 38 | } |
| 39 | |
| 40 | int pile_vprintf(mess_pile *pile, const char *fmt, va_list args); |
| 41 | #ifdef __GNUC__ |
| 42 | int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/__attribute__((format(printf, 2, 3))); |
| 43 | #else |
| 44 | int pile_printf(mess_pile *pile, const char *fmt, ...)/* ATTR_PRINTF(2,3)*/; |
| 45 | #endif |
| 46 | int pile_putc(mess_pile *pile, char c); |
| 47 | int pile_puts(mess_pile *pile, const char *s); |
| 48 | |
| 49 | #endif /* PILE_H */ |
| 50 | |
| 51 | |
src/mess/tools/imgtool/windows/pile.c
| r0 | r13274 | |
| 1 | /**************************************************************************** |
| 2 | |
| 3 | pile.h |
| 4 | |
| 5 | Utility code to implement "piles"; blocks of data that are built up |
| 6 | and then detached |
| 7 | |
| 8 | ****************************************************************************/ |
| 9 | |
| 10 | #include <string.h> |
| 11 | #include <stdio.h> |
| 12 | |
| 13 | #include "osdcomm.h" |
| 14 | #include "pile.h" |
| 15 | |
| 16 | void pile_init(mess_pile *pile) |
| 17 | { |
| 18 | pile->ptr = NULL; |
| 19 | pile->physical_size = 0; |
| 20 | pile->logical_size = 0; |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | void pile_delete(mess_pile *pile) |
| 26 | { |
| 27 | if (pile->ptr) |
| 28 | { |
| 29 | free(pile->ptr); |
| 30 | pile_init(pile); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | |
| 36 | void pile_clear(mess_pile *pile) |
| 37 | { |
| 38 | pile_delete(pile); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | void *pile_detach(mess_pile *pile) |
| 44 | { |
| 45 | void *ptr; |
| 46 | |
| 47 | ptr = realloc(pile->ptr, pile->logical_size); |
| 48 | if (!ptr) |
| 49 | ptr = pile->ptr; |
| 50 | |
| 51 | pile_init(pile); |
| 52 | return ptr; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | int pile_write(mess_pile *pile, const void *ptr, size_t size) |
| 58 | { |
| 59 | size_t new_logical_size; |
| 60 | size_t new_physical_size; |
| 61 | size_t blocksize = 4096; |
| 62 | char *new_ptr; |
| 63 | |
| 64 | new_logical_size = pile->logical_size + size; |
| 65 | if (new_logical_size > pile->physical_size) |
| 66 | { |
| 67 | new_physical_size = new_logical_size + blocksize; |
| 68 | new_physical_size -= new_physical_size % blocksize; |
| 69 | |
| 70 | new_ptr = (char *) realloc(pile->ptr, new_physical_size); |
| 71 | if (!new_ptr) |
| 72 | return -1; |
| 73 | |
| 74 | pile->ptr = new_ptr; |
| 75 | pile->physical_size = new_physical_size; |
| 76 | } |
| 77 | memcpy(((char *) pile->ptr) + pile->logical_size, ptr, size); |
| 78 | pile->logical_size = new_logical_size; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | int pile_writebyte(mess_pile *pile, char byte, size_t size) |
| 85 | { |
| 86 | int rc = 0; |
| 87 | |
| 88 | while(size--) |
| 89 | { |
| 90 | rc = pile_write(pile, &byte, 1); |
| 91 | if (rc) |
| 92 | return rc; |
| 93 | } |
| 94 | return rc; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | int pile_vprintf(mess_pile *pile, const char *fmt, va_list args) |
| 100 | { |
| 101 | int count; |
| 102 | char buf[1024]; |
| 103 | |
| 104 | count = vsnprintf(buf, ARRAY_LENGTH(buf), fmt, args); |
| 105 | if (pile_write(pile, buf, count * sizeof(*buf))) |
| 106 | return 0; |
| 107 | return count; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | int pile_printf(mess_pile *pile, const char *fmt, ...) |
| 113 | { |
| 114 | int count; |
| 115 | va_list va; |
| 116 | |
| 117 | va_start(va, fmt); |
| 118 | count = pile_vprintf(pile, fmt, va); |
| 119 | va_end(va); |
| 120 | return count; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | int pile_putc(mess_pile *pile, char c) |
| 126 | { |
| 127 | return pile_write(pile, &c, sizeof(c)); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | int pile_puts(mess_pile *pile, const char *s) |
| 133 | { |
| 134 | size_t size; |
| 135 | size = strlen(s); |
| 136 | return pile_write(pile, s, size); |
| 137 | } |
| 138 | |
| 139 | |