void
get_next_file_block( char *datafile, unsigned char *block, int *length )
{
	static FILE *fp;
	static int first = 1;

	if( first )
	{
		fp = fopen( datafile, "r" );
		first = 0;
	}

	fread( block, 5, sizeof( char ), fp );
	while( ! match_end( block ) )
	{
		memmove( block, block + 1, 4 );
		fread( &block[4], 1, sizeof( char ), fp );
	}
	*length = 5;

	fread( &block[++(*length) - 1], 1, sizeof( char ), fp );
	while( ! match_end( block + *length - 4 - 1 ) )
	{
		fread( &block[++(*length) - 1], 1, sizeof( char ), fp );
	}
	fseek( fp, -5, SEEK_CUR );
	*length -= 4;
}
Example #2
0
int nexthashrec(FILE * fp, struct hashdb_rec *rec)
{

	int recordlength, wordlen;

	if (fread(&rec->rec_size, sizeof(rec->rec_size), 1, fp) != 1) {
	
		perror("fread");
		return -1;
	}

	recordlength = abs(rec->rec_size);
	wordlen = recordlength - (sizeof(rec->pmk) + sizeof(rec->rec_size));

	if (wordlen > 63 || wordlen < 8) {
		fprintf(stderr, "Invalid word length: %d\n", wordlen);
		return -1;
	}

	/* hackity, hack, hack, hack */
	rec->word = password_buf;

	if (fread(rec->word, wordlen, 1, fp) != 1) {
		perror("fread");
		return -1;
	}

	if (fread(rec->pmk, sizeof(rec->pmk), 1, fp) != 1) {
		perror("fread");
		return -1;
	}

	return recordlength;
}
Example #3
0
void CLoad3DS::ReadVertexIndices(t3DObject *pObject, tChunk *pPreviousChunk)
{
	unsigned short index = 0;					// This is used to read in the current face index

	// In order to read in the vertex indices for the object, we need to first
	// read in the number of them, then read them in.  Remember,
	// we only want 3 of the 4 values read in for each face.  The fourth is
	// a visibility flag for 3D Studio Max that doesn't mean anything to us.

	// Read in the number of faces that are in this object (int)
	pPreviousChunk->bytesRead += fread(&pObject->numOfFaces, 1, 2, m_FilePointer);

	// Alloc enough memory for the faces and initialize the structure
	pObject->pFaces = new tFace [pObject->numOfFaces];
	memset(pObject->pFaces, 0, sizeof(tFace) * pObject->numOfFaces);

	// Go through all of the faces in this object
	for(int i = 0; i < pObject->numOfFaces; i++)
	{
		// Next, we read in the A then B then C index for the face, but ignore the 4th value.
		// The fourth value is a visibility flag for 3D Studio Max, we don't care about this.
		for(int j = 0; j < 4; j++)
		{
			// Read the first vertice index for the current face 
			pPreviousChunk->bytesRead += fread(&index, 1, sizeof(index), m_FilePointer);

			if(j < 3)
			{
				// Store the index in our face structure.
				pObject->pFaces[i].vertIndex[j] = index;
			}
		}
	}
}
Example #4
0
int
palconv(char *palfile)
{
    uint8       palspace[1024], reds[256], greens[256], blues[256];
    uint8      *p;
    FILE       *fp;
    int         j, ret;

    fp = fopen(palfile, "rb");
    if (fp == NULL)
      {
          printf(" Error opening palette file %s\n", palfile);
          exit(1);
      }
    fread(reds, 1, 256, fp);
    fread(greens, 1, 256, fp);
    fread(blues, 1, 256, fp);
    fclose(fp);

    p = palspace;
    for (j = 0; j < 256; j++)
      {
          *p++ = reds[j];
          *p++ = greens[j];
          *p++ = blues[j];
      }

    ret = DFR8setpalette(palspace);
    if (ret < 0)
      {
          printf(" Error: %d, in writing palette %s\n", ret, palfile);
          exit(1);
      }
    return (0);
}
Example #5
0
File: FaCE.cpp Project: CCJY/ACE
void LoadParameterHistory()
{
    FILE* parameterFile = _wfopen(g_ParameterFileName, ACE_TEXT("r"));

    if (parameterFile != 0) {
        while (feof(parameterFile) == 0) {
            // Note: Remember that fwprintf takes wide-character format specifier but
            // save string as ASCII.  Thus, history must be read as ASCII then converted
            // to wide-character (Unicode on WinCE).
            char singleParameter[MAX_COMMAND_LINE];
            int size = 0;
            fread(&singleParameter[size], sizeof(char), 1, parameterFile);

            // WinCE does not have function that reads upto the end of line.
            while (singleParameter[size] != '\n') {
                fread(&singleParameter[++size], sizeof(char), 1, parameterFile);
            }

            if (size > 0) {
                singleParameter[size] = 0;  // NULL terminator
                g_Parameter.addParameter(singleParameter);
            }
        }
        fclose(parameterFile);
    }
}
static
void fft_file_real(FILE * fin, FILE * fout, int nfft, int isinverse)
{
    kiss_fftr_cfg st;
    kiss_fft_scalar * rbuf;
    kiss_fft_cpx * cbuf;

    rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft);
    cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft / 2 + 1));
    st = kiss_fftr_alloc(nfft , isinverse , 0, 0);

    if (isinverse == 0) {
        while (fread(rbuf , sizeof(kiss_fft_scalar) * nfft , 1, fin) > 0) {
            kiss_fftr(st , rbuf , cbuf);
            fwrite(cbuf , sizeof(kiss_fft_cpx) , (nfft / 2 + 1) , fout);
        }
    } else {
        while (fread(cbuf , sizeof(kiss_fft_cpx) * (nfft / 2 + 1) , 1, fin) > 0) {
            kiss_fftri(st , cbuf , rbuf);
            fwrite(rbuf , sizeof(kiss_fft_scalar) , nfft , fout);
        }
    }
    free(st);
    free(rbuf);
    free(cbuf);
}
Example #7
0
void NetworkManager::loadMD5()
{
	md5_.clear();

	FILE* fis = fopen(md5filename_.c_str(), "rb");

	if (fis == NULL)
		return;

	int nfiles;
	fread(&nfiles, sizeof(int), 1, fis);

	for (int i = 0; i < nfiles; ++i)
	{
		int strlen;
		fread(&strlen, sizeof(int), 1, fis);

		char* buffer = (char*)malloc(strlen);
		fread(buffer, 1, strlen, fis);
		std::string filename(buffer, strlen);
		free(buffer);

		std::vector<unsigned char> md5(16);
		fread(&md5[0], 1, 16, fis);

		md5_[filename] = md5;
	}
}
Example #8
0
int main(int argc, char *argv[]) {
        int num_vert, t;
        unsigned char *a, *b;

	if(argc != 3) {
		printf("Usage: dupe [file] [vertex number]\n");
		return 1;
	}

	FILE *fp = fopen(argv[1], "r");
	num_vert = atoi(argv[2]);
        a = (unsigned char *)malloc(num_vert);
        b = (unsigned char *)malloc(num_vert);
        t = fread(a, num_vert, 1, fp);

        while(1) {
                t = fread(b, num_vert, 1, fp);
                if(!t)
                        break;
                if(!memcmp(a,b,num_vert)) {
                        printf("FOUND DUPE!\n");
                        return 0;
                }

                memcpy(a,b,num_vert);
        }

        fclose(fp);

        return 0;
}
Example #9
0
mrb_irep*
mrb_read_irep_file(mrb_state *mrb, FILE* fp)
{
  mrb_irep *irep = NULL;
  uint8_t *buf;
  const size_t header_size = sizeof(struct rite_binary_header);
  size_t buf_size = 0;
  uint8_t flags = 0;
  int result;

  if ((mrb == NULL) || (fp == NULL)) {
    return NULL;
  }

  buf = (uint8_t*)mrb_malloc(mrb, header_size);
  if (fread(buf, header_size, 1, fp) == 0) {
    goto irep_exit;
  }
  result = read_binary_header(buf, &buf_size, NULL, &flags);
  if (result != MRB_DUMP_OK || buf_size <= header_size) {
    goto irep_exit;
  }

  buf = (uint8_t*)mrb_realloc(mrb, buf, buf_size);
  if (fread(buf+header_size, buf_size-header_size, 1, fp) == 0) {
    goto irep_exit;
  }
  irep = read_irep(mrb, buf, FLAG_SRC_MALLOC);

irep_exit:
  mrb_free(mrb, buf);
  return irep;
}
Example #10
0
void Arecord_remove(FILE *fp, char *hostname)
{
	char *tmpfname = tempnam(NULL, "A_DB_");
	FILE *tmpf = fopen(tmpfname, "wb");
	char buf[256];
	struct diskArecord rec;
	long int oldpos = ftell(fp);
	fseek(fp, 0L, SEEK_SET);
	while (fread(&rec, sizeof(struct diskArecord), 1, fp))
	{
		fread(buf, rec.hostname, 1, fp);
		buf[rec.hostname] = 0; //null terminate it
		if (strcmp(buf, hostname))
		{
			fwrite(&rec, sizeof(struct diskArecord), 1, tmpf);
			fwrite(buf, rec.hostname, 1, tmpf);
		}
	}
	fflush(tmpf);
	freopen(NULL, "rb", tmpf);
	freopen(NULL, "wb", fp);
	while (fread(buf, 1, 1, tmpf)) fwrite(buf, 1, 1, fp);
	fflush(fp);
	fclose(tmpf);
	unlink(tmpfname);
	freopen(NULL, "rb", fp);
	fseek(fp, oldpos, SEEK_SET);
}
/* Read planar YUV frames with 4:2:0 chroma sub-sampling */
static int read_yuv(FILE *file, struct segment_yuv image) {
	size_t len = 0;

	/* Read Y. The size of Y is the same as the size of the image. The indices
	 represents the color component (0 is Y, 1 is U, and 2 is V) */
	len += fread((void*) image.Y, 1, width * height, file);

	/* Read U. Given 4:2:0 chroma sub-sampling, the size is 1/4 of Y
	 because (height/2)*(width/2) = (height*width)/4. */
	len += fread((void*) image.U, 1, (width * height) / 4, file);

	/* Read V. Given 4:2:0 chroma sub-sampling, the size is 1/4 of Y. */
	len += fread((void*) image.V, 1, (width * height) / 4, file);

	if (ferror(file)) {
		perror("ferror");
		exit(EXIT_FAILURE);
	}

	if (feof(file)) {
		return 0;
	} else if (len != width * height * 1.5) {
		fprintf(stderr, "Reached end of file, but incorrect bytes read.\n");
		fprintf(stderr, "Wrong input? (height: %d width: %d)\n", height, width);

		return 0;
	}

	return len;
}
Example #12
0
int ross_crt_attach(FILE *fd, BYTE *rawcart)
{
    BYTE chipheader[0x10];
    int amount=0;

    while (1) {
        if (fread(chipheader, 0x10, 1, fd) < 1) {
            break;
        }

        amount++;

        if (chipheader[0xc] != 0x80 && chipheader[0xe] != 0x40 && chipheader[0xb] > 1) {
            return -1;
        }

        if (fread(&rawcart[chipheader[0xb] << 14], 0x4000, 1, fd) < 1) {
            return -1;
        }
    }

    if (amount == 1) {
        memcpy(&rawcart[0x4000], &rawcart[0x0000], 0x4000);
    }
    return ross_common_attach();
}
Example #13
0
void *cDataPackage::Load(char *Filename, unsigned long *Size)
{
  FILE *fp;

  // Free a prior buffer
  Free();

  fopen_s(&fp, Filename, "rb");
  if(fp!=NULL) {
    // Read in size and data
    fread(&m_Size, 1, 4, fp);
    if((m_Buf = (void*)new char[m_Size]) != NULL)
      fread(m_Buf, 1, m_Size, fp);
    fclose(fp);

    // Store size to return
    if(Size != NULL)
      *Size = m_Size;

    // return pointer
    return m_Buf;
  }

  return NULL;
}
Example #14
0
void ReaddayFile_QDA()
{
	FILE * fp = fopen("SQ2007.QDA", "rb");

	struct header header;
	struct data_struct data;
	struct day_struct day;
	int i ; 
	int j;

	fread(&header, sizeof(header), 1, fp);

	FILE *out = fopen("01.data", "w");

	for(i = 0 ; i < header.StockCount; i++)
	{
		fread(&data, sizeof(data), 1, fp);
		fprintf(out, "%s  %d\n", data.Id, data.DayCount);
		for(j = 0 ; j < data.DayCount; j++)
		{
			fread(&day, sizeof(day), 1, fp);
			fprintf(out , "%d,%f,%f,%f,%f,%f,%f,%d \n", day.Tm, day.OpenPrice, day.HighPrice, day.LowPrice, day.ClosePrice,
				day.Volum, day.amount, day.DealCount);
		}
	}
	fclose(out);


	fclose(fp);
};
Example #15
0
//-----------------------------------------------------------------------------
// GetParameters() extracts fp, nbit, wav_length from the .wav file
// This function is only used in wavread().
//-----------------------------------------------------------------------------
static bool GetParameters(FILE *fp, int *fs, int *nbit, int *wav_length) {
  char data_check[5] = {0};
  data_check[4] = '\0';
  unsigned char for_int_number[4];
  fread(for_int_number, 1, 4, fp);
  *fs = 0;
  for (int i = 3; i >= 0; --i) *fs = *fs * 256 + for_int_number[i];
  // Quantization
  fseek(fp, 6, SEEK_CUR);
  fread(for_int_number, 1, 2, fp);
  *nbit = for_int_number[0];

  // Skip until "data" is found. 2011/03/28
  while (0 != fread(data_check, 1, 1, fp)) {
    if (data_check[0] == 'd') {
      fread(&data_check[1], 1, 3, fp);
      if (0 != strcmp(data_check, "data")) {
        fseek(fp, -3, SEEK_CUR);
      } else {
        break;
      }
    }
  }
  if (0 != strcmp(data_check, "data")) {
    printf("data error.\n");
    return false;
  }

  fread(for_int_number, 1, 4, fp);  // "data"
  *wav_length = 0;
  for (int i = 3; i >= 0; --i)
    *wav_length = *wav_length * 256 + for_int_number[i];
  *wav_length /= (*nbit / 8);
  return true;
}
Example #16
0
bool GridMap::loadGridMapLiquidData(FILE *in, uint32 offset, uint32 /*size*/)
{
    GridMapLiquidHeader header;
    fseek(in, offset, SEEK_SET);
    fread(&header, sizeof(header), 1, in);
    if (header.fourcc != *((uint32 const*)(MAP_LIQUID_MAGIC)))
        return false;

    m_liquidType    = header.liquidType;
    m_liquid_offX   = header.offsetX;
    m_liquid_offY   = header.offsetY;
    m_liquid_width  = header.width;
    m_liquid_height = header.height;
    m_liquidLevel   = header.liquidLevel;

    if (!(header.flags & MAP_LIQUID_NO_TYPE))
    {
        m_liquid_type = new uint8 [16*16];
        fread(m_liquid_type, sizeof(uint8), 16*16, in);
    }

    if (!(header.flags & MAP_LIQUID_NO_HEIGHT))
    {
        m_liquid_map = new float [m_liquid_width*m_liquid_height];
        fread(m_liquid_map, sizeof(float), m_liquid_width*m_liquid_height, in);
    }

    return true;
}
Example #17
0
int liblo10k1lf_skip_part(FILE *file, liblo10k1_file_part_t *part)
{
	char tmp_char;
	int i;
	int err;
	int found_end_part = 0;
	
	if (part->part_type == LD10K1_FP_TYPE_NORMAL) {
		/* read all data */
		for (i = 0; i < part->part_length; i++)
			if (fread(&tmp_char, 1, 1, file) != 1)
				return LD10K1_LF_ERR_READ;
	} else if (part->part_type == LD10K1_FP_TYPE_END) {
		return 0;
	} else {	
		while (!found_end_part) {
			/* read next part */
			if (fread(part, sizeof(liblo10k1_file_part_t), 1, file) != 1)
				return LD10K1_LF_ERR_READ;
				
			/* check type & id */
			if (part->part_type == LD10K1_FP_TYPE_END)
				found_end_part = 1;
			else {
				if ((err = liblo10k1lf_skip_part(file, part)) < 0)
					return err;
			}
		}
	}
	return 0;
}
Example #18
0
void TileMap::LoadLiquidData(FILE* f, TileMapHeader & header)
{
	TileMapLiquidHeader liquidHeader;
	fseek(f, header.liquidMapOffset, SEEK_SET);
	fread(&liquidHeader, 1, sizeof(liquidHeader), f);

	m_defaultLiquidType = liquidHeader.liquidType;
	m_liquidLevel = liquidHeader.liquidLevel;
	m_liquidOffX = liquidHeader.offsetX;
	m_liquidOffY = liquidHeader.offsetY;
	m_liquidWidth = liquidHeader.width;
	m_liquidHeight = liquidHeader.height;

	if(!(liquidHeader.flags & MAP_LIQUID_NO_TYPE))
	{
		m_liquidType = new uint8[16 * 16];
		fread(m_liquidType, sizeof(uint8), 16 * 16, f);
	}

	if(!(liquidHeader.flags & MAP_LIQUID_NO_HEIGHT))
	{
		m_liquidMap = new float[m_liquidWidth * m_liquidHeight];
		fread(m_liquidMap, sizeof(float), m_liquidWidth * m_liquidHeight, f);
	}
}
Example #19
0
void CLoad3DS::ReadColorChunk(mo3DSMaterialInfo *pMaterial, tChunk *pChunk, int colortype)
{
    // Read the color chunk info
    ReadChunk(m_TempChunk);

	switch(colortype) {
		case MAT_DIFFUSE_COLOR:                        // This holds the R G B color of our object
			m_TempChunk->bytesRead += fread(pMaterial->colorDiffuse, 1, m_TempChunk->length - m_TempChunk->bytesRead, m_FilePointer);
            break;

		case MAT_AMBIENT_COLOR:
			m_TempChunk->bytesRead += fread(pMaterial->colorAmbient, 1, m_TempChunk->length - m_TempChunk->bytesRead, m_FilePointer);
			break;

		case MAT_SPECULAR_COLOR:
			m_TempChunk->bytesRead += fread(pMaterial->colorSpecular, 1, m_TempChunk->length - m_TempChunk->bytesRead, m_FilePointer);
			break;
		// Read in the R G B color(3 bytes - 0 through 255)
		default:
			break;
	}

    // Add the bytes read to our chunk
    pChunk->bytesRead += m_TempChunk->bytesRead;
}
Example #20
0
static int xvec_fread (long unit_size, FILE * f, void * v, int d_alloc)
{
  int d;
  int ret = fread (&d, sizeof (int), 1, f);

  if (feof (f))
    return 0;

  if (ret != 1) {
    perror ("# xvec_fread error 1");
    return -1;
  }

  if (d < 0 || d > d_alloc) {
    fprintf(stderr, "xvec_fread: weird vector size (expect %d found %d)\n", d_alloc, d);
    return -1;
  }

  ret = fread (v, unit_size, d, f);
  if (ret != d) {
    perror ("# xvec_fread error 2");
    return -1;
  }

  return d;
}
/* load matrix from binary file 
 * the nonzeros are in order of double loop over rows and columns
format:
num_rows (int) 
num_columns (int)
nnz (double)
...
nnz (double)
*/
mat * matrix_load_from_binary_file(char *fname){
    int i, j, num_rows, num_columns, row_num, col_num;
    double nnz_val;
    size_t one = 1;
    FILE *fp;
    mat *M;
    
    fp = fopen(fname,"r");
    fread(&num_rows,sizeof(int),one,fp); //read m
    fread(&num_columns,sizeof(int),one,fp); //read n
    printf("initializing M of size %d by %d\n", num_rows, num_columns);
    M = matrix_new(num_rows,num_columns);
    printf("done..\n");

    // read and set elements
    for(i=0; i<num_rows; i++){
        for(j=0; j<num_columns; j++){
            fread(&nnz_val,sizeof(double),one,fp); //read nnz
            matrix_set_element(M,i,j,nnz_val);
        }
    }
    fclose(fp);

    return M;
}
Example #22
0
bool Replay::OpenReplay(const wchar_t* name) {
	wchar_t fname[256];
	myswprintf(fname, L"./replay/%ls", name);
#ifdef WIN32
	fp = _wfopen(fname, L"rb");
#else
	char fname2[256];
	BufferIO::EncodeUTF8(fname, fname2);
	fp = fopen(fname2, "rb");
#endif
	if(!fp)
		return false;
	fread(&pheader, sizeof(pheader), 1, fp);
	if(pheader.flag & REPLAY_COMPRESSED) {
		comp_size = fread(comp_data, 1, 0x1000, fp);
		fclose(fp);
		replay_size = pheader.datasize;
		if(LzmaUncompress(replay_data, &replay_size, comp_data, &comp_size, pheader.props, 5) != SZ_OK)
			return false;
	} else {
		comp_size = fread(replay_data, 1, 0x20000, fp);
		fclose(fp);
		replay_size = comp_size;
	}
	pdata = replay_data;
	is_replaying = true;
	return true;
}
Example #23
0
bool NDSSYSTEM::nds_loadfile(const char *fpath)
{
    struct nds_header_t header;
    rom = fopen(fpath, "rb");
    if( !rom )
        return false;
    else{
        fseek(rom, 0, SEEK_SET);
        fread(&header, sizeof(struct nds_header_t), sizeof(u8), rom);
        fseek(rom, header.ARM9rom_offset, SEEK_SET);

        /* Arm9 program */
        fread(mmu->memory, header.ARM9size, sizeof(u8), rom);
        memcpy(&mmu->memory[0x400000], mmu->memory, sizeof(u8)*0x400000);
        memcpy(&mmu->memory[0x800000], mmu->memory, sizeof(u8)*0x400000);
        memcpy(&mmu->memory[0xC00000], mmu->memory, sizeof(u8)*0x400000);

        /* Header */
        memcpy(&mmu->memory[0x7FFFE0], &header, sizeof(struct nds_header_t) );

        cpu->m_newpc = header.ARM9entry_adress;
        cpu->m_r[15] = header.ARM9entry_adress;

        cpu->arm9_entry = cpu->m_newpc;
        cpu->arm7_entry = header.ARM9entry_adress;

        cpu->arm9size = header.ARM9size;
        cpu->arm7size = header.ARM7size;

        return true;
    }
    return false;
}
Example #24
0
/* The image header tells us if the image is in monochrome or color, and
 * if the latter, if the input colors are interleaved. If interleaved
 * color, lines are interleaved R, G, B, R, G, B etc. Technically, some
 * interleaving of infra-red, visible and ultra-violet.
 *
 * In the description field below,
 *  element 0 == P --> monochrome
 *  element 0 == X --> color
 *  element 9 == S --> sequential (i.e only one color here)
 *  element 9 == I --> interleaved (1 or more colors)
 */
static int 
get_imghdr(int length)
{
    struct Imghdr
    {
        long linewidth;
        char dummy1[36];
        char description[16];   /* Type of image */
    } header;

    if (pm_readbiglong (spotfile, &header.linewidth) == -1)
        pm_error ("EOF / read error reading header");
#ifdef DEBUG
    if (fread (header.dummy1, 1, 36, spotfile) != 36)
        pm_error ("EOF / read error reading header");
#else
    if (fseek (spotfile, 36, 1) == EOF)
        pm_error ("seek error");
#endif
    if (fread (header.description, 1, 16, spotfile) != 16)
        pm_error ("EOF / read error reading header");

    /* Determine mono or colour */
    if (header.description[0] == 'X' && header.description[9] == 'S')
        Colbool = 1;
    else Colbool = 0;

#ifdef DEBUG
    fprintf(stderr, "Dummy str is >%s<\n", header.dummy1);
    fprintf(stderr, "Imghdr str is >%s<, col %d\n", 
            header.description, Colbool);
#endif
    /* Return the amount to fseek */
    return (length - 56);
}
Example #25
0
int main(int argc,char **argv) {
  FILE *fin; int limit,blen=0,p,l,bcnt=0; unsigned char *buf=NULL;
  assert(argc >= 2);
  sprintf(filename,"/Library/Dictionaries/%s.dictionary/Contents/Body.data",argv[1]);
  if((fin=fopen(filename,"rb"))) {
    fseek(fin,0x40,SEEK_SET);
    fread(&l,1,4,fin);
    limit=0x40+l;
    p=0x60;
    do {
      fseek(fin,p,SEEK_SET);
      fread(&l,1,4,fin);
//      if(0==l) break;
      if(blen<l) {
        if(buf!=NULL) free(buf);
        blen=l;
        buf=(unsigned char *)malloc(blen);
      }
      fread(buf,1,l,fin);
      //fprintf(stderr, "%x@%06x: %x>%06x\n",bcnt,p,l,((int *)buf)[1]);
      unpack(buf+8,l-8);
      p+=4+l;
      ++bcnt;
    } while(p<limit);
    free(buf);
    fclose(fin);
  }
  return 0;
}
Example #26
0
	bool ObjModel::load(const char *filename) {
		// open model file
		FILE *model = fopen(filename, "rb");
		if (model == NULL)
			return false;

		// read face count
		size_t len = fread(&_facecount, sizeof(_facecount), 1, model);
		std::cerr << "TWS::ObjModel got " << _facecount << " faces" << std::endl;

		// alloc memory for these faces
		_faces = (ModelFace_t *)malloc(sizeof(ModelFace_t) * _facecount);

		// read face data
		for (uint32_t i=0; i<_facecount; i++) {
			// get component count
			len = fread(&_faces[i].component_count, sizeof(GLuint), 1, model);

			// alloc memory for components
			_faces[i].vertices = (float *)malloc(sizeof(float) * _faces[i].component_count * 3);
			_faces[i].normals = (float *)malloc(sizeof(float) * _faces[i].component_count * 3);
			_faces[i].texcoords = (float *)malloc(sizeof(float) * _faces[i].component_count * 2);

			// read vertices, normals, texcoords
			len = fread(_faces[i].vertices, sizeof(float), 3*_faces[i].component_count, model);
			len = fread(_faces[i].normals, sizeof(float), 3*_faces[i].component_count, model);
			len = fread(_faces[i].texcoords, sizeof(float), 2*_faces[i].component_count, model);
		}

		fclose(model);
		return true;
	}
Example #27
0
void
readdata(char *buffer, afs_sfsize_t size)
{
    int code;
    afs_int32 s;

    if (!buffer) {
	while (size > 0) {
	    s = (afs_int32) ((size > BUFSIZE) ? BUFSIZE : size);
	    code = fread(buf, 1, s, dumpfile);
	    if (code != s)
		fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
	    size -= s;
	}
    } else {
	code = fread(buffer, 1, size, dumpfile);
	if (code != size) {
	    if (code < 0)
		fprintf(stderr, "Code = %d; Errno = %d\n", code, errno);
	    else
		fprintf(stderr, "Read %d bytes out of %" AFS_INT64_FMT "\n", code, (afs_uintmax_t)size);
	}
	if ((code >= 0) && (code < BUFSIZE))
	    buffer[size] = 0;	/* Add null char at end */
    }
}
Example #28
0
dt_imageio_retval_t dt_imageio_open_pfm(dt_image_t *img, const char *filename, dt_mipmap_cache_allocator_t a)
{
  const char *ext = filename + strlen(filename);
  while(*ext != '.' && ext > filename) ext--;
  if(strcasecmp(ext, ".pfm")) return DT_IMAGEIO_FILE_CORRUPTED;
  FILE *f = fopen(filename, "rb");
  if(!f) return DT_IMAGEIO_FILE_CORRUPTED;
  int ret = 0;
  int cols = 3;
  char head[2] = { 'X', 'X' };
  ret = fscanf(f, "%c%c\n", head, head + 1);
  if(ret != 2 || head[0] != 'P') goto error_corrupt;
  if(head[1] == 'F')
    cols = 3;
  else if(head[1] == 'f')
    cols = 1;
  else
    goto error_corrupt;
  ret = fscanf(f, "%d %d\n%*[^\n]", &img->width, &img->height);
  if(ret != 2) goto error_corrupt;
  ret = fread(&ret, sizeof(char), 1, f);
  if(ret != 1) goto error_corrupt;
  ret = 0;

  float *buf = (float *)dt_mipmap_cache_alloc(img, DT_MIPMAP_FULL, a);
  if(!buf) goto error_cache_full;

  if(cols == 3)
  {
    ret = fread(buf, 3 * sizeof(float), (size_t)img->width * img->height, f);
    for(size_t i = (size_t)img->width * img->height; i > 0; i--)
      for(int c = 0; c < 3; c++) buf[4 * (i - 1) + c] = fmaxf(0.0f, fminf(FLT_MAX, buf[3 * (i - 1) + c]));
  }
  else
    for(size_t j = 0; j < img->height; j++)
      for(size_t i = 0; i < img->width; i++)
      {
        ret = fread(buf + 4 * (img->width * j + i), sizeof(float), 1, f);
        buf[4 * (img->width * j + i) + 2] = buf[4 * (img->width * j + i) + 1]
            = buf[4 * (img->width * j + i) + 0];
      }
  float *line = (float *)calloc(4 * img->width, sizeof(float));
  for(size_t j = 0; j < img->height / 2; j++)
  {
    memcpy(line, buf + img->width * j * 4, 4 * sizeof(float) * img->width);
    memcpy(buf + img->width * j * 4, buf + img->width * (img->height - 1 - j) * 4,
           4 * sizeof(float) * img->width);
    memcpy(buf + img->width * (img->height - 1 - j) * 4, line, 4 * sizeof(float) * img->width);
  }
  free(line);
  fclose(f);
  return DT_IMAGEIO_OK;

error_corrupt:
  fclose(f);
  return DT_IMAGEIO_FILE_CORRUPTED;
error_cache_full:
  fclose(f);
  return DT_IMAGEIO_CACHE_FULL;
}
Example #29
0
void   read4file(char *fname, float **s0, float **sinit, float **s, int nx, int nz)
{
    //int i;
    FILE *fp;

    if((fp=fopen(fname, "rb"))==NULL) {
	printf("Cannot open file.\n");
    }

    /* nm 
    if(fread(&i, sizeof(int), 1, fp) != 1) {
	if (feof(fp))
	    printf("File read error - nm.");
    }
    assert(i == nx*nz); */

    /* m0 */
    if(fread(s0[0], sizeof(float), nx*nz, fp) != nx*nz) {
	if (feof(fp))
	    printf("File read error - too small.");
    }

    /* minit */
    if(fread(sinit[0], sizeof(float), nx*nz, fp) != nx*nz) {
	if (feof(fp))
	    printf("File read error - too small.");
    }
    /* s */
    if(fread(s[0], sizeof(float), nx*nz, fp) != nx*nz) {
	if (!feof(fp))
	    printf("File read error - too big.");
    }

    fclose(fp);
}
Example #30
0
size_t quip_file_reader(void* param, uint8_t* data, size_t datalen)
{
    FILE* f = (FILE*) param;

    if (data == NULL) {
        if (fseek(f, datalen, SEEK_CUR) == 0) return datalen;
        else {
            /* The stream is not seekable, so we have
             * to read and discard datalen bytes. */
            const size_t bufsize = 4096;
            size_t readcnt = 0;
            uint8_t* buf = malloc_or_die(bufsize);
            size_t remaining = datalen;

            while (remaining > 0) {
                readcnt = fread(buf, 1, remaining < bufsize ? remaining : bufsize, f);
                if (readcnt == 0) break;
                else remaining -= readcnt;
            }

            free(buf);
            return datalen - remaining;
        }
    }
    else return fread(data, 1, datalen, (FILE*) param);
}