From 5f38527b0338feb8f2cbd0a81f9505b9a407cb12 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Sat, 4 Mar 2017 18:48:10 +0000 Subject: [PATCH] - WIP also creating stub header (include guards missing) --- src/stubser/stubser.c | 67 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index 5d09c63..5f15312 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -17,6 +17,7 @@ #include #include "xtypes.h" #include "xstring.h" +#include "xmalloc.h" #include "cfunction_if.h" #include "cfile_parser_if.h" #include "stubser_if.h" @@ -34,12 +35,12 @@ STATIC bool isDatatypeVoid(char *aType) return true; } -STATIC int8_t createStubFunctionBlock(FILE *aFile, cfunction_t *aFunction) +STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *aFunction) { cfunction_parameter_t *parameter = NULL; char parameterIndex = 'a'; - if (NULL == aFile || NULL == aFunction) + if (NULL == aFile || NULL == aHeader || NULL == aFunction) { return -1; } @@ -86,6 +87,21 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, cfunction_t *aFunction) aFunction->name); NEWLINE(aFile); + // stub function init header + fprintf(aHeader, "void init_%s(uint8_t "STUBINIT_PARAM_INSTANCE_S", uint8_t "STUBINIT_PARAM_CALLTEST_S, aFunction->name); + if (!isDatatypeVoid(aFunction->dataType)) + { + fprintf(aHeader, ", %s "STUBINIT_PARAM_RETURN_S, aFunction->dataType); + } + parameter = aFunction->parameter.head; + parameterIndex = 'a'; + while (parameter) + { + fprintf(aHeader, ", %s "STUBINIT_PARAM_PARAMETER_S1, NCHECK(parameter->type), parameterIndex++); + parameter = parameter->next; + } + fprintf(aHeader, ");" NEWLINES); + // stub function init fprintf(aFile, "void init_%s(uint8_t "STUBINIT_PARAM_INSTANCE_S", uint8_t "STUBINIT_PARAM_CALLTEST_S, aFunction->name); if (!isDatatypeVoid(aFunction->dataType)) @@ -108,7 +124,8 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, cfunction_t *aFunction) fprintf(aFile, "\t"STUBINSTANCE_S1" = 0;" NEWLINES, aFunction->name); if (!isDatatypeVoid(aFunction->dataType)) { - fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_RETURN_S" = "STUBINIT_PARAM_RETURN_S";" NEWLINES, aFunction->name); + fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_RETURN_S" = "STUBINIT_PARAM_RETURN_S";" NEWLINES, + aFunction->name); } parameter = aFunction->parameter.head; parameterIndex = 'a'; @@ -177,7 +194,10 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, cfunction_t *aFunction) STATIC int8_t createStub(char *aOutput, cfunction_list_t *aFunctionList) { - FILE *fdesc; + FILE *cfile; + FILE *cheader; + char *cFileName = NULL; + char *cHeaderName = NULL; cfunction_t *function = NULL; if (NULL == aOutput || NULL == aFunctionList) @@ -185,25 +205,48 @@ STATIC int8_t createStub(char *aOutput, cfunction_list_t *aFunctionList) return -1; } - fdesc = fopen(aOutput, "w"); - if (NULL == fdesc) + xmallocStrlcpy(&cFileName, aOutput, strlen(aOutput)); + xmallocStrlcat(&cFileName, ".c", 2); + xmallocStrlcpy(&cHeaderName, aOutput, strlen(aOutput)); + xmallocStrlcat(&cHeaderName, ".h", 2); + + cfile = fopen(cFileName, "w"); + if (NULL == cfile) { + free(cFileName); perror("Error open file"); return -1; } - fprintf(fdesc, "/* @file %s"NEWLINES, gnu_basename(aOutput)); - fprintf(fdesc, " * @details"NEWLINES" * This is a stub for CUnit.\\n"NEWLINES); - fprintf(fdesc, " * - generated by stubser -"NEWLINES" */" NEWLINES NEWLINES); + cheader = fopen(cHeaderName, "w"); + if (NULL == cfile) + { + free(cFileName); + free(cHeaderName); + fclose(cfile); + perror("Error open file"); + return -1; + } + + 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(cheader, "/* @file %s"NEWLINES, gnu_basename(cHeaderName)); + fprintf(cheader, " * @details"NEWLINES" * This is a stub header for CUnit.\\n"NEWLINES); + fprintf(cheader, " * - generated by stubser -"NEWLINES" */" NEWLINES NEWLINES); function = aFunctionList->head; while (function) { - (void) createStubFunctionBlock(fdesc, function); + (void) createStubFunctionBlock(cfile, cheader, function); function = function->next; } - fclose(fdesc); + free(cFileName); + free(cHeaderName); + fclose(cfile); + fclose(cheader); return 0; } @@ -231,7 +274,7 @@ int stubser_createStub(char *path, char *aOutput) } printf(STUB_CONSOLE_PARSE_OK_S1, gnu_basename(path)); - sprintf(fileName, "%s/%s_stub.c", aOutput, strtok(gnu_basename(path), ".")); // todo only cut suffix (last ".") + sprintf(fileName, "%s/stub_%s", aOutput, strtok(gnu_basename(path), ".")); // todo only cut suffix (last ".") ret = createStub(fileName, &functionList); if (0 != ret) {