2016-05-09 21:35:23 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
|
|
|
#include <base/system.h>
|
|
|
|
|
|
|
|
#include "sql_string_helpers.h"
|
|
|
|
|
2016-09-25 08:41:34 +00:00
|
|
|
void sqlstr::FuzzyString(char *pString, int size)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2016-09-25 08:41:34 +00:00
|
|
|
char * newString = new char [size * 4 - 1];
|
2016-05-09 21:35:23 +00:00
|
|
|
int pos = 0;
|
|
|
|
|
2016-09-25 08:41:34 +00:00
|
|
|
for(int i = 0; i < size; i++)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
if(!pString[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
newString[pos++] = pString[i];
|
|
|
|
if (pString[i] != '\\' && str_utf8_isstart(pString[i+1]))
|
|
|
|
newString[pos++] = '%';
|
|
|
|
}
|
|
|
|
|
|
|
|
newString[pos] = '\0';
|
2016-09-25 08:41:34 +00:00
|
|
|
str_copy(pString, newString, size);
|
|
|
|
delete [] newString;
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// anti SQL injection
|
|
|
|
void sqlstr::ClearString(char *pString, int size)
|
|
|
|
{
|
|
|
|
char *newString = new char [size * 2 - 1];
|
|
|
|
int pos = 0;
|
|
|
|
|
2016-09-25 08:41:34 +00:00
|
|
|
for(int i = 0; i < size; i++)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
if(pString[i] == '\\')
|
|
|
|
{
|
|
|
|
newString[pos++] = '\\';
|
|
|
|
newString[pos++] = '\\';
|
|
|
|
}
|
|
|
|
else if(pString[i] == '\'')
|
|
|
|
{
|
|
|
|
newString[pos++] = '\\';
|
|
|
|
newString[pos++] = '\'';
|
|
|
|
}
|
|
|
|
else if(pString[i] == '"')
|
|
|
|
{
|
|
|
|
newString[pos++] = '\\';
|
|
|
|
newString[pos++] = '"';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newString[pos++] = pString[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newString[pos] = '\0';
|
|
|
|
|
2016-09-25 08:41:34 +00:00
|
|
|
str_copy(pString, newString, size);
|
2016-05-09 21:35:23 +00:00
|
|
|
delete [] newString;
|
|
|
|
}
|
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
void sqlstr::AgoTimeToString(int AgoTime, char *pAgoString)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
char aBuf[20];
|
2017-03-21 10:24:44 +00:00
|
|
|
int aTimes[7] =
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
60 * 60 * 24 * 365 ,
|
|
|
|
60 * 60 * 24 * 30 ,
|
|
|
|
60 * 60 * 24 * 7,
|
|
|
|
60 * 60 * 24 ,
|
|
|
|
60 * 60 ,
|
|
|
|
60 ,
|
|
|
|
1
|
|
|
|
};
|
2017-03-21 10:24:44 +00:00
|
|
|
char aaNames[7][6] =
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
"year",
|
|
|
|
"month",
|
|
|
|
"week",
|
|
|
|
"day",
|
|
|
|
"hour",
|
|
|
|
"min",
|
|
|
|
"sec"
|
|
|
|
};
|
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
int Seconds = 0;
|
|
|
|
char aName[6];
|
|
|
|
int Count = 0;
|
2016-05-09 21:35:23 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
// finding biggest match
|
2017-03-21 10:24:44 +00:00
|
|
|
for(i = 0; i < 7; i++)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
Seconds = aTimes[i];
|
|
|
|
strcpy(aName, aaNames[i]);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
Count = floor((float)AgoTime/(float)Seconds);
|
|
|
|
if(Count != 0)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
if(Count == 1)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "%d %s", 1 , aName);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), "%d %ss", Count , aName);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
2017-03-21 10:24:44 +00:00
|
|
|
strcat(pAgoString, aBuf);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
|
|
|
if (i + 1 < 7)
|
|
|
|
{
|
|
|
|
// getting second piece now
|
2017-03-21 10:24:44 +00:00
|
|
|
int Seconds2 = aTimes[i+1];
|
|
|
|
char aName2[6];
|
|
|
|
strcpy(aName2, aaNames[i+1]);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
|
|
|
// add second piece if it's greater than 0
|
2017-03-21 10:24:44 +00:00
|
|
|
int Count2 = floor((float)(AgoTime - (Seconds * Count)) / (float)Seconds2);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
if (Count2 != 0)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
if(Count2 == 1)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), " and %d %s", 1 , aName2);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
str_format(aBuf, sizeof(aBuf), " and %d %ss", Count2 , aName2);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
2017-03-21 10:24:44 +00:00
|
|
|
strcat(pAgoString, aBuf);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 10:24:44 +00:00
|
|
|
void sqlstr::GetTimeStamp(char *pDest, unsigned int Size)
|
2016-05-09 21:35:23 +00:00
|
|
|
{
|
2017-03-21 10:24:44 +00:00
|
|
|
std::time_t Rawtime;
|
|
|
|
std::time(&Rawtime);
|
2016-05-09 21:35:23 +00:00
|
|
|
|
2017-08-04 20:38:22 +00:00
|
|
|
str_timestamp_ex(Rawtime, pDest, Size, FORMAT_SPACE);
|
2016-05-09 21:35:23 +00:00
|
|
|
}
|