Files
pymol-open-source/layer0/File.cpp
Thomas Holder 59d84a385b assembly fix, ref count fix, ...
* fix assemblies for cases like 4f3r which have multiple entries in the _pdbx_struct_assembly_gen table
* fix ref counts in iterate/alter subscripts
* improve side_chain_helper and nucleic_adic_mode situation
* eliminate all "try/catch" blocks for std::map lookups
* clean up some MemoryDebug stuff and remove unused jenarix wrapper
2015-06-23 14:07:26 +00:00

62 lines
1.2 KiB
C++

/*
* Copyright (c) Schrodinger, LLC.
*
* Basic file IO.
*/
#include <stdio.h>
#include <stdlib.h>
#include "File.h"
#include "MemoryDebug.h"
/*
* Get the size from the current file pointer to the end of the file
*/
static long fgetsize(FILE *fp) {
long filesize, current = ftell(fp);
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
fseek(fp, current, SEEK_SET);
return filesize;
}
/*
* Allocate memory and read the entire file from the given file pointer.
* The file size is stored into the size pointer if not NULL.
*/
static char * fgetcontents(FILE *fp, long *size) {
long filesize = fgetsize(fp);
char *contents = (char*) mmalloc(filesize + 255);
if (!contents)
return NULL;
if (1 != fread(contents, filesize, 1, fp)) {
mfree(contents);
return NULL;
}
if (size)
*size = filesize;
contents[filesize] = '\0';
return contents;
}
/*
* Allocate memory and read the entire file for the given filename.
* The file size is stored into the size pointer if not NULL.
*/
char * FileGetContents(const char *filename, long *size) {
char *contents;
FILE *fp = fopen(filename, "rb");
if (!fp)
return NULL;
contents = fgetcontents(fp, size);
fclose(fp);
return contents;
}