factor/native/image.c

78 lines
1.5 KiB
C
Raw Normal View History

2004-07-16 02:26:21 -04:00
#include "factor.h"
void load_image(char* filename)
{
FILE* file;
HEADER h;
CELL size;
printf("Loading %s...",filename);
2004-08-20 18:48:08 -04:00
2004-07-16 02:26:21 -04:00
file = fopen(filename,"rb");
if(file == NULL)
2004-08-20 18:48:08 -04:00
fatal_error("Cannot open image for reading",errno);
2004-07-16 02:26:21 -04:00
2004-07-20 02:59:32 -04:00
/* read it in native byte order */
fread(&h,sizeof(HEADER)/sizeof(CELL),sizeof(CELL),file);
2004-07-16 02:26:21 -04:00
if(h.magic != IMAGE_MAGIC)
fatal_error("Bad magic number",h.magic);
if(h.version != IMAGE_VERSION)
fatal_error("Bad version number",h.version);
allot(h.size);
size = h.size / CELLS;
if(size != fread((void*)active.base,sizeof(CELL),size,file))
2004-07-16 02:26:21 -04:00
fatal_error("Wrong image length",h.size);
active.here = active.base + h.size;
2004-07-16 02:26:21 -04:00
fclose(file);
printf(" relocating...");
fflush(stdout);
2004-07-16 02:26:21 -04:00
clear_environment();
2004-08-20 18:48:08 -04:00
userenv[GLOBAL_ENV] = h.global;
userenv[BOOT_ENV] = h.boot;
2004-07-16 02:26:21 -04:00
relocate(h.relocation_base);
printf(" done\n");
fflush(stdout);
2004-07-16 02:26:21 -04:00
}
bool save_image(char* filename)
{
FILE* file;
HEADER h;
2004-08-20 18:48:08 -04:00
fprintf(stderr,"Saving %s...\n",filename);
2004-07-16 02:26:21 -04:00
file = fopen(filename,"wb");
if(file == NULL)
2004-08-20 18:48:08 -04:00
fatal_error("Cannot open image for writing",errno);
2004-07-16 02:26:21 -04:00
h.magic = IMAGE_MAGIC;
h.version = IMAGE_VERSION;
h.relocation_base = active.base;
2004-08-20 18:48:08 -04:00
h.boot = userenv[BOOT_ENV];
h.size = (active.here - active.base);
2004-08-20 18:48:08 -04:00
h.global = userenv[GLOBAL_ENV];
2004-07-16 02:26:21 -04:00
fwrite(&h,sizeof(HEADER),1,file);
fwrite((void*)active.base,h.size,1,file);
2004-07-16 02:26:21 -04:00
fclose(file);
return true;
}
void primitive_save_image(void)
{
F_STRING* filename = untag_string(dpop());
2004-07-16 02:26:21 -04:00
save_image(to_c_string(filename));
}