Changed from "[filepath].[ext].[temp]" to "[filepath].[temp].[ext]" so file extensions are preserved.

There are also a few changes to the test files from testing the above.
At this point everything seems to work for a version 0.1 first release.
This commit is contained in:
Thomas Wilkie 2023-12-04 23:57:20 +09:00
parent 83fc44037e
commit a00b727816
6 changed files with 54 additions and 17 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
*.exe
*.code-workspace
*.temp
*.temp.*
.vscode/
testing/*

View File

@ -11,4 +11,4 @@ build_ppsr:
gcc -o Preprocessor/app.exe Preprocessor/*.c -I Preprocessor -I ../ReferencesFileInfo.h -Wall --pedantic
run_ppsr:
./Preprocessor/app.exe -R send -D debug Test_files/*test.c doesnt_exist.c
./Preprocessor/app.exe -R send -D debug Test_files/*test.c doesnt_exist.c Test_files/random.long_extension

View File

@ -5,7 +5,8 @@ argsList application_arguments =
{
.references_output_file_location = "References.txt",
.replacement_token = "Send",
.debug_token = "DEBUG"
.debug_token = "DEBUG",
.temp_file_token = ".temp"
};
// Makes the argument cases a bit clearer and slightly easier to add more
@ -48,6 +49,7 @@ uint8_t CheckArguments (argsList * app_arguments, int argc, char ** argv)
AddArgumentCase('O', app_arguments->references_output_file_location);
AddArgumentCase('D', app_arguments->debug_token);
AddArgumentCase('R', app_arguments->replacement_token);
AddArgumentCase('T', app_arguments->temp_file_token);
default:
printf("%s is not a valid argument.\n", next_arg);
break;

View File

@ -20,6 +20,7 @@ typedef struct
char * replacement_token;
char * debug_token;
uint8_t * args_list_input_files_indexs;
char * temp_file_token;
} argsList;
/**

View File

@ -26,6 +26,7 @@ HRESULT OpenInputFile(char * input_file_path, FILE ** input_file, FILE ** tempor
void DebugTokenLine(FILE * temporary_file, FILE * output_file, char * line_copy);
void PrintHeaderInformation(FILE * file, fpos_t file_start);
void PadFile(FILE * file);
void GetTempFilePath(char * input_file_path, char ** temp_file_path);
int main (int argc, char ** argv)
@ -271,22 +272,55 @@ HRESULT OpenInputFile(char * input_file_path, FILE ** input_file, FILE ** tempor
return HFILE_ERROR;
}
uint16_t filepath_length = strlen(input_file_path);
// + 6 is to fit the extra ".temp" appeneded to the end along with a NULL termination
char * temp_filepath = (char *) malloc(filepath_length + 6);
if (!temp_filepath)
{
printf("Memory error\n");
return E_OUTOFMEMORY;
}
memcpy(temp_filepath, input_file_path, filepath_length);
const char * temp_name_suffix = ".temp";
memcpy((temp_filepath + filepath_length),temp_name_suffix, 6);
*temporary_file = fopen(temp_filepath, "w");
// To Do: This should leave the extension as .c or .cpp, instead ".temp" should be added
// between the name and extension, i.e. "main.temp.c"
char * temp_file_path;
GetTempFilePath(input_file_path, &temp_file_path);
free(temp_filepath);
*temporary_file = fopen(temp_file_path, "w");
free(temp_file_path);
return S_OK;
}
/**
* @brief Get a filepath for the temp file. This is a bit of janky way to do it.
*
* @param[in] input_file_path
* @param[out] temp_file_path
*/
void GetTempFilePath(char * input_file_path, char ** temp_file_path)
{
// Length of the filename extension, i.e. ".c" or ".cpp". Start from one as there is always '\n'.
uint8_t extension_length = 1;
const uint8_t input_file_path_length = strlen(input_file_path);
while(1)
{
// Go from the end of the path backwards until the first period seperator
if(*(input_file_path + (input_file_path_length - extension_length)) == '.')
{
// This will count the newline character as the extension
extension_length++;
break;
}
extension_length++;
}
// Now need to create the name with the temp extension
const uint8_t temp_token_length = strlen(application_arguments.temp_file_token);
*temp_file_path = (char *) malloc(sizeof(char) * (input_file_path_length + temp_token_length) + 1);
// Places the temp string from the app arguments between the name and the extension
const uint8_t path_length_without_extension = (input_file_path_length - extension_length) + 1;
memcpy(*temp_file_path, input_file_path, path_length_without_extension);
memcpy((*temp_file_path) + path_length_without_extension,
application_arguments.temp_file_token,
temp_token_length);
memcpy((*temp_file_path) + (path_length_without_extension + temp_token_length),
(input_file_path + path_length_without_extension),
(extension_length + 1)); // + 1 to include null termination
return;
}

View File