/* * upt - update pre-sized thread */ static NuError UpdatePresizedThreadFunc(ExerciserState* pState, int argc, char** argv) { NuError err; NuDataSource* pDataSource = NULL; char* lineBuf = NULL; long ourLen; int32_t maxLen; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ assert(ExerciserState_GetNuArchive(pState) != NULL); assert(argc == 2); lineBuf = (char*)malloc(kNiceLineLen); assert(lineBuf != NULL); err = GetLine("Enter data for thread", lineBuf, kNiceLineLen); if (err != kNuErrNone) goto bail; ourLen = strlen(lineBuf); /* use "ourLen" for both buffer len and data len */ err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, ourLen, (uint8_t*)lineBuf, 0, ourLen, FreeCallback, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "Exerciser: data source create failed (err=%d)\n", err); goto bail; } lineBuf = NULL; /* now owned by the library */ err = NuUpdatePresizedThread(ExerciserState_GetNuArchive(pState), strtol(argv[1], NULL, 0), pDataSource, &maxLen); if (err == kNuErrNone) printf("Exerciser: success; function returned maxLen=%d\n", maxLen); bail: NuFreeDataSource(pDataSource); if (lineBuf != NULL) free(lineBuf); return err; }
/* * at - add thread to record */ static NuError AddThreadFunc(ExerciserState* pState, int argc, char** argv) { NuError err; NuDataSource* pDataSource = nil; char* lineBuf = nil; long ourLen, maxLen; NuThreadID threadID; NuThreadIdx threadIdx; (void) pState, (void) argc, (void) argv; /* shut up, gcc */ assert(ExerciserState_GetNuArchive(pState) != nil); assert(argc == 3); lineBuf = (char*)malloc(kNiceLineLen); assert(lineBuf != nil); threadID = strtol(argv[2], nil, 0); if (NuThreadIDGetClass(threadID) == kNuThreadClassData) { /* load data from a file on disk */ maxLen = 0; err = GetLine("Enter filename", lineBuf, kNiceLineLen); if (err != kNuErrNone) goto bail; if (!lineBuf[0]) { fprintf(stderr, "Invalid filename\n"); err = kNuErrInvalidArg; goto bail; } err = NuCreateDataSourceForFile(kNuThreadFormatUncompressed, 0, lineBuf, false, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "Exerciser: file data source create failed (err=%d)\n", err); goto bail; } } else { if (threadID == kNuThreadIDFilename || threadID == kNuThreadIDComment) { /* select the buffer pre-size */ err = GetLine("Enter max buffer size", lineBuf, kNiceLineLen); if (err != kNuErrNone) goto bail; maxLen = strtol(lineBuf, nil, 0); if (maxLen <= 0) { fprintf(stderr, "Bad length\n"); err = kNuErrInvalidArg; goto bail; } } else { maxLen = 0; } err = GetLine("Enter the thread contents", lineBuf, kNiceLineLen); if (err != kNuErrNone) goto bail; ourLen = strlen(lineBuf); /* create a data source from the buffer */ err = NuCreateDataSourceForBuffer(kNuThreadFormatUncompressed, maxLen, (unsigned char*)lineBuf, 0, ourLen, FreeCallback, &pDataSource); if (err != kNuErrNone) { fprintf(stderr, "Exerciser: buffer data source create failed (err=%d)\n", err); goto bail; } lineBuf = nil; /* now owned by the library */ } err = NuAddThread(ExerciserState_GetNuArchive(pState), strtol(argv[1], nil, 0), threadID, pDataSource, &threadIdx); if (err == kNuErrNone) { pDataSource = nil; /* library owns it now */ printf("Exerciser: success; function returned threadIdx=%ld\n", threadIdx); } bail: NuFreeDataSource(pDataSource); if (lineBuf != nil) free(lineBuf); return err; }