2017-08-30 22:43:46 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2017-08-30 22:08:54 +00:00
|
|
|
#include "race.h"
|
|
|
|
|
|
|
|
int CRaceHelper::TimeFromSecondsStr(const char *pStr) // x.xxx
|
|
|
|
{
|
2017-08-30 22:43:46 +00:00
|
|
|
while(*pStr == ' ') // skip leading spaces
|
|
|
|
pStr++;
|
|
|
|
if(!isdigit(*pStr))
|
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
int Time = str_toint(pStr) * 1000;
|
2017-08-30 22:43:46 +00:00
|
|
|
while(isdigit(*pStr))
|
2017-08-30 22:08:54 +00:00
|
|
|
pStr++;
|
|
|
|
if(*pStr == '.' || *pStr == ',')
|
|
|
|
{
|
|
|
|
pStr++;
|
|
|
|
static const int s_aMult[3] = { 100, 10, 1 };
|
2017-08-30 22:43:46 +00:00
|
|
|
for(int i = 0; isdigit(pStr[i]) && i < 3; i++)
|
2017-08-30 22:08:54 +00:00
|
|
|
Time += (pStr[i] - '0') * s_aMult[i];
|
|
|
|
}
|
|
|
|
return Time;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CRaceHelper::TimeFromStr(const char *pStr) // x minute(s) x.xxx second(s)
|
|
|
|
{
|
|
|
|
static const char * const s_pMinutesStr = " minute(s) ";
|
|
|
|
static const char * const s_pSecondsStr = " second(s)";
|
|
|
|
|
|
|
|
const char *pSeconds = str_find(pStr, s_pSecondsStr);
|
|
|
|
if(!pSeconds)
|
2017-08-30 22:43:46 +00:00
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
const char *pMinutes = str_find(pStr, s_pMinutesStr);
|
|
|
|
if(pMinutes)
|
2017-08-30 22:43:46 +00:00
|
|
|
{
|
|
|
|
while(*pStr == ' ') // skip leading spaces
|
|
|
|
pStr++;
|
|
|
|
int SecondsTime = TimeFromSecondsStr(pMinutes + str_length(s_pMinutesStr));
|
|
|
|
if(SecondsTime == -1 || !isdigit(*pStr))
|
|
|
|
return -1;
|
|
|
|
return str_toint(pStr) * 60 * 1000 + SecondsTime;
|
|
|
|
}
|
2017-08-30 22:08:54 +00:00
|
|
|
else
|
|
|
|
return TimeFromSecondsStr(pStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CRaceHelper::TimeFromFinishMessage(const char *pStr, char *pNameBuf, int NameBufSize) // xxx finished in: x minute(s) x.xxx second(s)
|
|
|
|
{
|
|
|
|
static const char * const s_pFinishedStr = " finished in: ";
|
|
|
|
const char *pFinished = str_find(pStr, s_pFinishedStr);
|
|
|
|
if(!pFinished)
|
2017-08-30 22:43:46 +00:00
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
int FinishedPos = pFinished - pStr;
|
|
|
|
if(FinishedPos == 0 || FinishedPos >= NameBufSize)
|
2017-08-30 22:43:46 +00:00
|
|
|
return -1;
|
2017-08-30 22:08:54 +00:00
|
|
|
|
|
|
|
str_copy(pNameBuf, pStr, FinishedPos + 1);
|
|
|
|
|
|
|
|
return TimeFromStr(pFinished + str_length(s_pFinishedStr));
|
|
|
|
}
|