Code:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
// Pragmas //
#pragma GCC diagnostic ignored "-Wwrite-strings" // Suppress the `deprecated conversion from string constant to ‘char*’` warning.
// Built-in library includes //
#include <windows.h> // Must always be included first.
//#include <mmsystem.h> // Must always be included second. Necessary for PlaySound (). Its included in "Windows.h".
#include <iostream>
#include <dirent.h>
#include <inttypes.h> // Necessary for format identifiers like %llu, %lld and etc. %llu can be replaced with %I64u.
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h> // For iQuadraticEquation (void).
#define iInputFromUser 99
using namespace std;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
// Function declarations //
int iWriteToFile (char caText [4096], char caFileName [4096], char *cpFileOpenMode); // Writes to a file a text with maximum
// length of 4096 characters, a file path
// name with a maximum length of 4096
// characters and mode for opening the file.
int iScanDir (char caDirectoryPath [4096], char *cpWriteMode);// Scan a directory. and write all file names from it to a file in mode.
int AddDashToPath (char *cpInputfilename, int iFileOrInput);
int iPlaySound (); // Plays a sound.
int iFindFactorial(void); // Find the factorial.
int iQuadraticEquation (void); // Calculates the roots of a quadratic equation.
int iEvaluateCharacter(); // Evaluates if a character is a vowel or a constant.
int AddRequestToDatabase (char *cpStringInput);
int ReadRequestFromDatabase (char *cpStringInput, char *cpReturnedString);
// Global variables
short shortTemporaryVariable1 = 0;
int iTemporaryVariable4 = 0;
int iTemporaryVariable3 = 0;
int iTemporaryVariable2 = 0;
int iTemporaryVariable1 = 0;
int ipTemporaryVariable2 = 0;
int ipTemporaryVariable1 = 0;
long liTemporaryVariable1 = 0; // equivalent to long int;
long long llTemporaryVariable1; // equivalent to long long int c;
double dTemporaryVariable1;
long double ldTemporaryVariable1;
float fTemporaryVariable1 = 0.0;
char cTemporaryVariable1;
char caTemporaryVariable11 [1024];
char caTemporaryVariable10 [1024];
char caTemporaryVariable9 [1024];
char caTemporaryVariable8 [1024];
char *cpTemporaryVariable2;
char *cpTemporaryVariable1;
char *cpInformation3 = "The INCA variables always correspond to the actual parameter value, they can not be changed except by calibrations, which is forbidden without an approval from your team leader. A change by calibrations is rarely needed.\0";
char *cpInformation2 = "The boxes on the left are the ControlDesk controls. They are updated by pressing update on the right.\0";
char *cpInformation1 = "The INCA variables can be set by either the ControlDesk controls or the ControlDesk BUS signals. As long as the BUS is active, the BUS signals are with priority and all values must be the same.\0";
char *cpInformationMessage1 = "The codecs must be installed.\n";
HWND hwnd; // Generic handle for our window.
// Handle for STATIC windows
HWND hHandle210; // Generic handle for all functions.
HWND hHandle209; // Generic handle for all functions.
HWND hHandle208; // Generic handle for all functions.
HWND hHandle207; // Generic handle for all functions.
HWND hHandle206; // Generic handle for all functions.
HWND hHandle205; // Generic handle for all functions.
HWND hHandle204; // Generic handle for all functions.
HWND hHandle203; // Generic handle for all functions.
HWND hHandle202; // Generic handle for all functions.
HWND hHandle201; // Generic handle for all functions.
HWND hHandle200; // Generic handle for all functions.
// Handle for BUTTON windows
HWND hHandle107; // Generic handle for all functions.
HWND hHandle106; // Generic handle for all functions.
HWND hHandle105; // Generic handle for all functions.
HWND hHandle104; // Generic handle for all functions.
HWND hHandle103; // Generic handle for all functions.
HWND hHandle102; // Generic handle for all functions.
HWND hHandle101; // Generic handle for all functions.
HWND hHandle100; // Generic handle for all functions.
// Handles for EDIT windows
HWND hHandle9; // Generic handle for all functions.
HWND hHandle8; // Generic handle for all functions.
HWND hHandle7; // Generic handle for all functions.
HWND hHandle6; // Generic handle for all functions.
HWND hHandle5; // Generic handle for all functions.
HWND hHandle4; // Generic handle for all functions.
HWND hHandle3; // Generic handle for all functions.
HWND hHandle2; // Generic handle for all functions.
HWND hHandle1; // Generic handle for all functions.
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) // HINSTANCE is a handle to an instance, its a typedefstruct, has the same role as an ID.
{ // hPrevInstance is an ID for bacwards compatibility only its not relevant for today's computers.
// LPSTR lpszArgument is a pointer to a char
// int nCmdShow = how your program is displayed.
//HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Simple media player"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
650, /* The programs width */
130, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
/**** Local variables *****/
switch (message) /* handle the messages */
{
/////////////////////////////////////////////////////////////////////////////////////////////////
// This "case" creates the graphical interface.
case WM_CREATE:
hHandle200 = CreateWindow("Static", "The codecs must be installed!\n",
WS_CHILD | WS_VISIBLE | SS_LEFT,
10, 60, 415, 20,
hwnd, (HMENU) 200, NULL, NULL);
hHandle104 = CreateWindow ("BUTTON", // There is no need for a handle for a simple button. Element type "BUTTON".
"Skip", // Element displayed text "SEND".
WS_VISIBLE | WS_CHILD | WS_BORDER, // Has border, is child window, is visible.
430, 35, 100, 20, // Left from main window end, Down main from window top, Width, Hight.
hwnd,(HMENU) 104, NULL, NULL);
hHandle103 = CreateWindow ("BUTTON", // There is no need for a handle for a simple button. Element type "BUTTON".
"Stop", // Element displayed text "SEND".
WS_VISIBLE | WS_CHILD | WS_BORDER, // Has border, is child window, is visible.
325, 35, 100, 20, // Left from main window end, Down main from window top, Width, Hight.
hwnd,(HMENU) 103, NULL, NULL);
hHandle102 = CreateWindow ("BUTTON", // There is no need for a handle for a simple button. Element type "BUTTON".
"Resume", // Element displayed text "SEND".
WS_VISIBLE | WS_CHILD | WS_BORDER, // Has border, is child window, is visible.
220, 35, 100, 20, // Left from main window end, Down main from window top, Width, Hight.
hwnd,(HMENU) 102, NULL, NULL);
hHandle101 = CreateWindow ("BUTTON", // There is no need for a handle for a simple button. Element type "BUTTON".
"Pause", // Element displayed text "SEND".
WS_VISIBLE | WS_CHILD | WS_BORDER, // Has border, is child window, is visible.
115, 35, 100, 20, // Left from main window end, Down main from window top, Width, Hight.
hwnd,(HMENU) 101, NULL, NULL);
hHandle100 = CreateWindow ("BUTTON", // There is no need for a handle for a simple button. Element type "BUTTON".
"Play", // Element displayed text "SEND".
WS_VISIBLE | WS_CHILD | WS_BORDER, // Has border, is child window, is visible.
10, 35, 100, 20, // Left from main window end, Down main from window top, Width, Hight.
hwnd,(HMENU) 100, NULL, NULL);
hHandle1 = CreateWindow ("EDIT", // User unput text box type child windows. "hHandle" is the handle name.
"The codecs must be installed! Enter song path here.",
WS_BORDER | WS_CHILD | WS_VISIBLE, // Has border, is child window, is visible.
10, 10, 415, 20, // Left from main window end, Down main from window top, Width, Hight.
hwnd, // Handle name to handle for our main window.
(HMENU) 1, NULL, NULL);
break; // The break ends the message after the functionality we added is executed.
/////////////////////////////////////////////////////////////////////////////////////////////////
// When the media playing finishes, this case will be executed
case MM_MCINOTIFY:
break;
// This "case" is the actual action performed by our graphical elements.
case WM_COMMAND:
switch (LOWORD(wParam)) // Local variables can not be added in the cases themselves.
{
case 104:
// User input
mciSendString("skip mp3", NULL, 0, NULL); // Pause the currently playing file.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Skip"); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
break;
// Stops the playing of the media file, after which it has to start from the beginning
case 103:
mciSendString("stop mp3", NULL, 0, NULL);// Stop the currently playing file.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Stop"); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
break;
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Resume the media file
case 102:
mciSendString("resume mp3", NULL, 0, NULL); // Pause the currently playing file.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Resume"); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
break;
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Pause the media file
case 101:
mciSendString("pause mp3", NULL, 0, NULL); // Pause the currently playing file.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Pause"); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
break;
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Prints Hello world! in the static box
case 100:
iTemporaryVariable1 = GetWindowText (hHandle1, &caTemporaryVariable8 [0], 1024); // hHandle processes the input from the EDIT text box. Save from element 0 of the global variable "caTemporaryVariable10" to maximum 1024. // Add " gear" before updating th STATIC text box down with the value from the EDIT text box up.
iScanDir (caTemporaryVariable8, "a");
iPlaySound ();
break;
//////////////////////////////////////////////////////////////////////////////////////////////////////
}
break;
/////////////////////////////////////////////////////////////////////////////////////////////////
// Last case for exiting the program
case WM_DESTROY:
PostQuitMessage (0); // send a WM_QUIT to the message queue
break;
default: // for messages that we don't deal with
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
/**************** Write to file *************************/
int iWriteToFile (char caText [4096], char caFileName [4096], char *cpFileOpenMode) // A file name and path with a maximum of 4096 characters.
{
// Local variables and initialization
FILE *fpFilePointer; // A pointer to data of type FILE in order to process the file.
char *cpErrorMessage = "Error:\n The file can not be opened!\nPossible reasons:\n Wrong permissions.\n The file path is not correct.\n The file name is not correct.\n Other\n.";
fpFilePointer = fopen(caFileName, cpFileOpenMode); // Open the file cpFileName in mode cpFileOpenMode.
// Generic error handling
if (fpFilePointer == NULL) // fopen will return NULL if it can not open the file.
{
printf (cpErrorMessage);
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorMessage); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
return -1; // Error code for can not open the file.
}
else
{
fputs (caText, fpFilePointer);
return 0;
}
// Finishing the function
fclose (fpFilePointer); // All files must be closed.
}
/**************** Scan for all the files in a directory *************************/
int iScanDir (char caDirectoryPath [4096], char *cpWriteMode)
{
// Local variables and initialization
char *cpErrorText = "ERROR:\n Could not open the current directory.\n";
char *cpFileNameInDirectory; // The file name scanned from the directory.
char cpFileNamePathInDirectory [4096]; // The file name scanned from the directory with the path added. Must be an
// or the program will close unexpectedly and the compiler will give an
// maybe uninitialized error.
char *cpFileNameToWriteTo = "FilesList.cfc"; // The file in which to write the list of file names from the directory.
struct dirent *de; // Pointer for directory entry.
AddDashToPath(caDirectoryPath, iInputFromUser);// The user input is in format C:\Path\File, for C it should be C:\\Path\\File
// or C:/Path/File.
DIR *dr = opendir(caDirectoryPath); // Example for opening program directory: DIR *dr = opendir(".");
if (dr == NULL) // opendir returns NULL if couldn't open directory. NULL is not null.
{
printf(cpErrorText); // C version.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorText);// Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
return -2; // Error code for can not open the directory.
}
/************* !!!!!!!!! The file must be erased before the new directory is scanned. ************/
iWriteToFile("", cpFileNameToWriteTo, "w"); // Erases the file by opening it in write mode an writing nothing.
// The file is closed in the function iWriteToFile.
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL) // While the directory is readable.
{
cpFileNameInDirectory = (de->d_name); // Record the next scanned file name in cpFileNameInDirectory.
strcpy (cpFileNamePathInDirectory, caDirectoryPath);// Add the path: empty character pointer, path.
strcat (cpFileNamePathInDirectory, "/");// Add the directory path to the file name in the directory.
strcat (cpFileNamePathInDirectory, cpFileNameInDirectory);// Add the directory path to the file name in the directory.
// Directory path already in, file name.
strcat (cpFileNamePathInDirectory, "\n"); // Spearate all file names from the directory in rows.
//strcat (cpFileNamePathInDirectory, '\0'); // Only for debugging.
//printf("DEBUG %s\n", de->d_name); // Print the name of the next file in the directory.
iWriteToFile(cpFileNamePathInDirectory, cpFileNameToWriteTo, cpWriteMode);// Write the: file name scanned in the directory,
// into cpFileNameToWriteTo, mode for writing is append.
//SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)de->d_name);
//iPlaySound (de->d_name);
}
closedir(dr);
return 0;
}
/****************** Add \ to the file path, making it \\ or / *************************/
int AddDashToPath (char *cpInputfilename, int iFileOrInput)// Adds / to the file path, making it / and usable in programs, instead of the \.
{
unsigned int i = 0; // Counter.
char c; // Used for character reading.
FILE *ifp; // pointer to type file.
char *InMode = "r"; // Pointer to the mode for processing the file.
if (iFileOrInput == 99) // We will replace the dash in an input from the user, not from a file.
{
for (i = 0; cpInputfilename [i] != '\0'; i++) // Until the end of the user input in *cpInputfilename.
if (cpInputfilename [i] == '\\') // If we find a single dash, replace it with /.
cpInputfilename [i] = '/'; // Instead of / we can use \\.
}
else { // We will replace the dash on a file path in a file, not a user input.
ifp = fopen(cpInputfilename, InMode);
if (ifp == NULL)
{
return -1;
}
else
{
for (i = 0; (c = fgetc(ifp)) != EOF; i++)
if (c == '\\')
cpInputfilename [i] = '/';
}
}
return 0; // Code for all is ok.
}
/************************************** Plays most music/ video files **********************************************/
// In order for the function to work, you must download and add Winmm.dll to Build options -> linker settings.
// You must also add -lwinmm to Settings -> Compiler -> Linker settings -> Other linker options.
int iPlaySound ()
{
// Local variables and initialization //
// User input and initial warnings //
//::MessageBox (hwnd, "The codecs must be installed.\n" , "Information:", MB_OK); /* MB_OK == Message box with button "OK" only. */
//iTemporaryVariable1 = GetWindowText (hHandle1, &caTemporaryVariable8 [0], 1024); // hHandle processes the input from the EDIT text box. Save from element 0 of the global variable "caTemporaryVariable10" to maximum 1024. // Add " gear" before updating th STATIC text box down with the value from the EDIT text box up.
//strcpy (caTemporaryVariable8, cpMediafileName);
//AddDashToPath (cpMediafileName, iInputFromUser); // Replace \ with \\ for the file path. Unnecessary, since its already done in
// the iScanDir () function.
char *cpErrorText = "DEBUG: Can not open the file in iPlaySound ().";
FILE *fpFilePointer;
char *cpFileOpenMode = "r";
char caFilePathName [4096] = "FilesList.cfc";
char cpMediafileName [4096];
fpFilePointer = fopen (caFilePathName, cpFileOpenMode);
if (fpFilePointer == NULL)
{
printf (cpErrorText);
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)cpErrorText); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
}
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"DEBUG: Before while cycle."); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
while (fscanf(fpFilePointer, "%s", cpMediafileName) == 1) // Until we reach the end of the file, record every row in cpMediafileName.
{
printf ("DEBUG: Play sound.");
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Play"); // Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
sprintf (caTemporaryVariable9, "open \"%s\" type mpegvideo alias mp3", cpMediafileName);
//::MessageBox (hwnd, caTemporaryVariable9, "DEBUG:", MB_OK); /* MB_OK == Message box with button "OK" only. */
printf ("%s", caTemporaryVariable9);
// Processing //
mciSendString("close mp3", NULL, 0, NULL);// The close for the media file is at the beginning in order to close the old media file after a new has been loaded and allow the other toolbox functions.
mciSendString(caTemporaryVariable9, NULL, 0, NULL);
//mciSendString("play mp3 from 0 wait", NULL, 0, NULL);
//mciSendString("play mp3 from 0", NULL, 0, NULL);
mciSendString("play mp3", NULL, 0, NULL);
}
fclose (fpFilePointer); // All files must be closed.
return 0; // Status code for everything is OK.
}
/********************************** Processing the request *********************************************/
int ReadRequestFromDatabase (char *cpStringInput, char *cpReturnedString)
{
// Local variables //
//char caStringInput [1024]; // The request to be searched for will be stored in this character array.
char caLineStorage[1024]; // Declare storage for 1 line of type char.
FILE *fpFilePointer; // Declare a pointer to a type file.
// User input //
//puts ("This program simulates an ECU diagnostic.\n");
//puts ("1. Please enter the request to search for with no spaces!\n");
//gets (caStringInput);
// File opening //
fpFilePointer=fopen("database.cfc","r"); // Open the file for reading.
// Error handling for file opening.
if(fpFilePointer == NULL) { // If the file pointer is NULL.
perror("Unable to open file!\n"); // Print error text.
return 1; // Exit with error code 1.
}
// Function calls //
strcpy (cpReturnedString, "\n"); // Use strcpy () and strcat () or the program will return only 1 of the lines containing the searched request.
while (fgets(caLineStorage, sizeof (caLineStorage), fpFilePointer) != NULL)// While get 1 line as string and the end of the file is not reached.
if (strstr(caLineStorage, cpStringInput) != NULL)// Compare if the string line contains the string from user input.
strcat(cpReturnedString, caLineStorage); // strcat () can only be used after using strcpy () with the same string first.
fclose(fpFilePointer); // All files must be closed.
return 0; // Error code for all is OK.
}
int AddRequestToDatabase (char *cpStringInput)
{
// Local variables //
unsigned int i = 0; // Counter.
int iSpacesFlag = 1; // While the spaces are present flag is 1, the program will ask the user for a new input without spaces.
//char caStringInput [1024]; // The request to be searched for will be stored in this character array.
FILE *fpFilePointer; // Declare a pointer to a type file.
fpFilePointer=fopen("database.cfc", "a"); // Open the file for append the end of the file.
// Error handling for file opening.
if(fpFilePointer == NULL) { // If the file can not be opened.
//puts ("Unable to open file!\n"); // Print error text.
SendMessage (hHandle200, WM_SETTEXT, 200, (LPARAM)"Unable to open file!\n");// Updates a CreateWindow("Static"... or CreateWindow("EDIT" textbox. If we replace argument 1 with hwnd, it will update the title bar.
return(1); // Exit with error code 1.
}
// User input //
//puts ("This program adds a request and response to the database.\n");// Print string.
if (iSpacesFlag == 1){ // While there are present spaces in the user input.
//puts ("1. Please enter the request and response to add in the database and don't use spaces!\n");// Print string.
//gets (caStringInput); // Record the user input into the character array.
// Error handling for user input.
for (i = 0; cpStringInput [i] != '\0'; i++) // Scan the character array from the user input until we reach '\0'.
if (cpStringInput [i] == ' ') { // If there is a space in the user input(its a not allowed character).
iSpacesFlag = 1;
}
else iSpacesFlag = 0;
}
strcat (cpStringInput, "\n"); // If there is no error opening the file, write the character array string in the file.
fputs (cpStringInput, fpFilePointer);
fclose(fpFilePointer); // All files must be closed or the OS will not update them and no writing will be performed.
return 0;
}