📄 libmesh3.c
字号:
if(format[j] == 'i') fprintf(mesh->handle, "%d ", int_buffer[ i * size + j ]); else if(format[j] == 'r') fprintf(mesh->handle, "%g ", flt_buffer[ i * size + j ]); else if(format[j] == 'c') { fputc('"', mesh->handle); fprintf(mesh->handle, "%s", (char *)&flt_buffer[ i * size + j ]); fputc('"', mesh->handle); } fprintf(mesh->handle, "\n"); } } else fwrite(buffer, nbl * size * 4, 1, mesh->handle); mesh->kw_pos[ kw_code ][1] = ftell(mesh->handle); return(1);}/*----------------------------------------------------------*//* Single line read *//*----------------------------------------------------------*/int LM_read_line(LM_mesh_struct *mesh, int kw_code, ...){ float buffer[10], *ptr_flt; int i, size; char format[256]; va_list pa; /* Check wether this kw is not a simple header */ if(!strlen(LM_kw_table[ kw_code ][2])) return(0); /* Get a one line buffer */ LM_read_field(mesh, kw_code, 1, buffer); /* Start decoding the arguments */ va_start(pa, kw_code); size = expand_format(mesh, kw_code, format); for(i=0;i<size;i++) { ptr_flt = va_arg(pa, float *); *ptr_flt = buffer[i]; } va_end(pa); /* return the number of arguments filled */ return(size);}/*----------------------------------------------------------*//* Single line write *//*----------------------------------------------------------*/int LM_write_line(LM_mesh_struct *mesh, int kw_code, ...){ float buffer[10], *ptr_flt; int i, size; char format[256]; va_list pa; /* Check wether this kw is not a simple header */ if(!strlen(LM_kw_table[ kw_code ][2])) return(0); /* Start decoding the arguments */ va_start(pa, kw_code); size = expand_format(mesh, kw_code, format); for(i=0;i<size;i++) { ptr_flt = va_arg(pa, float *); buffer[i] = *ptr_flt; } va_end(pa); /* Write a one line buffer */ LM_write_field(mesh, kw_code, 1, buffer); /* return the number of arguments filled */ return(size);}/*----------------------------------------------------------*//* Find every kw present in a meshfile *//*----------------------------------------------------------*/static void file2kw_tab(LM_mesh_struct *mesh){ int kw_code, next_pos; char str[256]; if(mesh->type & LM_ASCII) { /* Scan each string of the file until the end */ while(fscanf(mesh->handle, "%s", str) != EOF) { /* Fast test in order to reject quickly the numeric values */ if(isalpha(str[0])) { /* Search which kw code this string is associated with */ for(kw_code=1; kw_code<= LM_NBKW; kw_code++) if(!strcmp(str, LM_kw_table[ kw_code ][0])) { /* The base position (0) in the file is set right after the kw */ mesh->kw_pos[ kw_code ][0] = ftell(mesh->handle); /* If this kw has a header, read the number of lines */ if(!strcmp(LM_kw_table[ kw_code ][1], "i")) mesh->kw_counters[ kw_code ] = read_int(mesh); else mesh->kw_counters[ kw_code ] = 1; /* The curent position (1) in the file is set right before the data */ mesh->kw_pos[ kw_code ][1] = ftell(mesh->handle); break; } } else if(str[0] == '#') while(fgetc(mesh->handle) != '\n'); } } else { /* Jump through kw positions in the file */ do { kw_code = read_int(mesh); /* Check if this kw belongs to this mesh version */ if( (kw_code >= 1) && (kw_code <= LM_NBKW) ) { /* The base position (0) in the file is set right after the kw */ mesh->kw_pos[ kw_code ][0] = ftell(mesh->handle); /* Read the next kw position */ next_pos = read_int(mesh); /* If this kw has a header, read the number of lines */ if(!strcmp(LM_kw_table[ kw_code ][1], "i")) mesh->kw_counters[ kw_code ] = read_int(mesh); else mesh->kw_counters[ kw_code ] = 1; /* The curent position (1) in the file is set right before the data */ mesh->kw_pos[ kw_code ][1] = ftell(mesh->handle); } else { /* Otherwise, read the next kw position in order to skip these unknown kw */ next_pos = read_int(mesh); } /* Go to the next kw */ if(next_pos) fseek(mesh->handle, next_pos, SEEK_SET); }while(next_pos); }}/*----------------------------------------------------------*//* Update the number of lines written for each kw *//*----------------------------------------------------------*/static void kw_tab2file(LM_mesh_struct *mesh){ int i; for(i=1;i<=LM_NBKW;i++) if( mesh->kw_counters[i] && strlen(LM_kw_table[i][2]) ) write_kw(mesh, i);}/*----------------------------------------------------------*//* Write the string associated with the kw code *//*----------------------------------------------------------*/static void write_kw(LM_mesh_struct *mesh, int kw_code){ int i; if(mesh->type & LM_ASCII) { /* Test if it is the first time this kw is written */ if(!mesh->kw_counters[ kw_code ]) { /* If so, write the string and reserve some place afterward in order to store the number of lines */ fprintf(mesh->handle, "\n%s\n", LM_kw_table[ kw_code ][0]); mesh->kw_pos[ kw_code ][0] = ftell(mesh->handle); if(!strcmp("i", LM_kw_table[ kw_code ][1])) fprintf(mesh->handle, " \n"); /* In case of solution field, write the extended header at once */ if(mesh->sol_headers[ kw_code ]) { fprintf(mesh->handle, "%d ", mesh->sol_headers[ kw_code ][0]); for(i=1;i<=mesh->sol_headers[ kw_code ][0];i++) fprintf(mesh->handle, "%d ", mesh->sol_headers[ kw_code ][i+1]); fprintf(mesh->handle, "\n\n"); } /* Store the positions right after the kw in order to write the number of lines at closing time. Store the position right before the data for the write_field process. */ mesh->kw_pos[ kw_code ][1] = ftell(mesh->handle); } else { /* If this kw has already be written in the file and has a header, go to pos(0) and write down the final value */ if(strcmp("i", LM_kw_table[ kw_code ][1])) return; fseek(mesh->handle, mesh->kw_pos[ kw_code][0], SEEK_SET); fprintf(mesh->handle, "%d\n", mesh->kw_counters[ kw_code ]); } } else { /* Test if it is the first time this kw is written */ if(!mesh->kw_counters[ kw_code ]) { /* If so, write the code, store the position afterward, reserve an int for the next kw position and write the number of lines */ write_int(mesh, kw_code); mesh->kw_pos[ kw_code ][0] = ftell(mesh->handle); write_int(mesh, 0); /* Set the next kw pos in the previously written kw */ if(mesh->current_kw) { fseek(mesh->handle, mesh->kw_pos[ mesh->current_kw ][0], SEEK_SET); write_int(mesh, mesh->kw_pos[ kw_code ][0] - 4); fseek(mesh->handle, mesh->kw_pos[ kw_code ][0] + 4, SEEK_SET); } mesh->current_kw = kw_code; if(!strcmp("i", LM_kw_table[ kw_code ][1])) write_int(mesh, 0); /* In case of solution field, write the extended header at once */ if(mesh->sol_headers[ kw_code ]) { write_int(mesh, mesh->sol_headers[ kw_code ][0]); for(i=1;i<=mesh->sol_headers[ kw_code ][0];i++) write_int(mesh, mesh->sol_headers[ kw_code ][i+1]); } mesh->kw_pos[ kw_code ][1] = ftell(mesh->handle); } else { /* Write the number of lines written at closing time */ if(strcmp("i", LM_kw_table[ kw_code ][1])) return; fseek(mesh->handle, mesh->kw_pos[ kw_code ][0] + 4, SEEK_SET); write_int(mesh, mesh->kw_counters[ kw_code ]); } }}/*----------------------------------------------------------*//* Read an integer in a mesh file *//*----------------------------------------------------------*/static int read_int(LM_mesh_struct *mesh){ int swaped, integer = 0; if(mesh->type & LM_ASCII) fscanf(mesh->handle, "%d", &integer); else { /* Read a 4 bytes block in the file */ fread(&integer, 4, 1, mesh->handle); if(mesh->endian != 1) { swap_bytes((void *)&integer, (void *)&swaped, 4); integer = swaped; } } return(integer);}/*----------------------------------------------------------*//* Write an integer in a mesh file *//*----------------------------------------------------------*/static void write_int(LM_mesh_struct *mesh, int integer){ if(mesh->type & LM_ASCII) fprintf(mesh->handle, "%d ", integer); else fwrite(&integer, 4, 1, mesh->handle);}/*----------------------------------------------------------*//* Convert little endian <-> big endian *//*----------------------------------------------------------*/static void swap_bytes(void *c1, void *c2, int nbytes){ int k; char *c11, *c22; c11 = (char*)c1; c22 = (char*)c2; for (k=0; k<nbytes; k++) c22[k] = c11[ nbytes-k-1 ];}/*----------------------------------------------------------*//* Expand the compacted format and compute the line size *//*----------------------------------------------------------*/static int expand_format(LM_mesh_struct *mesh, int kw_code, char *out_format){ int i, j, duplicate_next = 0, size = 0; char *in_format = LM_kw_table[ kw_code ][2]; out_format[0] = '\0'; /* Scan each character of the format string */ for(i=0;i<strlen(in_format);i++) { if( (in_format[i] == 'i') || (in_format[i] == 'r') ) { if(!duplicate_next) duplicate_next = 1; for(j=1;j<=duplicate_next;j++) { strncat(out_format, &in_format[i], 1); size++; } duplicate_next = 0; } else if(in_format[i] == 'c') { strncat(out_format, &in_format[i], 1); size += 64; } else if(in_format[i] == 'd') { /* 'd' means duplicate the next character mesh->dimension times */ duplicate_next = mesh->dimension; } else if(in_format[i] == 's') { /* 's' means duplicate the next character mesh->solsize times */ duplicate_next = mesh->sol_headers[ kw_code ][1]; } } /* Return the sum of item to be read */ return(size);}/*----------------------------------------------------------*//* Read the extended sol headers *//*----------------------------------------------------------*/static void read_sol_headers(LM_mesh_struct *mesh){ int i, j, nbsol, solsize; for(i=1;i<=LM_NBKW;i++) { /* If the kw is a special Sol field, read the extended header */ if(!mesh->kw_counters[i] || strcmp(LM_kw_table[i][2], "sr")) continue; /* Set the curent position in file to the begining of this kw's data */ fseek(mesh->handle, mesh->kw_pos[i][1], SEEK_SET); nbsol = read_int(mesh); if( (mesh->sol_headers[i] = malloc((nbsol+2) * sizeof(int)))) { mesh->sol_headers[i][0] = nbsol; solsize = 0; for(j=1;j<=nbsol;j++) { mesh->sol_headers[i][j+1] = read_int(mesh); switch(mesh->sol_headers[i][j+1]) { case LM_SCALAR : solsize += 1; break; case LM_VECTOR : solsize += mesh->dimension; break; case LM_SYM_MATRIX : solsize += (mesh->dimension * (mesh->dimension+1)) / 2; break; case LM_MATRIX : solsize += mesh->dimension * mesh->dimension; break; } mesh->sol_headers[i][1] = solsize; } mesh->kw_pos[i][1] = ftell(mesh->handle); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -