/****************************************************************************** * Continue to get the image code in compressed form. This routine should be * * called until NULL block is returned. * * The block should NOT be freed by the user (not dynamically allocated). * ******************************************************************************/ int DGifGetCodeNext(GifFileType *GifFile, GifByteType **CodeBlock) { GifByteType Buf; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; if (my_fread(&Buf, 1, 1, Private) != 1) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } if (Buf > 0) { *CodeBlock = Private->Buf; /* Use private unused buffer. */ (*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */ if (my_fread(&((*CodeBlock)[1]), 1, Buf, Private) != Buf) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } } else { *CodeBlock = NULL; Private->Buf[0] = 0; /* Make sure the buffer is empty! */ Private->PixelCount = 0; /* And local info. indicate image read. */ } return GIF_OK; }
static int gad_seek_block(FILE *snap,char name[]) { ////// // Seeks block from title gad_title tit; int block1,block2; rewind(snap); while(1>0) { if(!(fread(&block1,sizeof(int),1,snap))|| feof(snap)||ferror(snap)) { report_error(1,"Block %s not found!!\n",name); } my_fread(&tit,sizeof(gad_title),1,snap); my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); if(strncmp(tit.label,name,2)!=0) fseek(snap,tit.size,SEEK_CUR); else break; } return 0; }
/****************************************************************************** * This routines read one gif data block at a time and buffers it internally * * so that the decompression routine could access it. * * The routine returns the next byte from its internal buffer (or read next * * block in if buffer empty) and returns GIF_OK if succesful. * ******************************************************************************/ static int DGifBufferedInput(GifFilePrivateType *File, GifByteType *Buf, GifByteType *NextByte) { if (Buf[0] == 0) { /* Needs to read the next buffer - this one is empty: */ if (my_fread(Buf, 1, 1, File) != 1) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } if (my_fread(&Buf[1], 1, Buf[0], File) != Buf[0]) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } *NextByte = Buf[1]; Buf[1] = 2; /* We use now the second place as last char read! */ Buf[0]--; } else { *NextByte = Buf[Buf[1]++]; Buf[0]--; } return GIF_OK; }
void convert_tunix (FILE *infile, FILE *outfile) { unsigned char a; unsigned char *in_bufpos; unsigned char *out_bufpos; unsigned char *in_bufend; unsigned char *out_bufend; size_t file_remain; in_bufpos = in_buffer; out_bufpos = out_buffer; (void) fseek(infile,0L,SEEK_END); file_remain = ftell(infile); rewind(infile); in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); out_bufend = out_buffer + BUFFERSIZE; while (file_remain != 0) { a = *in_bufpos; in_bufpos++; if (in_bufpos >= in_bufend) { file_remain -= in_bufend - in_buffer; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); in_bufpos = in_buffer; } if(a == CR) { *out_bufpos = LF; out_bufpos++; if (out_bufpos == out_bufend) { my_fwrite(out_buffer,outfile,BUFFERSIZE); out_bufpos = out_buffer; } if (*in_bufpos == LF && file_remain != 0) { in_bufpos++; if (in_bufpos >= in_bufend) { file_remain -= in_bufend - in_buffer; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); in_bufpos = in_buffer; } } } else { *out_bufpos = a; out_bufpos++; if (out_bufpos == out_bufend) { my_fwrite(out_buffer,outfile,BUFFERSIZE); out_bufpos = out_buffer; } } } /* Check for remainder in output buffer */ if (out_bufpos != out_buffer) my_fwrite(out_buffer,outfile,(int)(out_bufpos - out_buffer)); }
void read_gadget_header(char *prefix,int input,gad_header *head_out) { int ii,block1,block2; char fname[256]; int nfils=check_num_files(prefix); unsigned long long nptot_64[6]; gad_header head; FILE *snap; if(nfils<=0) report_error(1,"Can't find snapshot files\n"); else if(nfils==1) sprintf(fname,"%s",prefix); else sprintf(fname,"%s.0",prefix); snap=my_fopen(fname,"r"); //Read header if(input==2) gad_seek_block(snap,"HEAD"); my_fread(&block1,sizeof(int),1,snap); my_fread(&head,sizeof(gad_header),1,snap); my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); if(head.num_files!=nfils) { report_error(1, "Header and existing files do not match %d != %d.\n" " There may be some files missing\n", nfils,head.num_files); } if(NodeThis==0) { printf(" The cosmological model is:\n"); printf(" - Omega_M = %.3lf\n",head.Omega0); printf(" - Omega_L = %.3lf\n",head.OmegaLambda); printf(" - h = %.3lf\n",head.HubbleParam); printf(" This file contains: \n"); for(ii=0;ii<6;ii++) { nptot_64[ii]=head.npartTotal[ii]+((unsigned long long)(head.npartTotalHigh[ii]) << 32); printf(" - %llu particles of type %d with mass %.3lE\n",nptot_64[ii], (int)ii,head.mass[ii]); } printf(" The box size is %.3lf\n",head.BoxSize); printf(" Redshift z = %.3lf \n",head.redshift); printf("\n"); } *head_out=head; for(ii=0;ii<6;ii++) { (*head_out).npart[ii]=head.npartTotal[ii]; (*head_out).num_files=1; } }
/****************************************************************************** * This routine should be called before any attemp to read an image. * * Note it is assumed the Image desc. header (',') has been read. * ******************************************************************************/ int DGifGetImageDesc(GifFileType *GifFile) { int i, BitsPerPixel; GifByteType Buf[3]; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; if (!IS_READABLE(Private)) { /* This file was NOT open for reading: */ _GifError = D_GIF_ERR_NOT_READABLE; return GIF_ERROR; } if (DGifGetWord(Private, &GifFile->Image.Left) == GIF_ERROR || DGifGetWord(Private, &GifFile->Image.Top) == GIF_ERROR || DGifGetWord(Private, &GifFile->Image.Width) == GIF_ERROR || DGifGetWord(Private, &GifFile->Image.Height) == GIF_ERROR) return GIF_ERROR; if (my_fread(Buf, 1, 1, Private) != 1) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } BitsPerPixel = (Buf[0] & 0x07) + 1; GifFile->Image.Interlace = (Buf[0] & 0x40); if (Buf[0] & 0x80) { /* Does this image have local color map? */ if (GifFile->Image.ColorMap) FreeMapObject(GifFile->Image.ColorMap); GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL); /* Get the image local color map: */ for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) { if (my_fread(Buf, 1, 3, Private) != 3) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } GifFile->Image.ColorMap->Colors[i].Red = Buf[0]; GifFile->Image.ColorMap->Colors[i].Green = Buf[1]; GifFile->Image.ColorMap->Colors[i].Blue = Buf[2]; } } GifFile->ImageCount++; Private->PixelCount = (s32) GifFile->Image.Width * (s32) GifFile->Image.Height; DGifSetupDecompress(GifFile); /* Reset decompress algorithm parameters. */ return GIF_OK; }
/* reads/writes one int */ void in(int *x, int modus) { if(modus) my_fread(x, 1, sizeof(int), fd); else my_fwrite(x, 1, sizeof(int), fd); }
char *my_read(int fd, int ret) { static int i = 0; static char buff[4097]; if (i == 0 || buff[i] == '\0') { i = 0; if ((ret = read(fd, buff, 4096)) > 0 && !my_str_isprintable(buff)) ret = my_fread(buff, fd); if (ret <= 0) { if (ret) my_warn(NULL, fd); return (NULL); } buff[ret] = '\0'; } ret = i; while (buff[i] && buff[i] != '\n' && buff[i] != ';') jump_inhibitors(buff, &i); if (buff[i]) i = i + 1; return (read_one_line(buff + ret)); }
int convert_fast_messy (FILE *infile, FILE *outfile) { unsigned char *in_bufend; enum file_format infile_type; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); *in_bufend = '\0'; infile_type = get_file_format (in_buffer); switch (infile_type) { case dos: if (verbose) printf("%s: %s is already in MS-DOS format, skipping.\n", program_name,current_file); return (FALSE); break; case tunix: /* drop through */ case apple: /* Wasn't able to speed this up, call old routine */ convert_messy (infile, outfile); return (TRUE); break; case binary: return (FALSE); break; default: fprintf(stderr,"%s: Fatal internal error\n",program_name); exit (EXIT_FAILURE); break; } /* switch */ }
/****************************************************************************** * This routine should be called before any attemp to read an image. * ******************************************************************************/ int DGifGetRecordType(GifFileType *GifFile, GifRecordType *Type) { GifByteType Buf; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; if (!IS_READABLE(Private)) { /* This file was NOT open for reading: */ _GifError = D_GIF_ERR_NOT_READABLE; return GIF_ERROR; } if (my_fread(&Buf, 1, 1, Private) != 1) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } switch (Buf) { case ',': *Type = IMAGE_DESC_RECORD_TYPE; break; case '!': *Type = EXTENSION_RECORD_TYPE; break; case ';': *Type = TERMINATE_RECORD_TYPE; break; default: *Type = UNDEFINED_RECORD_TYPE; _GifError = D_GIF_ERR_WRONG_RECORD; return GIF_ERROR; } return GIF_OK; }
void DAC_Buffer_Process_Stereo_S16bit() { int i, halfBufferSize = dac_intr.bufferSize >> 1; uint32_t *pabuf; uint8_t *outbuf; if(SOUND_DMA_HALF_TRANS_BB){ // Half SOUND_DMA_CLEAR_HALF_TRANS_BB = 1; outbuf = (uint8_t*)dac_intr.buff; pabuf = (uint32_t*)outbuf; } else if(SOUND_DMA_FULL_TRANS_BB) { // Full SOUND_DMA_CLEAR_FULL_TRANS_BB = 1; outbuf = (uint8_t*)&dac_intr.buff[halfBufferSize]; pabuf = (uint32_t*)outbuf; } my_fread(outbuf, 1, halfBufferSize, dac_intr.fp); for(i = 0;i < halfBufferSize >> 2;i += 8){ // signed to unsigned pabuf[0] ^= 0x80008000; pabuf[1] ^= 0x80008000; pabuf[2] ^= 0x80008000; pabuf[3] ^= 0x80008000; pabuf[4] ^= 0x80008000; pabuf[5] ^= 0x80008000; pabuf[6] ^= 0x80008000; pabuf[7] ^= 0x80008000; pabuf += 8; } dac_intr.sound_reads += halfBufferSize; }
/****************************************************************************** * Setup the LZ decompression for this image: * ******************************************************************************/ static int DGifSetupDecompress(GifFileType *GifFile) { int i, BitsPerPixel; GifByteType CodeSize; unsigned int *Prefix; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; my_fread(&CodeSize, 1, 1, Private); /* Read Code size from file. */ BitsPerPixel = CodeSize; Private->Buf[0] = 0; /* Input Buffer empty. */ Private->BitsPerPixel = BitsPerPixel; Private->ClearCode = (1 << BitsPerPixel); Private->EOFCode = Private->ClearCode + 1; Private->RunningCode = Private->EOFCode + 1; Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */ Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */ Private->StackPtr = 0; /* No pixels on the pixel stack. */ Private->LastCode = NO_SUCH_CODE; Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */ Private->CrntShiftDWord = 0; Prefix = Private->Prefix; for (i = 0; i <= LZ_MAX_CODE; i++) Prefix[i] = NO_SUCH_CODE; return GIF_OK; }
/* reads/writes n bytes */ void byten(void *x, int n, int modus) { if(modus) my_fread(x, n, 1, fd); else my_fwrite(x, n, 1, fd); }
/****************************************************************************** * This routine should be called before any other DGif calls. Note that * * this routine is called automatically from DGif file open routines. * ******************************************************************************/ int DGifGetScreenDesc(GifFileType *GifFile) { int i, BitsPerPixel; GifByteType Buf[3]; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; if (!IS_READABLE(Private)) { /* This file was NOT open for reading: */ _GifError = D_GIF_ERR_NOT_READABLE; return GIF_ERROR; } /* Put the screen descriptor into the file: */ if (DGifGetWord(Private, &GifFile->SWidth) == GIF_ERROR || DGifGetWord(Private, &GifFile->SHeight) == GIF_ERROR) return GIF_ERROR; if (my_fread(Buf, 1, 3, Private) != 3) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1; BitsPerPixel = (Buf[0] & 0x07) + 1; GifFile->SBackGroundColor = Buf[1]; if (Buf[0] & 0x80) { /* Do we have global color map? */ GifFile->SColorMap = MakeMapObject(1 << BitsPerPixel, NULL); /* Get the global color map: */ for (i = 0; i < GifFile->SColorMap->ColorCount; i++) { if (my_fread(Buf, 1, 3, Private) != 3) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } GifFile->SColorMap->Colors[i].Red = Buf[0]; GifFile->SColorMap->Colors[i].Green = Buf[1]; GifFile->SColorMap->Colors[i].Blue = Buf[2]; } } return GIF_OK; }
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s) { int halfBufferSize = dac_intr.bufferSize >> 1; uint8_t *outbuf; outbuf = (uint8_t*)&dac_intr.buff[halfBufferSize]; my_fread(outbuf, 1, halfBufferSize, dac_intr.fp); dac_intr.sound_reads += halfBufferSize; }
static int read_magic_number(FILE* f) { int magic; if (!seek_after_header(f, magic_name)) { if (!PCU_Comm_Self()) fprintf(stderr,"warning: not swapping bytes\n"); rewind(f); return 0; } my_fread(&magic, sizeof(int), 1, f); return magic != MAGIC; }
int find_files(char *fname) { FILE *fd; char buf[200], buf1[200]; int dummy; sprintf(buf, "%s.%d", fname, 0); sprintf(buf1, "%s", fname); if((fd = fopen(buf, "r"))) { my_fread(&dummy, sizeof(dummy), 1, fd); my_fread(&header, sizeof(header), 1, fd); my_fread(&dummy, sizeof(dummy), 1, fd); fclose(fd); return header.num_files; } if((fd = fopen(buf1, "r"))) { my_fread(&dummy, sizeof(dummy), 1, fd); my_fread(&header, sizeof(header), 1, fd); my_fread(&dummy, sizeof(dummy), 1, fd); fclose(fd); header.num_files = 1; return header.num_files; } FatalError(121); return 0; }
void DAC_Buffer_Process_Stereo_S16bit_PhotoFrame() { int i, halfBufferSize = dac_intr.bufferSize >> 1; uint32_t *pabuf; uint8_t *outbuf; if(SOUND_DMA_HALF_TRANS_BB){ // Half SOUND_DMA_CLEAR_HALF_TRANS_BB = 1; outbuf = (uint8_t*)dac_intr.buff; pabuf = (uint32_t*)outbuf; } else if(SOUND_DMA_FULL_TRANS_BB) { // Full SOUND_DMA_CLEAR_FULL_TRANS_BB = 1; outbuf = (uint8_t*)&dac_intr.buff[halfBufferSize]; pabuf = (uint32_t*)outbuf; } my_fread(outbuf, 1, halfBufferSize, dac_intr.fp); for(i = 0;i < halfBufferSize >> 2;i += 8){ // signed to unsigned pabuf[0] ^= 0x80008000; pabuf[1] ^= 0x80008000; pabuf[2] ^= 0x80008000; pabuf[3] ^= 0x80008000; pabuf[4] ^= 0x80008000; pabuf[5] ^= 0x80008000; pabuf[6] ^= 0x80008000; pabuf[7] ^= 0x80008000; pabuf += 8; } dac_intr.sound_reads += halfBufferSize; if(dac_intr.sound_reads > (halfBufferSize * 10)){ AUDIO_OUT_ENABLE; } if(dac_intr.sound_reads >= dac_intr.contentSize){ NVIC_InitTypeDef NVIC_InitStructure; AUDIO_OUT_SHUTDOWN; DMA_ITConfig(DMA1_Stream1, DMA_IT_TC | DMA_IT_HT, DISABLE); DMA_Cmd(DMA1_Stream1, DISABLE); /* Disable DMA1_Stream1 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; NVIC_Init(&NVIC_InitStructure); my_fclose(dac_intr.fp); dac_intr.fp = '\0'; dac_intr.comp = 1; } }
/****************************************************************************** * Get a following extension block (see GIF manual) from gif file. This * * routine sould be called until NULL Extension is returned. * * The Extension should NOT be freed by the user (not dynamically allocated).* ******************************************************************************/ int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **Extension) { GifByteType Buf; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; if (my_fread(&Buf, 1, 1, Private) != 1) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } if (Buf > 0) { *Extension = Private->Buf; /* Use private unused buffer. */ (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */ if (my_fread(&((*Extension)[1]), 1, Buf, Private) != Buf) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } } else *Extension = NULL; return GIF_OK; }
int my_fgeti(t_fle *file) { int i; int j; char buff[12]; i = 0; if (file == NULL) return (0); while ((j = my_fread(file, buff + i, 1)) > 0 && my_isnum(buff[i]) && i++ < 11) buff[i + j] = '\0'; return (my_atoi(buff)); }
void my_dispfile(char *path) { t_fle *file; char buff[4097]; int i; if (path == NULL || (file = my_fopen(path, O_RDONLY)) == NULL) return; while ((i = my_fread(file, buff, 10)) > 0) { buff[i] = '\0'; my_printf("%s", buff); } }
int * bgc_read_grouplist(FILE *fp, const OUTPUT_HEADER hdr) { int pad; int *nParticlesPerGroup; /* int tmp; */ //nParticlesPerGroup = calloc(hdr.ngroups+1, sizeof(int)); nParticlesPerGroup = calloc(hdr.ngroups, sizeof(int)); assert(nParticlesPerGroup != NULL); my_fread(&pad,sizeof(int),1,fp); assert(pad==sizeof(int)*hdr.ngroups); my_fread(nParticlesPerGroup,sizeof(int),hdr.ngroups,fp); /* for(i=0; i < hdr.ngroups; i++) { */ /* fread(&tmp,sizeof(int),1,fp); */ /* nParticlesPerGroup[i] = tmp; */ /* if(BGC_VERBOSE) */ /* printf(" grp %4d: %5d\n", (i+hdr.first_group_id), tmp); */ /* } */ my_fread(&pad,sizeof(int),1,fp); assert(pad==sizeof(int)*hdr.ngroups); return nParticlesPerGroup; }
void read_and_compare (int fd, char *read_buf, char *mmap_buf, unsigned long len) { /* Read the file and compare with mmap_buf[] */ if (my_fread (fd, read_buf, len) != len) { fprintf (stderr, "read problem: %s\n", strerror (errno)); exit (1); } if (memcmp (read_buf, mmap_buf, len) != 0) { fprintf (stderr, "buffer miscompare\n"); exit (1); } }
void DAC_Buffer_Process_Mono_U8bit() { int halfBufferSize = dac_intr.bufferSize >> 1; uint8_t *outbuf; if(SOUND_DMA_HALF_TRANS_BB){ // Half SOUND_DMA_CLEAR_HALF_TRANS_BB = 1; outbuf = (uint8_t*)dac_intr.buff; } else if(SOUND_DMA_FULL_TRANS_BB) { // Full SOUND_DMA_CLEAR_FULL_TRANS_BB = 1; outbuf = (uint8_t*)&dac_intr.buff[halfBufferSize]; } my_fread(outbuf, 1, halfBufferSize, dac_intr.fp); dac_intr.sound_reads += halfBufferSize; }
void ph_read_field(FILE* f, const char* field, double** data, int* nodes, int* vars, int* step) { long bytes, n; char header[PH_LINE]; int should_swap; int ok; should_swap = read_magic_number(f); ok = find_header(f, field, header); assert(ok); parse_params(header, &bytes, nodes, vars, step); assert(((bytes - 1) % sizeof(double)) == 0); n = (bytes - 1) / sizeof(double); assert((int)n == (*nodes) * (*vars)); *data = malloc(bytes); my_fread(*data, sizeof(double), n, f); if (should_swap) pcu_swap_doubles(*data, n); }
/****************************************************************************** * Get an extension block (see GIF manual) from gif file. This routine only * * returns the first data block, and DGifGetExtensionNext shouldbe called * * after this one until NULL extension is returned. * * The Extension should NOT be freed by the user (not dynamically allocated).* * Note it is assumed the Extension desc. header ('!') has been read. * ******************************************************************************/ int DGifGetExtension(GifFileType *GifFile, int *ExtCode, GifByteType **Extension) { GifByteType Buf; GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private; if (!IS_READABLE(Private)) { /* This file was NOT open for reading: */ _GifError = D_GIF_ERR_NOT_READABLE; return GIF_ERROR; } if (my_fread(&Buf, 1, 1, Private) != 1) { _GifError = D_GIF_ERR_READ_FAILED; return GIF_ERROR; } *ExtCode = Buf; return DGifGetExtensionNext(GifFile, Extension); }
void do_log(const char *logfile) { FILE *f; uchar buf[4096]; int size; if (!logfile) do_usage(); f= my_fopen(logfile, O_WRONLY|O_APPEND|O_CREAT, MYF(MY_WME)); if (!f) my_exit(1); while ((size= my_fread(stdin, buf, sizeof(buf), MYF(MY_WME))) > 0) if ((int)my_fwrite(f, buf, size, MYF(MY_WME)) != size) my_exit(1); my_fclose(f, MYF(0)); my_exit(0); }
void read_gadget(char *prefix,int input,ulint *npart, gad_header *head_out,int interp_mode) { int nfils=check_num_files(prefix); if(nfils<=0) report_error(1,"Can't find snapshot files\n"); else { lint ii; char fname[256]; gad_header head; int block1,block2; FILE *snap; float norm_vel; float agrid=Lbox/Ngrid; float x_left_proper,x_right_proper,x_left_bleft,x_right_bleft,x_left_bright,x_right_bright; float x_min=0,x_max=Lbox,x_shift_bright=0,x_shift_bleft=0; x_left_proper=Ix0_here*agrid; x_right_proper=(Ix0_here+Nx_here)*agrid; x_left_bleft=2*Lbox; x_left_bright=2*Lbox; x_right_bleft=-2*Lbox; x_right_bright=-2*Lbox; if(interp_mode==0) {//NGP x_min=-agrid/2; x_max=Lbox-agrid/2; x_left_proper=(Ix0_here-0.5f)*agrid; x_right_proper=(Ix0_here+Nx_here-0.5f)*agrid; } else if(interp_mode==1) {//CIC x_min=0; x_max=Lbox; x_left_proper=Ix0_here*agrid; x_right_proper=(Ix0_here+Nx_here)*agrid; x_left_bleft=x_left_proper-agrid; x_right_bleft=x_left_proper; if(NNodes>1 && NodeThis==0) { x_left_bleft=(Ngrid-1)*agrid; x_right_bleft=Lbox; x_shift_bleft=-Lbox; } } else if(interp_mode==2) {//TSC x_min=-agrid/2; x_max=Lbox-agrid/2; x_left_proper=(Ix0_here-0.5f)*agrid; x_right_proper=(Ix0_here+Nx_here-0.5f)*agrid; x_left_bleft=x_left_proper-agrid; x_left_bright=x_left_proper; x_right_bleft=x_right_proper; x_right_bright=x_right_proper+agrid; if(NNodes>1 && NodeThis==0) { x_left_bleft=(Ngrid-1.5f)*agrid; x_right_bleft=(Ngrid-0.5f)*agrid; x_shift_bleft=-Lbox; } if(NNodes>1 && NodeThis==NNodes-1) { x_left_bright=-0.5f*agrid; x_right_bright=0.5f*agrid; x_shift_bright=Lbox; } } else report_error(1,"Wrong interpolation scheme %d\n",interp_mode); if(NodeThis==0) printf(" Reading %d snapshot files \n",nfils); if(nfils>1) sprintf(fname,"%s.0",prefix); else sprintf(fname,"%s",prefix); snap=my_fopen(fname,"r"); //Read header if(input==2) gad_seek_block(snap,"HEAD"); my_fread(&block1,sizeof(int),1,snap); my_fread(&head,sizeof(gad_header),1,snap); my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); if(head.num_files!=nfils) { report_error(1, "Header and existing files do not match %d != %d.\n" " There may be some files missing\n", nfils,head.num_files); } *head_out=head; for(ii=0;ii<6;ii++) { (*head_out).npart[ii]=head.npartTotal[ii]; (*head_out).num_files=1; } norm_vel=(float)(1./sqrt(head.time)); fclose(snap); ulint np_read=0; ulint np_saved=0; for(ii=0;ii<nfils;ii++) { gad_header head_dum; ulint np_new,np_got_here=0,np_got_here_b; lint jj; if(nfils>1) sprintf(fname,"%s.%d",prefix,(int)ii); else sprintf(fname,"%s",prefix); snap=my_fopen(fname,"r"); #ifdef _DEBUG printf(" Node %d, reading file %s \n",NodeThis,fname); #endif //_DEBUG //Read header if(input==2) gad_seek_block(snap,"HEAD"); my_fread(&block1,sizeof(int),1,snap); my_fread(&head_dum,sizeof(gad_header),1,snap); my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); np_new=0; for(jj=0;jj<6;jj++) np_new+=head_dum.npart[jj]; #ifdef _DEBUG printf(" Node %d : %ld parts in file %ld \n",NodeThis,(long)np_new,(long)ii); #endif //_DEBUG if(np_read+np_new>Npart_total) { report_error(1, "files seem to contain too many particles\n" " file %s, %ld > %ld \n", fname,(long)(np_read+np_new),(long)(Npart_total)); } if(input==2) gad_seek_block(snap,"POS"); my_fread(&block1,sizeof(int),1,snap); for(jj=0;jj<np_new;jj++) { int ax; int include_part=0; float *p=&(Pos[3*np_saved]); my_fread(p,sizeof(float),3,snap); for(ax=0;ax<3;ax++) { if(p[ax]>=x_max) p[ax]-=Lbox; else if(p[ax]<x_min) p[ax]+=Lbox; } if(p[0]>=x_left_proper && p[0]<x_right_proper) include_part=1; else if(p[0]>=x_left_bleft && p[0]<x_right_bleft) { include_part=1; p[0]+=x_shift_bleft; } else if(p[0]>=x_left_bright && p[0]<x_right_bright) { include_part=1; p[0]+=x_shift_bright; } if(include_part) { Ids[np_saved]=jj; np_saved++; np_got_here++; if(np_saved>=Npart_alloc) report_error(1,"Too many particles, increase buffer size %llu %llu\n",np_saved,Npart_alloc); } } my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); np_read+=np_new; if(np_got_here==0) continue; np_got_here_b=0; if(input==2) gad_seek_block(snap,"VEL"); my_fread(&block1,sizeof(int),1,snap); for(jj=0;jj<np_new;jj++) { float v[3]; lint new_index=np_saved-np_got_here+np_got_here_b; my_fread(v,sizeof(float),3,snap); if(Ids[new_index]==jj) { int ax; np_got_here_b++; for(ax=0;ax<3;ax++) Vel[3*new_index+ax]=norm_vel*v[ax]; } } if(np_got_here_b!=np_got_here) report_error(1,"This shouldn't have happened\n"); my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); np_got_here_b=0; if(input==2) gad_seek_block(snap,"ID"); my_fread(&block1,sizeof(int),1,snap); for(jj=0;jj<np_new;jj++) { ulint id; lint new_index=np_saved-np_got_here+np_got_here_b; my_fread(&id,sizeof(ulint),1,snap); if(Ids[new_index]==jj) { np_got_here_b++; Ids[new_index]=id; } } if(np_got_here_b!=np_got_here) report_error(1,"This shouldn't have happened\n"); my_fread(&block2,sizeof(int),1,snap); gad_check_block(block1,block2); fclose(snap); } if(np_read!=Npart_total) { report_error(1, "#particles read disagrees with header: %ld != %ld\n", (long)np_read,(long)(Npart_total)); } *npart=np_saved; } }
/*! This function reads a snapshot file and distributes the data it contains * to tasks 'readTask' to 'lastTask'. */ void read_file(char *fname, int readTask, int lastTask) { int blockmaxlen; int i, n_in_file, n_for_this_task, ntask, pc, offset = 0, task; int blksize1, blksize2; MPI_Status status; FILE *fd = 0; int nall; int type; char label[4]; int nstart, bytes_per_blockelement, npart, nextblock, typelist[6]; enum iofields blocknr; #ifdef HAVE_HDF5 char buf[500]; int rank, pcsum; hid_t hdf5_file, hdf5_grp[6], hdf5_dataspace_in_file; hid_t hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataset; hsize_t dims[2], count[2], start[2]; #endif #define SKIP {my_fread(&blksize1,sizeof(int),1,fd);} #define SKIP2 {my_fread(&blksize2,sizeof(int),1,fd);} if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) { if(!(fd = fopen(fname, "r"))) { printf("can't open file `%s' for reading initial conditions.\n", fname); endrun(123); } if(All.ICFormat == 2) { SKIP; my_fread(&label, sizeof(char), 4, fd); my_fread(&nextblock, sizeof(int), 1, fd); printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3], nextblock); SKIP2; } SKIP; my_fread(&header, sizeof(header), 1, fd); SKIP2; if(blksize1 != 256 || blksize2 != 256) { printf("incorrect header format\n"); fflush(stdout); endrun(890); } } #ifdef HAVE_HDF5 if(All.ICFormat == 3) { read_header_attributes_in_hdf5(fname); hdf5_file = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT); for(type = 0; type < 6; type++) { if(header.npart[type] > 0) { sprintf(buf, "/PartType%d", type); hdf5_grp[type] = H5Gopen(hdf5_file, buf); } } } #endif for(task = readTask + 1; task <= lastTask; task++) MPI_Ssend(&header, sizeof(header), MPI_BYTE, task, TAG_HEADER, MPI_COMM_WORLD); } else MPI_Recv(&header, sizeof(header), MPI_BYTE, readTask, TAG_HEADER, MPI_COMM_WORLD, &status); if(All.TotNumPart == 0) { if(header.num_files <= 1) for(i = 0; i < 6; i++) header.npartTotal[i] = header.npart[i]; All.TotN_gas = header.npartTotal[0] + (((long long) header.npartTotalHighWord[0]) << 32); for(i = 0, All.TotNumPart = 0; i < 6; i++) { All.TotNumPart += header.npartTotal[i]; All.TotNumPart += (((long long) header.npartTotalHighWord[i]) << 32); } for(i = 0; i < 6; i++) All.MassTable[i] = header.mass[i]; All.MaxPart = All.PartAllocFactor * (All.TotNumPart / NTask); /* sets the maximum number of particles that may */ All.MaxPartSph = All.PartAllocFactor * (All.TotN_gas / NTask); /* sets the maximum number of particles that may reside on a processor */ allocate_memory(); if(RestartFlag == 2) All.Time = All.TimeBegin = header.time; } if(ThisTask == readTask) { for(i = 0, n_in_file = 0; i < 6; i++) n_in_file += header.npart[i]; printf("\nreading file `%s' on task=%d (contains %d particles.)\n" "distributing this file to tasks %d-%d\n" "Type 0 (gas): %8d (tot=%6d%09d) masstab=%g\n" "Type 1 (halo): %8d (tot=%6d%09d) masstab=%g\n" "Type 2 (disk): %8d (tot=%6d%09d) masstab=%g\n" "Type 3 (bulge): %8d (tot=%6d%09d) masstab=%g\n" "Type 4 (stars): %8d (tot=%6d%09d) masstab=%g\n" "Type 5 (bndry): %8d (tot=%6d%09d) masstab=%g\n\n", fname, ThisTask, n_in_file, readTask, lastTask, header.npart[0], (int) (header.npartTotal[0] / 1000000000), (int) (header.npartTotal[0] % 1000000000), All.MassTable[0], header.npart[1], (int) (header.npartTotal[1] / 1000000000), (int) (header.npartTotal[1] % 1000000000), All.MassTable[1], header.npart[2], (int) (header.npartTotal[2] / 1000000000), (int) (header.npartTotal[2] % 1000000000), All.MassTable[2], header.npart[3], (int) (header.npartTotal[3] / 1000000000), (int) (header.npartTotal[3] % 1000000000), All.MassTable[3], header.npart[4], (int) (header.npartTotal[4] / 1000000000), (int) (header.npartTotal[4] % 1000000000), All.MassTable[4], header.npart[5], (int) (header.npartTotal[5] / 1000000000), (int) (header.npartTotal[5] % 1000000000), All.MassTable[5]); fflush(stdout); } ntask = lastTask - readTask + 1; /* to collect the gas particles all at the beginning (in case several snapshot files are read on the current CPU) we move the collisionless particles such that a gap of the right size is created */ for(type = 0, nall = 0; type < 6; type++) { n_in_file = header.npart[type]; n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; nall += n_for_this_task; } memmove(&P[N_gas + nall], &P[N_gas], (NumPart - N_gas) * sizeof(struct particle_data)); nstart = N_gas; for(blocknr = 0; blocknr < IO_NBLOCKS; blocknr++) { if(blockpresent(blocknr)) { if(RestartFlag == 0 && blocknr > IO_U) continue; /* ignore all other blocks in initial conditions */ bytes_per_blockelement = get_bytes_per_blockelement(blocknr); blockmaxlen = ((int) (All.BufferSize * 1024 * 1024)) / bytes_per_blockelement; npart = get_particles_in_block(blocknr, &typelist[0]); if(npart > 0) { if(ThisTask == readTask) { if(All.ICFormat == 2) { SKIP; my_fread(&label, sizeof(char), 4, fd); my_fread(&nextblock, sizeof(int), 1, fd); printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3], nextblock); SKIP2; if(strncmp(label, Tab_IO_Labels[blocknr], 4) != 0) { printf("incorrect block-structure!\n"); printf("expected '%c%c%c%c' but found '%c%c%c%c'\n", label[0], label[1], label[2], label[3], Tab_IO_Labels[blocknr][0], Tab_IO_Labels[blocknr][1], Tab_IO_Labels[blocknr][2], Tab_IO_Labels[blocknr][3]); fflush(stdout); endrun(1890); } } if(All.ICFormat == 1 || All.ICFormat == 2) SKIP; } for(type = 0, offset = 0; type < 6; type++) { n_in_file = header.npart[type]; #ifdef HAVE_HDF5 pcsum = 0; #endif if(typelist[type] == 0) { n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; offset += n_for_this_task; } else { for(task = readTask; task <= lastTask; task++) { n_for_this_task = n_in_file / ntask; if((task - readTask) < (n_in_file % ntask)) n_for_this_task++; if(task == ThisTask) if(NumPart + n_for_this_task > All.MaxPart) { printf("too many particles\n"); endrun(1313); } do { pc = n_for_this_task; if(pc > blockmaxlen) pc = blockmaxlen; if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) my_fread(CommBuffer, bytes_per_blockelement, pc, fd); #ifdef HAVE_HDF5 if(All.ICFormat == 3) { get_dataset_name(blocknr, buf); hdf5_dataset = H5Dopen(hdf5_grp[type], buf); dims[0] = header.npart[type]; dims[1] = get_values_per_blockelement(blocknr); if(dims[1] == 1) rank = 1; else rank = 2; hdf5_dataspace_in_file = H5Screate_simple(rank, dims, NULL); dims[0] = pc; hdf5_dataspace_in_memory = H5Screate_simple(rank, dims, NULL); start[0] = pcsum; start[1] = 0; count[0] = pc; count[1] = get_values_per_blockelement(blocknr); pcsum += pc; H5Sselect_hyperslab(hdf5_dataspace_in_file, H5S_SELECT_SET, start, NULL, count, NULL); switch (get_datatype_in_block(blocknr)) { case 0: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT); break; case 1: hdf5_datatype = H5Tcopy(H5T_NATIVE_FLOAT); break; case 2: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT64); break; } H5Dread(hdf5_dataset, hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataspace_in_file, H5P_DEFAULT, CommBuffer); H5Tclose(hdf5_datatype); H5Sclose(hdf5_dataspace_in_memory); H5Sclose(hdf5_dataspace_in_file); H5Dclose(hdf5_dataset); } #endif } if(ThisTask == readTask && task != readTask) MPI_Ssend(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, task, TAG_PDATA, MPI_COMM_WORLD); if(ThisTask != readTask && task == ThisTask) MPI_Recv(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, readTask, TAG_PDATA, MPI_COMM_WORLD, &status); if(ThisTask == task) { empty_read_buffer(blocknr, nstart + offset, pc, type); offset += pc; } n_for_this_task -= pc; } while(n_for_this_task > 0); } } } if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) { SKIP2; if(blksize1 != blksize2) { printf("incorrect block-sizes detected!\n"); printf("Task=%d blocknr=%d blksize1=%d blksize2=%d\n", ThisTask, blocknr, blksize1, blksize2); fflush(stdout); endrun(1889); } } } } } } for(type = 0; type < 6; type++) { n_in_file = header.npart[type]; n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; NumPart += n_for_this_task; if(type == 0) N_gas += n_for_this_task; } if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) fclose(fd); #ifdef HAVE_HDF5 if(All.ICFormat == 3) { for(type = 5; type >= 0; type--) if(header.npart[type] > 0) H5Gclose(hdf5_grp[type]); H5Fclose(hdf5_file); } #endif } }
int convert_fast_tunix (FILE *infile, FILE *outfile) { unsigned char a; unsigned char *in_bufpos; unsigned char *in_bufend; size_t file_remain; enum file_format infile_type; in_bufpos = in_buffer; (void) fseek(infile,0L,SEEK_END); file_remain = ftell(infile); rewind(infile); in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); *in_bufend = '\0'; infile_type = get_file_format (in_buffer); switch (infile_type) { case tunix: if (verbose) printf("%s: %s is already in Unix format, skipping.\n", program_name,current_file); return (FALSE); break; case apple: /* Replace "in-vitro", so out_buffer isn't used */ while (file_remain != 0) { a = *in_bufpos; if (a == CR) *in_bufpos++ = LF; else if (a == '\0'){ /* End of buffer reached */ /* Write changed buffer out */ my_fwrite(in_buffer,outfile,(int)(in_bufend - in_buffer)); /* And reload */ file_remain -= in_bufend - in_buffer; in_bufend = in_buffer + my_fread(infile, BUFFERSIZE); *in_bufend = '\0'; in_bufpos = in_buffer; } else in_bufpos++; } return (TRUE); break; case dos: /* Couldn't speed it up, so use old routine */ convert_tunix (infile, outfile); return (TRUE); break; case binary: return (FALSE); break; default: fprintf(stderr,"%s: Fatal internal error\n", program_name); exit (EXIT_FAILURE); break; } /* switch */ }