Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <stdarg.h> |
| 6 | #include <stdio.h> |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame^] | 7 | #include <string.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 8 | |
| 9 | #include "SStream.h" |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 10 | #include "cs_priv.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 11 | |
| 12 | void SStream_Init(SStream *ss) |
| 13 | { |
| 14 | ss->index = 0; |
| 15 | ss->buffer[0] = '\0'; |
| 16 | } |
| 17 | |
Nguyen Anh Quynh | b76233c | 2014-06-04 18:31:02 +0800 | [diff] [blame^] | 18 | void SStream_concat0(SStream *ss, char *s) |
| 19 | { |
| 20 | #ifndef CAPSTONE_DIET |
| 21 | strcpy(ss->buffer + ss->index, s); |
| 22 | ss->index += strlen(s); |
| 23 | #endif |
| 24 | } |
| 25 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 26 | void SStream_concat(SStream *ss, const char *fmt, ...) |
| 27 | { |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 28 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 29 | va_list ap; |
Nguyen Anh Quynh | bb0744d | 2014-05-12 13:41:49 +0800 | [diff] [blame] | 30 | int ret; |
| 31 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 32 | va_start(ap, fmt); |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 33 | ret = cs_vsnprintf(ss->buffer + ss->index, sizeof(ss->buffer) - (ss->index + 1), fmt, ap); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 34 | va_end(ap); |
| 35 | ss->index += ret; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 36 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* |
| 40 | int main() |
| 41 | { |
| 42 | SStream ss; |
| 43 | int64_t i; |
| 44 | |
| 45 | SStream_Init(&ss); |
| 46 | |
| 47 | SStream_concat(&ss, "hello "); |
| 48 | SStream_concat(&ss, "%d - 0x%x", 200, 16); |
| 49 | |
| 50 | i = 123; |
| 51 | SStream_concat(&ss, " + %ld", i); |
| 52 | SStream_concat(&ss, "%s", "haaaaa"); |
| 53 | |
| 54 | printf("%s\n", ss.buffer); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | */ |