Browse Source

move parse methods to result type

master
Daniel Ledda 1 week ago
parent
commit
a337c0d9ac
3 changed files with 33 additions and 25 deletions
  1. +14
    -13
      app.cpp
  2. +15
    -10
      djstdlib/core.cpp
  3. +4
    -2
      djstdlib/core.h

+ 14
- 13
app.cpp View File

@@ -143,19 +143,19 @@ int gymTrackerStatus(Arena *arena, list<string> args) {
GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION)); GymLogDbParsed *db = parseDb(arena, os_readEntireFile(arena, DB_FILE_LOCATION));


Timestamp startTs = {0}; Timestamp startTs = {0};
int numDays = 1;
ParsePositiveIntResult numDays = {1, false};
bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s); bool showAll = args.length == 1 && strEql(args.data[0], "--all"_s);
if (!showAll) { if (!showAll) {
if (args.length == 2 && (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;
numDays = parsePositiveInt(args.data[1], &l); numDays = parsePositiveInt(args.data[1], &l);
} }
if (numDays == -1) {
if (!numDays.valid) {
puts("Bad argument for --days (-d) parameter."); puts("Bad argument for --days (-d) parameter.");
statusCode = 1; statusCode = 1;
} else { } else {
uint64 todayUnix = getSystemUnixTime(); uint64 todayUnix = getSystemUnixTime();
UnixTimestamp startUnix = todayUnix - numDays * 24 * 60 * 60;
UnixTimestamp startUnix = todayUnix - numDays.result * 24 * 60 * 60;
startTs = timestampFromUnixTime(&startUnix); startTs = timestampFromUnixTime(&startUnix);
} }
} }
@@ -267,14 +267,14 @@ int gymTrackerDeleteEntries(Arena *arena, list<string> args) {
statusCode = 1; statusCode = 1;
} else { } else {
size_t position = 0; size_t position = 0;
int numToDelete = parsePositiveInt(args.data[0], &position);
if (numToDelete != -1) {
ParsePositiveIntResult numToDeleteParsed = parsePositiveInt(args.data[0], &position);
if (numToDeleteParsed.valid) {
list<GymLogEntry> logEntries = loadEntryLog(arena, LOG_FILE_LOCATION); 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);
if (numToDeleteParsed.result > logEntries.length) {
log("%i is more than the current number of log entries (%i). Aborting.", numToDeleteParsed, logEntries.length);
statusCode = 1; statusCode = 1;
} else { } else {
os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDelete) * sizeof(GymLogEntry));
os_writeEntireFile(arena, LOG_FILE_LOCATION, (byte *)logEntries.data, (logEntries.length - numToDeleteParsed.result) * sizeof(GymLogEntry));
} }
} else { } else {
log("Invalid number to delete.\n"); log("Invalid number to delete.\n");
@@ -321,17 +321,18 @@ int gymTrackerDo(Arena *arena, list<string> args) {
if (statusCode == 0) { if (statusCode == 0) {
exercise.id = existingEntry->id; exercise.id = existingEntry->id;
size_t parsedCount = 0; size_t parsedCount = 0;
real32 kg = parsePositiveReal32(args.data[1], &parsedCount);
uint8 reps = parsePositiveInt(args.data[2], &parsedCount);
if (parsedCount == 0 || kg == NAN || reps == 0 || kg == 0) {
ParsePositiveReal32Result kg = parsePositiveReal32(args.data[1], &parsedCount);
ParsePositiveIntResult reps = parsePositiveInt(args.data[2], &parsedCount);
if (!kg.valid || !reps.valid) {
log("%zu, %f, %\n", parsedCount, kg, reps);
log("Invalid reps or weight input.\n"); log("Invalid reps or weight input.\n");
statusCode = 1; statusCode = 1;
} else { } else {
GymLogEntry entry = { GymLogEntry entry = {
getSystemUnixTime(), getSystemUnixTime(),
exercise.id, exercise.id,
reps,
kg,
reps.result,
kg.result,
}; };


os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry)); os_fileAppend(arena, LOG_FILE_LOCATION, (byte *)&entry, sizeof(entry));


+ 15
- 10
djstdlib/core.cpp View File

@@ -274,7 +274,7 @@ list<string> strSplit(Arena *arena, string splitStr, string inputStr) {
return result; return result;
} }


int8 parsePositiveInt(string str, size_t *lengthPointer) {
ParsePositiveIntResult parsePositiveInt(string str, size_t *lengthPointer) {
size_t numEnd = 0; size_t numEnd = 0;
char currChar = str.str[numEnd]; char currChar = str.str[numEnd];
while (numEnd < str.length && isNumeric(currChar)) { while (numEnd < str.length && isNumeric(currChar)) {
@@ -288,14 +288,14 @@ int8 parsePositiveInt(string str, size_t *lengthPointer) {
result *= 10; result *= 10;
result += str.str[i] - '0'; result += str.str[i] - '0';
} }
return result;
return {result, true};
} else { } else {
return -1;
return {0, false};
} }
} }


real32 parsePositiveReal32(string str, size_t *lengthPointer) {
real32 result = NAN;
ParsePositiveReal32Result parsePositiveReal32(string str, size_t *lengthPointer) {
ParsePositiveReal32Result result = {NAN, false};


string wholePartStr = string{0}; string wholePartStr = string{0};
string fractionalPartStr = string{0}; string fractionalPartStr = string{0};
@@ -314,16 +314,21 @@ real32 parsePositiveReal32(string str, size_t *lengthPointer) {
c++; c++;
} }
if (split) { if (split) {
int wholePart = parsePositiveInt(wholePartStr, lengthPointer);
ParsePositiveIntResult wholePartParsed = parsePositiveInt(wholePartStr, lengthPointer);
*lengthPointer += 1; *lengthPointer += 1;
int fractionalPart = parsePositiveInt(fractionalPartStr, lengthPointer);
if (wholePart >= 0 && fractionalPart >= 0) {
ParsePositiveIntResult fractionalPartParsed = parsePositiveInt(fractionalPartStr, lengthPointer);
if (wholePartParsed.valid && fractionalPartParsed.valid) {
// TODO(dledda): implement powf with intrinsics? or just custom // TODO(dledda): implement powf with intrinsics? or just custom
real32 fractionalPartMultiplier = 1.0f / powf(10.0f, (real32)fractionalPartStr.length); real32 fractionalPartMultiplier = 1.0f / powf(10.0f, (real32)fractionalPartStr.length);
result = (real32)wholePart + (real32)fractionalPart * (real32)fractionalPartMultiplier;
result.result = (real32)wholePartParsed.result + (real32)fractionalPartParsed.result * (real32)fractionalPartMultiplier;
result.valid = true;
} }
} else if (c > 0) { } else if (c > 0) {
result = (real32)parsePositiveInt(str, lengthPointer);
ParsePositiveIntResult intPartParsed = parsePositiveInt(str, lengthPointer);
if (intPartParsed.valid) {
result.result = (real32)intPartParsed.result;
result.valid = true;
}
} }
return result; return result;
} }


+ 4
- 2
djstdlib/core.h View File

@@ -176,8 +176,10 @@ list<string> strSplit(Arena *arena, string splitStr, string inputStr);
string strPrintfv(Arena *arena, const char *fmt, va_list args); string strPrintfv(Arena *arena, const char *fmt, va_list args);
string strPrintf(Arena *arena, const char *fmt, ...); string strPrintf(Arena *arena, const char *fmt, ...);


int8 parsePositiveInt(string str, size_t *lengthPointer);
real32 parsePositiveReal32(Arena *arena, string str, size_t *lengthPointer);
struct ParsePositiveIntResult { uint8 result; bool valid; };
ParsePositiveIntResult parsePositiveInt(string str, size_t *lengthPointer);
struct ParsePositiveReal32Result { real32 result; bool valid; };
ParsePositiveReal32Result parsePositiveReal32(Arena *arena, string str, size_t *lengthPointer);


inline function bool isNumeric(char c); inline function bool isNumeric(char c);




Loading…
Cancel
Save