void assertMem(size_t used, size_t total)
{ if (used <= total) return;
fprintf(stderr, "Buffer size %zu is not big enough to complete operation; we need %zu bytes\n", total, used); exit(2);
}
// read file of known size, make sure the size is correct bool readFile(void *dst, uint32_t len, constchar *fileName)
{
FILE *f = fopen(fileName, "rb"); bool ret = false;
if (!f) returnfalse;
if (len != fread(dst, 1, len, f)) goto out;
if (fread(&len, 1, 1, f)) //make sure file is actually over goto out;
ret = true;
out:
fclose(f); return ret;
}
// read complete file of unknown size, return malloced buffer and size void *loadFile(constchar *fileName, uint32_t *size)
{
FILE *f = fopen(fileName, "rb");
uint8_t *dst = NULL;
uint32_t len = 0, grow = 16384, total = 0;
uint32_t block;
if (!f) {
fprintf(stderr, "couldn't open %s: %s\n", fileName, strerror(errno)); exit(2);
}
do {
len += grow; dst = reallocOrDie(dst, len);
block = fread(dst + total, 1, grow, f);
total += block;
} while (block == grow);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.