diff --git a/src/main.c b/src/main.c index 3eeac96..6aecb38 100644 --- a/src/main.c +++ b/src/main.c @@ -67,7 +67,7 @@ int main(int argc, char **argv) { sprintf(filePath, "%s/%s", argv[1], eps[cnt]->d_name); printf("%s (%d)\n", filePath, eps[cnt]->d_type); - stubser_createStub(filePath); + stubser_createStub(filePath, argv[2]); } } } diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index d20d496..0d58968 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -19,6 +19,79 @@ #include "xstring.h" #include "cfunction_if.h" #include "cfile_parser_if.h" +#include "stubser_if.h" +#include "stubser_loc.h" + +STATIC int8_t createStubFunctionBlock(FILE *aFile, cfunction_t *aFunction) +{ + cfunction_parameter_t *parameter = NULL; + + if (NULL == aFile || NULL == aFunction) + { + return -1; + } + + if (CFUNCTION_TYPE_REGULAR != aFunction->type && CFUNCTION_TYPE_STATIC != aFunction->type) + { + return 1; + } + // start comments + fprintf(aFile, STUB_START_S1 NEWLINES, aFunction->name); + + // prep + fprintf(aFile, "// Maximum amount of instances within test subject functions" NEWLINES); + fprintf(aFile, "#define STUB_INSTANCES_AMOUNT_%s %d" NEWLINES, aFunction->name, STUB_INSTANCES_AMOUNT); + NEWLINE(aFile); + fprintf(aFile, "// Expected parameter and return value structures" NEWLINES); + fprintf(aFile, "typedef struct _STUBPARAMETER_%s" NEWLINES, aFunction->name); + fprintf(aFile, "{" NEWLINES); + fprintf(aFile, "\tuint8_t u8StubCallTest; /**< @brief If stub call is a STUB_CALL_PASS or STUB_CALL_FAIL */" NEWLINES); + fprintf(aFile, "}" NEWLINES); + + fprintf(aFile, "%s %s %s(", NCHECK(aFunction->prefix), NCHECK(aFunction->dataType), NCHECK(aFunction->name)); // todo remove leading " " when prefix is missing + parameter = aFunction->parameter.head; + while (parameter) + { + fprintf(aFile, "%s %s%s", NCHECK(parameter->type), NCHECK(parameter->name), (NULL != parameter->next ? ", " : "")); + parameter = parameter->next; + } + fprintf(aFile, "){}" NEWLINES); + + // end comments + fprintf(aFile, STUB_END_S1 NEWLINES, aFunction->name, (NULL != aFunction->next ? NEWLINES : "")); + + return 0; +} + +STATIC int8_t createStub(char *aOutput, cfunction_list_t *aFunctionList) +{ + FILE *fdesc; + cfunction_t *function = NULL; + + if (NULL == aOutput || NULL == aFunctionList) + { + return -1; + } + + fdesc = fopen(aOutput, "w"); + if (NULL == fdesc) + { + perror("Error open file"); + return -1; + } + + fprintf(fdesc, "/* This is a stub for %s */" NEWLINES NEWLINES, gnu_basename(aOutput)); + + function = aFunctionList->head; + while (function) + { + (void) createStubFunctionBlock(fdesc, function); + function = function->next; + } + + fclose(fdesc); + return 0; +} /* * @brief Opens given file for line by line function matching @@ -26,16 +99,38 @@ * @retval 0 matching complete * @retval -1 error opening file */ -int stubser_createStub(char *path) +int stubser_createStub(char *path, char *aOutput) { + int8_t ret = -1; cfunction_list_t functionList = CFUNCTION_LIST_DEFAULT; - if (0 == cfile_parser(path, &functionList)) + char fileName[512] = + { '\0' }; + + ret = cfile_parser(path, &functionList); + printf("\n"); // todo remove when output from cfile_parser() is removed + if (0 != ret) { - printf("\n"); - cfunction_printList(&functionList); + printf(STUB_CONSOLE_PARSE_ERROR_S2, gnu_basename(path), ret); + ret = -1; + goto end_stubser_createStub; } + printf(STUB_CONSOLE_PARSE_OK_S1, gnu_basename(path)); + + sprintf(fileName, "%s/%s_stub.d", aOutput, strtok(gnu_basename(path), ".")); // todo only cut suffix (last ".") + ret = createStub(fileName, &functionList); + if (0 != ret) + { + printf(STUB_CONSOLE_CREATE_ERROR_S2, gnu_basename(path), ret); + ret = -1; + goto end_stubser_createStub; + } + printf(STUB_CONSOLE_CREATE_OK_S1, fileName); + + end_stubser_createStub: // function end target + cfunction_freeList(&functionList); - return 0; + + return ret; } diff --git a/src/stubser/stubser_if.h b/src/stubser/stubser_if.h index ddc651a..927928a 100644 --- a/src/stubser/stubser_if.h +++ b/src/stubser/stubser_if.h @@ -14,6 +14,6 @@ #ifndef STUBSER_STUBSER_IF_H_ #define STUBSER_STUBSER_IF_H_ -int stubser_createStub(char *path); +int stubser_createStub(char *path, char *aOutput); #endif /* STUBSER_STUBSER_IF_H_ */ diff --git a/src/stubser/stubser_loc.h b/src/stubser/stubser_loc.h new file mode 100644 index 0000000..d0e345c --- /dev/null +++ b/src/stubser/stubser_loc.h @@ -0,0 +1,36 @@ +/*! + * @file stubser_loc.h + * @brief + * @details + * Project: \n + * Subsystem: \n + * Module: \n + * Code: GNU-C\n + * + * @date 04.03.2017 + * @author SESA354004 + */ + +#ifndef STUBSER_STUBSER_LOC_H_ +#define STUBSER_STUBSER_LOC_H_ + +#define NEWLINES "\r\n" +#define NEWLINE(output) fputs(NEWLINES, output) +#define NCHECK(string) (NULL == string ? "" : string) + +// Maximum amount of instances within test subject functions +#define STUB_INSTANCES_AMOUNT 32 + +#define STUB_START_S1 "// ---------------------------------------------->" NEWLINES \ + "/* Start of stub %s() */" +#define STUB_END_S1 "/* End of stub %s() */" NEWLINES \ + "// ----------------------------------------------<%s" + +#define STUB_CONSOLE_OK "[ OK ]" +#define STUB_CONSOLE_FAIL "[FAIL]" + +#define STUB_CONSOLE_PARSE_ERROR_S2 STUB_CONSOLE_FAIL " %s parsing failed (%d)\n" +#define STUB_CONSOLE_PARSE_OK_S1 STUB_CONSOLE_OK " %s parsing..." +#define STUB_CONSOLE_CREATE_ERROR_S2 STUB_CONSOLE_FAIL " %s creation failed (%d)\n" +#define STUB_CONSOLE_CREATE_OK_S1 "ok > %s\n" +#endif /* STUBSER_STUBSER_LOC_H_ */