Update consoleFunctions.cpp

This commit is contained in:
Anis 2016-02-26 20:11:27 +01:00
parent 7a60056365
commit 973e5a6c02

View file

@ -25,6 +25,11 @@
#include "console/consoleInternal.h"
#include "console/engineAPI.h"
#include "console/ast.h"
#ifndef _CONSOLFUNCTIONS_H_
#include "console/consoleFunctions.h"
#endif
#include "core/strings/findMatch.h"
#include "core/strings/stringUnit.h"
#include "core/strings/unicode.h"
@ -32,6 +37,7 @@
#include "console/compiler.h"
#include "platform/platformInput.h"
#include "core/util/journal/journal.h"
#include "gfx/gfxEnums.h"
#include "core/util/uuid.h"
#include "core/color.h"
#include "math/mPoint3.h"
@ -44,6 +50,132 @@ bool LinkConsoleFunctions = false;
// Buffer for expanding script filenames.
static char scriptFilenameBuffer[1024];
bool isInt(const char* str)
{
int len = dStrlen(str);
if(len <= 0)
return false;
// Ignore whitespace
int start = 0;
for(int i = start; i < len; i++)
if(str[i] != ' ')
{
start = i;
break;
}
for(int i = start; i < len; i++)
switch(str[i])
{
case '+': case '-':
if(i != 0)
return false;
break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0':
break;
case ' ': // ignore whitespace
for(int j = i+1; j < len; j++)
if(str[j] != ' ')
return false;
return true;
break;
default:
return false;
}
return true;
}
bool isFloat(const char* str, bool sciOk = false)
{
int len = dStrlen(str);
if(len <= 0)
return false;
// Ingore whitespace
int start = 0;
for(int i = start; i < len; i++)
if(str[i] != ' ')
{
start = i;
break;
}
bool seenDot = false;
int eLoc = -1;
for(int i = 0; i < len; i++)
switch(str[i])
{
case '+': case '-':
if(sciOk)
{
//Haven't found e or scientific notation symbol
if(eLoc == -1)
{
//only allowed in beginning
if(i != 0)
return false;
}
else
{
//if not right after the e
if(i != (eLoc + 1))
return false;
}
}
else
{
//only allowed in beginning
if(i != 0)
return false;
}
break;
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0':
break;
case 'e': case 'E':
if(!sciOk)
return false;
else
{
//already saw it so can't have 2
if(eLoc != -1)
return false;
eLoc = i;
}
break;
case '.':
if(seenDot | (sciOk && eLoc != -1))
return false;
seenDot = true;
break;
case ' ': // ignore whitespace
for(int j = i+1; j < len; j++)
if(str[j] != ' ')
return false;
return true;
break;
default:
return false;
}
return true;
}
bool isValidIP(const char* ip)
{
unsigned b1, b2, b3, b4;
unsigned char c;
int rc = dSscanf(ip, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c);
if (rc != 4 && rc != 5) return false;
if ((b1 | b2 | b3 | b4) > 255) return false;
if (dStrspn(ip, "0123456789.") < dStrlen(ip)) return false;
return true;
}
bool isValidPort(U16 port)
{
return (port >= 0 && port <=65535);
}
//=============================================================================
// String Functions.
@ -238,6 +370,40 @@ DefineConsoleFunction( strlen, S32, ( const char* str ),,
return dStrlen( str );
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( strlenskip, S32, ( const char* str, const char* first, const char* last ),,
"Calculate the length of a string in characters, skipping everything between and including first and last.\n"
"@param str A string.\n"
"@param first First character to look for to skip block of text.\n"
"@param last Second character to look for to skip block of text.\n"
"@return The length of the given string skipping blocks of text between characters.\n"
"@ingroup Strings" )
{
const UTF8* pos = str;
U32 size = 0;
U32 length = dStrlen(str);
bool count = true;
//loop through each character counting each character, skipping tags (anything with < followed by >)
for(U32 i = 0; i < length; i++, pos++)
{
if(count)
{
if(*pos == first[0])
count = false;
else
size++;
}
else
{
if(*pos == last[0])
count = true;
}
}
return S32(size);
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( strstr, S32, ( const char* string, const char* substring ),,
@ -284,6 +450,33 @@ DefineConsoleFunction( strpos, S32, ( const char* haystack, const char* needle,
//-----------------------------------------------------------------------------
DefineConsoleFunction( strposr, S32, ( const char* haystack, const char* needle, S32 offset ), ( 0 ),
"Find the start of @a needle in @a haystack searching from right to left beginning at the given offset.\n"
"@param haystack The string to search.\n"
"@param needle The string to search for.\n"
"@return The index at which the first occurrence of @a needle was found in @a heystack or -1 if no match was found.\n\n"
"@tsexample\n"
"strposr( \"b ab\", \"b\", 1 ) // Returns 2.\n"
"@endtsexample\n"
"@ingroup Strings" )
{
U32 sublen = dStrlen( needle );
U32 strlen = dStrlen( haystack );
S32 start = strlen - offset;
if(start < 0 || start > strlen)
return -1;
if (start + sublen > strlen)
start = strlen - sublen;
for(; start >= 0; start--)
if(!dStrncmp(haystack + start, needle, sublen))
return start;
return -1;
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( ltrim, const char*, ( const char* str ),,
"Remove leading whitespace from the string.\n"
"@param str A string.\n"
@ -630,6 +823,18 @@ DefineConsoleFunction( stripTrailingNumber, String, ( const char* str ),,
return String::GetTrailingNumber( str, suffix );
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( getFirstNumber, String, ( const char* str ),,
"Get the first occuring number from @a str.\n"
"@param str The string from which to read out the first number.\n"
"@return String representation of the number or "" if no number.\n\n")
{
U32 start;
U32 end;
return String::GetFirstNumber(str, start, end);
}
//----------------------------------------------------------------
DefineConsoleFunction( isspace, bool, ( const char* str, S32 index ),,
@ -896,6 +1101,110 @@ DefineConsoleFunction(ColorHSBToRGB, ColorI, (Point3I hsb), ,
return color;
}
//----------------------------------------------------------------
DefineConsoleFunction( strToggleCaseToWords, const char*, ( const char* str ),,
"Parse a Toggle Case word into separate words.\n"
"@param str The string to parse.\n"
"@return new string space separated.\n\n"
"@tsexample\n"
"strToggleCaseToWords( \"HelloWorld\" ) // Returns \"Hello World\".\n"
"@endtsexample\n"
"@ingroup Strings" )
{
String newStr;
for(S32 i = 0; str[i]; i++)
{
//If capitol add a space
if(i != 0 && str[i] >= 65 && str[i] <= 90)
newStr += " ";
newStr += str[i];
}
return Con::getReturnBuffer(newStr);
}
//----------------------------------------------------------------
// Warning: isInt and isFloat are very 'strict' and might need to be adjusted to allow other values. //seanmc
DefineConsoleFunction( isInt, bool, ( const char* str),,
"Returns true if the string is an integer.\n"
"@param str The string to test.\n"
"@return true if @a str is an integer and false if not\n\n"
"@tsexample\n"
"isInt( \"13\" ) // Returns true.\n"
"@endtsexample\n"
"@ingroup Strings" )
{
return isInt(str);
}
//----------------------------------------------------------------
DefineConsoleFunction( isFloat, bool, ( const char* str, bool sciOk), (false),
"Returns true if the string is a float.\n"
"@param str The string to test.\n"
"@param sciOk Test for correct scientific notation and accept it (ex. 1.2e+14)"
"@return true if @a str is a float and false if not\n\n"
"@tsexample\n"
"isFloat( \"13.5\" ) // Returns true.\n"
"@endtsexample\n"
"@ingroup Strings" )
{
return isFloat(str, sciOk);
}
//----------------------------------------------------------------
DefineConsoleFunction( isValidPort, bool, ( const char* str),,
"Returns true if the string is a valid port number.\n"
"@param str The string to test.\n"
"@return true if @a str is a port and false if not\n\n"
"@tsexample\n"
"isValidPort( \"8080\" ) // Returns true.\n"
"@endtsexample\n"
"@ingroup Strings" )
{
if(isInt(str))
{
U16 port = dAtous(str);
return isValidPort(port);
}
else
return false;
}
//----------------------------------------------------------------
DefineConsoleFunction( isValidIP, bool, ( const char* str),,
"Returns true if the string is a valid ip address, excepts localhost.\n"
"@param str The string to test.\n"
"@return true if @a str is a valid ip address and false if not\n\n"
"@tsexample\n"
"isValidIP( \"localhost\" ) // Returns true.\n"
"@endtsexample\n"
"@ingroup Strings" )
{
if(dStrcmp(str, "localhost") == 0)
{
return true;
}
else
return isValidIP(str);
}
//----------------------------------------------------------------
// Torque won't normally add another string if it already exists with another casing,
// so this forces the addition. It should be called once near the start, such as in main.cs.
ConsoleFunction(addCaseSensitiveStrings,void,2,0,"[string1, string2, ...]"
"Adds case sensitive strings to the StringTable.")
{
for(int i = 1; i < argc; i++)
StringTable->insert(argv[i], true);
}
//=============================================================================
// Field Manipulators.
//=============================================================================
@ -914,6 +1223,7 @@ DefineConsoleFunction( getWord, const char*, ( const char* text, S32 index ),,
"@endtsexample\n\n"
"@see getWords\n"
"@see getWordCount\n"
"@see getToken\n"
"@see getField\n"
"@see getRecord\n"
"@ingroup FieldManip" )
@ -937,6 +1247,7 @@ DefineConsoleFunction( getWords, const char*, ( const char* text, S32 startIndex
"@endtsexample\n\n"
"@see getWord\n"
"@see getWordCount\n"
"@see getTokens\n"
"@see getFields\n"
"@see getRecords\n"
"@ingroup FieldManip" )
@ -961,6 +1272,7 @@ DefineConsoleFunction( setWord, const char*, ( const char* text, S32 index, cons
"setWord( \"a b c d\", 2, \"f\" ) // Returns \"a b f d\"\n"
"@endtsexample\n\n"
"@see getWord\n"
"@see setToken\n"
"@see setField\n"
"@see setRecord\n"
"@ingroup FieldManip" )
@ -980,6 +1292,7 @@ DefineConsoleFunction( removeWord, const char*, ( const char* text, S32 index ),
"@tsexample\n"
"removeWord( \"a b c d\", 2 ) // Returns \"a b d\"\n"
"@endtsexample\n\n"
"@see removeToken\n"
"@see removeField\n"
"@see removeRecord\n"
"@ingroup FieldManip" )
@ -997,6 +1310,7 @@ DefineConsoleFunction( getWordCount, S32, ( const char* text ),,
"@tsexample\n"
"getWordCount( \"a b c d e\" ) // Returns 5\n"
"@endtsexample\n\n"
"@see getTokenCount\n"
"@see getFieldCount\n"
"@see getRecordCount\n"
"@ingroup FieldManip" )
@ -1006,6 +1320,49 @@ DefineConsoleFunction( getWordCount, S32, ( const char* text ),,
//-----------------------------------------------------------------------------
DefineEngineFunction( monthNumToStr, String, ( S32 num, bool abbreviate ), (false),
"@brief returns month as a word given a number or \"\" if number is bad"
"@return month as a word given a number or \"\" if number is bad"
"@ingroup FileSystem")
{
switch(num)
{
case 1: return abbreviate ? "Jan" : "January"; break;
case 2: return abbreviate ? "Feb" : "February"; break;
case 3: return abbreviate ? "Mar" : "March"; break;
case 4: return abbreviate ? "Apr" : "April"; break;
case 5: return "May"; break;
case 6: return abbreviate ? "Jun" : "June"; break;
case 7: return abbreviate ? "Jul" : "July"; break;
case 8: return abbreviate ? "Aug" : "August"; break;
case 9: return abbreviate ? "Sep" : "September"; break;
case 10: return abbreviate ? "Oct" : "October"; break;
case 11: return abbreviate ? "Nov" : "November"; break;
case 12: return abbreviate ? "Dec" : "December"; break;
default: return "";
}
}
DefineEngineFunction( weekdayNumToStr, String, ( S32 num, bool abbreviate ), (false),
"@brief returns weekday as a word given a number or \"\" if number is bad"
"@return weekday as a word given a number or \"\" if number is bad"
"@ingroup FileSystem")
{
switch(num)
{
case 0: return abbreviate ? "Sun" : "Sunday"; break;
case 1: return abbreviate ? "Mon" : "Monday"; break;
case 2: return abbreviate ? "Tue" : "Tuesday"; break;
case 3: return abbreviate ? "Wed" : "Wednesday"; break;
case 4: return abbreviate ? "Thu" : "Thursday"; break;
case 5: return abbreviate ? "Fri" : "Friday"; break;
case 6: return abbreviate ? "Sat" : "Saturday"; break;
default: return "";
}
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( getField, const char*, ( const char* text, S32 index ),,
"Extract the field at the given @a index in the newline and/or tab separated list in @a text.\n"
"Fields in @a text must be separated by newlines and/or tabs.\n"
@ -1327,6 +1684,114 @@ DefineConsoleFunction( nextToken, const char*, ( const char* str1, const char* t
return ret;
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( getToken, const char*, ( const char* text, const char* delimiters, S32 index ),,
"Extract the substring at the given @a index in the @a delimiters separated list in @a text.\n"
"@param text A @a delimiters list of substrings.\n"
"@param delimiters Character or characters that separate the list of substrings in @a text.\n"
"@param index The zero-based index of the substring to extract.\n"
"@return The substring at the given index or \"\" if the index is out of range.\n\n"
"@tsexample\n"
"getToken( \"a b c d\", \" \", 2 ) // Returns \"c\"\n"
"@endtsexample\n\n"
"@see getTokens\n"
"@see getTokenCount\n"
"@see getWord\n"
"@see getField\n"
"@see getRecord\n"
"@ingroup FieldManip" )
{
return Con::getReturnBuffer( StringUnit::getUnit(text, index, delimiters));
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( getTokens, const char*, ( const char* text, const char* delimiters, S32 startIndex, S32 endIndex ), ( -1 ),
"Extract a range of substrings separated by @a delimiters at the given @a startIndex onwards thru @a endIndex.\n"
"@param text A @a delimiters list of substrings.\n"
"@param delimiters Character or characters that separate the list of substrings in @a text.\n"
"@param startIndex The zero-based index of the first substring to extract from @a text.\n"
"@param endIndex The zero-based index of the last substring to extract from @a text. If this is -1, all words beginning "
"with @a startIndex are extracted from @a text.\n"
"@return A string containing the specified range of substrings from @a text or \"\" if @a startIndex "
"is out of range or greater than @a endIndex.\n\n"
"@tsexample\n"
"getTokens( \"a b c d\", \" \", 1, 2, ) // Returns \"b c\"\n"
"@endtsexample\n\n"
"@see getToken\n"
"@see getTokenCount\n"
"@see getWords\n"
"@see getFields\n"
"@see getRecords\n"
"@ingroup FieldManip" )
{
if( endIndex < 0 )
endIndex = 1000000;
return Con::getReturnBuffer( StringUnit::getUnits( text, startIndex, endIndex, delimiters ) );
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( setToken, const char*, ( const char* text, const char* delimiters, S32 index, const char* replacement ),,
"Replace the substring in @a text separated by @a delimiters at the given @a index with @a replacement.\n"
"@param text A @a delimiters list of substrings.\n"
"@param delimiters Character or characters that separate the list of substrings in @a text.\n"
"@param index The zero-based index of the substring to replace.\n"
"@param replacement The string with which to replace the substring.\n"
"@return A new string with the substring at the given @a index replaced by @a replacement or the original "
"string if @a index is out of range.\n\n"
"@tsexample\n"
"setToken( \"a b c d\", \" \", 2, \"f\" ) // Returns \"a b f d\"\n"
"@endtsexample\n\n"
"@see getToken\n"
"@see setWord\n"
"@see setField\n"
"@see setRecord\n"
"@ingroup FieldManip" )
{
return Con::getReturnBuffer( StringUnit::setUnit( text, index, replacement, delimiters) );
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( removeToken, const char*, ( const char* text, const char* delimiters, S32 index ),,
"Remove the substring in @a text separated by @a delimiters at the given @a index.\n"
"@param text A @a delimiters list of substrings.\n"
"@param delimiters Character or characters that separate the list of substrings in @a text.\n"
"@param index The zero-based index of the word in @a text.\n"
"@return A new string with the substring at the given index removed or the original string if @a index is "
"out of range.\n\n"
"@tsexample\n"
"removeToken( \"a b c d\", \" \", 2 ) // Returns \"a b d\"\n"
"@endtsexample\n\n"
"@see removeWord\n"
"@see removeField\n"
"@see removeRecord\n"
"@ingroup FieldManip" )
{
return Con::getReturnBuffer( StringUnit::removeUnit( text, index, delimiters ) );
}
//-----------------------------------------------------------------------------
DefineConsoleFunction( getTokenCount, S32, ( const char* text, const char* delimiters),,
"Return the number of @a delimiters substrings in @a text.\n"
"@param text A @a delimiters list of substrings.\n"
"@param delimiters Character or characters that separate the list of substrings in @a text.\n"
"@return The number of @a delimiters substrings in @a text.\n\n"
"@tsexample\n"
"getTokenCount( \"a b c d e\", \" \" ) // Returns 5\n"
"@endtsexample\n\n"
"@see getWordCount\n"
"@see getFieldCount\n"
"@see getRecordCount\n"
"@ingroup FieldManip" )
{
return StringUnit::getUnitCount( text, delimiters );
}
//=============================================================================
// Tagged Strings.
//=============================================================================
@ -2682,3 +3147,10 @@ DefineEngineFunction( isToolBuild, bool, (),,
return false;
#endif
}
DefineEngineFunction( getMaxDynamicVerts, S32, (),,
"Get max number of allowable dynamic vertices in a single vertex buffer.\n\n"
"@return the max number of allowable dynamic vertices in a single vertex buffer" )
{
return MAX_DYNAMIC_VERTS / 2;
}