tinyvm/libtvm/tvm_file.c
Joseph Kogut ca87b3c5e9 Refactor with a consistent coding style
This commit refactors the project using a single, consistent coding
style, derived from the Linux Kernel Coding Style, available here:

https://www.kernel.org/doc/Documentation/CodingStyle

This includes, but is not limited to:
* Removal of typedefs, especially for structs
* Limiting lines to a reasonable length, 80 characters, mostly
* K&R style braces
* Removal of CamelCase
2016-08-28 20:31:12 -07:00

46 lines
814 B
C

#include <tvm/tvm_file.h>
FILE *tvm_fopen(const char *filename, const char *extension, const char *mode)
{
FILE *filp = NULL;
size_t fname_chars = strlen(filename) + strlen(extension) + 1;
char *fname = calloc(fname_chars, sizeof(char));
int i;
strcpy(fname, filename);
for (i = 0; i < 2 && !filp; i++) {
if (i > 0)
strcat(fname, extension);
filp = fopen(fname, mode);
}
free(fname);
return filp;
}
int tvm_fcopy(char *dest, size_t size, FILE *filp)
{
size_t i;
long pos = ftell(filp);
for (i = 0; i < size && !feof(filp); i++)
dest[i] = fgetc(filp);
dest[i - 1] = 0;
fseek(filp, pos, SEEK_SET);
return 0;
}
int tvm_flength(FILE *filp)
{
int length;
long pos = ftell(filp);
for (length = 0; !feof(filp); length++)
fgetc(filp);
fseek(filp, pos, SEEK_SET);
return length;
}