gator: Merge gator version 5.23.1
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / tools / gator / daemon / DynBuf.h
1 /**
2  * Copyright (C) ARM Limited 2013-2015. All rights reserved.
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 <stdarg.h>
13 #include <stdlib.h>
14
15 class DynBuf {
16 public:
17         DynBuf() : capacity(0), length(0), buf(NULL) {}
18         ~DynBuf() {
19                 reset();
20         }
21
22         inline void reset() {
23                 capacity = 0;
24                 length = 0;
25                 if (buf != NULL) {
26                         free(buf);
27                         buf = NULL;
28                 }
29         }
30
31         bool read(const char *const path);
32         // On error instead of printing the error and returning false, this returns -errno
33         int readlink(const char *const path);
34         __attribute__ ((format(printf, 2, 3)))
35         bool printf(const char *format, ...);
36         __attribute__ ((format(printf, 2, 3)))
37         bool append(const char *format, ...);
38         bool append(const char *format, va_list ap);
39         bool appendStr(const char *str);
40
41         size_t getLength() const { return length; }
42         const char *getBuf() const { return buf; }
43         char *getBuf() { return buf; }
44
45 private:
46         int resize(const size_t minCapacity);
47
48         size_t capacity;
49         size_t length;
50         char *buf;
51
52         // Intentionally undefined
53         DynBuf(const DynBuf &);
54         DynBuf &operator=(const DynBuf &);
55 };
56
57 #endif // DYNBUF_H