summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/DynBuf.h
blob: d7865e8132ab96e1b6c7c297b83410cbc894ab2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * Copyright (C) ARM Limited 2013-2016. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef DYNBUF_H
#define DYNBUF_H

#include <stdarg.h>
#include <stdlib.h>

class DynBuf {
public:
	DynBuf() : capacity(0), length(0), buf(NULL) {}
	~DynBuf() {
		reset();
	}

	inline void reset() {
		capacity = 0;
		length = 0;
		if (buf != NULL) {
			free(buf);
			buf = NULL;
		}
	}

	bool read(const char *const path);
	// On error instead of printing the error and returning false, this returns -errno
	int readlink(const char *const path);
	__attribute__ ((format(printf, 2, 3)))
	bool printf(const char *format, ...);
	__attribute__ ((format(printf, 2, 3)))
	bool append(const char *format, ...);
	bool append(const char *format, va_list ap);
	bool appendStr(const char *str);

	size_t getLength() const { return length; }
	const char *getBuf() const { return buf; }
	char *getBuf() { return buf; }

private:
	int resize(const size_t minCapacity);

	size_t capacity;
	size_t length;
	char *buf;

	// Intentionally undefined
	DynBuf(const DynBuf &);
	DynBuf &operator=(const DynBuf &);
};

#endif // DYNBUF_H