cleanup build. add demo project. add timing features to sync. switch to more std::string and std::vector usage over custom, hacky Array template
This commit is contained in:
101
src/util.cpp
101
src/util.cpp
@@ -1,94 +1,25 @@
|
||||
#include <cstdio>
|
||||
#include <assert.h>
|
||||
#include <cstring>
|
||||
#include <cwctype>
|
||||
#include <stdlib.h>
|
||||
#include <glm/ext/quaternion_float.hpp>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include "util.hpp"
|
||||
#include "body.hpp"
|
||||
|
||||
Array<char*> _split_str_inner(const char* s, char delim, bool just_check_ws);
|
||||
using namespace std;
|
||||
|
||||
template<class T> void append(Array<T>& a, T el) {
|
||||
if (a.len == a.cap) {
|
||||
resize(a, std::max((size_t)8, a.cap * 2));
|
||||
}
|
||||
a[a.len] = el;
|
||||
a.len++;
|
||||
}
|
||||
|
||||
template<class T> T pop(Array<T>& a) {
|
||||
assert(a.len >= 1);
|
||||
a.len--;
|
||||
return a.data[a.len + 1];
|
||||
}
|
||||
|
||||
template<class T> void resize(Array<T>& a, size_t new_cap) {
|
||||
T* new_data = (T*)malloc(sizeof(T) * new_cap);
|
||||
if (a.len > 0) {
|
||||
memcpy(new_data, a.data, std::min(a.len, new_cap));
|
||||
}
|
||||
free(a.data);
|
||||
a.data = new_data;
|
||||
a.len = std::min(a.len, new_cap);
|
||||
a.cap = new_cap;
|
||||
}
|
||||
|
||||
template<class T> void clear(Array<T>& a) {
|
||||
a.len = 0;
|
||||
}
|
||||
|
||||
bool read_file(Array<char>* out, const char* filepath) {
|
||||
FILE* fp = NULL;
|
||||
if (fopen_s(&fp, filepath, "rb") != 0) {
|
||||
printf("ERROR Failed to open file %s\n", filepath);
|
||||
bool read_file(string* s, const char* filepath) {
|
||||
ifstream file(filepath);
|
||||
if (!file.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
size_t sz = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
char* data = (char*)malloc(sizeof(char) * sz);
|
||||
fread(data, sizeof(char), sz, fp);
|
||||
fclose(fp);
|
||||
out->data = data;
|
||||
out->len = sz;
|
||||
return true;
|
||||
*s = { istreambuf_iterator<char>(file), istreambuf_iterator<char>() };
|
||||
}
|
||||
|
||||
Array<char*> split_str(const char* s, char delimiter) { return _split_str_inner(s, delimiter, false); }
|
||||
|
||||
Array<char*> split_str(const char* s) { return _split_str_inner(s, ' ', true); }
|
||||
|
||||
Array<char*> _split_str_inner(const char* s, char delim, bool just_check_ws) {
|
||||
Array<char*> res = { NULL, 0, 0 };
|
||||
|
||||
char c;
|
||||
int i = 0;
|
||||
Array<char> cur_word = { NULL, 0, 0 };
|
||||
while (true) {
|
||||
c = s[i++];
|
||||
bool is_delim = just_check_ws ? iswspace(c) : c == delim;
|
||||
if ((is_delim || c == '\0') && cur_word.len > 0) {
|
||||
char* word = (char*)malloc(sizeof(char) * cur_word.len + 1);
|
||||
memcpy(word, cur_word.data, cur_word.len);
|
||||
word[cur_word.len] = '\0';
|
||||
append(res, word);
|
||||
clear(cur_word);
|
||||
if (c == '\0') {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
append(cur_word, c);
|
||||
}
|
||||
|
||||
if (c == '\0') {
|
||||
break;
|
||||
}
|
||||
vector<string> split_str(string s, char delim) {
|
||||
vector<string> res;
|
||||
string cur_word;
|
||||
istringstream ss(s);
|
||||
while (getline(ss, cur_word, delim)) {
|
||||
res.push_back(cur_word);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -109,6 +40,4 @@ glm::mat4 quat_to_mat4(glm::quat q) {
|
||||
return glm::mat4(col0, col1, col2, col3);
|
||||
}
|
||||
|
||||
float randf() {
|
||||
return (float)rand() / (float)RAND_MAX;
|
||||
}
|
||||
float randf() { return (float)rand() / (float)RAND_MAX; }
|
||||
Reference in New Issue
Block a user