int main(int argc, char **argv) { char *str; l_uint8 *data1, *data2; l_int32 i, n, same1, same2; size_t size1, size2, slice, total, start, end; FILE *fp; L_DNA *da; SARRAY *sa; L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5; static char mainName[] = "byteatest"; if (argc != 1) return ERROR_INT("syntax: byteatest", mainName, 1); lept_mkdir("bytea"); /* Test basic init, join and split */ lba1 = l_byteaInitFromFile("feyn.tif"); lba2 = l_byteaInitFromFile("test24.jpg"); size1 = l_byteaGetSize(lba1); size2 = l_byteaGetSize(lba2); l_byteaJoin(lba1, &lba2); lba3 = l_byteaInitFromMem(lba1->data, size1); lba4 = l_byteaInitFromMem(lba1->data + size1, size2); /* Split by hand */ l_binaryWrite("/tmp/bytea/junk1.dat", "w", lba3->data, lba3->size); l_binaryWrite("/tmp/bytea/junk2.dat", "w", lba4->data, lba4->size); filesAreIdentical("feyn.tif", "/tmp/bytea/junk1.dat", &same1); filesAreIdentical("test24.jpg", "/tmp/bytea/junk2.dat", &same2); if (same1 && same2) fprintf(stderr, "OK for join file\n"); else fprintf(stderr, "Error: files are different!\n"); /* Split by function */ l_byteaSplit(lba1, size1, &lba5); l_binaryWrite("/tmp/bytea/junk3.dat", "w", lba1->data, lba1->size); l_binaryWrite("/tmp/bytea/junk4.dat", "w", lba5->data, lba5->size); filesAreIdentical("feyn.tif", "/tmp/bytea/junk3.dat", &same1); filesAreIdentical("test24.jpg", "/tmp/bytea/junk4.dat", &same2); if (same1 && same2) fprintf(stderr, "OK for split file\n"); else fprintf(stderr, "Error: files are different!\n"); l_byteaDestroy(&lba1); l_byteaDestroy(&lba2); l_byteaDestroy(&lba3); l_byteaDestroy(&lba4); l_byteaDestroy(&lba5); /* Test appending with strings */ data1 = l_binaryRead("kernel_reg.c", &size1); sa = sarrayCreateLinesFromString((char *)data1, 1); lba1 = l_byteaCreate(0); n = sarrayGetCount(sa); for (i = 0; i < n; i++) { str = sarrayGetString(sa, i, L_NOCOPY); l_byteaAppendString(lba1, str); l_byteaAppendString(lba1, (char *)"\n"); } data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("/tmp/bytea/junk5.dat", "w", data2, size2); filesAreIdentical("kernel_reg.c", "/tmp/bytea/junk5.dat", &same1); if (same1) fprintf(stderr, "OK for appended string data\n"); else fprintf(stderr, "Error: appended string data is different!\n"); lept_free(data1); sarrayDestroy(&sa); l_byteaDestroy(&lba1); /* Test appending with binary data */ slice = 1000; total = nbytesInFile("breviar-a38.jp2"); lba1 = l_byteaCreate(100); n = 1 + total / slice; fprintf(stderr, "******************************************************\n"); fprintf(stderr, "* Testing error checking: ignore two reported errors *\n"); for (i = 0, start = 0; i <= n; i++, start += slice) { data1 = l_binaryReadSelect("breviar-a38.jp2", start, slice, &size1); l_byteaAppendData(lba1, data1, size1); lept_free(data1); } fprintf(stderr, "******************************************************\n"); data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("/tmp/bytea/junk6.dat", "w", data2, size2); filesAreIdentical("breviar-a38.jp2", "/tmp/bytea/junk6.dat", &same1); if (same1) fprintf(stderr, "OK for appended binary data\n"); else fprintf(stderr, "Error: appended binary data is different!\n"); l_byteaDestroy(&lba1); /* Test search */ convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "/tmp/bytea/junk7.pdf", 0, 0, 100, NULL, NULL, 0); lba1 = l_byteaInitFromFile("/tmp/bytea/junk7.pdf"); l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da); /* l_dnaWriteStream(stderr, da); */ n = l_dnaGetCount(da); if (n == 6) fprintf(stderr, "OK for search: found 6 instances\n"); else fprintf(stderr, "Error in search: found %d instances, not 6\n", n); l_byteaDestroy(&lba1); l_dnaDestroy(&da); /* Test write to file */ lba1 = l_byteaInitFromFile("feyn.tif"); fp = lept_fopen("/tmp/bytea/junk8.dat", "wb"); size1 = l_byteaGetSize(lba1); for (start = 0; start < size1; start += 1000) { end = L_MIN(start + 1000 - 1, size1 - 1); l_byteaWriteStream(fp, lba1, start, end); } lept_fclose(fp); filesAreIdentical("feyn.tif", "/tmp/bytea/junk8.dat", &same1); if (same1) fprintf(stderr, "OK for written binary data\n"); else fprintf(stderr, "Error: written binary data is different!\n"); l_byteaDestroy(&lba1); return 0; }
int main(int argc, char **argv) { char *filein, *str, *prestring, *outprotos, *protostr; const char *spacestr = " "; char buf[L_BUF_SIZE]; l_uint8 *allheaders; l_int32 i, maxindex, in_line, nflags, protos_added, firstfile, len, ret; size_t nbytes; L_BYTEA *ba, *ba2; SARRAY *sa, *safirst; static char mainName[] = "xtractprotos"; if (argc == 1) { fprintf(stderr, "xtractprotos [-prestring=<string>] [-protos=<where>] " "[list of C files]\n" "where the prestring is prepended to each prototype, and \n" "protos can be either 'inline' or the name of an output " "prototype file\n"); return 1; } /* ---------------------------------------------------------------- */ /* Parse input flags and find prestring and outprotos, if requested */ /* ---------------------------------------------------------------- */ prestring = outprotos = NULL; in_line = FALSE; nflags = 0; maxindex = L_MIN(3, argc); for (i = 1; i < maxindex; i++) { if (argv[i][0] == '-') { if (!strncmp(argv[i], "-prestring", 10)) { nflags++; ret = sscanf(argv[i] + 1, "prestring=%s", buf); if (ret != 1) { fprintf(stderr, "parse failure for prestring\n"); return 1; } if ((len = strlen(buf)) > L_BUF_SIZE - 3) { L_WARNING("prestring too large; omitting!\n", mainName); } else { buf[len] = ' '; buf[len + 1] = '\0'; prestring = stringNew(buf); } } else if (!strncmp(argv[i], "-protos", 7)) { nflags++; ret = sscanf(argv[i] + 1, "protos=%s", buf); if (ret != 1) { fprintf(stderr, "parse failure for protos\n"); return 1; } outprotos = stringNew(buf); if (!strncmp(outprotos, "inline", 7)) in_line = TRUE; } } } if (argc - nflags < 2) { fprintf(stderr, "no files specified!\n"); return 1; } /* ---------------------------------------------------------------- */ /* Generate the prototype string */ /* ---------------------------------------------------------------- */ ba = l_byteaCreate(500); /* First the extern C head */ sa = sarrayCreate(0); sarrayAddString(sa, (char *)"/*", 1); snprintf(buf, L_BUF_SIZE, " * These prototypes were autogen'd by xtractprotos, v. %s", version); sarrayAddString(sa, buf, 1); sarrayAddString(sa, (char *)" */", 1); sarrayAddString(sa, (char *)"#ifdef __cplusplus", 1); sarrayAddString(sa, (char *)"extern \"C\" {", 1); sarrayAddString(sa, (char *)"#endif /* __cplusplus */\n", 1); str = sarrayToString(sa, 1); l_byteaAppendString(ba, str); lept_free(str); sarrayDestroy(&sa); /* Then the prototypes */ firstfile = 1 + nflags; protos_added = FALSE; for (i = firstfile; i < argc; i++) { filein = argv[i]; len = strlen(filein); if (filein[len - 1] == 'h') /* skip .h files */ continue; snprintf(buf, L_BUF_SIZE, "cpp -ansi -DNO_PROTOS %s %s", filein, tempfile); ret = system(buf); if (ret) { fprintf(stderr, "cpp failure for %s; continuing\n", filein); continue; } if ((str = parseForProtos(tempfile, prestring)) == NULL) { fprintf(stderr, "parse failure for %s; continuing\n", filein); continue; } if (strlen(str) > 1) { /* strlen(str) == 1 is a file without protos */ l_byteaAppendString(ba, str); protos_added = TRUE; } lept_free(str); } /* Lastly the extern C tail */ sa = sarrayCreate(0); sarrayAddString(sa, (char *)"\n#ifdef __cplusplus", 1); sarrayAddString(sa, (char *)"}", 1); sarrayAddString(sa, (char *)"#endif /* __cplusplus */", 1); str = sarrayToString(sa, 1); l_byteaAppendString(ba, str); lept_free(str); sarrayDestroy(&sa); protostr = (char *)l_byteaCopyData(ba, &nbytes); l_byteaDestroy(&ba); /* ---------------------------------------------------------------- */ /* Generate the output */ /* ---------------------------------------------------------------- */ if (!outprotos) { /* just write to stdout */ fprintf(stderr, "%s\n", protostr); lept_free(protostr); return 0; } /* If no protos were found, do nothing further */ if (!protos_added) { fprintf(stderr, "No protos found\n"); lept_free(protostr); return 1; } /* Make the output files */ ba = l_byteaInitFromFile("allheaders_top.txt"); if (!in_line) { snprintf(buf, sizeof(buf), "#include \"%s\"\n", outprotos); l_byteaAppendString(ba, buf); l_binaryWrite(outprotos, "w", protostr, nbytes); } else { l_byteaAppendString(ba, protostr); } ba2 = l_byteaInitFromFile("allheaders_bot.txt"); l_byteaJoin(ba, &ba2); l_byteaWrite("allheaders.h", ba, 0, 0); l_byteaDestroy(&ba); lept_free(protostr); return 0; }
main(int argc, char **argv) { char *str; l_uint8 *data1, *data2; l_int32 i, n, start, end, same1, same2; size_t size1, size2; FILE *fp; L_DNA *da; SARRAY *sa; L_BYTEA *lba1, *lba2, *lba3, *lba4, *lba5; static char mainName[] = "byteatest"; if (argc != 1) exit(ERROR_INT("syntax: whatever11", mainName, 1)); /* Test basic init, join and split */ lba1 = l_byteaInitFromFile("feyn.tif"); lba2 = l_byteaInitFromFile("test24.jpg"); size1 = l_byteaGetSize(lba1); size2 = l_byteaGetSize(lba2); l_byteaJoin(lba1, &lba2); lba3 = l_byteaInitFromMem(lba1->data, size1); lba4 = l_byteaInitFromMem(lba1->data + size1, size2); /* Split by hand */ l_binaryWrite("junk1", "w", lba3->data, lba3->size); l_binaryWrite("junk2", "w", lba4->data, lba4->size); filesAreIdentical("feyn.tif", "junk1", &same1); filesAreIdentical("test24.jpg", "junk2", &same2); if (same1 && same2) fprintf(stderr, "OK for join file\n"); else fprintf(stderr, "Error: files are different!\n"); /* Split by function */ l_byteaSplit(lba1, size1, &lba5); l_binaryWrite("junk3", "w", lba1->data, lba1->size); l_binaryWrite("junk4", "w", lba5->data, lba5->size); filesAreIdentical("feyn.tif", "junk3", &same1); filesAreIdentical("test24.jpg", "junk4", &same2); if (same1 && same2) fprintf(stderr, "OK for split file\n"); else fprintf(stderr, "Error: files are different!\n"); l_byteaDestroy(&lba1); l_byteaDestroy(&lba2); l_byteaDestroy(&lba3); l_byteaDestroy(&lba4); l_byteaDestroy(&lba5); /* Test appending */ data1 = l_binaryRead("whatever10.c", &size1); sa = sarrayCreateLinesFromString((char *)data1, 1); lba1 = l_byteaCreate(0); n = sarrayGetCount(sa); for (i = 0; i < n; i++) { str = sarrayGetString(sa, i, L_NOCOPY); l_byteaAppendString(lba1, str); l_byteaAppendString(lba1, (char *)"\n"); } data2 = l_byteaGetData(lba1, &size2); l_binaryWrite("junk1.txt", "w", data2, size2); filesAreIdentical("whatever10.c", "junk1.txt", &same1); if (same1) fprintf(stderr, "OK for appended file\n"); else fprintf(stderr, "Error: appended file is different!\n"); lept_free(data1); sarrayDestroy(&sa); l_byteaDestroy(&lba1); /* Test search */ convertToPdf("test24.jpg", L_JPEG_ENCODE, 0, "junk3.pdf", 0, 0, 100, NULL, 0, NULL); lba1 = l_byteaInitFromFile("junk3.pdf"); l_byteaFindEachSequence(lba1, (l_uint8 *)" 0 obj\n", 7, &da); l_dnaWriteStream(stderr, da); l_byteaDestroy(&lba1); l_dnaDestroy(&da); /* Test write to file */ lba1 = l_byteaInitFromFile("feyn.tif"); fp = lept_fopen("junk5", "wb"); size1 = l_byteaGetSize(lba1); for (start = 0; start < size1; start += 1000) { end = L_MIN(start + 1000 - 1, size1 - 1); l_byteaWriteStream(fp, lba1, start, end); } lept_fclose(fp); l_byteaDestroy(&lba1); return 0; }