update
This commit is contained in:
211
app.cpp
211
app.cpp
@@ -60,7 +60,41 @@ GymLogDbParsed *parseDb(Arena *arena, string database) {
|
|||||||
return dbParsed;
|
return dbParsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) {
|
list<GymLogEntry> loadEntryLog(Arena *arena, string fileLocation) {
|
||||||
|
list<GymLogEntry> result = {0};
|
||||||
|
string logfile = readEntireFile(arena, LOG_FILE_LOCATION);
|
||||||
|
|
||||||
|
if (logfile.length % sizeof(GymLogEntry) != 0) {
|
||||||
|
log("Log file corrupted.\n");
|
||||||
|
} else {
|
||||||
|
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||||
|
result = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct WorkSummary {
|
||||||
|
real32 totalWork;
|
||||||
|
uint32 restTime;
|
||||||
|
};
|
||||||
|
|
||||||
|
WorkSummary workSummaryForExercise(list<GymLogEntry> entries) {
|
||||||
|
WorkSummary result = {0};
|
||||||
|
UnixTimestamp lastTimestamp = 0;
|
||||||
|
for (EachInReversed(entries, i)) {
|
||||||
|
GymLogEntry logEntry = entries.data[i];
|
||||||
|
result.totalWork += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
|
||||||
|
if (lastTimestamp > 0) {
|
||||||
|
result.restTime += (uint32)(lastTimestamp - logEntry.timestamp);
|
||||||
|
}
|
||||||
|
lastTimestamp = logEntry.timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gymTrackerWorkToday(Arena *arena, uint32 exerciseId, string exerciseName) {
|
||||||
int statusCode = 0;
|
int statusCode = 0;
|
||||||
string logfile = readEntireFile(arena, LOG_FILE_LOCATION);
|
string logfile = readEntireFile(arena, LOG_FILE_LOCATION);
|
||||||
|
|
||||||
@@ -69,23 +103,27 @@ int gymTrackerWorkForExercise(Arena *arena, uint32 exerciseId) {
|
|||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
} else {
|
} else {
|
||||||
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
size_t entryCount = logfile.length / sizeof(GymLogEntry);
|
||||||
int currentDay = 0;
|
|
||||||
int currentYear = 0;
|
|
||||||
list<GymLogEntry> logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
list<GymLogEntry> logEntries = { (GymLogEntry *)logfile.str, entryCount, entryCount };
|
||||||
|
|
||||||
UnixTimestamp todayUnix = getSystemUnixTime();
|
UnixTimestamp todayUnix = getSystemUnixTime();
|
||||||
Timestamp todayTs = timestampFromUnixTime(&todayUnix);
|
Timestamp todayTs = timestampFromUnixTime(&todayUnix);
|
||||||
|
list<GymLogEntry> todaysEntries = {0};
|
||||||
real32 work = 0;
|
todaysEntries.data = logEntries.data;
|
||||||
for (EachIn(logEntries, i)) {
|
for (EachInReversed(logEntries, i)) {
|
||||||
GymLogEntry logEntry = logEntries.data[i];
|
GymLogEntry logEntry = logEntries.data[i];
|
||||||
Timestamp logTs = timestampFromUnixTime(&logEntry.timestamp);
|
Timestamp logTs = timestampFromUnixTime(&logEntry.timestamp);
|
||||||
if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) {
|
if (logTs.tm_yday == todayTs.tm_yday && todayTs.tm_year == logTs.tm_year) {
|
||||||
work += logEntry.weightRepsInfo.weight * logEntry.weightRepsInfo.reps;
|
todaysEntries.head += 1;
|
||||||
|
todaysEntries.data = &logEntries.data[i + 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log("Total work for this exercise today:\n%.2f J * m/ex\n", work);
|
if (todaysEntries.data) {
|
||||||
|
todaysEntries.length = todaysEntries.head;
|
||||||
|
WorkSummary summary = workSummaryForExercise(todaysEntries);
|
||||||
|
log("Total work today for %S:\n%.2fkg in ~%.2fmin.\n", exerciseName, summary.totalWork, (real32)summary.restTime / 60.0f);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return statusCode;
|
return statusCode;
|
||||||
@@ -101,94 +139,115 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
|||||||
puts("Log file corrupted.");
|
puts("Log file corrupted.");
|
||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
} else {
|
} else {
|
||||||
Timestamp stopTs = {0};
|
Timestamp startTs = {0};
|
||||||
if (args.length > 0 && strEql(args.data[0], "--today"_s)) {
|
int numDays = 1;
|
||||||
UnixTimestamp nowUnix = getSystemUnixTime();
|
bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
|
||||||
stopTs = timestampFromUnixTime(&nowUnix);
|
if (!showAll) {
|
||||||
} else if (args.length > 1 && strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s)) {
|
if (args.length == 2 && (strEql(args.data[0], "--days"_s) || strEql(args.data[0], "-d"_s))) {
|
||||||
size_t l;
|
size_t l;
|
||||||
int numDays = parsePositiveInt(args.data[1], &l);
|
numDays = parsePositiveInt(args.data[1], &l);
|
||||||
|
}
|
||||||
if (numDays == -1) {
|
if (numDays == -1) {
|
||||||
puts("Bad argument for --days parameter.");
|
puts("Bad argument for --days (-d) parameter.");
|
||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
} else {
|
} else {
|
||||||
uint64 todayUnix = getSystemUnixTime();
|
uint64 todayUnix = getSystemUnixTime();
|
||||||
UnixTimestamp stopUnix = todayUnix - numDays * 24 * 60 * 60;
|
UnixTimestamp startUnix = todayUnix - numDays * 24 * 60 * 60;
|
||||||
stopTs = timestampFromUnixTime(&stopUnix);
|
startTs = timestampFromUnixTime(&startUnix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusCode == 0) {
|
if (statusCode == 0) {
|
||||||
|
int lastDay = -1;
|
||||||
|
int lastYear = -1;
|
||||||
size_t entryCount = file.length / sizeof(GymLogEntry);
|
size_t entryCount = file.length / sizeof(GymLogEntry);
|
||||||
int currentDay = -1;
|
|
||||||
int currentYear = -1;
|
|
||||||
list<GymLogEntry> logEntries = { (GymLogEntry *)file.str, entryCount, entryCount };
|
list<GymLogEntry> logEntries = { (GymLogEntry *)file.str, entryCount, entryCount };
|
||||||
|
|
||||||
list<real32> workPerExerciseByDay = PushFullList(arena, real32, db->header.nextId);
|
list<string> nameByExercise = PushFullListZero(arena, string, db->header.nextId);
|
||||||
list<string> nameByExercise = PushFullList(arena, string, db->header.nextId);
|
|
||||||
zeroListFull(&workPerExerciseByDay);
|
list<real32> workPerExerciseByDay = PushFullListZero(arena, real32, db->header.nextId);
|
||||||
zeroListFull(&nameByExercise);
|
list<uint32> restPerExerciseByDay = PushFullListZero(arena, uint32, db->header.nextId);
|
||||||
|
list<uint32> lastTsPerExerciseByDay = PushFullListZero(arena, uint32, db->header.nextId);
|
||||||
|
|
||||||
int dayCount = 0;
|
int dayCount = 0;
|
||||||
|
|
||||||
Timestamp timestamp = {0};
|
Timestamp timestamp = {0};
|
||||||
if (logEntries.length > 0) {
|
|
||||||
timestamp = timestampFromUnixTime(&logEntries.data[logEntries.length - 1].timestamp);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (EachInReversed(logEntries, i)) {
|
GymLogEntry *prevEntry = 0;
|
||||||
GymLogEntry entry = logEntries.data[i];
|
GymLogEntry *entry = 0;
|
||||||
if (timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) {
|
for (EachIn(logEntries, i)) {
|
||||||
if (timestamp.tm_year < stopTs.tm_year || timestamp.tm_yday < stopTs.tm_yday) {
|
prevEntry = entry;
|
||||||
break;
|
entry = &logEntries.data[i];
|
||||||
}
|
|
||||||
|
timestamp = timestampFromUnixTime(&entry->timestamp);
|
||||||
|
|
||||||
|
if (timestamp.tm_year < startTs.tm_year || timestamp.tm_yday < startTs.tm_yday) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timestamp.tm_yday != lastDay || timestamp.tm_year != lastYear) {
|
||||||
if (dayCount > 0) {
|
if (dayCount > 0) {
|
||||||
log("\n");
|
log("\n");
|
||||||
}
|
}
|
||||||
log("--- %S ---\n", formatTimeYmd(arena, ×tamp));
|
log("================ %S =================================\n", formatTimeYmd(arena, ×tamp));
|
||||||
currentDay = timestamp.tm_yday;
|
lastDay = timestamp.tm_yday;
|
||||||
currentYear = timestamp.tm_year;
|
lastYear = timestamp.tm_year;
|
||||||
dayCount++;
|
dayCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
workPerExerciseByDay.data[entry.exerciseId] += entry.weightRepsInfo.reps * entry.weightRepsInfo.weight;
|
workPerExerciseByDay.data[entry->exerciseId] += entry->weightRepsInfo.reps * entry->weightRepsInfo.weight;
|
||||||
|
uint32 lastTsForExercise = lastTsPerExerciseByDay.data[entry->exerciseId];
|
||||||
|
if (lastTsForExercise > 0) {
|
||||||
|
restPerExerciseByDay.data[entry->exerciseId] += (uint32)(entry->timestamp - lastTsForExercise);
|
||||||
|
}
|
||||||
|
lastTsPerExerciseByDay.data[entry->exerciseId] = (uint32)entry->timestamp;
|
||||||
|
|
||||||
const char *format;
|
const char *format;
|
||||||
if (entry.weightRepsInfo.weight == (int32)entry.weightRepsInfo.weight) {
|
if (entry->weightRepsInfo.weight == (int32)entry->weightRepsInfo.weight) {
|
||||||
format = "%S: %S, %.0fkg X %i\n";
|
format = "%S: %S %.0fkg X %i\n";
|
||||||
} else {
|
} else {
|
||||||
format = "%S: %S, %.2fkg X %i\n";
|
format = "%S: %S %.2fkg X %i\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
string *exerciseName = &(nameByExercise.data[entry.exerciseId]);
|
string *exerciseName = &(nameByExercise.data[entry->exerciseId]);
|
||||||
if (exerciseName->str == 0) {
|
if (exerciseName->str == 0) {
|
||||||
for (EachIn(db->entries, j)) {
|
for (EachIn(db->entries, j)) {
|
||||||
GymLogDbParsedEntry dbEntry = db->entries.data[j];
|
GymLogDbParsedEntry dbEntry = db->entries.data[j];
|
||||||
if (dbEntry.id == entry.exerciseId) {
|
if (dbEntry.id == entry->exerciseId) {
|
||||||
*exerciseName = dbEntry.name;
|
*exerciseName = dbEntry.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string nameToPrint = {0};
|
||||||
|
if (prevEntry && entry->exerciseId == prevEntry->exerciseId) {
|
||||||
|
nameToPrint = PushStringFill(arena, exerciseName->length, '.');
|
||||||
|
} else {
|
||||||
|
nameToPrint = exerciseName->str ? *exerciseName : "unknown-exercise"_s;
|
||||||
|
log("\n");
|
||||||
|
}
|
||||||
log(format,
|
log(format,
|
||||||
formatTimeHms(arena, ×tamp),
|
formatTimeHms(arena, ×tamp),
|
||||||
exerciseName->str ? *exerciseName : "unknown-exercise"_s,
|
nameToPrint,
|
||||||
entry.weightRepsInfo.weight,
|
entry->weightRepsInfo.weight,
|
||||||
entry.weightRepsInfo.reps);
|
entry->weightRepsInfo.reps);
|
||||||
|
|
||||||
if (i > 0) {
|
Timestamp nextTimestamp = {0};
|
||||||
timestamp = timestampFromUnixTime(&logEntries.data[i - 1].timestamp);
|
if (i < logEntries.head - 1) {
|
||||||
|
nextTimestamp = timestampFromUnixTime(&logEntries.data[i + 1].timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == 0 || timestamp.tm_yday != currentDay || timestamp.tm_year != currentYear) {
|
if (i == logEntries.head + 1 || nextTimestamp.tm_yday != lastDay || nextTimestamp.tm_year != lastYear) {
|
||||||
log("\n");
|
log("\n");
|
||||||
log("Work summary:\n");
|
log("Work summary:\n");
|
||||||
for (size_t j = 0; j < workPerExerciseByDay.length; j++) {
|
for (size_t j = 0; j < workPerExerciseByDay.length; j++) {
|
||||||
if (workPerExerciseByDay.data[j] != 0.0f) {
|
if (workPerExerciseByDay.data[j] != 0.0f) {
|
||||||
log("%S: %.2f J * m/ex\n", nameByExercise.data[j], workPerExerciseByDay.data[j]);
|
log("%S: %.2fkg in %.2fmin\n", nameByExercise.data[j], workPerExerciseByDay.data[j], (real32)restPerExerciseByDay.data[j] / 60.0f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zeroListFull(&workPerExerciseByDay);
|
zeroListFull(&workPerExerciseByDay);
|
||||||
|
zeroListFull(&restPerExerciseByDay);
|
||||||
|
zeroListFull(&lastTsPerExerciseByDay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,6 +256,32 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
|
|||||||
return statusCode;
|
return statusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int gymTrackerDeleteEntries(Arena *arena, list<string> args) {
|
||||||
|
int statusCode = 0;
|
||||||
|
|
||||||
|
if (args.length == 0) {
|
||||||
|
log("Please pass the number of entries to delete starting from the most recent.");
|
||||||
|
statusCode = 1;
|
||||||
|
} else {
|
||||||
|
size_t position = 0;
|
||||||
|
int numToDelete = parsePositiveInt(args.data[0], &position);
|
||||||
|
if (numToDelete != -1) {
|
||||||
|
list<GymLogEntry> logEntries = loadEntryLog(arena, LOG_FILE_LOCATION);
|
||||||
|
if (numToDelete > logEntries.length) {
|
||||||
|
log("%i is more than the current number of log entries (%i). Aborting.", numToDelete, logEntries.length);
|
||||||
|
statusCode = 1;
|
||||||
|
} else {
|
||||||
|
writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log("Invalid number to delete.\n");
|
||||||
|
statusCode = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
// Syntax: do <exercise-name> weightKg reps
|
// Syntax: do <exercise-name> weightKg reps
|
||||||
int gymTrackerDo(Arena *arena, list<string> args) {
|
int gymTrackerDo(Arena *arena, list<string> args) {
|
||||||
int statusCode = 0;
|
int statusCode = 0;
|
||||||
@@ -214,12 +299,14 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
|||||||
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
||||||
for (EachIn(db->entries, i)) {
|
for (EachIn(db->entries, i)) {
|
||||||
GymLogDbParsedEntry entry = db->entries.data[i];
|
GymLogDbParsedEntry entry = db->entries.data[i];
|
||||||
if (strEql(entry.name, newExerciseName)) {
|
if (strStartsWith(entry.name, newExerciseName)) {
|
||||||
existingEntry = &entry;
|
existingEntry = &entry;
|
||||||
|
log("Assuming exercise \"%S\".\n\n", entry.name);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!existingEntry) {
|
if (!existingEntry) {
|
||||||
|
log("The exercise \"%S\" hasn't been registered.", newExerciseName);
|
||||||
statusCode = 1;
|
statusCode = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,7 +328,23 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
|
fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));
|
||||||
statusCode = gymTrackerWorkForExercise(arena, exerciseId);
|
statusCode = gymTrackerWorkToday(arena, exerciseId, newExerciseName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
int gymTrackerListExercises(Arena *arena, list<string> args) {
|
||||||
|
int statusCode = 0;
|
||||||
|
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
||||||
|
|
||||||
|
if (db->entries.length == 0) {
|
||||||
|
log("No entries currently registered in the exercise database.");
|
||||||
|
} else {
|
||||||
|
log("%i entries currently registered in the exercise database:\n\n", db->entries.length);
|
||||||
|
for (EachIn(db->entries, i)) {
|
||||||
|
log("#%i: %S\n", i + 1, db->entries.data[i].name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,6 +353,8 @@ int gymTrackerDo(Arena *arena, list<string> args) {
|
|||||||
|
|
||||||
int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
int gymTrackerAddExercise(Arena *arena, list<string> args) {
|
||||||
int statusCode = 0;
|
int statusCode = 0;
|
||||||
|
GymLogDbParsed *db = parseDb(arena, readEntireFile(arena, DB_FILE_LOCATION));
|
||||||
|
|
||||||
string newExerciseName = args.data[0];
|
string newExerciseName = args.data[0];
|
||||||
if (newExerciseName.length == 0) {
|
if (newExerciseName.length == 0) {
|
||||||
log("No exercise name provided.\n");
|
log("No exercise name provided.\n");
|
||||||
@@ -329,6 +434,10 @@ int main(int argc, char **argv) {
|
|||||||
statusCode = gymTrackerStatus(arena, listSlice(args, 1));
|
statusCode = gymTrackerStatus(arena, listSlice(args, 1));
|
||||||
} else if (strEql(args.data[0], "do"_s)) {
|
} else if (strEql(args.data[0], "do"_s)) {
|
||||||
statusCode = gymTrackerDo(arena, listSlice(args, 1));
|
statusCode = gymTrackerDo(arena, listSlice(args, 1));
|
||||||
|
} else if (strEql(args.data[0], "delete"_s)) {
|
||||||
|
statusCode = gymTrackerDeleteEntries(arena, listSlice(args, 1));
|
||||||
|
} else if (strEql(args.data[0], "list"_s)) {
|
||||||
|
statusCode = gymTrackerListExercises(arena, listSlice(args, 1));
|
||||||
} else if (strEql(args.data[0], "add"_s)) {
|
} else if (strEql(args.data[0], "add"_s)) {
|
||||||
statusCode = gymTrackerAddExercise(arena, listSlice(args, 1));
|
statusCode = gymTrackerAddExercise(arena, listSlice(args, 1));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ if NOT EXIST .\target mkdir .\target
|
|||||||
set commonLinkerFlags=-opt:ref
|
set commonLinkerFlags=-opt:ref
|
||||||
set commonCompilerFlags=^
|
set commonCompilerFlags=^
|
||||||
-MT %= Make sure the C runtime library is statically linked =%^
|
-MT %= Make sure the C runtime library is statically linked =%^
|
||||||
-Gm- %= Turns off incremently building =%^
|
-Gm- %= Turns off incremental building =%^
|
||||||
-nologo %= No one cares you made the compiler Microsoft =%^
|
-nologo %= No one cares you made the compiler Microsoft =%^
|
||||||
-Oi %= Always use intrinsics =%^
|
-Oi %= Always use intrinsics =%^
|
||||||
-EHa- %= Disable exception handling =%^
|
-EHa- %= Disable exception handling =%^
|
||||||
-GR- %= Never use runtime type info from C++ =%^
|
-GR- %= Never use runtime type info from C++ =%^
|
||||||
-WX -W4 -wd4201 -wd4100 -wd4189 -wd4505 %= Compiler warnings, -WX warnings as errors, -W4 warning level 4, -wdXXXX disable warning XXXX =%^
|
-WX -W4 -wd4201 -wd4100 -wd4189 -wd4505 %= Compiler warnings, -WX warnings as errors, -W4 warning level 4, -wdXXXX disable warning XXXX =%^
|
||||||
-DAPP_DEBUG=0 -DSLOWMODE=1 -DENVIRONMENT_WINDOWS=1 %= Custom #defines =%^
|
-DAPP_DEBUG=0 -DENABLE_ASSERT=1 -DOS_WINDOWS=1 %= Custom #defines =%^
|
||||||
|
-D_CRT_SECURE_NO_WARNINGS=1^
|
||||||
-FC %= Full path of source code file in diagnostics =%^
|
-FC %= Full path of source code file in diagnostics =%^
|
||||||
-Zi %= Generate debugger info =%
|
-Zi %= Generate debugger info =%
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
|
#include "os.cpp"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "os.cpp"
|
|
||||||
#define STB_SPRINTF_IMPLEMENTATION
|
#define STB_SPRINTF_IMPLEMENTATION
|
||||||
#include "vendor/stb_sprintf.h"
|
#include "vendor/stb_sprintf.h"
|
||||||
|
|
||||||
|
void *pushSizeFill(Arena *arena, size_t bytes, byte fill) {
|
||||||
|
if (arena->capacity - arena->head >= bytes) {
|
||||||
|
void *ptr = (char *)arena->memory + arena->head;
|
||||||
|
arena->head += bytes;
|
||||||
|
memset(ptr, fill, bytes);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void *pushSize(Arena *arena, size_t bytes) {
|
void *pushSize(Arena *arena, size_t bytes) {
|
||||||
if (arena->capacity - arena->head >= bytes) {
|
if (arena->capacity - arena->head >= bytes) {
|
||||||
void *ptr = (char *)arena->memory + arena->head;
|
void *ptr = (char *)arena->memory + arena->head;
|
||||||
@@ -112,6 +121,18 @@ const char *cstring(Arena *arena, string str) {
|
|||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool strStartsWith(string str, string testStr) {
|
||||||
|
if (str.length < testStr.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < testStr.length; i++) {
|
||||||
|
if (str.str[i] != testStr.str[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool strEql(string s1, string s2) {
|
bool strEql(string s1, string s2) {
|
||||||
if (s1.length != s2.length) {
|
if (s1.length != s2.length) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ struct Scratch {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void *pushSize(Arena *arena, size_t bytes);
|
void *pushSize(Arena *arena, size_t bytes);
|
||||||
|
void *pushSizeFill(Arena *arena, size_t bytes, byte fill);
|
||||||
Arena *arenaAlloc(size_t capacity);
|
Arena *arenaAlloc(size_t capacity);
|
||||||
void arenaFree(Arena *arena);
|
void arenaFree(Arena *arena);
|
||||||
void arenaFreeFrom(Arena *arena, size_t pos);
|
void arenaFreeFrom(Arena *arena, size_t pos);
|
||||||
@@ -68,7 +69,9 @@ Scratch scratchStart(Arena **conflicts, size_t conflictCount);
|
|||||||
void scratchEnd(Scratch scratch);
|
void scratchEnd(Scratch scratch);
|
||||||
|
|
||||||
#define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
|
#define PushArray(arena, type, size) (type *)pushSize(arena, sizeof(type) * (size))
|
||||||
|
#define PushArrayZero(arena, type, size) (type *)pushSizeFill(arena, sizeof(type) * (size), 0)
|
||||||
#define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type))
|
#define PushStruct(arena, type) (type *)pushSize(arena, sizeof(type))
|
||||||
|
#define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)
|
||||||
|
|
||||||
// ### Vectors ###
|
// ### Vectors ###
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -134,7 +137,9 @@ struct list {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
|
#define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
|
||||||
|
#define PushListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, 0 })
|
||||||
#define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
|
#define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
|
||||||
|
#define PushFullListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, size })
|
||||||
|
|
||||||
template <typename T> T *appendList(list<T> *list, T element);
|
template <typename T> T *appendList(list<T> *list, T element);
|
||||||
template <typename T> void zeroList(list<T> *list);
|
template <typename T> void zeroList(list<T> *list);
|
||||||
@@ -151,6 +156,7 @@ struct string {
|
|||||||
|
|
||||||
#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
|
#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
|
||||||
#define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
|
#define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
|
||||||
|
#define PushStringFill(arena, length, characterByte) (string{ (char *)pushSizeFill(arena, length, characterByte), (length) })
|
||||||
string operator""_s(const char *cstrLiteral, size_t length);
|
string operator""_s(const char *cstrLiteral, size_t length);
|
||||||
|
|
||||||
// C Strings
|
// C Strings
|
||||||
@@ -160,6 +166,7 @@ size_t calcStringLen(const char *str);
|
|||||||
string strFromCString(Arena *arena, const char *str);
|
string strFromCString(Arena *arena, const char *str);
|
||||||
|
|
||||||
bool strEql(string s1, string s2);
|
bool strEql(string s1, string s2);
|
||||||
|
bool strStartsWith(string str, string testStr);
|
||||||
bool stringContains(string str, char c);
|
bool stringContains(string str, char c);
|
||||||
|
|
||||||
string strReverse(Arena *arena, string str);
|
string strReverse(Arena *arena, string str);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
void *os_alloc(size_t capacity) {
|
void *os_alloc(size_t capacity) {
|
||||||
return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
return mmap(0, capacity, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#ifndef OS_IMPL_WIN32_CPP
|
#ifndef OS_IMPL_WIN32_CPP
|
||||||
#define OS_IMPL_WIN32_CPP
|
#define OS_IMPL_WIN32_CPP
|
||||||
|
|
||||||
#include "os.h"
|
|
||||||
#include "Windows.h"
|
#include "Windows.h"
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
void *os_alloc(size_t commitSize) {
|
void *os_alloc(size_t commitSize) {
|
||||||
return VirtualAlloc(NULL, commitSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
return VirtualAlloc(NULL, commitSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||||
|
|||||||
Reference in New Issue
Block a user