/*! \brief Split a string into 2 parts using the search string. Save the right part into the result parameter. \param [in] original_parameter The parameter to search. \param [in] search The text preceding the text we're looking for. \param [in] result_parameter The name of the parameter to store the result in. \b Example: \code lr_save_string("AstrixObelixIdefix", "Test"); lr_message(lr_eval_string("Original: {Test}\n")); // {Test}=AstrixObelixIdefix y_right( "Test", "Obelix", "Test4" ); lr_message(lr_eval_string("New Param: {Test4}\n")); // {Test4}=Idefix \endcode \author Floris Kraak */ void y_right( const char *original_parameter, const char *search, const char *result_parameter) { char* original = y_get_parameter_or_null(original_parameter); if( original == NULL ) { lr_error_message("y_right(): Error: Parameter %s does not exist!", original_parameter); lr_abort(); } else if( search == NULL || strlen(search) == 0 ) { lr_save_string(original, result_parameter); lr_log_message("Warning: Empty search parameter passed to y_right()"); return; } else { char* posPtr = (char *)strstr(original, search); //int pos = (int)(posPtr - original); //lr_log_message("y_right: original=%s, search=%s, resultParam=%s", original, search, result_parameter); if( posPtr == NULL ) { lr_save_string(original, result_parameter); return; } //lr_log_message("pos = %d", pos); posPtr = posPtr + strlen(search); lr_save_string(posPtr, result_parameter); } }
/*! \brief Get the content of a parameter without embedded null bytes (\0 characters) from the named parameter, if any. In some cases we want to fetch the content of a parameter but the parameter contains embedded NULL characters which make further processing harder. This will fetch a parameter but "cleanse" it from such contamination, leaving the rest of the data unaltered before returning it. \warning The return value of this function needs to be freed using lr_eval_string_ext_free(). \param [in] param_name The parameter to cleanse of nulls. \param [in] replacement A character that replaces any embedded nulls found. \returns The resulting parameter content. \b Example: \code { char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' }; char *tmp; lr_save_var(buffer, 11, 0, "broken"); tmp = y_get_cleansed_parameter("broken", '!'); lr_log_message("Result: %s", tmp); // Prints "Result: !b!ro!ken!!". free(tmp); } \endcode */ char* y_get_cleansed_parameter(const char* param_name, char replacement) { char* result; unsigned long result_size; size_t param_eval_size = strlen(param_name) +3; // parameter name + "{}" + '\0' (end of string) char* param_eval_string = y_mem_alloc(param_eval_size); //lr_log_message("y_cleanse_parameter(%s)", param_name ); // Get the contents of the parameter using lr_eval_string_ext() - we can't use the // regular version if we expect to find NULL in there. snprintf( param_eval_string, param_eval_size, "{%s}", param_name ); lr_eval_string_ext(param_eval_string, param_eval_size-1, &result, &result_size, 0, 0, -1); if( strcmp(param_eval_string, result) == 0 ) { lr_error_message("y_get_cleansed_parameter: Parameter %s does not exist.", param_name); lr_abort(); } free(param_eval_string); //lr_log_message("Cleansing param %s, result starts with '%-*.*s' and contains %d bytes.", param_name, result_size, result_size, result, result_size); if (result_size) { char *ptr; for(ptr = result; result_size--; ptr++) if (*ptr == 0) *ptr = replacement; /* // Now replace NULL bytes (NULL) in the input with something else.. for( result_strlen = strlen(result); result_strlen < result_size; result_strlen = strlen(result)) { result[result_strlen] = replacement; //lr_log_message("Cleansing param %s, result now '%-*.*s' and contains %d bytes.", param_name, result_size, result_size, result, result_size); }*/ } return result; }
/*! \brief Split a string into 2 parts using the search string. Save the left part into the result parameter. \param [in] original_parameter The parameter to search. \param [in] search The text after the text we're looking for. \param [in] result_parameter The name of the parameter to store the result in. \b Example: \code lr_save_string("AstrixObelixIdefix", "Test"); lr_message(lr_eval_string("Original: {Test}\n")); // {Test}=AstrixObelixIdefix y_left( "Test", "Obelix", "Test2" ); lr_message(lr_eval_string("New Param: {Test2}\n")); // {Test2}=Astrix \endcode \author Floris Kraak */ void y_left( const char *original_parameter, const char *search, const char *result_parameter ) { char *original = y_get_parameter_or_null(original_parameter); if( original == NULL ) { lr_error_message("y_left(): Error: Parameter %s does not exist!", original_parameter); lr_abort(); } else if( search == NULL || strlen(search) == 0 ) { lr_save_string(original, result_parameter); lr_log_message("Warning: Empty search parameter passed to y_left()"); return; } else { char *buffer; char *posPtr = (char *)strstr(original, search); int pos = (int)(posPtr - original); //lr_log_message("y_left: original=%s, search=%s, resultParam=%s", original, search, resultParam); if( posPtr == NULL ) { lr_save_string(original, result_parameter); return; } //lr_log_message("pos = %d", pos); // Copy the original to a temporary buffer buffer = y_strdup(original); buffer[pos] = '\0'; // make the cut lr_save_string(buffer, result_parameter); // save the result free(buffer); } }
/*! \brief Save a substring of a parameter into a new parameter. Search for a specific substring inside a parameter using left and right boundaries and save that into a new parameter. \param [in] original_parameter The parameter to search. \param [in] result_parameter The name of the parameter to store the result in. \param [in] left The left boundary - the text immediately preceding the substring in question. \param [in] right The right boundary. \b Example: \code char* str = "LorumIpsumLipsum"; lr_save_string(str, "param"); y_substr("param", "param", "Lorum", "Lipsum"); lr_log_message(lr_eval_string("{param}")); // Prints "Ipsum". \endcode \author André Luyer, Marcel Jepma & Floris Kraak */ void y_substr(const char *original_parameter, const char *result_parameter, const char *left, const char *right) { char *p1; char *str = y_get_parameter_or_null(original_parameter); if( str == NULL ) { lr_error_message("y_substr(): Error: Parameter %s does not exist!", original_parameter); lr_abort(); } // zoek start if (left) { p1 = strstr(str, left); if (p1) str = p1 + strlen(left); // else start is positie 0... } // zoek eind if (right) { p1 = strstr(str, right); if (p1) { lr_param_sprintf(result_parameter, "%.*s", p1 - str, str); // of sprintf return; } } lr_save_string(str, result_parameter); }
/** * Gets the process ID of the mmdrv.exe process that is running the VuGen script that called * this function. * * @return This function returns the process ID of the calling process. * * Example code: * // Print the vuser's process ID * int vuser_pid; vuser_pid = wi_get_vuser_pid(); * lr_output_message("vuser_pid: %d", vuser_pid); * * Note: This function only works on Windows. * From lr-libc. */ int wi_get_vuser_pid() { int rc=LR_PASS; // return code int pid=0; // the process id (usually 4 digits) static int is_msvcrt_dll_loaded = FALSE; // A static variable inside a function keeps its value between // invocations. The FALSE value is assigned only on the first // invocation. char* dll_name = "MSVCRT.DLL"; // This DLL contains the _getpid() function. It is a standard // Windows DLL, usually found in C:\WINDOWS\system32. // Note: on Windows platforms, if you do not specify a path, // lr_load_dll searches for the DLL using the standard sequence // used by the C++ function, LoadLibrary. // Only load the DLL the first time this function is called: if (is_msvcrt_dll_loaded == FALSE) { rc = lr_load_dll(dll_name); if (rc != 0) { lr_error_message("Error loading %s.", dll_name); lr_abort(); } is_msvcrt_dll_loaded = TRUE; } pid = _getpid(); return pid; } // wi_get_vuser_pid
/*! \brief Copy a string into a ::malloc'd piece of memory using ::strdup(), and lr_abort() if the allocation fails. See the ::strdup() C documentation for what it does. This is just a simple wrapper around it that catches the strdup return value and handles any errors by aborting the script. \param [in] source The string to copy. \returns A copy of the string, allocated via ::strdup(). \author Floris Kraak */ char* y_strdup(char* source) { char* result = strdup(source); if (!result) { lr_error_message("Out of memory while calling strdup()"); lr_abort(); } return result; }
/*! \brief Convert the content of a parameter to UPPERCASE. This will replace the content of the paramenter named in 'param_name' with the uppercased version. Does not affect non-alphabetic characters. \param [in] param_name The parameter to convert to uppercase. \b Example: \code lr_save_string("aBcDeFgHiJ &*45#$@#)!({}", "Test"); lr_message(lr_eval_string("Original: {Test}\n")); y_uppercase_parameter("Test"); lr_message(lr_eval_string("Altered: {Test}\n")); // prints "Altered: ABCDEFGHIJ &*45#$@#)!({}". \endcode \author Floris Kraak */ void y_uppercase_parameter(const char* param_name) { char *result = y_get_parameter_or_null(param_name); if(result == NULL) { lr_error_message("Nonexistant parameter %s passed to y_uppercase_parameter(): Aborting.", param_name); lr_abort(); } strupr(result); lr_save_string(result, param_name); }
/*! \brief Allocates a character array and initializes all elements to zero As ::y_mem_alloc(), but using the '::calloc' function, rather than '::malloc()'. \param [in] length Expected number of characters. \param [in] size How much space a single character requires. Usually this should contain "sizeof char". \returns A pre-zeroed block of memory of the requisite size allocated using ::calloc(). \warning The memory resulting from this call will need to be freed using ::free(). \sa ::y_mem_alloc(), ::calloc() */ char *y_array_alloc(size_t length, size_t size) { char *buff; buff = calloc(length, size); if (!buff) { lr_error_message("Insufficient memory available, requested %u * %u bytes", length, size); // If this happens you're pretty much screwed anyway. lr_abort(); } return buff; }
/*! \brief Clean a parameter by replacing any embedded NULL (null) characters with a replacement character. This would normally only happen if you have used to web_reg_save_param() and the result contains one or more null-character(s). Any such characters are replaced with replacement_char and the result is stored in the original parameter. When no null-character is found, the result is unaltered. \warning Since the return value is allocated with malloc(), it will need to be freed using free() at some point. \param [in] param_name The parameter to cleanse of nulls. \param [in] replacement A character that replaces any embedded nulls found. \warning Since this changes existing parameters be careful what types of parameters you use this on. \b Example: \code { char buffer[11] = { '\0', 'b', '\0', 'r','o', '\0', 'k', 'e', 'n', '\0', '\0' }; lr_save_var(buffer, 11, 0, "broken"); y_cleanse_parameter_ext("broken", '!'); // will save "!b!ro!ken!!" into the "broken" parameter. } \endcode */ void y_cleanse_parameter_ext(const char* param_name, char replacement) { if( param_name && strlen(param_name) ) { char* result = y_get_cleansed_parameter(param_name, replacement); lr_save_string(result, param_name); lr_eval_string_ext_free(&result); } else { lr_error_message("Empty or NULL parameter name passed to y_cleanse_parameter_ext(): %s", param_name); lr_abort(); } }
// -------------------------------------------------------------------------------------------------- // Allocates a block of memory for a string // Adds some simple checks to catch common errors. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // example usage: // char *test = y_mem_alloc(999); // .. // free(test); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - char *y_mem_alloc(const int size) { char *buff; int mem = size * sizeof(char); if(mem <= 0) { lr_error_message("Requested non positive amounts (%d) of memory! Bailing out ..", mem); return NULL; } //lr_output_message("Dynamic allocation of %d bytes of memory", mem); if ((buff = (char *)malloc(mem)) == NULL) { // Fixme: implement some generic error handling facility to send this stuff to. lr_error_message("Insufficient memory available, requested %d", mem); // If this happens you're pretty much screwed anyway. lr_abort(); } return buff; }
Portfolio() { lr_think_time(1); lr_start_transaction("Portfolio"); status = web_url("Portfolio2", "URL=http://{Server}/TRADE/portfolio.aspx", "Resource=0", "RecContentType=text/html", "Referer=http://{Server}/trade2/Order.aspx", "Snapshot=t81.inf", "Mode=HTTP", LAST); if (status == LR_FAIL) { lr_error_message("Error: %s", "Portfolio Failed - Aborted VU"); lr_abort(); } lr_end_transaction("Portfolio", LR_AUTO); return 0; }
/*! \brief Split a string into 2 parts using the search string. Save the rightmost part into the result parameter. This is almost the same as y_right(), but doesn't stop at the first match - instead, it uses the *last* match. It's pretty much the difference between 'greedy' and 'not greedy' in a regular expression.. \param [in] original_parameter The parameter to search. \param [in] search The text preceding the text we're looking for. \param [in] result_parameter The name of the parameter to store the result in. \b Example: \code lr_save_string("AstrixObelixIdefix", "Test"); lr_message(lr_eval_string("Original: {Test}\n")); // {Test}=AstrixObelixIdefix y_right( "Test", "Obelix", "Test4" ); lr_message(lr_eval_string("New Param: {Test4}\n")); // {Test4}=Idefix \endcode \author Floris Kraak */ void y_last_right( const char *original_parameter, const char *search, const char *result_parameter) { char *result = y_get_parameter_or_null(original_parameter); if( result == NULL ) { lr_error_message("y_last_right(): Error: Parameter %s does not exist!", original_parameter); lr_abort(); } else if( search == NULL || strlen(search) == 0 ) { lr_save_string(result, result_parameter); lr_log_message("Warning: Empty search parameter passed to y_last_right()"); return; } else { char *posPtr; //lr_log_message("y_last_right: original=%s, search=%s, resultParam=%s", original, search, resultParameter); do { posPtr = (char *)strstr(result, search); //pos = (int)(posPtr - result); //lr_log_message("pos = %d", pos); // not found, save what we have as the result. if( posPtr == NULL ) { lr_save_string(result, result_parameter); return; } // found, update the result pointer and go find more.. result = posPtr + strlen(search); } while(1); } }
MySQLTemplate() { int rc = 0; MYSQL *db_connection; MYSQL_RES *query_result; MYSQL_ROW result_row; char *server = "localhost"; char *user = "******"; char *password = "******"; char *database = "LRDB"; int port = 3306; // default MySQL port //static char *server_options[] = { "mysql_test", "--defaults-file=my.cnf" }; //int num_elements = sizeof(server_options)/ sizeof(char *); lr_whoami(&vuser_id, &vuser_group, &scid); lr_message( "Group: %s, vuser id: %d, scenario id %d", vuser_group, vuser_id, scid); if (vuser_id == -1) { vuser_id = 1; } rc = lr_load_dll("C:\\Program Files\\MySQL\\MySQL Server 5.0\\lib\\opt\\libmysql.dll"); // You should be able to find the MySQL DLL somewhere in your MySQL install directory. if (rc != 0) { lr_error_message("Could not load libmysql.dll"); lr_abort(); } // Allocate and initialise a new MySQL object db_connection = mysql_init(NULL); if (db_connection == NULL) { lr_error_message("Insufficient memory"); return -1; } // Connect to the database if (mysql_real_connect(db_connection, server, user, password, database, port, NULL, 0) == NULL) { lr_error_message("error on connect: %s\n", mysql_error(db_connection)); return -1; } if (mysql_query(db_connection, "SELECT * FROM LATLogin;")!= 0) { lr_error_message("error on query: %s\n", mysql_error(db_connection)); return -1; } if ((query_result = mysql_store_result(db_connection)) == NULL) { lr_error_message("error on store: %s\n", mysql_error(db_connection)); return -1; } while (result_row = mysql_fetch_row(query_result)) { lr_output_message("%s - %s", result_row[0]); } mysql_free_result(query_result); // Free the MySQL object created by mysql_init mysql_close(db_connection); mysql_server_end(); return 0; }