/* string.regexSearchAll(string str, string regex) Returns: string Test if the value fits the regex return all matching parts as multiple return values */ int String_addon::regexsearchall( lua_State * L) { if ( lua_gettop( L ) != 2 ) wrongArgs( L ); checkType( L, LT_STRING, 1 ); checkType( L, LT_STRING, 2 ); std::string string = (char *) lua_tostring( L, 1 ); std::string dummy = (char *) lua_tostring( L, 2 ); std::string result; int counter = 0; try { std::regex reg( dummy ); std::sregex_iterator next( string.begin(), string.end(), reg ); std::sregex_iterator end; while ( next != end ) { std::smatch match = *next; char * re = const_cast < char * >( match.str().c_str()); lua_pushstring( L,re ); next++; counter++; } return counter; } catch ( std::regex_error& e ) { return 0; } }
/* string.explode(string str, string delim) Returns: table Splits string 'str' by delimiter 'delim'. Returns results as a table. */ int String_addon::explode(lua_State *L) { if( lua_gettop(L) != 2 ) wrongArgs(L); checkType(L, LT_STRING, 1); checkType(L, LT_STRING, 2); std::string str = (char *)lua_tostring(L, 1); size_t delimLen; const char *delim = lua_tolstring(L, 2, &delimLen); lua_newtable(L); size_t key = 1; while(true) { std::size_t found = str.find(delim); // Push the string. lua_pushinteger(L, key); // Key lua_pushstring(L, str.substr(0, found).c_str()); // Value str = str.substr(found+delimLen); // Set it lua_settable(L, -3); ++key; if( found == std::string::npos ) break; } return 1; }
/* string.regexMatch(string str, string regex) Returns: string Test if the value fits the regex return true if it does and false if it doesn't */ int String_addon::regexmatch( lua_State *L ) { if ( lua_gettop( L ) != 2 ) wrongArgs( L ); checkType( L, LT_STRING, 1 ); checkType( L, LT_STRING, 2 ); std::string string = (char *) lua_tostring( L, 1 ); std::string dummy = (char *) lua_tostring( L, 2 ); try { std::regex reg( dummy ); if ( std::regex_match( string, reg) ) { lua_pushboolean(L, true ); return 1; } else { lua_pushboolean( L, false ); return 1; } } catch ( std::regex_error& e ) { return 0; } }
/* string.regexSearch(string str, string regex) Returns: string Test if the value fits the regex return first the matching parts */ int String_addon::regexsearch( lua_State * L) { if ( lua_gettop( L ) != 2 ) wrongArgs( L ); checkType( L, LT_STRING, 1 ); checkType( L, LT_STRING, 2 ); std::string string = (char *) lua_tostring( L, 1 ); std::string dummy = (char *) lua_tostring( L, 2 ); std::string result; try { std::regex reg( dummy ); std::smatch match; if ( std::regex_search( string, match, reg ) && match.size() > 1 ) { result = match.str( 1 ); char * re = const_cast < char * >(result.c_str()); lua_pushstring(L, re); return 1; } else { return 0; } } catch ( std::regex_error& e ) { return 0; } }
/* timer.deltaTime() Returns: number delta Returns the deltaTime for the current logic cycle */ int Timer_lua::deltaTime(lua_State *L) { if( lua_gettop(L) != 0 ) wrongArgs(L); double dt = Macro::instance()->getEngine()->getDeltaTime(); lua_pushnumber(L, dt); return 1; }
/* timer.getNow() Returns: table (int64) Returns the current high-precision time as an int64 table. */ int Timer_lua::getNow(lua_State *L) { if( lua_gettop(L) != 0 ) wrongArgs(L); TimeType now = ::getNow(); lua_pushint64(L, now); return 1; }
/* include(string filename [, boolean force]) Returns: result of file "include" (dofile) a file. If 'force' is false or ungiven, this will not include the same file multiple times. NOTE: This function modified the CWD, both before AND after loading the target file. */ int Global_addon::include(lua_State *L) { bool forceRun = false; char *filename; int top = lua_gettop(L); if( top != 1 && top != 2 ) wrongArgs(L); checkType(L, LT_STRING, 1); filename = (char *)lua_tostring(L, 1); if( top == 2 ) { checkType(L, LT_BOOLEAN, 2); forceRun = (bool)lua_toboolean(L, 2); } // Get the full (non-relative) path and filename char *newFilenamePtr = NULL; // This will point to the filename in fullNewPath below char fullNewPath[MAX_PATH+1]; GetFullPathName(filename, MAX_PATH, fullNewPath, &newFilenamePtr); // Make sure we even need to run it if( !forceRun ) { for(size_t i = 0; i < includedList.size(); i++) { if( includedList.at(i) == fullNewPath ) return 0; // Already included, skip it } } // Copy current CWD char cwdBuffer[MAX_PATH+1]; GetCurrentDirectory(MAX_PATH,(LPTSTR)&cwdBuffer); // Set temporary CWD SetCurrentDirectory(::getFilePath(fullNewPath, false).c_str()); // And run it! if( luaL_dofile(L, newFilenamePtr) != LUA_OK ) { luaL_error(L, lua_tostring(L, -1)); } // Reset original CWD SetCurrentDirectory((LPCTSTR)&cwdBuffer); // Insert it into our included list includedList.push_back(fullNewPath); // Return count is stack size - original return lua_gettop(L) - top; }
/* string.toUnicode(string str) Returns: string Attempt to convert the input string 'str' to a wide string. */ int String_addon::toUnicode(lua_State *L) { if( lua_gettop(L) != 1 ) wrongArgs(L); size_t origStrLen; std::string origStr = (char *)lua_tolstring(L, 1, &origStrLen); std::wstring wStr = std::wstring(origStr.begin(), origStr.end()); lua_pushlstring(L, (char*)wStr.c_str(), wStr.size()*sizeof(wchar_t)); return 1; }
/* string.trim(string str) Returns: string "Trim" whitespace off head and tail of string 'str', return result. */ int String_addon::trim(lua_State *L) { if( lua_gettop(L) != 1 ) wrongArgs(L); checkType(L, LT_STRING, 1); std::string str = (char *)lua_tostring(L, 1); // Strip spaces from both ends //str.erase(str.begin(), std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun<int, int>(std::isspace)))); //str.erase(std::find_if(str.rbegin(), str.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end()); str.erase(str.find_last_not_of(" \n\r\t")+1); lua_pushstring(L, str.c_str()); return 1; }
int main(int argc, char **argv) { /* * Check number of command line arguments */ if(argc <= 1){ return wrongArgs(); } /* * make copy of input string for rpn calculation */ char *rpn_input = calloc(strlen(argv[1]),255); strcpy(rpn_input, argv[1]); /* * Creating and execute RPN Validator * Push Down Automaton */ Pda *val_pda = pda_create(); validator(val_pda); pda_execute(val_pda, argv[1], false); /* * creating and executing RPN Calculator * Push Down Automaton */ if(val_pda->succeed) { Pda *calc_pda = pda_create(); calculator(calc_pda); pda_execute(calc_pda, rpn_input, false); pda_free(calc_pda); } /* * Clean up */ free(rpn_input); pda_free(val_pda); return 0; }
/* string.regexReplace(string str, string regex, string withwhat, int start, int ende) Returns: string replace all positions in which the regex match with given string and then return the string */ int String_addon::regexreplace( lua_State * L ) { if ( lua_gettop( L ) != 5 ) wrongArgs( L ); checkType( L, LT_STRING, 1 ); checkType( L, LT_STRING, 2 ); checkType( L, LT_STRING, 3 ); checkType( L, LT_NUMBER, 4 ); checkType( L, LT_NUMBER, 5 ); std::string string = (char *) lua_tostring( L, 1 ); std::string dummy = (char *) lua_tostring( L, 2 ); std::string withwhat = (char *) lua_tostring( L, 3 ); int start = lua_tointeger( L, 4 ) - 1 ;//make it lua style c start from 0 and lua from 1 so +1 int ende = lua_tointeger( L, 5 ) - 1 ; std::string result = ""; std::string left = ""; std::string middle = ""; std::string right = ""; if ( start > 0 && start <= ende ) { left = string.substr( 0, start - 1 ); } if (start >= 0 && ende >= 0 && ende < (string.size()-1) && start <= ende) { right = string.substr( ende , (string.size()-1) ); } if ( start >= 0 && ende >= 0 && start <= ende ) { middle = string.substr( start, ende ); } try { std::regex reg( dummy ); std::regex_replace( std::back_inserter( result ), middle.begin(), middle.end(), reg, withwhat ); const char * re = ( left + result + right ).c_str(); lua_pushstring( L , re ); return 1; } catch ( std::regex_error& e ) { return 0; } }
/* timer.diff(int64 t2, int64 t1) Returns: number delta Compares two high-precision time values (from timer.getNow()) and returns the amount of time that has elapsed between them in seconds. */ int Timer_lua::diff(lua_State *L) { if( lua_gettop(L) != 2 ) wrongArgs(L); checkType(L, LT_TABLE, 1); checkType(L, LT_TABLE, 2); if( !lua_isint64(L, 1) ) luaL_typerror(L, 1, LuaType::metatable_int64); if( !lua_isint64(L, 2) ) luaL_typerror(L, 2, LuaType::metatable_int64); TimeType t2, t1; t2 = lua_toint64(L, 1); t1 = lua_toint64(L, 2); double dt = ::deltaTime(t2, t1); lua_pushnumber(L, dt); return 1; }
/* string.regexReplaceAll(string str, string regex, string withwhat) Returns: string replace all positions in which the regex match with given string and then return the string */ int String_addon::regexreplaceall( lua_State * L) { if ( lua_gettop( L ) != 3 ) wrongArgs( L ); checkType( L, LT_STRING, 1 ); checkType( L, LT_STRING, 2 ); checkType( L, LT_STRING, 3 ); std::string string = (char *) lua_tostring( L, 1 ); std::string dummy = (char *) lua_tostring( L, 2 ); std::string what = (char *) lua_tostring( L, 3 ); std::string result; try { std::regex reg( dummy ); char * re = const_cast < char * >( std::regex_replace( string, reg, what ).c_str()); lua_pushstring(L,re); return 1; } catch ( std::regex_error& e ) { return 0; } }
// TODO: Add table option for type int String_addon::random(lua_State *L) { if( lua_gettop(L) != 2 ) wrongArgs(L); checkType(L, LT_STRING, 1); checkType(L, LT_NUMBER, 2); static char alnum[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0}; static char letters[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0}; static char numbers[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0}; std::string type = (char *)lua_tostring(L, 1); size_t len = (size_t)lua_tointeger(L, 2); char *validChars = NULL; if( type == "alnum" ) validChars = alnum; else if( type == "letters" ) validChars = letters; else if( type == "numbers" ) validChars = numbers; else // Default alnum validChars = alnum; // shoudn't be longer than usigned int size_t validCharsLen = strlen(validChars); std::string buff = ""; for(size_t i = 0; i < len; i++) buff += validChars[::random(0, validCharsLen)]; lua_pushstring(L, buff.c_str()); return 1; }