The button now changes from Start to Stop when pressed, and the value is removed when the Start button is pressed. While the simulation runs, the user will not be able to input a new value. Also, I added a new attribute to the Results
struct to check if the getting the results operations worked.
Updated Results struct
struct Results
{
double maxHeight;
double timeToMaximumHeight;
double timeToLand;
bool doResultsExist;
};
Updated the GetResults
function to return the value for doResultsExist
.
struct Results GetResults(const char *location, char *value)
{
struct Results results;
if (value == NULL || value[0] == '\0')
{
results.maxHeight = 0;
results.timeToMaximumHeight = 0;
results.timeToLand = 0;
results.doResultsExist = false;
return results;
}
char *commandPartFirst = "cd ";
char *commandPartSecond = " && fpm run -- ";
char *command = calloc( strlen(location) + strlen(value) + strlen(commandPartFirst) + strlen(commandPartSecond),
sizeof(char));
char *contentsCSV = "/app/output/calculations.csv";
char *csvLocation = calloc(strlen(location) + strlen(contentsCSV), sizeof(char));
strcpy(csvLocation, location);
strcat(csvLocation, contentsCSV);
strcpy(command, commandPartFirst);
strcat(command, location);
strcat(command, commandPartSecond);
strcat(command, value);
int getSystemInfo = system(command);
if (getSystemInfo != 0)
{
results.maxHeight = 0;
results.timeToMaximumHeight = 0;
results.timeToLand = 0;
results.doResultsExist = false;
free(command);
free(csvLocation);
exit(-1);
}
FILE *file = fopen(csvLocation, "r");
char line[1024];
int counter = 0;
if (file == NULL)
{
printf("Failed to open file\n");
exit(EXIT_FAILURE);
}
while(fgets(line, sizeof(line), file) != NULL)
{
if (counter == 0)
{
counter++;
continue;
}
char *temp = strdup(line);
char *separatedValues;
char *tempCopy = temp;
// Separate results
// Read more :
// https://stackoverflow.com/questions/6205195/given-a-starting-and-ending-indices-how-can-i-copy-part-of-a-string-in-c
for (int i = 0; i < 3; i++)
{
char *characterLoc = strchr(tempCopy, ',');
size_t index = (size_t)(characterLoc - tempCopy);
char *toCopy = calloc(strlen(tempCopy) - index, sizeof(char));
char *getResult = calloc(index, sizeof(char));
strncpy(getResult, tempCopy, index);
strncpy(toCopy, tempCopy + index + 1, strlen(tempCopy )- index);
tempCopy = toCopy;
char *endString;
switch(i)
{
case 0:
//results.maxHeight = atof(&getResult[0]);
results.maxHeight = strtod(getResult, &endString);
break;
case 1:
//results.timeToMaximumHeight = atof(&getResult[0]);
results.timeToMaximumHeight = strtod(getResult, &endString);
break;
case 2:
//results.timeToLand = atof(&getResult[0]);
results.timeToLand = strtod(getResult, &endString);
break;
}
free(getResult);
}
}
results.doResultsExist = true;
free(command);
free(csvLocation);
fclose(file);
return results;
}
Updated the switch case in the main function to handle the new conditions.
if (IsHoveringButton(mouseX, mouseY, startButton))
{
executionResults = GetResults( miniApplicationLocationCopy,
interiorTextBox.content);
if (!hasSimStarted && executionResults.doResultsExist)
{
hasSimStarted = true;
free(interiorTextBox.content);
interiorTextBox.content = calloc(4, sizeof(char));
}else
{
hasSimStarted = false;
}
I added an if-else statement to change the text on the button.
if(hasSimStarted)
{
startButton.text = "Stop";
}else
{
startButton.text = "Start";
}
I found an issue with the mini-app. It is hard to run the application because I need to add fpm to the path of the main application. This is confusing and not user-friendly. I might need to remove the Fortran mini-app and implement its logic in C.
Demonstration