📄 magic1.cpp
字号:
#include <stdio.h>
#include <string.h>
#include "elements.h"
#include "fileform.h"
#include "magic1.h"
int FT_MAGIC1;
//===========================================================================
static int
MAGIC1_query (char* name)
{
FILE *input = fopen(name,"rb");
if ( !input )
return -1;
char buffer[6];
fread(buffer,sizeof(char),5,input);
buffer[5] = 0;
fclose(input);
if ( strcmp(buffer,"dims=") == 0 )
return 1;
else
return 0;
}
//---------------------------------------------------------------------------
static int
MAGIC1_gethead (char* name, int* dims, int** dim, char* elform,
int* order)
{
FILE *input = fopen(name,"rb");
if ( !input )
return 0;
// read dimension information
fscanf(input,"dims=%d\r\n",dims);
// size of each dimension
*dim = new int[*dims]; // assumes *dim is a valid address
for (int i = 0; i < *dims; i++) {
int dummy;
fscanf(input,"dim[%d]=%d\r\n",&dummy,&((*dim)[i]));
}
// read element type information
int eltype;
fscanf(input,"type=%d\r\n\x1a",&eltype);
strcpy(elform,mgcElementDBM::Description(eltype));
*order = 0;
fclose(input);
return 1;
}
//---------------------------------------------------------------------------
static int
MAGIC1_puthead (char* name, int dims, const int* dim, const char* elform,
int)
{
FILE *output = fopen(name,"wb");
if ( !output )
return 0;
// write dimension information
fprintf(output,"dims=%d\r\n",dims);
int i;
for (i = 0; i < dims; i++)
fprintf(output,"dim[%d]=%d\r\n",i,dim[i]);
// write element type information
int eltype = mgcElementDBM::Type(elform);
fprintf(output,"type=%d\r\n",eltype);
fprintf(output,"\x1a"); // terminate with ASCII EOF character
// allocate file space to hold all the elements
const int buffer_size = 16384;
char* buffer = new char[buffer_size];
for (i = 0; i < buffer_size; i++)
buffer[i] = 0;
int quantity = 1;
for (i = 0; i < dims; i++)
quantity *= dim[i];
int byte_quantity = quantity*mgcElementDBM::PackedSize(eltype);
int block_count = byte_quantity / buffer_size;
int block_remainder = byte_quantity % buffer_size;
for (i = 0; i < block_count; i++)
fwrite(buffer,sizeof(char),buffer_size,output);
if ( block_remainder )
fwrite(buffer,sizeof(char),block_remainder,output);
delete[] buffer;
fclose(output);
return 1;
}
//---------------------------------------------------------------------------
static int
MAGIC1_getdata (char* name, long offset, int quantity, char* buffer)
{
FILE* file = fopen(name,"r+b");
if ( !file )
return 0;
// move file pointer to first element of buffer
fseek(file,0,0);
char c = 0;
while ( c != '\x1a' )
fread(&c,sizeof(char),1,file);
fseek(file,offset,1); // seek from current
fread(buffer,sizeof(char),quantity,file);
fclose(file);
return 1;
}
//---------------------------------------------------------------------------
static int
MAGIC1_putdata (char* name, long offset, int quantity, char* buffer)
{
FILE* file = fopen(name,"r+b");
if ( !file )
return 0;
// move file pointer to first element of buffer
fseek(file,0,0);
char c = 0;
while ( c != '\x1a' )
fread(&c,sizeof(char),1,file);
fseek(file,offset,1); // seek from current
fwrite(buffer,sizeof(char),quantity,file);
fclose(file);
return 1;
}
//---------------------------------------------------------------------------
static int
MAGIC1_getmessage (char*, void*)
{
return 0;
}
//---------------------------------------------------------------------------
static int
MAGIC1_putmessage (char*, void*)
{
return 0;
}
//---------------------------------------------------------------------------
int __MAGIC1_register__()
{
// automatic registration of MAGIC1 file format
static int is_registered = 0;
if ( is_registered == 0 ) {
FT_MAGIC1 = mgcFileFormat::AddFileFormat(
MAGIC1_query,
MAGIC1_gethead,
MAGIC1_puthead,
MAGIC1_getdata,
MAGIC1_putdata,
MAGIC1_getmessage,
MAGIC1_putmessage
);
is_registered = 1;
}
return is_registered;
}
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -