Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 1 | /** |
Jon Medhurst | b1d0744 | 2015-05-08 12:04:18 +0100 | [diff] [blame] | 2 | * Copyright (C) ARM Limited 2013-2015. All rights reserved. |
Jon Medhurst | 15ce78d | 2014-04-10 09:02:02 +0100 | [diff] [blame] | 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 |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #ifndef DYNBUF_H |
| 10 | #define DYNBUF_H |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | class DynBuf { |
| 15 | public: |
| 16 | DynBuf() : capacity(0), length(0), buf(NULL) {} |
| 17 | ~DynBuf() { |
| 18 | reset(); |
| 19 | } |
| 20 | |
| 21 | inline void reset() { |
| 22 | capacity = 0; |
| 23 | length = 0; |
| 24 | if (buf != NULL) { |
| 25 | free(buf); |
| 26 | buf = NULL; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | bool read(const char *const path); |
| 31 | // On error instead of printing the error and returning false, this returns -errno |
| 32 | int readlink(const char *const path); |
| 33 | __attribute__ ((format(printf, 2, 3))) |
| 34 | bool printf(const char *format, ...); |
| 35 | |
| 36 | size_t getLength() const { return length; } |
| 37 | const char *getBuf() const { return buf; } |
| 38 | char *getBuf() { return buf; } |
| 39 | |
| 40 | private: |
| 41 | int resize(const size_t minCapacity); |
| 42 | |
| 43 | size_t capacity; |
| 44 | size_t length; |
| 45 | char *buf; |
| 46 | |
| 47 | // Intentionally undefined |
| 48 | DynBuf(const DynBuf &); |
| 49 | DynBuf &operator=(const DynBuf &); |
| 50 | }; |
| 51 | |
| 52 | #endif // DYNBUF_H |