Everything working for single file processing and reception.

This commit is contained in:
Thomas Wilkie 2023-11-29 18:10:07 +09:00
parent 347c2e8702
commit cff9b551ee
6 changed files with 120 additions and 14 deletions

View File

@ -0,0 +1,53 @@
#include "GetArguments.h"
// Initialises with the default values
argsList application_arguments =
{
.input_file_location = "Test_files/first_test.c",
.references_output_file_location = "References.txt",
.replacement_token = "Send",
.debug_token = "DEBUG"
};
// Makes the argument cases a bit clearer and slightly easier to add
#define AddArgumentCase(character, variable) case character: \
{\
variable = *(argv + arg_counter + 1);\
break;\
}
/**
* @brief Goes through the arguments passed in on the command line to parse them and put them into the arguments list for further use
*
* @param app_arguments Pointer to struct containing all the possible arguments, initialised with defaults
* @param argc The number of argument passed in on the command line
* @param argv List of pointers to the arguments passed in
*/
void CheckArguments (argsList * app_arguments, int argc, char ** argv)
{
// Skip the first argument on the list, it's just the path
uint8_t arg_counter = 1;
while (arg_counter < argc)
{
char * next_arg = *(argv + arg_counter);
if (*next_arg == '-')
{
// Each argument is prepended with a marker, i.e. -
char arg_type = *(next_arg + 1);
switch (arg_type)
{
AddArgumentCase('I', app_arguments->input_file_location);
AddArgumentCase('O', app_arguments->references_output_file_location);
AddArgumentCase('D', app_arguments->debug_token);
AddArgumentCase('R', app_arguments->replacement_token);
default:
printf("%s is not a valid argument.\n", next_arg);
break;
}
}
arg_counter++;
}
return;
}

View File

@ -0,0 +1,32 @@
#ifndef GET_ARGUMENTS_H_
#define GET_ARGUMENTS_H_
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @brief Struct to hold pointers to all the possible arguments
*
* @param input_file_location The file to scan through to find the debug tokens
* @param references_output_file_location The file to write all the token value pairs to
* @param replacement_token The token for the debug token to be replaced by
* @param debug_token The token to be used to mark a debug statment`
*
*/
typedef struct
{
char * input_file_location;
char * references_output_file_location;
char * replacement_token;
char * debug_token;
} argsList;
/**
* @brief Struct holding all the changeable arguments for the program. Initialised with defaults. This will be global.
*
*/
extern argsList application_arguments;
void CheckArguments (argsList * app_arguments, int argc, char ** argv);
#endif

View File

@ -181,17 +181,18 @@ void DebugTokenLine(FILE * temporary_file, FILE * output_file, char * line_copy)
uint16_t token_length = strlen(message_token);
if ((token_length + 1) > references_header_info.max_line_length)
{
references_header_info.max_line_length = token_length + 1;
// Needs to be +2 to fit whitespace and newline character of the largest message
references_header_info.max_line_length = token_length + 2;
}
// Create the new line to go into the temp file. Also put in unique value for the number of the debug message
fprintf(temporary_file, "%s%s(%hhu);\n",
tabs_token,
application_arguments.replacement_token,
references_header_info.number_debug_messages);
references_header_info.number_debug_messages + 1); // Needs to be + 1 to avoid zero, null character can't be easiy received
// This uses a formatted string to allow for printing the integer value correctly as a character in the text file
fprintf(output_file, "%hhu : %s\n", references_header_info.number_debug_messages, message_token);
fprintf(output_file, "%hhu : %s\n", references_header_info.number_debug_messages + 1, message_token);
// Finally, increment the debug message counter
references_header_info.number_debug_messages++;

View File

@ -11,7 +11,7 @@ static inline void GetHeaderInformation(FILE * references_file, referencesFileHe
*/
void PrintMessage(char * messages_list, uint8_t message_index, referencesFileHeader * references_header)
{
if (message_index >= references_header->number_debug_messages)
if (message_index > references_header->number_debug_messages)
{
printf("Message %hhu doesn't exist.\n", message_index);
return;
@ -19,7 +19,8 @@ void PrintMessage(char * messages_list, uint8_t message_index, referencesFileHea
// return DEBUG_MESSAGE_DOESNT_EXIST;
} else
{
printf("%hu : %s", message_index, (messages_list + (message_index * references_header->max_line_length)));
// Need to use - 1 as message numbers are all one higher to avoid receiving a NULL over serial
printf("%hu : %s", message_index, (messages_list + ((message_index - 1) * references_header->max_line_length)));
return;
}
}
@ -47,7 +48,7 @@ void CollectMessages(FILE * references_file, char ** messages, referencesFileHea
// The buffer position is a bit redundant but makes it clearer.
char * buffer_position = *messages + (i * references_header->max_line_length);
fgets(buffer_position, references_header->max_line_length, references_file);
printf("%hhu message: |%s|\n", message_number, buffer_position);
// printf("%hhu message: |%s|", message_number, buffer_position);
}

View File

@ -1,8 +1,8 @@
Version : 0.1, Debug messages : 7, Max line length : 93
0 : This is the first debug statement.
1 : This is the second debug statement.
2 : Third
3 : Again
4 : Much longer message this time I think it should be able to handle this all without a problem
5 : shorter
6 : This has the debug token in the name and it isn't an issue.
Version : 0.1, Debug messages : 7, Max line length : 94
1 : This is the first debug statement.
2 : This is the second debug statement.
3 : Third
4 : Again
5 : Much longer message this time I think it should be able to handle this all without a problem
6 : shorter
7 : This has the debug token in the name and it isn't an issue.

19
ReferencesFileInfo.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef REFERENCES_FILE_INFO_H
#define REFERENCES_FILE_INFO_H
#include <stdint.h>
#include <stdbool.h>
/**
* @brief This struct holds metadata about the references file which is in the first line of the file. It will be extended in time to include more metadata as required.
*
*/
typedef struct
{
uint8_t version_major;
uint8_t version_minor;
uint8_t number_debug_messages;
uint16_t max_line_length;
} referencesFileHeader;
#endif