private void BTN_CONNECT_CLICK(object SENDER, EventArgs E) { LOGGER.METHOD_ENTER(); int RESULT = CONNECTION.CONNECT_TO_HOST(TXT_BOX_HOST.Text, TXT_BOX_PORT.Text, TXT_BOX_USER.Text, TXT_BOX_PASSWORD.Text); //Connect to the host. if (RESULT != 0) { DialogResult CHOICE = MessageBox.Show("Connection failed. Would you like to view the log for details?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error); LOGGER.EXCEPTION("Connection failed."); if (CHOICE == DialogResult.Yes) { LOGGER.OPEN_LOG(); } LOGGER.METHOD_EXIT_FAIL(); return; } DGV_1.DataSource = CONNECTION.GET_DATABASE_LISTING(); //Get a DataTable of all the databases on the host. try { DGV_1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } //Try to format the column to fill mode. catch (Exception EX) //If something didn't populate right, for whatever reason, capture it. { LOGGER.EXCEPTION(EX.Message); LOGGER.METHOD_EXIT_FAIL(); return; } LOGGER.METHOD_EXIT_SUCCESS(); }
/// <summary> /// This method will connect to the database selected. If all goes well, it will echo back the name of the Database. /// </summary> /// <param name="NAME">The name of the database to connect to.</param> /// <returns>On success it will echo back the name of the database. On failure it will return null.</returns> public string CONNECT_TO_DATABASE(string NAME) { if (LOGGER_ON) { LOGGER.METHOD_ENTER(); } if (!FULLY_CONNECTED) //If the connection is not fully connected... { if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); //Escape out. } try { MySqlCommand COMMAND = new MySqlCommand(string.Format("use {0};", NAME), CONNECTION); int RESULT = COMMAND.ExecuteNonQuery(); //Execute command. if (RESULT == 0) //If everything went fine. { if (LOGGER_ON) { LOGGER.LOG(string.Format("Connection to {0} successful.", NAME)); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_SUCCESS(); } return(NAME); //Echo back the database you've connected to. } else //If the command exited with a non-zero code... { if (LOGGER_ON) { LOGGER.LOG(string.Format("ExecuteNonQuery returned {0}.", RESULT)); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); } } catch (Exception EX) //If something went wrong... { if (LOGGER_ON) { LOGGER.EXCEPTION(EX.Message); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); //Escape out. } }
/// <summary> /// This function returns a image converted into ASCII art. This function will throw an exception up to the calling method there is a problem, /// so be ready to catch it before blindly marching on. /// </summary> /// <param name="FILEPATH">The location of the file.</param> /// <param name="RESIZE_X">Optional, new width to scale to.</param> /// <param name="RESIZE_Y">Optional, new height to scale to.</param> /// <returns>A list of character arrays. Each character array element /// in the list represents a frame from the image (Animations supported). /// The first dimension of the array is the row number and the second dimension is the colum. /// List Element = FRAME[ROW][COLUMN]</returns> public static List <char[, ]> PROCESS_IMAGE(string FILEPATH, int RESIZE_X = 0, int RESIZE_Y = 0) { LOGGER.METHOD_ENTER(); List <char[, ]> FRAME_LIST = new List <char[, ]>(); if (!File.Exists(FILEPATH)) //Check if the file exists. { throw new Exception("File does not exist"); //The file does not exist. } try { //Could use additional checks here to verify format and such. Image IMAGE = Image.Load(FILEPATH); //Load in the image. int FRAME_COUNT = IMAGE.Frames.Count; //Get the number of frames in the GIF. int X_DIM, Y_DIM; if (RESIZE_X != 0) //If the image is to be resized in the X direction... { X_DIM = RESIZE_X; //Set that as the new dimension. } else //If its not to be resized... { X_DIM = IMAGE.Width; //Get the original image width. } if (RESIZE_Y != 0) //If the image is to be resized in the X direction... { Y_DIM = RESIZE_Y; //Set that as the new dimension. } else //If its not to be resized... { Y_DIM = IMAGE.Height; //Get the original image height. } for (int CURRENT_FRAME = 0; CURRENT_FRAME < FRAME_COUNT; CURRENT_FRAME++) //Loop through each frame... { Image <Rgba32> FRAME_IMAGE = (Image <Rgba32>)IMAGE.Frames.CloneFrame(CURRENT_FRAME); //Convert each frame into its own image. byte[,] FRAME_BYTE_ARRAY = CONVERT_IMAGE(FRAME_IMAGE, X_DIM, Y_DIM); //Create a byte array of frame pixels. char[,] FRAME_CHAR_ARRAY = CONVERT_TO_ASCII(FRAME_BYTE_ARRAY); //Convert the frame pixels to ASCII art. FRAME_LIST.Add(FRAME_CHAR_ARRAY); //Add the ASCII art frame to the list. } } catch (Exception EX) { LOGGER.EXCEPTION(EX.Message); LOGGER.METHOD_EXIT_FAIL(); throw; } //Throw any problems up to the calling method to deal with. GC.Collect(); //Force cleanup. LOGGER.METHOD_EXIT_SUCCESS(); return(FRAME_LIST); }
private void BTN_USE_SELECTED_DB_CLICK(object sender, EventArgs e) { LOGGER.METHOD_ENTER(); string SELECTED_DB = DGV_1.SelectedCells[0].Value.ToString(); string RESULT = CONNECTION.CONNECT_TO_DATABASE(SELECTED_DB); DATABASE_CONNECTION_SWITCH(RESULT); DGV_2.DataSource = CONNECTION.GET_TABLE_LISTING(); try { DGV_2.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } //Try to format the column to fill mode. catch (Exception EX) //If something didn't populate right, for whatever reason, capture it. { LOGGER.EXCEPTION(EX.Message); LOGGER.METHOD_EXIT_FAIL(); return; } LOGGER.METHOD_EXIT_SUCCESS(); }
/// <summary> /// This will center a string inside a string of a longer length. Note that this has not been protected from every case. /// It is wrapped in a try-catch but be sure to test your own edge cases and such because this is a pretty basic centering tool, mostly for use on things like console table headers. /// </summary> /// <param name="VALUE">The string to center.</param> /// <param name="LENGTH">How many characters to center it in.</param> /// <param name="PAD_CHARACTER">The character to pad with, defaults to space.</param> /// <returns></returns> public static string CENTER(this string VALUE, int LENGTH, char PAD_CHARACTER) { if (LOGGER_ON) { LOGGER.METHOD_ENTER(); } try { int VALUE_HALF_COUNT = VALUE.Length / 2; int LENGTH_HALF_COUNT = LENGTH / 2; int START_LOCATION = LENGTH_HALF_COUNT - VALUE_HALF_COUNT; //Figure out where we need to start the new string. string OUTPUT = null; for (int i = 0; i < START_LOCATION; i++) { OUTPUT += PAD_CHARACTER; } //Loop through to pad the left side. OUTPUT += VALUE; //Add the original string. if (VALUE.Length % 2 != 0) { START_LOCATION -= 1; } //Determine if there is a odd amount of characters in the string. //If there are, subtract one to help keep it centered. for (int i = 0; i < START_LOCATION; i++) { OUTPUT += PAD_CHARACTER; } //Loop through to pad the right side. if (LOGGER_ON) { LOGGER.METHOD_EXIT_SUCCESS(); } return(OUTPUT); } catch (Exception EX) { if (LOGGER_ON) { LOGGER.EXCEPTION(EX.Message); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(VALUE); } }
private int FUNCT_2() { LOGGER.METHOD_ENTER(); LOGGER.LOG("Gonna divide by zero."); try { int ZERO = 0; int NOT_ZERO = 5; int RESULT = NOT_ZERO / ZERO; } catch (Exception EX) { LOGGER.EXCEPTION(EX.Message); LOGGER.METHOD_EXIT_FAIL(); return(-1); } LOGGER.METHOD_EXIT_SUCCESS(); return(0); }
static int Main(string[] args) //Debug setup of this test applicatrion { LOGGER.METHOD_ENTER(); string FILEPATH = args[0]; //Get the filepath. No checks in place because this is a test application. var FRAME_SET = new List <char[, ]>(); //Where the ASCII art will be stored. try { FRAME_SET = ASCII_RENDER.PROCESS_IMAGE(FILEPATH, 30, 30); } //Try to process the image. catch (Exception EX) //If anything failed, catch the exception. { LOGGER.EXCEPTION(EX.Message); //Write the exception to the log. LOGGER.METHOD_EXIT_FAIL(); //Write the failed exit to the log. return(-1); } int RETURN_CODE = ASCII_RENDER.RENDER_IMAGE(FRAME_SET, 10, 10); //Render the image. LOGGER.LOG(string.Format("Return code: {0}", RETURN_CODE)); LOGGER.METHOD_EXIT_SUCCESS(); LOGGER.OPEN_LOG(); return(RETURN_CODE); //Exit with the RENDER_IMAGE return code. }
/// <summary> /// This method is used to issue database commands. If the command is a hardcoded command, it can be just sent in COMMAND_STRING. If the command /// string in any way depended on variables or user inputs, a array of parameters should be sent containing those values, otherwise SQL injections may be possible. /// </summary> /// <param name="COMMAND_STRING">The command you wish to execute.</param> /// <param name="PARAMETERS_ARRAY">The parameters you wish to send. See documentation for the COMMAND_PARAMETER structure in this namespace for more details.</param> /// <returns> /// <para>Success: A value greater than or equal to zero. This value represents the number of rows affected.</para> /// <para>Failure: -1</para> /// </returns> public int EXECUTE_COMMAND(string COMMAND_STRING, COMMAND_PARAMETER[] PARAMETERS_ARRAY = null) { if (LOGGER_ON) { LOGGER.METHOD_ENTER(); } try { int NUMBER_OF_ROWS_AFFECTED; //This will be returned. MySqlTransaction TRANSACTION = CONNECTION.BeginTransaction(); //Start transaction. MySqlCommand COMMAND = CONNECTION.CreateCommand(); //Create a command object. COMMAND.CommandText = COMMAND_STRING; //Set the command literal text. if (PARAMETERS_ARRAY != null) //If parameters were passed... { foreach (COMMAND_PARAMETER PARAMETER in PARAMETERS_ARRAY) //For each parameter passed... { COMMAND.Parameters.AddWithValue(PARAMETER.ESCAPE_STRING, PARAMETER.STRING_TO_INSERT); //Find the escape string in the literal string and substitute it with STRING_TO_INSERT. } } NUMBER_OF_ROWS_AFFECTED = COMMAND.ExecuteNonQuery(); //Execute the command. TRANSACTION.Commit(); //Finalize transaction. if (LOGGER_ON) { LOGGER.METHOD_EXIT_SUCCESS(); } return(NUMBER_OF_ROWS_AFFECTED); } catch (Exception EX) { if (LOGGER_ON) { LOGGER.EXCEPTION(EX.Message); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } } return(-1); }
/// <summary> /// This method returns a DataTable object of the MySql table specified. /// </summary> /// <param name="TABLE_NAME">The name of the MySQL table.</param> /// <returns>A DataTable of the the specified MySQL table.</returns> public DataTable GET_TABLE(string TABLE_NAME) { if (LOGGER_ON) { LOGGER.METHOD_ENTER(); } if (!FULLY_CONNECTED) //If the connection is not fully connected... { if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); //Escape out. } try //Protect in a try-catch. { DataTable DATA = new DataTable(); //Create a DataTable to put the returned information in. string QUERY = string.Format("SELECT * FROM {0}", TABLE_NAME); //Construct the command string. MySqlCommand COMMAND = new MySqlCommand(QUERY, CONNECTION); //Build the command. MySqlDataAdapter RETURNED_DATA_ADAPTER = new MySqlDataAdapter(QUERY, CONNECTION); //Create a DataAdapter for the incoming data. RETURNED_DATA_ADAPTER.Fill(DATA); //Put the information into the DataTable. if (LOGGER_ON) { LOGGER.METHOD_EXIT_SUCCESS(); } return(DATA); //Return the data. } catch (Exception EX) //If something went wrong... { if (LOGGER_ON) { LOGGER.EXCEPTION(EX.Message); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); //Escape out. } }
/// <summary> /// This method will return a DataTable listing of all databases on the host. /// </summary> /// <returns>A DataTable of visible databases on host.</returns> public DataTable GET_DATABASE_LISTING() { if (LOGGER_ON) { LOGGER.METHOD_ENTER(); } if (!FULLY_CONNECTED) //If the connection is not fully connected... { if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); //Escape out. } try { MySqlCommand COMMAND = new MySqlCommand("show databases;", CONNECTION); MySqlDataReader READER = COMMAND.ExecuteReader(); DataTable DATA = new DataTable(); DATA.Load(READER); if (LOGGER_ON) { LOGGER.METHOD_EXIT_SUCCESS(); } return(DATA); } catch (Exception EX) //If something went wrong... { if (LOGGER_ON) { LOGGER.EXCEPTION(EX.Message); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(null); //Escape out. } }
/// <summary> /// This function will render ASCII animation and images to the console. /// </summary> /// <param name="FRAME_SET">The data to print, List Element = FRAME[ROW][COLUMN]</param> /// <param name="FRAME_DELAY">The amount of time in mS to pause after rendering each frame.</param> /// <param name="LOOP_COUNT">The number of times to loop the animation.</param> /// <returns>0 on success.</returns> /// <returns>1 on generic failure.</returns> public static int RENDER_IMAGE(List <char[, ]> FRAME_SET, int FRAME_DELAY = 0, int LOOP_COUNT = 10) { LOGGER.METHOD_ENTER(); try { char[,] SAMPLED_FRAME = FRAME_SET[0]; //Extract the first frame from the image so we can get some details on what was passed to us. int COLUMNS = SAMPLED_FRAME.GetLength(0); //Get the width of the frame (columns of pixels). int ROWS = SAMPLED_FRAME.GetLength(1); //Get the height of the frame (rows of pixels). int FRAME_COUNT = FRAME_SET.Count; //Get the amount of frames passed. for (int CURRENT_ITERATION = 0; CURRENT_ITERATION < LOOP_COUNT; CURRENT_ITERATION++) //Loop the animation the required amount of times... { for (int CURRENT_FRAME = 0; CURRENT_FRAME < FRAME_COUNT; CURRENT_FRAME++) //Loop through each frame... { System.Threading.Thread.Sleep(FRAME_DELAY); //Add in an animation delay Console.Clear(); //Before rendering a frame, clear the console. SAMPLED_FRAME = FRAME_SET[CURRENT_FRAME]; //Transfer the frame over to its own variable. Its easier than querying the list on each operation. for (int CURRENT_ROW = 0; CURRENT_ROW < ROWS; CURRENT_ROW++) //Loop down through each row... { string ROW_CHARACTERS = ""; //At the beginning of each row, clear out the variable that holds all the characters. for (int CURRENT_COLUMN = 0; CURRENT_COLUMN < COLUMNS; CURRENT_COLUMN++) //Loop through each pixel in the row... { ROW_CHARACTERS += SAMPLED_FRAME[CURRENT_COLUMN, CURRENT_ROW]; //Add the pixel character to the string. } Console.WriteLine(ROW_CHARACTERS); //At the end of each row, print the data. } } } GC.Collect(); //Force garbage collection. } catch (Exception EX) { LOGGER.EXCEPTION(EX.Message); LOGGER.METHOD_EXIT_FAIL(); return(-1); } LOGGER.METHOD_EXIT_SUCCESS(); return(0); }
/// <summary> /// Connects to the database defined in intialization. /// </summary> /// <returns> /// <para> 0: Connection success.</para> /// <para> 1: A connection-level error occurred while opening the connection.</para> /// <para> 2: Cannot open a connection without specifying a data source or server.</para> /// <para> 3: Unhandled exception type.</para> /// </returns> public int CONNECT_TO_HOST(string HOST, string PORT, string USER, string PASSWORD) { if (LOGGER_ON) { LOGGER.METHOD_ENTER(); } try { string CONNECTION_STRING_NO_PW = "server=" + HOST + ";User ID=" + USER + ";Port=" + PORT; string CONNECTION_STRING = CONNECTION_STRING_NO_PW + ";Password="******";Password={Hidden, nice try}"; if (LOGGER_ON) { LOGGER.LOG(string.Format("Connection string constructed: {0}", CONNECTION_STRING_HIDDEN_PW)); } CONNECTION.ConnectionString = CONNECTION_STRING; CONNECTION.Open(); } catch (Exception EX) { if (LOGGER_ON) { LOGGER.EXCEPTION(EX.Message); } if (EX.InnerException is MySqlException) { if (LOGGER_ON) { LOGGER.EXCEPTION("A connection-level error occurred while opening the connection."); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(1); } else if (EX.InnerException is InvalidOperationException) { if (LOGGER_ON) { LOGGER.EXCEPTION("Cannot open a connection without specifying a data source or server."); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(2); } else { if (LOGGER_ON) { LOGGER.EXCEPTION("Unhandled exception type."); } if (LOGGER_ON) { LOGGER.METHOD_EXIT_FAIL(); } return(3); } } if (LOGGER_ON) { LOGGER.METHOD_EXIT_SUCCESS(); } return(0); }