Index: ossp-pkg/l2/l2_epreuve.c RCS File: /v/ossp/cvs/ossp-pkg/l2/Attic/l2_epreuve.c,v rcsdiff -q -kk '-r1.1' '-r1.2' -u '/v/ossp/cvs/ossp-pkg/l2/Attic/l2_epreuve.c,v' 2>/dev/null --- l2_epreuve.c 2001/08/20 17:17:16 1.1 +++ l2_epreuve.c 2001/08/22 18:08:13 1.2 @@ -29,7 +29,19 @@ */ #include "l2.h" -#define SZ_LOGSTRING "String LogTest c'est deja la.\n" +#include "l2_p.h" + +#define L2_HEXBASE 16 +#define L2_COMMANDARG 0 +#define L2_BUFSIZEARG 1 +#define L2_FILEARG 2 +#define L2_MESSAGEARG 3 +#define L2_VONBUFSTRING " in L2_buffer.\n" +#define L2_VONFILESTRING " von L2_file Kanal.\n" + +void throwup(char *); +int trybuf(char **); +int tryfile(char **); /*--------------------------------------------------------*/ /* void throwup(char *pszError) */ @@ -42,54 +54,153 @@ } /*--------------------------------------------------------*/ -/* int main(int argc, char *argv[]) */ -/* Main program code */ +/* void trybuf(char *pszArgs[]) */ +/* Test logic for the buffer channel handler */ /*--------------------------------------------------------*/ -int main(int argc, char *argv[]) +int trybuf(char *pszArgs[]) +{ + int iRet = 0; /* for checking return value */ + int iMsgSize = 0; /* holds the message size */ + char *pszMsgIn = NULL; /* string we pass to ch_write */ + l2_channel_t *pMyBChannel = NULL; /* handle to channel instance */ + l2_handler_t *g_pBufferHandler = &l2_handler_buffer; + + fprintf(stdout, "Trying buffer channel handler...\n"); + + pMyBChannel = l2_channel_create(g_pBufferHandler); + if (pMyBChannel == NULL) + return 1; + + /* Params "size", */ + iRet = l2_channel_configure(pMyBChannel, "size",\ + strtol(pszArgs[L2_BUFSIZEARG], NULL, L2_HEXBASE)); + if (iRet != L2_OK) + return 1; + + iRet = l2_channel_open(pMyBChannel); + if (iRet != L2_OK) + return 1; + + /* Need to add extra bytes to string length to allow for the text we add */ + iMsgSize = strlen(pszArgs[L2_MESSAGEARG]) + strlen(L2_VONBUFSTRING); + pszMsgIn = malloc(iMsgSize); + strcpy(pszMsgIn, pszArgs[L2_MESSAGEARG]); + strcat(pszMsgIn, L2_VONBUFSTRING); + iRet = l2_channel_write(pMyBChannel, pszMsgIn, iMsgSize); + if (iRet != L2_OK) + return 1; + free(pszMsgIn); + + /* We are breaking an opaque data type to examine its contents by */ + /* casting it. ANSI C allows this hack, but it's not good practice. */ + fprintf(stderr, "0x%X octets Speichersize.\n",\ + (int)(*((char **)pMyBChannel->context.vp + 2))); + fprintf(stderr, *(char **)pMyBChannel->context.vp); + + iRet = l2_channel_flush(pMyBChannel); + if (iRet != L2_OK) + return 1; + + fprintf(stderr, "\nThis next test should not return any data, because "\ + "we are\ntrying to read the buffer after it has been flushed.\n"\ + " Buffer contents: "); + if (strlen(*(char **)pMyBChannel->context.vp)) + fprintf(stderr, *(char **)pMyBChannel->context.vp); + else + fprintf(stderr, "(Nada)"); + fputc('\n', stderr); + fputc('\n', stderr); + + iRet = l2_channel_close(pMyBChannel); + if (iRet != L2_OK) + return 1; + + iRet = l2_channel_destroy(pMyBChannel); + if (iRet != L2_OK) + return 1; + + return 0; /* All stages of execution completed successfully */ +} + +/*--------------------------------------------------------*/ +/* void tryfile(char *pszArgs[]) */ +/* Test logic for the file channel handler */ +/*--------------------------------------------------------*/ +int tryfile(char *pszArgs[]) { int iRet = 0; - l2_channel_t *pMyChannel = NULL; + int iMsgSize = 0; + char *pszMsgIn = NULL; + l2_channel_t *pMyFChannel = NULL; l2_handler_t *g_pFileHandler = &l2_handler_file; - if (argc < 2) - { - fprintf(stdout, "Usage: %s filepath\n", *argv); - exit(1); - } - fprintf(stdout, "Trying file channel handler...\n"); - pMyChannel = l2_channel_create(g_pFileHandler); - if (pMyChannel == NULL) - throwup("Channel create failed!\n"); + pMyFChannel = l2_channel_create(g_pFileHandler); + if (pMyFChannel == NULL) + return 1; /* Params "path", , "append", , "perm", <0xValue>, */ - iRet = l2_channel_configure(pMyChannel, "path", argv[1],\ + iRet = l2_channel_configure(pMyFChannel, "path", pszArgs[L2_FILEARG],\ "append", TRUE, "perm", 0x755); if (iRet != L2_OK) - throwup("Channel configure failed!\n"); + return 1; - iRet = l2_channel_open(pMyChannel); + iRet = l2_channel_open(pMyFChannel); if (iRet != L2_OK) - throwup("Channel open failed!\n"); + return 1; - /* Need to subtract one to throw away null termination */ - iRet = l2_channel_write(pMyChannel, SZ_LOGSTRING, sizeof(SZ_LOGSTRING) - 1); + /* Need to add n bytes to string length to allow for the text we add */ + iMsgSize = strlen(pszArgs[L2_MESSAGEARG]) + strlen(L2_VONFILESTRING); + pszMsgIn = malloc(iMsgSize); + strcpy(pszMsgIn, pszArgs[L2_MESSAGEARG]); + strcat(pszMsgIn, L2_VONFILESTRING); + iRet = l2_channel_write(pMyFChannel, pszMsgIn, iMsgSize); if (iRet != L2_OK) - throwup("Channel write failed!\n"); + return 1; + free(pszMsgIn); - iRet = l2_channel_flush(pMyChannel); + iRet = l2_channel_flush(pMyFChannel); if (iRet != L2_OK) - throwup("Channel flush failed!\n"); + return 1; - iRet = l2_channel_close(pMyChannel); + iRet = l2_channel_close(pMyFChannel); if (iRet != L2_OK) - throwup("Channel close failed!\n"); + return 1; - iRet = l2_channel_destroy(pMyChannel); + iRet = l2_channel_destroy(pMyFChannel); if (iRet != L2_OK) - throwup("Channel destroy failed!\n"); + return 1; + + return 0; /* All stages of execution completed successfully */ +} + +/*--------------------------------------------------------*/ +/* int main(int argc, char *argv[]) */ +/* Main program code */ +/*--------------------------------------------------------*/ +int main(int argc, char *argv[]) +{ + if (argc < 4) + { + fprintf(stdout, "Usage: %s 0xbufsize file string\n"\ + "Example: %s 2F myoutfile DontPutAnySpacesInThisString\n",\ + *argv, *argv); + exit(1); + } + + if (trybuf(argv)) + { + throwup("Buffer channel handler failed!\n"); + exit(1); + } + + if (tryfile(argv)) + { + throwup("File channel handler failed!\n"); + exit(1); + } - fprintf(stdout, "Success, exiting...\n"); + fprintf(stdout, "Success, exiting.\n"); return 0; }