From 8690c0845ac5fde2f1677279514cfa421453ded7 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Fri, 10 Mar 2017 15:01:07 +0000 Subject: [PATCH] - WIP creating user header and including it stub.c - todo: write stubs even when there are warnings for some lines --- src/stubser/stubser.c | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index 33fef9e..fcc8c92 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "xtypes.h" #include "xstring.h" #include "xmalloc.h" @@ -194,6 +195,53 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a return 0; } +/*! + * @brief Check if file exists using stat + * @param [in] *filename Path to file + * @retval 0 file does not exist + * @retval 1 file exists + */ +int file_exist(const char *filename) +{ + struct stat buffer; + return (stat(filename, &buffer) == 0); +} + +STATIC void createUserHeader(char *aOutput, char *aNoSuffix) +{ + FILE *cheader; + char *cHeaderName = NULL; + xmallocStrlcpy(&cHeaderName, aOutput, strlen(aOutput)); + xmallocStrlcat(&cHeaderName, "_user.h", 8); + + if (file_exist(cHeaderName)) + { + free(cHeaderName); + return; + } + + // user header doesn't exist + cheader = fopen(cHeaderName, "w"); + if (NULL == cheader) + { + free(cHeaderName); + return; + } + + fprintf(cheader, "/* @file %s"NEWLINES, gnu_basename(cHeaderName)); + fprintf(cheader, " * @details"NEWLINES" * This is a user defined stub header.\\n"NEWLINES); + fprintf(cheader, " * Define subject header needed for compilation of stub (e.g. typedefs, structures, ...).\\n"NEWLINES); + fprintf(cheader, " * - generated by stubser -"NEWLINES" */" NEWLINES NEWLINES); + + fprintf(cheader, "#ifndef _STUB_%s_USER_H"NEWLINES, aNoSuffix); + fprintf(cheader, "#define _STUB_%s_USER_H"NEWLINES NEWLINES, aNoSuffix); + + fprintf(cheader, NEWLINES"#endif // _STUB_%s_USER_H"NEWLINES, aNoSuffix); + + free(cHeaderName); + fclose(cheader); +} + STATIC int8_t createStub(char *aOutput, char *aNoSuffix, cfile_t *aCfile) { FILE *cfile; @@ -231,12 +279,15 @@ STATIC int8_t createStub(char *aOutput, char *aNoSuffix, cfile_t *aCfile) return -1; } + createUserHeader(aOutput, aNoSuffix); + fprintf(cfile, "/* @file %s"NEWLINES, gnu_basename(cFileName)); fprintf(cfile, " * @details"NEWLINES" * This is a stub for CUnit.\\n"NEWLINES); fprintf(cfile, " * - generated by stubser -"NEWLINES" */" NEWLINES NEWLINES); fprintf(cfile, "#include "NEWLINES); fprintf(cfile, "#include \"xtypes.h\""NEWLINES); fprintf(cfile, "#include \"stub.h\""NEWLINES); + fprintf(cfile, "#include \"stub_%s_user.h\""NEWLINES, aNoSuffix); fprintf(cfile, "#include \"%s\""NEWLINES NEWLINES, gnu_basename(cHeaderName)); fprintf(cheader, "/* @file %s"NEWLINES, gnu_basename(cHeaderName));