implement aux_dequeue, test, and remove aux span

This commit is contained in:
Gregor Weiss 2024-10-18 10:55:00 +02:00
parent df2ad56f14
commit 58ab32e393
Signed by: Gregor Weiss
GPG key ID: 61E170A8BBFE5756
3 changed files with 46 additions and 29 deletions

View file

@ -368,8 +368,7 @@
float *f; float *f;
} field; } field;
span *aux; // TODO: implement aux as queue
uchar *uchar_aux; uchar *uchar_aux;
uint32 aux_pos; uint32 aux_pos;
uint32 aux_len; uint32 aux_len;

View file

@ -253,6 +253,16 @@ eas3_init_stream(uchar* memory, uint32 size, char instr)
aux_pos += chunck_len; \ aux_pos += chunck_len; \
} }
#define aux_dequeue(aux, aux_pos, aux_len, chunck, chunck_len) \
{ \
if(aux_pos + chunck_len <= aux_len) { \
memcpy(chunck, aux + aux_pos, chunck_len); \
aux_pos += chunck_len; \
} else { \
fprintf(stderr, MEMERROR); \
} \
}
/*----------------------------------------------------------------------------------------------------------*\ /*----------------------------------------------------------------------------------------------------------*\
! FUNCTION NAME: void *test(void) ! ! FUNCTION NAME: void *test(void) !
! -------------- ! ! -------------- !
@ -442,16 +452,16 @@ eas3_free_data(eas3_data* data)
{ {
if(data) if(data)
{ {
if(data->aux) //if(data->aux)
{ //{
free(data->aux->memory); //free(data->aux->memory);
data->aux->access = NULL; //data->aux->access = NULL;
data->aux->position = 0; //data->aux->position = 0;
data->aux->size = 0; //data->aux->size = 0;
// TODO: remove // TODO: remove
data->aux->L = 0; //data->aux->L = 0;
} //}
free(data->aux); //free(data->aux);
free(data); free(data);
} }
} }
@ -617,18 +627,6 @@ read_eas3_header(FILE *const fp, eas3_data *const data)
return 1; return 1;
} }
/*--------------------------------------------------------*\
! Allocate the auxiliary information packed stream. !
\*--------------------------------------------------------*/
data->aux = calloc(1, sizeof(span));
if(!data->aux)
{
// memory allocation error
fprintf(stderr, MEMERROR);
free(buffer_char);
return 1;
}
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Initialize the stream for the auxiliary information mem- ! ! Initialize the stream for the auxiliary information mem- !
! ory block. The initial size of the auxiliary memory ! ! ory block. The initial size of the auxiliary memory !
@ -949,13 +947,25 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Initialize the auxiliary information stream. ! ! Initialize the auxiliary information stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
aux = eas3_init_stream(data->aux->memory, data->aux->size, 'd'); aux = eas3_init_stream(data->uchar_aux, data->aux_len, 'd');
// Rewind aux
data->aux_pos = 0;
/*--------------------------------------------------------*\ /*--------------------------------------------------------*\
! Get the standard parameters from the auxiliary informa- ! ! Get the standard parameters from the auxiliary informa- !
! memory block and write them to the file stream. ! ! memory block and write them to the file stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
params = (eas3_std_params*)eas3_get_chunck(aux, 176); params = calloc(1, sizeof(eas3_std_params));
if(!params)
{
// memory allocation error
fprintf(stderr, MEMERROR);
free(params);
return 1;
}
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
params, sizeof(eas3_std_params));
if(fwrite(params, sizeof(uint64), 22, fp) != 22) if(fwrite(params, sizeof(uint64), 22, fp) != 22)
{ {
@ -983,7 +993,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
! the timestep array from the auxiliary information block ! ! the timestep array from the auxiliary information block !
! and write it to the file stream. ! ! and write it to the file stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
buffer_char = eas3_get_chunck(aux, data->params.nts * sizeof(uint64)); buffer_char = calloc(data->params.nts * sizeof(uint64), sizeof(uchar));
if(!buffer_char) if(!buffer_char)
{ {
// memory allocation error // memory allocation error
@ -991,6 +1001,8 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(buffer_char); free(buffer_char);
return 1; return 1;
} }
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, data->params.nts * sizeof(uint64));
if(fwrite(buffer_char, sizeof(uint64), data->params.nts, fp) != data->params.nts) if(fwrite(buffer_char, sizeof(uint64), data->params.nts, fp) != data->params.nts)
{ {
@ -1012,7 +1024,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
! the timestep attribute array from the auxiliary informa- ! ! the timestep attribute array from the auxiliary informa- !
! tion block and write it to the file stream. ! ! tion block and write it to the file stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
buffer_char = eas3_get_chunck(aux, data->params.nts * ATTRLEN); buffer_char = calloc(data->params.nts * ATTRLEN, sizeof(uchar));
if(!buffer_char) if(!buffer_char)
{ {
// memory allocation error // memory allocation error
@ -1020,6 +1032,8 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(buffer_char); free(buffer_char);
return 1; return 1;
} }
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, data->params.nts * ATTRLEN);
if(fwrite(buffer_char, sizeof(uchar), data->params.nts * ATTRLEN, fp) != (data->params.nts * ATTRLEN)) if(fwrite(buffer_char, sizeof(uchar), data->params.nts * ATTRLEN, fp) != (data->params.nts * ATTRLEN))
{ {
@ -1076,7 +1090,7 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
! the remaining eas header bytes from the auxiliary infor- ! ! the remaining eas header bytes from the auxiliary infor- !
! mation block and write it to the file stream. ! ! mation block and write it to the file stream. !
\*--------------------------------------------------------*/ \*--------------------------------------------------------*/
buffer_char = eas3_get_chunck(aux, Lwrite); buffer_char = calloc(Lwrite, sizeof(uchar));
if(!buffer_char) if(!buffer_char)
{ {
// memory allocation error // memory allocation error
@ -1084,6 +1098,8 @@ write_eas3_header(FILE *const fp, eas3_data *const data)
free(buffer_char); free(buffer_char);
return 1; return 1;
} }
aux_dequeue(data->uchar_aux, data->aux_pos, data->aux_len,
buffer_char, Lwrite);
if(fwrite(buffer_char, sizeof(uchar), Lwrite, fp) != Lwrite) if(fwrite(buffer_char, sizeof(uchar), Lwrite, fp) != Lwrite)
{ {

View file

@ -705,7 +705,9 @@ int main(int argc, char *argv[])
// TODO: implement I/O of bwc file // TODO: implement I/O of bwc file
// TODO: bwc_header_info // TODO: bwc_header_info
//write_eas3(data, "output2.eas"); write_eas3(data, "output.eas");
data = read_eas3("output.eas");
write_eas3(data, "output2.eas");
eas3_free_data(data); eas3_free_data(data);
free(output); free(output);