DynBuf.h 1.07 KB
Newer Older
Abhijith PA's avatar
Abhijith PA committed
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
/**
 * Copyright (C) ARM Limited 2013-2014. 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 <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, ...);

	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