Migrate to STL

This commit is contained in:
topjohnwu
2019-01-19 23:59:37 -05:00
parent 03c39e692a
commit 3e4c12cf56
23 changed files with 226 additions and 548 deletions

View File

@@ -9,7 +9,6 @@ LOCAL_SRC_FILES := \
misc.cpp \
selinux.cpp \
logging.cpp \
xwrap.cpp \
CharArray.cpp
xwrap.cpp
include $(BUILD_STATIC_LIBRARY)

View File

@@ -1,99 +0,0 @@
#include "CharArray.h"
#include "utils.h"
CharArray::CharArray() : _buf(nullptr), _size(0){}
CharArray::CharArray(const char *s) : CharArray() {
this->operator=(s);
}
CharArray::CharArray(const CharArray &s) : CharArray() {
this->operator=(s);
}
CharArray::CharArray(size_t i) {
_size = i;
_buf = new char[i](); /* Zero initialize */
}
CharArray::~CharArray() {
delete[] _buf;
}
CharArray::operator char *() {
return _buf;
}
CharArray::operator const char *() const {
return _buf;
}
const char *CharArray::c_str() const {
return _buf;
}
size_t CharArray::length() const {
return strlen(_buf);
}
size_t CharArray::size() const {
return _size;
}
CharArray &CharArray::operator=(const CharArray &s) {
delete[] _buf;
_size = s._size;
_buf = new char[_size];
memcpy(_buf, s._buf, _size);
return *this;
}
CharArray &CharArray::operator=(const char *s) {
delete[] _buf;
_buf = strdup2(s, &_size);
return *this;
}
CharArray &CharArray::operator=(CharArray &&s) {
delete[] _buf;
_size = s._size;
_buf = s._buf;
s._buf = nullptr;
s._size = 0;
return *this;
}
bool CharArray::operator==(const char *s) const {
if (_buf == nullptr || s == nullptr)
return false;
return strcmp(_buf, s) == 0;
}
bool CharArray::operator==(char *s) const {
return *this == (const char *) s;
}
bool CharArray::operator!=(const char *s) const {
return !(*this == s);
}
int CharArray::compare(const char *s) const {
return strcmp(_buf, s);
}
int CharArray::compare(const char *s, size_t len) const {
return strncmp(_buf, s, len);
}
bool CharArray::contains(const char *s) const {
return s == nullptr ? false : strstr(_buf, s) != nullptr;
}
bool CharArray::starts_with(const char *s) const {
return s == nullptr ? false : compare(s, strlen(s)) == 0;
}
bool CharArray::empty() const {
return _buf == nullptr || _buf[0] == '\0';
}

View File

@@ -15,6 +15,8 @@
#include "utils.h"
#include "selinux.h"
using namespace std;
const char **excl_list = nullptr;
static int is_excl(const char *name) {
@@ -307,7 +309,7 @@ void clone_attr(const char *source, const char *target) {
setattr(target, &a);
}
void fclone_attr(const int sourcefd, const int targetfd) {
void fclone_attr(int sourcefd, int targetfd) {
struct file_attr a;
fgetattr(sourcefd, &a);
fsetattr(targetfd, &a);
@@ -388,24 +390,25 @@ void write_zero(int fd, size_t size) {
lseek(fd, pos + size, SEEK_SET);
}
int file_to_vector(const char *filename, Vector<CharArray> &arr) {
vector<string> file_to_vector(const char *filename) {
auto arr = vector<string>();
if (access(filename, R_OK) != 0)
return 1;
return arr;
char *line = nullptr;
size_t len = 0;
ssize_t read;
FILE *fp = xfopen(filename, "re");
if (fp == nullptr)
return 1;
return arr;
while ((read = getline(&line, &len, fp)) != -1) {
// Remove end newline
if (line[read - 1] == '\n')
line[read - 1] = '\0';
arr.push_back(line);
arr.emplace_back(line);
}
fclose(fp);
free(line);
return 0;
return arr;
}

View File

@@ -1,39 +0,0 @@
#pragma once
#include <string.h>
/* A wrapper around char array */
class CharArray {
public:
CharArray();
CharArray(const char *s);
CharArray(const CharArray &s);
CharArray(size_t i);
~CharArray();
CharArray &operator=(const CharArray &s);
CharArray &operator=(CharArray &&s);
CharArray &operator=(const char *s);
operator char *();
operator const char *() const;
bool operator==(char *s) const;
bool operator==(const char *s) const;
bool operator!=(const char *s) const;
int compare(const char *s) const;
int compare(const char *s, size_t len) const;
bool starts_with(const char *s) const;
bool contains(const char *s) const;
bool empty() const;
const char *c_str() const;
size_t length() const;
size_t size() const;
/* These 2 ops are incompatible with implicit char* conversion */
// char &operator[](size_t i);
// const char &operator[](size_t i) const;
private:
char *_buf;
size_t _size;
};

View File

@@ -1,191 +0,0 @@
#pragma once
#include <stdlib.h>
#include "cpputils.h"
template <class T>
class Vector {
public:
Vector() : _data(0), _size(0), _capacity(0) {}
~Vector() { delete []_data; }
class iterator {
friend class Vector;
public:
iterator(T* n= 0): _node(n) {}
iterator(const iterator& i): _node(i._node) {}
~iterator() {} // Should NOT delete _node
const T& operator * () const { return (*_node); }
T& operator * () { return (*_node); }
iterator& operator ++ () {
++_node;
return (*this);
}
iterator operator ++ (int) {
iterator temp = *this;
++_node;
return temp;
}
iterator& operator -- () {
--_node;
return (*this);
}
iterator operator -- (int) {
iterator temp = *this;
--_node;
return temp;
}
iterator operator + (int i) const {
iterator temp = *this;
temp += i;
return temp;
}
iterator& operator += (int i) {
_node += i;
return (*this);
}
iterator& operator = (const iterator& i) {
_node = i._node;
return (*this);
}
bool operator != (const iterator& i) const {
return _node != i._node;
}
bool operator == (const iterator& i) const { return !(*this != i); }
private:
T* _node;
};
Vector &operator=(const Vector& a) {
delete [] _data;
_data = nullptr;
_size = a._size;
_capacity = a._capacity;
if (_capacity) {
_data = new T[_capacity];
for(int i = 0; i < _size; ++i)
_data[i] = a[i];
}
return *this;
}
Vector &operator=(Vector&& a) {
delete [] _data;
_size = a._size;
_capacity = a._capacity;
_data = a._data;
a._size = 0;
a._capacity = 0;
a._data = nullptr;
return *this;
}
iterator begin() const { return iterator(_data); }
iterator end() const { return iterator(_data + _size); }
bool empty() const { return !_size; }
size_t size() const { return _size; }
T& operator [] (size_t i) { return _data[i]; }
const T& operator [] (size_t i) const { return _data[i]; }
const T& back() const { return _data[_size - 1]; }
void push_back(const T& x) {
if(_size == _capacity)
expand();
_data[_size] = x;
++_size;
}
void push_back(T&& x) {
if(_size == _capacity)
expand();
_data[_size] = utils::move(x);
++_size;
}
void pop_front() { erase(begin()); }
void pop_back() { if(_size) --_size; }
bool erase(iterator pos) {
T* d = pos._node;
if (_size == 0 || d < _data || d >= _data + _size)
return false;
for (; d < _data + _size - 1; ++d)
*d = utils::move(*(d + 1));
--_size;
return true;
}
bool erase(const T& x) {
for (T* i = _data; i < _data + _size; ++i) {
if(*i == x) {
erase(iterator(i));
return true;
}
}
return false;
}
void clear(bool dealloc = false) {
_size = 0;
if (dealloc) {
_capacity = 0;
delete [] _data;
_data = nullptr;
}
}
void sort() {
qsort(_data, _size, sizeof(T), compare);
}
T* data() { return _data; }
const T* data() const { return _data; }
// void reserve(size_t n) { ... }
// void resize(size_t n) { ... }
private:
T* _data;
size_t _size; // number of valid elements
size_t _capacity; // max number of elements
static int(*_cmp)(T&, T&);
static int compare(const void *a, const void *b) {
return _cmp ? _cmp(*((T*) a), *((T*) b)) : 0;
}
void expand() {
if (_capacity == 0)
_capacity = 1;
else
_capacity *= 2;
T* temp = _data;
_data = new T[_capacity];
for(int i = 0; i < _size; ++i)
_data[i] = utils::move(temp[i]);
delete [] temp;
}
};
template<class T>
int(* Vector<T>::_cmp)(T&, T&) = nullptr;

View File

@@ -1,12 +0,0 @@
#pragma once
namespace utils {
template< class T > struct remove_reference {typedef T type;};
template< class T > struct remove_reference<T&> {typedef T type;};
template< class T > struct remove_reference<T&&> {typedef T type;};
template< class T >
constexpr typename remove_reference<T>::type&& move( T&& t ) noexcept {
return static_cast<typename remove_reference<T>::type&&>(t);
}
}

View File

@@ -13,15 +13,17 @@
#ifdef __cplusplus
#include "Vector.h"
#include "CharArray.h"
#include "cpputils.h"
#include <string>
#include <vector>
int file_to_vector(const char *filename, Vector<CharArray> &arr);
#define str_contains(s, ss) ((s).find(ss) != string::npos)
#define str_starts(s, ss) ((s).compare(0, strlen(ss), ss) == 0)
std::vector<std::string> file_to_vector(const char *filename);
char *strdup2(const char *s, size_t *size = nullptr);
int exec_array(bool err, int *fd, void (*pre_exec)(void), const char **argv);
int exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, ...);
int exec_array(bool err, int *fd, void (*pre_exec)(), const char **argv);
int exec_command(bool err, int *fd, void (*cb)(), const char *argv0, ...);
extern "C" {
#endif
@@ -134,7 +136,7 @@ int fgetattr(int fd, struct file_attr *a);
int setattr(const char *path, struct file_attr *a);
int setattrat(int dirfd, const char *pathname, struct file_attr *a);
int fsetattr(int fd, struct file_attr *a);
void fclone_attr(const int sourcefd, const int targetfd);
void fclone_attr(int sourcefd, int targetfd);
void clone_attr(const char *source, const char *target);
void mmap_ro(const char *filename, void **buf, size_t *size);
void mmap_rw(const char *filename, void **buf, size_t *size);

View File

@@ -11,10 +11,13 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/sysmacros.h>
#include <vector>
#include "logging.h"
#include "utils.h"
using namespace std;
unsigned get_shell_uid() {
struct passwd* ppwd = getpwnam("shell");
if (nullptr == ppwd)
@@ -199,7 +202,7 @@ int exec_array(bool err, int *fd, void (*pre_exec)(void), const char **argv) {
static int v_exec_command(bool err, int *fd, void (*cb)(void), const char *argv0, va_list argv) {
// Collect va_list into vector
Vector<const char *> args;
vector<const char *> args;
args.push_back(argv0);
for (const char *arg = va_arg(argv, char*); arg; arg = va_arg(argv, char*))
args.push_back(arg);

View File

@@ -1,6 +1,9 @@
#include <new>
#include <stdlib.h>
/* Override libc++ new implementation
* to optimize final build size */
void* operator new(std::size_t s) { return malloc(s); }
void* operator new[](std::size_t s) { return malloc(s); }
void operator delete(void *p) { free(p); }

View File

@@ -86,7 +86,7 @@ ssize_t xread(int fd, void *buf, size_t count) {
ssize_t xxread(int fd, void *buf, size_t count) {
int ret = read(fd, buf, count);
if (count != ret) {
PLOGE("read");
PLOGE("read (%d != %d)", count, ret);
}
return ret;
}