blob: da83cd65ca3eb9ff9a9795574592a6b00d4a73d4 [file] [log] [blame]
Jon Medhurst15ce78d2014-04-10 09:02:02 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2013-2015. All rights reserved.
Jon Medhurst15ce78d2014-04-10 09:02:02 +01003 *
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
14class DynBuf {
15public:
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
40private:
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