summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/DynBuf.h
blob: 67c06ba4a409776fd65cf3c0a2ae31c3d7f6739a (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-2015. 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