コード例 #1
0
ファイル: read_ctrees.c プロジェクト: forero/WalkTree
int read_ctrees(char * filename, struct treeData ** galacticusTrees, int * nOutputTrees,
		struct nodeData*** nodeArray, int * totalNodes) {
  /*  */
  /* open the file obtained from consistent_trees */
  FILE * file;
  file=fopen(filename,"r");
  if(file==0) {
    printf("Could not find file %s\n", filename);
    return 1;
  }

  /* make a struct for the tree, as read in from the ctrees output  */
  /* this is necessary, as ctrees outputs a seperated tree for each */
  /* subhalo. Galacticus expects the subhalos to be within the tree */
  /* of their parent halo. */
  struct inputTreeData {
    int id;
    int nNodes;
    int mainNodeId;
    int parentId;
    int upId;
    int upmostId;
    int gTreeIndex;	/* index of the corresponding galacticus tree */
    int gNodeIndex;	/* nodeIndex within the corresponding galacticus tree */
    fpos_t startPos;
    fpos_t endPos;
  };

  /* get the number of data fields */
  /* for information purposes only */
  char line[800];
  int nfields=0;
  int i=0;
  while((line[i] = fgetc(file))!='\n') {
    i++;
  }
  strtok(line, " ");
  nfields=1;
  while(strtok(NULL," ")) {
    nfields++;
  }
  printf("%s has %i fields\n",filename,nfields);

  /* go to the beginning of the data section */
  /* ATTENTION! This may change with future  */
  /* versions of consistent_trees */
  skipline(file,24);

  /* get the number of trees in the ctrees output file */
  /* as subhalos have their own trees, this number     */
  /* will not correspond to the number of trees in the */
  /* galacticus input file */
  int nTreesInput;

  fscanf(file,"%i\n",&nTreesInput);

  printf("%s contains %i trees\n",filename,nTreesInput);


  /* fill struct inputTreeData with the data from the input file */
  struct inputTreeData cTrees[nTreesInput];
  for(i=0; i<nTreesInput; i++) {
    cTrees[i].id=i;
    fscanf(file,"%*s %i\n",&cTrees[i].mainNodeId);
    fgetpos(file,&cTrees[i].startPos);
    fscanf(file,"%*f %*i %*f %*i %*i %i %i", &cTrees[i].parentId, &cTrees[i].upId);
    fsetpos(file, &cTrees[i].startPos);
    cTrees[i].nNodes = countNodes(file);
    fgetpos(file,&cTrees[i].endPos);
  }

    int nTrees;
  nTrees = 0;
  /* get the number of trees (without subhalo trees) */
  for(i=0; i<nTreesInput; i++) {
    if(cTrees[i].parentId==-1)
      nTrees++;
  }
  printf("%s contains %i distinct trees\n", filename,nTrees);

  /* make the array of galacticus trees */
  (*galacticusTrees) = malloc(nTrees*sizeof(struct treeData));

  if((*galacticusTrees)==NULL){
    fprintf(stdout, "Problem with the allocation\n");
    exit(1);
}

  /* give the galacticus trees an id */
  for(i=0; i<nTrees; i++) {
    (*galacticusTrees)[i].id=i;
  }

  /* fill the galacticus tree struct     */
  /* with the non-subhalo tree data from */
  /* the input file */
  int j=0;
  for(i=0; i<nTreesInput; i++) {
    if(cTrees[i].parentId==-1) { /* true if cTree is a non subhalo tree */
      (*galacticusTrees)[j].mainNodeId=cTrees[i].mainNodeId;
      (*galacticusTrees)[j].nNodes=cTrees[i].nNodes;
      cTrees[i].gTreeIndex=j;
      cTrees[i].gNodeIndex=0;
      j++;
    }
  }


  for(i=0; i<nTreesInput;i++) {
    cTrees[i].upmostId=cTrees[i].upId;
  }
  /* Get upmost Id, because a subhalo can have an upId         */
  /* which is not a distinct halo if the subhalo lies out      */
  /* of the virial radius of the host halo of the upId halo    */
  /* therefore make a loop to determine the really upmost halo */
  /* attention, several levels might be necessary              */

  int levels;
  int n_changed;
  int top_found;
  n_changed=0;
  for(levels=0; levels<4; levels++) {

    for(i=0; i<nTreesInput; i++) {
    top_found =0;
#ifdef PATCH
      if(cTrees[i].upmostId!=-1) {
#else
      if(cTrees[i].upId!=-1) {
#endif
	for(j=0; j<nTreesInput; j++) {
	  if(cTrees[i].upId==cTrees[j].mainNodeId) {
	    top_found=1;
	    if(cTrees[j].upId!=-1) {
	      cTrees[i].upmostId=cTrees[j].upId;
	      n_changed++;
	    }
	  }
	}
      
#ifdef PATCH
      if(top_found==0){
	fprintf(stderr, "upmostId for itree %i not found\n",i);
	exit(1);
      }
#endif
      }
    }

      
  }

    fprintf(stdout, "FInished the upmost ID calculation: %d changes\n",n_changed);


  /* correct the nNodes field for subhalo trees belonging */
  /* to this galacticus tree */
  for(j=0; j<nTrees; j++) {
    for(i=0; i<nTreesInput; i++) {
      if(cTrees[i].upmostId==(*galacticusTrees)[j].mainNodeId) {
	cTrees[i].gNodeIndex=(*galacticusTrees)[j].nNodes;
	(*galacticusTrees)[j].nNodes+=cTrees[i].nNodes;
      }
    }
  }
  fprintf(stdout, "Fnished correction 1\n");
  /* set the galacticus tree index in the inputTreeData struct */
  for(i=0; i<nTreesInput; i++) {
    if(cTrees[i].parentId!=-1) { /* true if tree is a subhalo tree */
      for(j=0; j<nTrees; j++) {
	if((*galacticusTrees)[j].mainNodeId==cTrees[i].upmostId)
	  cTrees[i].gTreeIndex=j;
      }
    }

  }
  fprintf(stdout, "Fnished correction 2\n");

  /* set the first node field for the galacticus tree data */
  /* needed, for galacticus to know, where a tree starts   */
  /* in the node data array */
  (*galacticusTrees)[0].firstNode=0;
  for(i=1; i<nTrees; i++) {
    (*galacticusTrees)[i].firstNode=(*galacticusTrees)[i-1].firstNode+(*galacticusTrees)[i-1].nNodes;
  }
  fprintf(stdout, "Fnished correction 3\n");

  /* allocate the memory for the node array */
  /* the node array has two dimensions, the */
  /* first for each tree, the second for    */
  /* the nodes within a tree */
  (*nodeArray) = (struct nodeData**)malloc(sizeof(struct nodeData *)*nTrees);
  if((*nodeArray)==NULL){
    fprintf(stderr,"Problem with node alllocation\n");
    exit(1);
  }
  for(i=0;i<nTrees;i++) {
    (*nodeArray)[i] = (struct nodeData*)malloc(sizeof(struct nodeData)*(*galacticusTrees)[i].nNodes);
    if((*nodeArray)[i]==NULL){
      fprintf(stdout, "Problem with the nodeArray allocation (%i nodes)\n", (*galacticusTrees)[i].nNodes);
      exit(1);
    }
  }
  fprintf(stdout, "Fnished allocation 3\n");
  /* fill the node array */
  for(i=0;i<nTreesInput;i++) {   /* loop over the input trees */
    /*
    fprintf(stdout, "%i %i [%i,%i]\n", i, cTrees[i].nNodes, 
	    cTrees[i].gTreeIndex, cTrees[i].gNodeIndex);
    */
    fsetpos(file,&cTrees[i].startPos); /* set file pointer to the start of tree i */
    for(j=0;j<cTrees[i].nNodes;j++) { /* loop over the node in each input tree */
      //      fprintf(stdout,"%i %i %f\n",j,cTrees[i].nNodes,&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].scale);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].scale);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].id);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].desc_scale);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].desc_id);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].num_prog);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].pid);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].upid);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].desc_pid);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].phantom);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].mVir);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].mVirOrig);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].rVir);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].scaleRadius);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].vRMS);
      fscanf(file,"%i",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].mmp);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].scaleLastMM);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].vMax);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].pos[0]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].pos[1]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].pos[2]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].v[0]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].v[1]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].v[2]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].l[0]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].l[1]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].l[2]);
      fscanf(file,"%f",&(*nodeArray)[cTrees[i].gTreeIndex][cTrees[i].gNodeIndex+j].spin);
      fscanf(file,"\n");
    }
  }

  fclose(file);
  /* set nOutputTrees and totalNodes */
  /* which are needed, so, that the  */
  /* other functions know about the  */
  /* length of the arrays */
  *nOutputTrees = nTrees;
  *totalNodes = 0;
  for(i=0;i<nTrees;i++) {
    *totalNodes += (*galacticusTrees)[i].nNodes;
  }
  printf("%s contains %i nodes\n",filename,*totalNodes);


#ifdef PATCH
  fprintf(stdout, "started the new ID\n");
  /*calculate the parent ID to be the corresponding most up ID*/
  /*
  int l, m;
  for(i=0;i<nTrees;i++){
    for(j=0;j<(*galacticusTrees)[i].nNodes;j++){
      (*nodeArray)[i][j].pid = (*nodeArray)[i][j].upid;
    }
  }
  
  fprintf(stdout, "started the new ID for subs\n");
  for(i=0;i<nTrees;i++){
    for(j=0;j<(*galacticusTrees)[i].nNodes;j++){

      if((*nodeArray)[i][j].pid!=-1){
	levels = 0;
	do{
	  for(l=0;l<nTrees;l++){
	    for(m=0;m<(*galacticusTrees)[l].nNodes;m++){
	      if((*nodeArray)[i][j].pid==(*nodeArray)[l][m].id){		
		(*nodeArray)[i][j].pid = (*nodeArray)[l][m].upid;
		if((*nodeArray)[l][m].upid==-1){
		  levels = 5;
		}
	      }
	    }
	  }
	  levels++;
	}while(levels<4);
	if(levels!=6){
	  fprintf(stdout, "the host wast not found for tree %i node %i (%i)levels %i\n", 
		  i, j, (*nodeArray)[i][j].pid, levels);
	  exit(1);
	}
      }
    }
  }
  */  
#endif

#ifdef PATCH
  int search;
  int found;
  int l;
  int notfound;
  notfound=0;
  for(i=0;i<nTrees;i++){
    for(j=0;j<(*galacticusTrees)[i].nNodes;j++){
      search = (*nodeArray)[i][j].pid;
      if(search!=-1){

	found = 0;
	l = 0;
	do{
	  if((*nodeArray)[i][l].id==search)
	    found = 1;
	  l++;
	}while(!found && l<(*galacticusTrees)[i].nNodes);
	
	if(!found){
	  //	  fprintf(stderr, "Couldn't find pid %d for halo %d\n", search, (*nodeArray)[i][j].id);
	  (*nodeArray)[i][j].pid = -1;
	  notfound++;
	}	      
      }
    }
  }
  fprintf(stdout, "The number of not found parent ids %d\n", notfound);
  
 notfound=0;
  for(i=0;i<nTrees;i++){
    for(j=0;j<(*galacticusTrees)[i].nNodes;j++){
      search = (*nodeArray)[i][j].desc_id;
      if(search!=-1){

	found = 0;
	l = 0;
	do{
	  if((*nodeArray)[i][l].id==search)
	    found = 1;
	  l++;
	}while(!found && l<(*galacticusTrees)[i].nNodes);
	
	if(!found){
	  //	  fprintf(stderr, "Couldn't find pid %d for halo %d\n", search, (*nodeArray)[i][j].id);
	  (*nodeArray)[i][j].desc_id = -1;
	  notfound++;
	  fprintf(stdout, "Not found desc in tree %i - node %i out of %i\n", 
		  i, j,(*galacticusTrees)[i].nNodes);
	}	      
      }
    }
  }
  fprintf(stdout, "The number of not found desc ids %d\n", notfound);
  
#endif

  for(i=0;i<nTrees;i++) {
    for(j=0;j<(*galacticusTrees)[i].nNodes;j++) {
      /* if host node has no parent node, consistent_trees sets parent id field to -1  */
      /* galacticus, in such a case expects parent id field to be equal to the node id */
      if((*nodeArray)[i][j].pid==-1) {
	(*nodeArray)[i][j].pid=(*nodeArray)[i][j].id;
      }
    }
  }



  return 0;

}
コード例 #2
0
ファイル: DevControl.cpp プロジェクト: crashatom/phoebemail
LONG CDevControl::StartUpgradeEx(LONG lLoginID, EM_UPGRADE_TYPE emType, char *pchFileName, fUpgradeCallBack cbUpgrade, DWORD dwUser)
{
	if (m_pManager->IsDeviceValid((afk_device_s*)lLoginID) < 0)
    {
		m_pManager->SetLastError(NET_INVALID_HANDLE);
        return 0;
    }

	if (!pchFileName)
	{
		m_pManager->SetLastError(NET_ILLEGAL_PARAM);
		return 0;
	}

    afk_device_s *device = (afk_device_s*)lLoginID;

    st_Upgrade_Info* pUI = new st_Upgrade_Info;
	if (!pUI)
	{
		m_pManager->SetLastError(NET_SYSTEM_ERROR);
		return 0;
	}

	int ret = 0;
	afk_channel_s *pchannel = 0;
	afk_upgrade_channel_param_s upgradechannel = {0};

	FILE *file = fopen(pchFileName, "rb");
    if (file)
    {
        fpos_t pos;
        fseek(file, 0, SEEK_END);
        fgetpos(file, &pos);
#ifdef WIN32
        upgradechannel.size = pos;
#else	//linux
        upgradechannel.size = pos.__pos;
#endif
    }
    else
    {
		delete pUI;
		m_pManager->SetLastError(NET_ERROR);
        return 0;
    }

	fclose(file);
	file = NULL;
	
	upgradechannel.filetype = (int)emType;
	upgradechannel.type = AFK_CHANNEL_UPLOAD_UPGRADE;

	pUI->pcsLock = new DEVMutex;
	if (!pUI->pcsLock)
	{
		m_pManager->SetLastError(NET_SYSTEM_ERROR);
		goto e_clearup;
	}
	
	ret = CreateEventEx(pUI->hRecEvent, TRUE, FALSE);
	if (ret < 0)
	{
		m_pManager->SetLastError(NET_SYSTEM_ERROR);
		goto e_clearup;
	}
	
	InterlockedSetEx(&pUI->life, 1);

	pUI->device = device;
	pUI->channel = 0;
	pUI->cbUpgrade = cbUpgrade;
	pUI->dwUser = dwUser;

    upgradechannel.base.func = UpgradeFunc; 
    upgradechannel.base.udata = pUI;
    strcpy(upgradechannel.filename, pchFileName);

    pchannel = (afk_channel_s*)device->open_channel(device, 
        AFK_CHANNEL_TYPE_UPGRADE, &upgradechannel);
    if (pchannel)
    {
		DWORD dwRet = WaitForSingleObjectEx(pUI->hRecEvent, 10*WAIT_TIME);
        ResetEventEx(pUI->hRecEvent);
        if (dwRet == WAIT_OBJECT_0)
        {
            pUI->channel = pchannel;

			m_csUI.Lock();
			m_lstUI.push_back(pUI);
			m_csUI.UnLock();
        }
		else
		{
			goto e_clearup;
		}
    }
	else
	{
		goto e_clearup;
	}

    return (LONG)pchannel;

e_clearup:
	if (file)
	{
		fclose(file);
		file = 0;
	}

	if (pchannel)
	{
		pchannel->close(pchannel);
		pchannel = 0;
	}

	if (pUI)
	{
		if (pUI->pcsLock)
		{
			delete pUI->pcsLock;
			pUI->pcsLock = 0;
		}
		
		CloseEventEx(pUI->hRecEvent);

		delete pUI;
		pUI = 0;
	}

	return 0;
}
コード例 #3
0
uint64_t ftello_adm(FILE *f)
{
	fpos_t pos;
	fgetpos(f,&pos);
	return (uint64_t)pos;
}
コード例 #4
0
ファイル: pngxread.c プロジェクト: is00hcw/page-speed
int PNGAPI
pngx_read_image(png_structp png_ptr, png_infop info_ptr,
                png_charp fmt_name_buf, png_size_t fmt_name_buf_size,
                png_charp fmt_desc_buf, png_size_t fmt_desc_buf_size)
{
   png_byte sig[128];
   png_size_t num;
   int (*read_fn)(png_structp, png_infop, FILE *);
   FILE *stream;
   fpos_t fpos;
   int result;

   /* Precondition. */
#ifdef PNG_FLAG_MALLOC_NULL_MEM_OK
   PNGX_ASSERT_MSG(!(png_ptr->flags & PNG_FLAG_MALLOC_NULL_MEM_OK),
      "pngxtern requires a safe allocator");
#endif

   /* Check the format name buffers. */
   /* Ensure that the longest short name ("PNG datastream") and
    * the longest long name ("Portable Network Graphics embedded datastream")
    * will fit.
    */
   if ((fmt_name_buf != NULL &&
        fmt_name_buf_size < sizeof(pngx_png_datastream_fmt_name)) ||
       (fmt_desc_buf != NULL &&
        fmt_desc_buf_size < sizeof(pngx_png_datastream_fmt_desc)))
      return -1;  /* invalid parameters */

   /* Read the signature bytes. */
   stream = (FILE *)png_get_io_ptr(png_ptr);
   PNGX_ASSERT(stream != NULL);
   if (fgetpos(stream, &fpos) != 0)
      png_error(png_ptr, "Can't ftell in input file stream");
   num = fread(sig, 1, sizeof(sig), stream);
   if (fsetpos(stream, &fpos) != 0)
      png_error(png_ptr, "Can't fseek in input file stream");

   /* Try the PNG format first. */
   if (pngx_sig_is_png(png_ptr, sig, num,
       fmt_name_buf, fmt_name_buf_size, fmt_desc_buf, fmt_desc_buf_size) > 0)
   {
      png_read_png(png_ptr, info_ptr, 0, NULL);
      if (getc(stream) != EOF)
      {
         png_warning(png_ptr, "Extraneous data found after IEND");
         fseek(stream, 0, SEEK_END);
      }
      return 1;
   }

   /* Check the signature bytes against other known image formats. */
   if (pngx_sig_is_bmp(sig, num,
       fmt_name_buf, fmt_name_buf_size, fmt_desc_buf, fmt_desc_buf_size) > 0)
      read_fn = pngx_read_bmp;
   else if (pngx_sig_is_gif(sig, num,
       fmt_name_buf, fmt_name_buf_size, fmt_desc_buf, fmt_desc_buf_size) > 0)
      read_fn = pngx_read_gif;
   else if (pngx_sig_is_jpeg(sig, num,
       fmt_name_buf, fmt_name_buf_size, fmt_desc_buf, fmt_desc_buf_size) > 0)
      read_fn = pngx_read_jpeg;
   else if (pngx_sig_is_pnm(sig, num,
       fmt_name_buf, fmt_name_buf_size, fmt_desc_buf, fmt_desc_buf_size) > 0)
      read_fn = pngx_read_pnm;
   else if (pngx_sig_is_tiff(sig, num,
       fmt_name_buf, fmt_name_buf_size, fmt_desc_buf, fmt_desc_buf_size) > 0)
      read_fn = pngx_read_tiff;
   else
      return 0;  /* not a known image format */

   /* Read the image. */
   result = read_fn(png_ptr, info_ptr, stream);
   /* Signature checking may give false positives; reading can still fail. */
   if (result <= 0)  /* this isn't the format we thought it was */
      if (fsetpos(stream, &fpos) != 0)
         png_error(png_ptr, "Can't fseek in input file stream");
   return result;
}
コード例 #5
0
ファイル: token.c プロジェクト: punund/popeye
fpos_t InputGetPosition(void)
{
  fpos_t result;
  fgetpos(InputMirror,&result);
  return result;
}
コード例 #6
0
ファイル: stdio.c プロジェクト: lijinlei/Kernel_BOOX60
int main( int argc, char **argv )
{
    int err;
    FILE *f;
    fpos_t fpos;
    
    int flibble = 4567;
    char *wibble = "abcdefghijk";

    int flibble1;
    char wibble1[20];
    
    CYG_TEST_INIT();

    
    f = fopen("/foo", "w" );
    if( f == NULL ) SHOW_RESULT( fopen, -1 );

    err = fprintf(f, "flibble %d wibble %s\n", flibble, wibble );
    if( err < 0 ) SHOW_RESULT( fprintf, err );
    
    err = fprintf(f, "another flibble %d another wibble %s\n", flibble, wibble );
    if( err < 0 ) SHOW_RESULT( fprintf, err );
    
    err = fclose( f );
    if( err == EOF ) SHOW_RESULT( fclose, -1 );


    
    f = fopen("/foo", "r" );
    if( f == NULL ) SHOW_RESULT( fopen, -1 );

    err = fscanf(f, "flibble %d wibble %s\n", &flibble1, wibble1 );
    if( err < 0 ) SHOW_RESULT( fscanf, err );

    diag_printf("<INFO>: flibble1 %d wibble1 %s\n",flibble1,wibble1);
    
    CYG_TEST_CHECK( flibble1 == flibble , "Bad flibble result from fscanf");
    CYG_TEST_CHECK( strcmp(wibble,wibble1) == 0, "Bad wibble result from fscanf");


    err = fgetpos( f, &fpos );
    if( err < 0 ) SHOW_RESULT( fgetpos, err );    
    
    err = fseek( f, 0, SEEK_SET );
    if( err < 0 ) SHOW_RESULT( fseek, err );

    err = fscanf(f, "flibble %d wibble %s\n", &flibble1, wibble1 );
    if( err < 0 ) SHOW_RESULT( fscanf, err );

    diag_printf("<INFO>: flibble1 %d wibble1 %s\n",flibble1,wibble1);
    
    CYG_TEST_CHECK( flibble1 == flibble , "Bad flibble result from fscanf");
    CYG_TEST_CHECK( strcmp(wibble,wibble1) == 0, "Bad wibble result from fscanf");


    err = fseek( f, 0, SEEK_END );
    if( err < 0 ) SHOW_RESULT( fseek, err );

    err = fsetpos( f, &fpos );
    if( err < 0 ) SHOW_RESULT( fsetpos, err );    
    
    err = fscanf(f, "another flibble %d another wibble %s\n", &flibble1, wibble1 );
    if( err < 0 ) SHOW_RESULT( fscanf, err );

    diag_printf("<INFO>: flibble1 %d wibble1 %s\n",flibble1,wibble1);
    
    CYG_TEST_CHECK( flibble1 == flibble , "Bad flibble result from fscanf");
    CYG_TEST_CHECK( strcmp(wibble,wibble1) == 0, "Bad wibble result from fscanf");


    
    err = fclose( f );


    
    CYG_TEST_PASS_FINISH("stdio");
    
    return 0;
}
コード例 #7
0
ファイル: csv.c プロジェクト: binarytemple/debian-vips
static int
read_csv( FILE *fp, VipsImage *out, 
	int skip, 
	int lines,
	const char *whitespace, const char *separator,
	gboolean read_image )
{
	int i;
	char whitemap[256];
	char sepmap[256];
	const char *p;
	fpos_t pos;
	int columns;
	int ch;
	double d;
	double *buf;
	int y;

	/* Make our char maps. 
	 */
	for( i = 0; i < 256; i++ ) {
		whitemap[i] = 0;
		sepmap[i] = 0;
	}
	for( p = whitespace; *p; p++ )
		whitemap[(int) *p] = 1;
	for( p = separator; *p; p++ )
		sepmap[(int) *p] = 1;

	/* Skip first few lines.
	 */
	for( i = 0; i < skip; i++ )
		if( !skip_line( fp ) ) {
			vips_error( "csv2vips", 
				"%s", _( "end of file while skipping start" ) );
			return( -1 );
		}

	/* Parse the first line to get number of columns. Only bother checking
	 * fgetpos() the first time we use it: assume it's working after this.
	 */
	if( fgetpos( fp, &pos ) ) {
		vips_error_system( errno, "csv2vips", 
			"%s", _( "unable to seek" ) );
		return( -1 );
	}
	for( columns = 0; 
		(ch = read_double( fp, whitemap, sepmap, 
			skip + 1, columns + 1, &d )) == 0; 
		columns++ )
		;
	fsetpos( fp, &pos );

	if( columns == 0 ) {
		vips_error( "csv2vips", "%s", _( "empty line" ) );
		return( -1 );
	}

	/* If lines is -1, we have to scan the whole file to get the
	 * number of lines out.
	 */
	if( lines == -1 ) {
		fgetpos( fp, &pos );
		for( lines = 0; skip_line( fp ); lines++ )
			;
		fsetpos( fp, &pos );
	}

	vips_image_init_fields( out,
		columns, lines, 1, 
		VIPS_FORMAT_DOUBLE, 
		VIPS_CODING_NONE, VIPS_INTERPRETATION_B_W, 1.0, 1.0 );
	vips_demand_hint( out, VIPS_DEMAND_STYLE_THINSTRIP, NULL );

	/* Just reading the header? We are done.
	 */
	if( !read_image )
		return( 0 );

	if( !(buf = VIPS_ARRAY( out, 
		VIPS_IMAGE_N_ELEMENTS( out ), double )) )
		return( -1 );

	for( y = 0; y < lines; y++ ) {
		int x;

		for( x = 0; x < columns; x++ ) {
			int lineno = y + skip + 1;
			int colno = x + 1;

			ch = read_double( fp, whitemap, sepmap,
				lineno, colno, &d );
			if( ch == EOF ) {
				vips_error( "csv2vips", 
					_( "unexpected EOF, line %d col %d" ), 
					lineno, colno );
				return( -1 );
			}
			else if( ch == '\n' ) {
				vips_error( "csv2vips", 
					_( "unexpected EOL, line %d col %d" ), 
					lineno, colno );
				return( -1 );
			}
			else if( ch )
				/* Parse error.
				 */
				return( -1 );

			buf[x] = d;
		}

		if( vips_image_write_line( out, y, (VipsPel *) buf ) )
			return( -1 );

		/* Skip over the '\n' to the next line.
		 */
		skip_line( fp );
	}

	return( 0 );
}
コード例 #8
0
ファイル: tail.c プロジェクト: Nils-TUD/Escape
int main(int argc,char *argv[]) {
	size_t n = 5;

	int opt;
	while((opt = getopt(argc,argv,"n:")) != -1) {
		switch(opt) {
			case 'n': n = atoi(optarg); break;
			default:
				usage(argv[0]);
		}
	}

	/* open file, if any */
	FILE *in = stdin;
	if(optind < argc) {
		in = fopen(argv[optind],"r");
		if(!in)
			error("Unable to open '%s'",argv[optind]);
	}

	/* check whether we can seek */
	if(fseek(in,0,SEEK_END) < 0) {
		/* remove error that fseek() has set */
		clearerr(in);

		/* no, ok, then the idea is the following:
		 * we don't need to keep all the memory and do a lot of malloc and realloc. it is sufficient
		 * to read the stuff block by block and keep only the last few blocks that contain at least
		 * <n> lines. therefore, we introduce a single-linked-list and reuse the first one if all
		 * others still contain at least <n> lines. this way, we save a lot of memory and time
		 * because we don't need so many heap-allocs/reallocs. */
		size_t lines = 0;
		sReadBuf *buf;
		sReadBuf *prev = NULL;
		sReadBuf *first = NULL;
		do {
			if(first && (lines - first->lines) > n) {
				buf = first;
				lines -= buf->lines;
				first = first->next;
			}
			else {
				buf = (sReadBuf*)malloc(sizeof(sReadBuf));
				if(!buf)
					error("Unable to allocate more memory");
			}

			buf->bytes = fread(buf->buffer,1,BUF_SIZE,in);
			buf->lines = countLines(buf->buffer,buf->bytes);
			lines += buf->lines;
			buf->next = NULL;
			if(prev)
				prev->next = buf;
			else if(first)
				first->next = buf;
			else
				first = buf;
			prev = buf;
		}
		while(buf->bytes > 0);

		/* throw away the first blocks if they are not needed */
		while((lines - first->lines) > n) {
			lines -= first->lines;
			first = first->next;
		}

		/* print out the lines */
		buf = first;
		while(!ferror(stdout) && buf != NULL) {
			for(size_t i = 0; i < buf->bytes; i++) {
				int c = buf->buffer[i];
				if(lines <= n)
					putchar(c);
				if(c == '\n')
					lines--;
			}
			buf = buf->next;
		}
	}
	else {
		/* we can seek, therefore we can do it better. we don't need to search through the whole
		 * file, but can seek to the end and stepwise backwards until we've found enough lines. */
		fpos_t pos;
		if(fgetpos(in,&pos) < 0)
			error("Unable to get file-position");

		/* search backwards until we've found the number of requested lines */
		size_t lines = 0;
		while(!ferror(in) && pos > 0 && lines < n) {
			size_t amount = MIN(BUF_SIZE,pos);
			pos -= amount;
			fseek(in,pos,SEEK_SET);
			for(size_t i = 0; i < amount; i++) {
				int c = fgetc(in);
				if(c == '\n')
					lines++;
			}
		}
		if(ferror(in))
			error("Read failed");

		/* print the lines */
		fseek(in,pos,SEEK_SET);
		int c;
		while((c = fgetc(in)) != EOF) {
			if(lines <= n)
				putchar(c);
			if(c == '\n') {
				if(ferror(stdout))
					break;
				lines--;
			}
		}
	}
	if(ferror(in))
		error("Read failed");
	if(ferror(stdout))
		error("Write failed");

	/* clean up */
	if(optind < argc)
		fclose(in);
	return EXIT_SUCCESS;
}
コード例 #9
0
static
int
globus_l_job_manager_parse_events(
    globus_l_job_manager_logfile_state_t *      state)
{
    int                                 rc;
    int                                 protocol_msg_type;
    time_t                              stamp;
    char                                jobid[129];
    char                                nl[2];
    int                                 job_state;
    int                                 exit_code;
    struct tm                           gmstamp, *gmstampp;
    fpos_t                              pos;

    SEG_JOB_MANAGER_DEBUG(SEG_JOB_MANAGER_DEBUG_INFO,
            ("globus_l_job_manager_parse_events() called\n"));


    fgetpos(state->fp, &pos);
    while ((rc = fscanf(state->fp, "%d;%ld;%128[^;];%d;%d%1[\n]",
                    &protocol_msg_type,
                    &stamp,
                    jobid,
                    &job_state,
                    &exit_code,
                    nl)) > 4)
    {
        if (rc == 4 && fscanf(state->fp, "%1[\n]", nl) != 1)
        {
            goto bad_line;
        }

        if (protocol_msg_type != 1)
        {
            goto bad_line;
        }

        gmstampp = globus_libc_gmtime_r(&stamp, &gmstamp);

        if (globus_l_time_is_newer(&state->start_timestamp, &gmstamp))
        {
            /* Ignore events that occur before our start timestamp */
            goto bad_line;
        }

        switch(job_state)
        {
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING:
            globus_scheduler_event_pending(stamp, jobid);
            break;

        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE:
            globus_scheduler_event_active(stamp, jobid);
            break;

        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE:
            globus_scheduler_event_done(stamp, jobid, exit_code);
            break;

        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED:
            globus_scheduler_event_failed(stamp, jobid, exit_code);
            break;

        default:
            goto bad_line;
        }
bad_line:
        fgetpos(state->fp, &pos);
    }
    if (feof(state->fp))
    {
        clearerr(state->fp);
        rc = SEG_JOB_MANAGER_ERROR_LOG_EOF;
    }
    else
    {
        rc = 0;
    }
    fsetpos(state->fp, &pos);

    SEG_JOB_MANAGER_DEBUG(SEG_JOB_MANAGER_DEBUG_INFO,
            ("globus_l_job_manager_parse_events() exits\n"));
    return rc;
}
コード例 #10
0
//
//	Declaration:
//	bool CBinaryDataFile::ReadEvtHeader(const CString &strNameWithPath, CString *pstrErrorMsg)
//
//	Input:	strNameWithPath		filename with full path that is to be opened
//			
//	Output:	pstrErrorMsg	error, if any
//
//			mdVersionNumber		version number read from header
//			msStaNum			station number read from header
//			miYr				year read from header
//			miMon				month read from header
//			miDay				day read from header
//			mulTimestampOfFirstRecordInFile		julian time of first record in the file
//
//	Return:	true (header read) / false (some kind of error, see pstrErroMsg)
//	
//  date    /	author	revision
//  -----------------	--------
//	17-Oct-2002	SFK		Created from ReadBIDHeader in GrandDataFile.cpp in GRAND COM
//	21-Jun-2005 SFK		Pick up station number from first record rather than getting it
//						out of the header due to a problem in MIC 1.9.0.7
//////////////////////////////////////////////////////////////////
bool CBinaryDataFile::ReadEvtHeader(const CString &strNameWithPath, CString *pstrErrorMsg)
{
    int iHdrSize;
    char str[54];
	struct BinaryEventFileRec Rec;
	struct OpCode OpRec;
	fpos_t pos;
	CMyDateTime MyDate;

   /* ------------------------------------------------------------------
    *	Open the file
    * ----------------------------------------------------------------*/
	if (!OpenDataFile(strNameWithPath, pstrErrorMsg)) return(false);

	// generate an error message in case we get an error in any of the reads,
	// will clear at end of function if all okay
	if (pstrErrorMsg) {
		miErrorNum = iFILE_READ_ERR;
		pstrErrorMsg->Format("\nError: Unexpected error reading header for file %s", strNameWithPath);
	}
	
   /* ------------------------------------------------------------------
    *	Read the first 4 bytes to get the number of bytes in header.
    *	Based on the location of the number, determine whether the data
    *	file is from CDMPC or LANL GRAND Collect.  The CDMPC number
    *	must be decremented by 1.
    * ----------------------------------------------------------------*/
    if (fread(str, 4, 1, mpFile) != 1) return(false);
    str[4] = '\0';    
    iHdrSize = atoi(str);
    if (str[3] == ' ') iHdrSize = iHdrSize - 1; // this file formed by CDMPC
	if (iHdrSize <= 22) return(false);

   /* ------------------------------------------------------------------
    *	The next 5 bytes no longer contain useful information,	just
    *	skip by them.
    * ----------------------------------------------------------------*/
    if (fread(str, 5, 1, mpFile) != 1) return(false);
       
   /* ------------------------------------------------------------------
    *	Read past the version number in the next 5 bytes.
    * ----------------------------------------------------------------*/
    if (fread(str, 5, 1, mpFile) != 1) return(false);
	str[5] = '\0';
	mdVersionNumber = atof(str);

   /* ------------------------------------------------------------------
    *  Read station of logging node and put it in return variable.
    * ----------------------------------------------------------------*/
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    msStaNum = atoi(str);
//	msStaNum += 1000;

   /* ------------------------------------------------------------------
    *	Read year and put it in return variable.
    * ----------------------------------------------------------------*/
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    miYr = atoi(str);
	//3-aug-2005 hn Added a four digit year.
	if (miYr < 86)
	{
		miYr4 = miYr + 2000;
	}
	else
	{
		miYr4 = miYr + 1900;
	}


   /* ------------------------------------------------------------------
    *	Read month and put it in return variable.
    * ----------------------------------------------------------------*/
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    miMon = atoi(str);
	if ((miMon < 1) || (miMon >12)) return(false);

   /* ------------------------------------------------------------------
    *	Read day and put it in return variable.
    * ----------------------------------------------------------------*/
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    miDay = atoi(str);
	if ((miDay < 1) || (miDay >31)) return(false);

	/* ------------------------------------------------------------------
    *	Read past the expansion space in the header so the file pointer
    *	is positioned at the beginning of the first data point at exit.
    * ----------------------------------------------------------------*/
    if (fread(str, (iHdrSize - 22), 1, mpFile)!= 1) return(false);
    
   /* ------------------------------------------------------------------
    *	Save the position of the file pointer.
	*	Read the first record in the file to get the time of it.  Will
	*	read the first record as if it is a binary record since we are
	*	only interested in the julian time in the record and that is in
	*	the same position in all types of records in an .evt/.bny file.
    *	Restore file pointer to just at the first record.
    * ----------------------------------------------------------------*/
	if(fgetpos(mpFile, &pos ) != 0) return(false);
	// 01-Sep-2005 SFK Fixed for reading old files that might have other types
	//					of records interspersed with binary records.
	bool bFoundBinaryRecord = false;
	do {
		if (fread(&OpRec, sizeof(struct OpCode), 1, mpFile) == 0) return(false);
		if ((OpRec.RecTypeA == '3') && (OpRec.RecTypeB == '2')) {
			if (fread(&Rec, sizeof(struct BinaryEventFileRec), 1, mpFile) == 0) return(false);
			msStaNum = Rec.usNode;		// 21-Jun-2005 SFK  Pick up station number from first record
			mdTimestampOfFirstRecordInFile = MyDate.MyTimestampToDATETimestamp((double)Rec.uiTime);
			if(fsetpos(mpFile, &pos ) != 0) return(false);
			bFoundBinaryRecord = true;
		}
		else {	// was not a binary record -- skip past the record
			fpos_t FilePosition;
			fgetpos(mpFile, &FilePosition);
			FilePosition += OpRec.sRecSize - 2;	// subtract the opcode bytes that you've already read
			fsetpos(mpFile, &FilePosition);
		}
	} while (!bFoundBinaryRecord);
               
	if (pstrErrorMsg) pstrErrorMsg->Empty();
	miErrorNum = 0;	// no error
    return(true);

}
コード例 #11
0
///////////////////////////////////////////////////////////////////////////
//	Name:	ReadEvtDataFile
//
//	Description:
//	Read the data from a single data file and put it into an Access database.
//	This routine supports the *.EVT format. 
//
//	An .EVT file can have several types of records in the file.  The first two characters 
//	determine the type of record.  The types possible are:
//							RecTypeA	RecTypeB		Number of Characters
//		BINARYEVENT_REC		"3"			"2"				13
//		VACOSSINFO_REC		"3"			"5"				31
//		VACOSSEVENT_REC		"3"			"6"				44
//		VACOSSERROR_REC		"3"			"9"				16
//		MICROGRAND_REC		"4"			"0"				17
//		GPSDATA_REC			"3"			"A"				65
//
//	The formats of these records can be found at the beginning of this file.
//
//	In addition to reading the file, information about the data read such
//	as (first record, last record, number of records read, number of records
//	out of order, number of records taken during offsets, number of records
//	with timestamps not in the day, etc) are reported and also whether the day was overwritten
//	or added to in the database.
//
//	Declaration:
//	bool CBinaryDataFile::ReadBinDataFile(CDbAccess* pDb, const CString &strNameWithPath, CString *pstrMsg)
//
//	Input:	
//			strNameWithPath		filename with full path that is to be opened
//			
//	Output:	pstrErrorMsg	error, if any
//
//			mdVersionNumber		version number read from header
//			msStaNum			station number read from header
//			miYr				year read from header
//			miMon				month read from header
//			miDay				day read from header
//			mulTimestampOfFirstRecordInFile		julian time of first record in the file
//			miErrorNum			problem during read
//				iFILE_READ_ERROR
//				iSKIP_FILE
//				iDB_BAD
//	
//	Return:	true (header read) / false (some kind of error, see pstrErroMsg and miErrorNum)
//	
//  date    /	author	revision
//  -----------------	--------
//	10-Dec-2001	SFK		Created from ImportData in DbImport.cpp
//////////////////////////////////////////////////////////////////
bool CBinaryDataFile::ReadEvtDataFile(CDbAccess* pDb, const CString &strNameWithPath, CString *pstrMsg)
{

	char szDateStr[MAX_DT_LEN+1], szFirst[MAX_DT_LEN+1], szLast[MAX_DT_LEN+1], szDum[MAX_DT_LEN+1];
	bool bOverwrote = false;
	CString TempStr;

	CFacCfgWrap FacCfg;	// 28-Sep-2005 SFK Removed static

	CDirUtilities Dir(m_bQuietMode);  //QUIET MODE IS ALWAYS OFF FOR the non-NDAR Binary Component pjm 11/27/2007 for B2R1

	CMyDateTime MyDate;
	
	//	During read of header, got the station number associated with this file.
	//	Verify from the	Facility Configuration Com that this is a valid station
	//	number and is a BINARY type.
	struct db_sta_rec dbSta;
	bool bExists = FacCfg.GetStationRecord(pDb->msFacNum, msStaNum, &dbSta);

	if (!bExists) {
		if (pstrMsg) pstrMsg->Format("\nError: Skipping file %s with unknown station %d",Dir.StripPathFromFilename(strNameWithPath), msStaNum);
		if (mpFile) fclose(mpFile);
	    return(false);
	}
	if (dbSta.s_sta_type != BINARY_TYPE) {
		if (pstrMsg) pstrMsg->Format("\nError: Skipping file %s with unexpected station type %d",Dir.StripPathFromFilename(strNameWithPath), dbSta.s_sta_type);
		if (mpFile) fclose(mpFile);
	    return(false);
	}

	// By the time get here, know we have binary data and a valid station number
	CString strStationName = dbSta.sz_sta_name;
	CBinaryData BInst(pDb, msStaNum, -1, BINARY_INST, true, m_bQuietMode);
	
    //	Determine the limits of julian times that belong in the day referred to in the file header
    sprintf(szDateStr,"%02d.%02d.%02d", miYr, miMon, miDay); 
	DB_D_INTERVAL FileDay, ActualDay;
	FileDay.dStart = MyDate.DateTimeStrsToDATETimestamp(szDateStr, "00:00:00");
	FileDay.dEnd = FileDay.dStart + 86399.0/86400.0;
	DATE dFirstTimeInNextDay = FileDay.dStart + 1.0;
    
       
	//	Check if any data already exists for this day for this node in database
	unsigned long ulPtsInDay = 0;
	bool bStatus = BInst.DayExists(FileDay.dStart, &ulPtsInDay, &ActualDay);

	//	If data already in database for the day, check if the new data can be appended to
	//	the existing data.  If not, if bOverwrite = true then delete all data for the day;
	//	if bOverwrite = false, then ask a question as to whether they want to overwrite the
	//	day's data.
	bool bDayAlreadyExists = false;
	if (ulPtsInDay > 0) {		// ulPtsInDay nonzero says some data from day in database
		if (mdTimestampOfFirstRecordInFile > ActualDay.dEnd) {	// data beyond end of db, can add to end of day
			bDayAlreadyExists = true;
		}
		else {	// our data file overlaps with data already in db
			if (!mbOverwrite) { // don't automatically overwrite data
        		TempStr.Format("File %s contains data from %s which already exists in database.  Do you want to overwrite the day?",
					mstrFilenameWithPath, szDateStr);
				if (MessageBox(NULL, TempStr, "EventCom: Day Already In Database", MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2) == IDNO) {
					if (pstrMsg) pstrMsg->Format("\nSkipping file %s (date=%02d.%02d.%02d): day's data already in database", 
						mstrFilenameWithPath, miYr, miMon, miDay);
					if (mpFile) CloseDataFile();
					miErrorNum = iSKIP_FILE;
					return(false);
				}
			}	
			// are going to overwrite the data so delete the entire day
			bStatus = BInst.DeleteDay(FileDay.dStart);
			if (!bStatus) { //if (glpDb->DeleteData(&BInst, DayRequested)) {
				miErrorNum = bStatus;
				if (mpFile) CloseDataFile();
				return(false);	
			}
   			bOverwrote = true;
			ulPtsInDay = 0;
		}
	}	
		
	int i = 0;
	unsigned long ulPtsInFile = 0;
	BinaryEventFileRec FilePt;
	OpCode Op;
	// To do yet:  add records for other types of data in event file
	DATE dCurrentPtTime = 0;
	DATE dLastPtTime;
	int iDuplicateTimestamps = 0;
	int iInvalidData = 0;
	int iOutOfOrder = 0;
	dLastPtTime = 0.0;
	short sLastStation = -1;
	CBinaryRawDataPt PtsForDb[NUM_RAW_DATA_PTS_IN_GROUP+16];	// allow for extra records written on last raw data point
	bool bFirstPoint = true;
	DATE dFirstValidTimeInFile;

	//	Read records from the raw data file one at a time.  First read the opcode and
	//	decide what kind of record you have.  Skip past any records other than the BinaryEvent type.
	//	For each data point in the BinaryEventRecs check whether it has the same timestamp as
	//	the previous point -- if yes, add a small increment to the timestamp read from the file.
	//	Also check if the point really belongs in the day and whether it is in chronological order.
	//	After you have accumulated a bunch of points, do a write to the database.
	while (fread(&Op, sizeof(struct OpCode), 1, mpFile) != 0) {	// read the op code
		// if record is BinaryEvent type, then read the record and process it.
		if ((Op.RecTypeA == '3') && (Op.RecTypeB == '2')) {
			fread(&FilePt, sizeof(struct BinaryEventFileRec), 1, mpFile);

			dCurrentPtTime = MyDate.MyTimestampToDATETimestamp(FilePt.uiTime);
			ulPtsInFile++;		// this just counts whether there was a point to read

			// catch two raw data points from the same node with the same timestamp
			if ((dCurrentPtTime == dLastPtTime) && (sLastStation == FilePt.usNode) ){			
				dCurrentPtTime = dLastPtTime + dINCREMENT;	// the base time of this point is past the last point's time
				iDuplicateTimestamps++;						// count the number of data points with duplicate timestamps
				FilePt.bStatus = FilePt.bStatus | DUPLICATE_POINT_BIT;
			}

			//	If the point's time is not in this day, note it and skip this point.
			if ((dCurrentPtTime < FileDay.dStart) || (dCurrentPtTime > FileDay.dEnd)) {
				iInvalidData++;
				continue;
			}
			
			//	If the point is out of order count it.  
			//	If the points are in order then set a new prev point
			//  Since there can be multiple node numbers logged into a single
			//	file, check that the node numbers are the same when
			//	checking if out of order.
			if (bFirstPoint) {
				dLastPtTime = dCurrentPtTime;
				sLastStation = FilePt.usNode;
			}
			if ((dCurrentPtTime < dLastPtTime)  && (sLastStation == FilePt.usNode)){
				iOutOfOrder++;
				if (mbSkipOutOfOrder == true) continue;
				FilePt.bStatus = FilePt.bStatus | OUT_OF_ORDER_BIT;
			}
			else {
				dLastPtTime = dCurrentPtTime;			// set for the next point read
				sLastStation = FilePt.usNode;
			}

			//	The largest point in the day is the last point and the smallest
			//	point in the day is the first point.
			if (dCurrentPtTime > ActualDay.dEnd) ActualDay.dEnd = dCurrentPtTime;
			if (bFirstPoint) {
				ActualDay.dStart = dCurrentPtTime;
				dFirstValidTimeInFile = dCurrentPtTime;
				bFirstPoint = false;
			}	
			else {
				if (dCurrentPtTime < ActualDay.dStart) {
					ActualDay.dStart = dCurrentPtTime;
					dFirstValidTimeInFile = dCurrentPtTime;
				}
			}

			// generate the stuff to write into the database
			// write one data record for each bit in the mask that is set
			BYTE bTempMask = 1;
			for (int j=0; j< 8; j++) {
				if ((bTempMask & FilePt.bMask) == bTempMask) {	// check if this bit is set
					PtsForDb[i].m_dJulianTime = dCurrentPtTime;
					PtsForDb[i].m_ucStatus = FilePt.bStatus;
					//if ((FilePt.bState & bTempMask) != 0) PtsForDb[i].m_ucState = 1;	// determine state of this bit
					//else PtsForDb[i].m_ucState = 0;
					PtsForDb[i].m_ucState = FilePt.bState;
					PtsForDb[i].m_usBit = j+1;			// bit number 1-8 corresponds to channel 1-8
					PtsForDb[i].m_ucReserved = FilePt.bReserved;
					PtsForDb[i].m_usLogNodeNumber = msStaNum;				// the "station" number in the file is really the log node number
//					PtsForDb[i].m_usStationNumber = FilePt.usNode + 1000;	// this is the real station of the data -- where the binary data came from
					PtsForDb[i].m_usStationNumber = FilePt.usNode;			// this is the real station of the data -- where the binary data came from
					i++;	// count the point just read from the file
				}
				bTempMask = bTempMask << 1;		// go on to next bit
			}
		   
			// when accumulate enough data points, write the group to the database
			if (i >= NUM_RAW_DATA_PTS_IN_GROUP) {
				BInst.m_ulNumPtsRequested = i;
				BInst.m_pBinaryRawPts = PtsForDb;
				BInst.AddData(pstrMsg);
				ulPtsInDay += i;	// count points read so far
				i = 0;
			}
		}
		else {	// was not a binary record -- skip past the record
			fpos_t FilePosition;
			fgetpos(mpFile, &FilePosition);
			FilePosition += Op.sRecSize - 2;	// subtract the opcode bytes that you've already read
			fsetpos(mpFile, &FilePosition);
		}
	}


	//	Got an error reading the data file.  Are expecting an EOF
	//	error.	If it's anything else, then abort and delete the partial
	//	data already in the db.  If it's EOF, close	the raw data file
	//	and continue.
	if (feof(mpFile) == 0) {		// check for any error other than end of file
		if (pstrMsg) pstrMsg->Format("\nImport Error Reading File %s.  File Error = %s", mstrFilenameWithPath, strerror(errno));
		if (mpFile) CloseDataFile();
		BInst.DeleteDay(FileDay.dStart);
		if (mpFile) CloseDataFile();
		return(false);
	}
	if (mpFile) CloseDataFile();

	//	Are at the end of the raw data file.  So write whatever points
	//	we have read in our group to the database
	if (i > 0) {
		BInst.m_ulNumPtsRequested = i;
		BInst.m_pBinaryRawPts = PtsForDb;
		BInst.AddData(pstrMsg);
		ulPtsInDay += i;	// accumulate points read so far
	}

	//	If there was no data for this day, make sure there is nothing about
	//	this day in the database.
	//	Print out some hints for the user as to why no data points are in the day.
	if (ulPtsInDay == 0) {
		BInst.DeleteDay(FileDay.dStart);		// delete data for entire day
		if (pstrMsg) {							// Can return hints to caller
			if (ulPtsInFile == 0) {				// the file is completely empty
				pstrMsg->Format("\nImport Warning Reading File %s.  No binary data in file.", Dir.StripPathFromFilename(strNameWithPath));
			}
			else {
				if (iInvalidData> 0) {			// all times were invalid
					MyDate.DATETimestampToDateTimeStrs(dCurrentPtTime, szFirst, szDum, GEN_DTF_IAEA, GEN_DTF_HMSM);
    				pstrMsg->Format("\nImport Error Reading File %s.  Header indicates data from %s, file data from %s\n", Dir.StripPathFromFilename(strNameWithPath), szDateStr, szFirst);
				}
   				if (iOutOfOrder > 0)			// all data was out of order
					TempStr.Format("\nImport Error Reading File %s.  %5d pts out of order.", Dir.StripPathFromFilename(strNameWithPath), iOutOfOrder);
				*pstrMsg += TempStr;
			}
		}    
		return(true);
	}

	//	Log which file was just imported successfully.  Include date, station
	//	name, file name and first/last time in the file.
	//	Also log if the day's data was overwritten and if there were
	//	any points out of order or data with invalid times.
	if (pstrMsg) { 
		MyDate.DATETimestampToDateTimeStrs(dFirstValidTimeInFile, szDum, szFirst, GEN_DTF_IAEA, GEN_DTF_HMSM);
		MyDate.DATETimestampToDateTimeStrs(ActualDay.dEnd, szDum, szLast, GEN_DTF_IAEA, GEN_DTF_HMSM);		
		TempStr.Format("\n%s  %25s  %s    %s    %s    %5ld", szDateStr, strStationName, Dir.StripPathFromFilename(strNameWithPath), szFirst, szLast, ulPtsInDay);
		*pstrMsg += TempStr;
		if (bOverwrote == TRUE) {
			TempStr.Format("  Overwrote existing day's data.");
			*pstrMsg += TempStr;
		}
		if (bDayAlreadyExists == TRUE) {
			TempStr.Format("  Added to existing day's data.");
			*pstrMsg += TempStr;
		}
		if (iOutOfOrder > 0) {
			TempStr.Format("  %5d pts out of order.", iOutOfOrder);
			*pstrMsg += TempStr;
		}
		if (iInvalidData > 0) {
			TempStr.Format("  %5d rec(s) with invalid times.",iInvalidData);
			*pstrMsg += TempStr;
		}
		if (iDuplicateTimestamps > 0) {
			TempStr.Format("  %5d rec(s) with duplicate times.",iDuplicateTimestamps);
			*pstrMsg += TempStr;
		}
	}    

	//  Calculate and add the information about this day's worth of data
	TempStr.Empty();
	BInst.AddDayData(ActualDay, ulPtsInDay, &TempStr);
	*pstrMsg += TempStr;
	
    return(true);
}
コード例 #12
0
////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Name	 : ReadBinHeader
//
//	Description:
//  Read header from binary raw data file (.BIN).  Bin files were created for use
//	with an early version of Media Tracker.  Defintion of header file
//	proposed in email from Pelowitz defines this format for the header structure:
//		struct TRGAcquireHeader	{	// 73 characters
//			char SizeOfHeader[4];
//			char unused1[5];
//			char Version[5];
//			char StationId[3];//node number
//			char Year[3];
//			char Month[3];
//			char Day[3];
//			char Year4[4];
//			char unused2[43];
//		};
//
//	Declaration:
//	int CBinaryDataFile::ReadBinHeader(char *szFilename, short *psSta,int *piYr, int *piMon, int *piDay, FILE **pHandle, double *pdVer, unsigned long *pulFirstRecordTime)
//
//	Input:	
//			strNameWithPath		filename with full path that is to be opened
//			
//	Output:	pstrErrorMsg	error, if any
//
//			mdVersionNumber		version number read from header
//			msStaNum			station number read from header
//			miYr				year read from header
//			miMon				month read from header
//			miDay				day read from header
//			mdTimestampOfFirstRecordInFile	time of first record in the file in DATE
//
//	Return:	true (header read) / false (some kind of error, see pstrErroMsg)
//	
//  date    /	author	revision
//  -----------------	--------
//	17-Oct-2002	SFK		Created from ReadBIDHeader in GrandDataFile.cpp in GRAND COM
////////////////////////////////////////////////////////////////////////////////////////////////
bool CBinaryDataFile::ReadBinHeader(const CString &strNameWithPath, CString *pstrErrorMsg)
{

    int iHdrSize;
    char str[54];
	fpos_t pos;
	struct TRGAcquireRecord BinaryRawPt;
	CMyDateTime MyDate;

	//	Open the file
	if (!OpenDataFile(strNameWithPath, pstrErrorMsg)) return(false);

	// generate an error message in case we get an error in any of the reads,
	// will clear at end of function if all okay
	if (pstrErrorMsg) {
		miErrorNum = iFILE_READ_ERR;
		pstrErrorMsg->Format("\nError: Unexpected error reading header for file %s", strNameWithPath);
	}
	
	//	Read the first 4 bytes to get the number of bytes in header.
    if (fread(str, 4, 1, mpFile) != 1) return(false);
    str[4] = '\0';    
    iHdrSize = atoi(str);
	if (iHdrSize <= 22) return(false);

    //	The next 5 bytes no longer contain useful information,	just
    //	skip by them.
    if (fread(str, 5, 1, mpFile) != 1) return(false);
       
    //	Read past the version number in the next 5 bytes.
    if (fread(str, 5, 1, mpFile) != 1) return(false);
	str[5] = '\0';
	mdVersionNumber = atof(str);

    //  Read station.
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    msStaNum = atoi(str);

    //	Read year.
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    miYr = atoi(str);

    //	Read month.
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    miMon = atoi(str);
	if ((miMon < 1) || (miMon >12)) return(false);

    //	Read day and put it in return variable.
    if (fread(str, 3, 1, mpFile) != 1) return(false);
    str[3] = '\0';
    miDay = atoi(str);
	if ((miDay < 1) || (miDay >31)) return(false);

    //	Read 4 digit year.
    if (fread(str, 4, 1, mpFile) != 1) return(false);
    str[4] = '\0';
    miYr4 = atoi(str);

    //	Read past the expansion space in the header so the file pointer
    //	is positioned at the beginning of the first data point at exit.
    if (fread(str, (iHdrSize - 26), 1, mpFile)!= 1) return(false);
    
    //	Save the position of the file pointer.
	//	Read the first record in the file to get the time of it.
    //	Restore file pointer to be positioned at the first record.
	if(fgetpos(mpFile, &pos ) != 0) return(false);
	if (fread(&BinaryRawPt, sizeof(struct TRGAcquireRecord), 1, mpFile) == 0) return(false);
	mdTimestampOfFirstRecordInFile = MyDate.MyTimestampToDATETimestamp((double)BinaryRawPt.uiJulianSeconds);
	if(fsetpos(mpFile, &pos ) != 0) return(false);
                    
    if (pstrErrorMsg) pstrErrorMsg->Empty();
	miErrorNum = 0;	// no error
    return(true);
}
コード例 #13
0
ファイル: psplit.c プロジェクト: gavin971/ncl
void from_ps(FILE *ifile, char *rname)
{
  FILE *output_file;
  int i,pict_count=0, line_count=0, npict;
  fpos_t *pict_start, tpos;
  char *pnum, *output_name;

/*
 *  Determine how many pictures and how many lines are in the file.
 */
  if (fseek(ifile,0L,SEEK_SET)) {
    printf("Error in repositioning the input file to the start\n");
    exit(10);
  }
  while (fgets(line, LINEL, ifile) != (char *) NULL) {
    if (!strncmp("%%Page:",line,7)) {
      pict_count++;
    }
    line_count++;
  }

/*
 *  Simply report the picture count and exit if requested.
 */
  if (count_only) {
    printf("For PostScript file %s:\n"  
           "  Number of pictures = %d\n", rname, pict_count);
    fclose(ifile);
    exit(4);
  }

/*
 *  Record the position numbers of where the pictures start and where
 *  the %%Trailer is.
 */
  pict_start = (fpos_t *) calloc(pict_count+1,sizeof(fpos_t));
  if ( pict_start == (fpos_t *) NULL ) {
    printf("Error in getting memory for file position pointers\n");
    exit(19);
  }
  if (fseek(ifile,0L,SEEK_SET)) {
    printf("Error in repositioning the input file to the start\n");
    exit(11);
  }
  npict = 0;
  for (i = 0; i < line_count; i++) {
    if (fgetpos(ifile,&tpos)) {
      printf("Error return from fgetpos.\n");
      exit(20);
    }
    fgets(line, LINEL, ifile);
    if (!strncmp("%%Page:",line,7)) {
      pict_start[npict] = tpos;
      npict++;
    } 
    else if (!strncmp("%%Trailer",line,9)) {
      pict_start[pict_count] = tpos;
    }
  }
  pnum = (char *) calloc(5,sizeof(char));
  output_name = (char *) calloc(strlen(rname)+1+4+4,sizeof(char));

/*
 *  Loop through the pictures.
 */
  for (i = 0; i < pict_count; i++) {

/*
 *  Create the output file name.
 */
    sprintf(pnum,"%04d",i+1);
    strcpy(output_name,"\0");
    strcat(output_name,rname);
    strcat(output_name,pnum);
    strcat(output_name,".ps");

/*
 *  Open the output file.
 */
    output_file = fopen(output_name,"w");
    if (output_file == (FILE *) NULL) {
       printf("Cannot open output file %s\n",output_name);
       exit(5);
    }
/*
 *  Write out the header, the prolog, and the color table.
 */
    begin_picture_ps(ifile,output_file);
    fflush(output_file);

/*
 *  Write out the picture body.
 */
    picture_body_ps(ifile, output_file, pict_start+i);
    fflush(output_file);

/*
 *  Write out the picture termination.
 */
    end_picture_ps(ifile, output_file, pict_start+pict_count);
    fflush(output_file);

/*
 *  Close the output file.
 */
    fclose(output_file);
  }

/*
 *  Free memory.
 */
  free(pict_start);
  free(pnum);
  free(output_name);

/*
 *  Close the input file.
 */
  fclose(ifile);
}
コード例 #14
0
ファイル: psplit.c プロジェクト: gavin971/ncl
void from_ncgm(FILE *ifile, char *rname)
{
  FILE *output_file;
  int i,pict_count=0;
  fpos_t *pict_start;
  char *pnum, *output_name;

  int line_count = 0;

/*
 *  Determine how many pictures are in the file.
 */
  while (fgets(line, LINEL, ifile) != (char *) NULL) {
    if (!strncmp(" h",line,2) || !strncmp("h ",line,2)) {
      pict_count++;
    }
  }

/*
 *  Simply report the picture count and exit if requested.
 */
  if (count_only) {
    printf("For PostScript file %s:\n"  
           "  Number of pictures = %d\n", rname, pict_count);
    fclose(ifile);
    exit(4);
  }

/*
 *  Record the position numbers of where the pictures start.
 */
  pict_start = (fpos_t *) calloc(pict_count+1,sizeof(fpos_t));
  if ( pict_start == (fpos_t *) NULL ) {
    printf("Error in getting memory for file position pointers\n");
    exit(19);
  }
  pict_count = 1;
  if (fseek(ifile,0L,SEEK_SET)) {
    printf("Error in repositioning the input file to the start\n");
    exit(9);
  }
  while (fgets(line, LINEL, ifile)) {
    line_count++;
    if (!strncmp(" h",line,2) || !strncmp("h ",line,2)) {
      if (fgetpos(ifile, pict_start+pict_count)) {
        printf("Error return from fgetpos.\n");
        exit(20);
      }
      pict_count++;
    }
    else if (!strncmp("/o {",line,4)) {
      if (fgetpos(ifile, pict_start)) {
        printf("Error return from fgetpos.\n");
        exit(20);
      }
    }
  }

/*
 *  pict_count is actually one larger than the total number
 *  of pictures, since we searched for picture end flags and
 *  added in where the first picture started.
 */
  pict_count--;

  pnum = (char *) calloc(5,sizeof(char));
  output_name = (char *) calloc(strlen(rname)+1+4+4,sizeof(char));

/*
 *  Loop through the pictures.
 */
  for (i = 0; i < pict_count; i++) {

/*
 *  Create the output file name.
 */
    sprintf(pnum,"%04d",i+1);
    strcpy(output_name,"\0");
    strcat(output_name,rname);
    strcat(output_name,pnum);
    strcat(output_name,".ps");

/*
 *  Open the output file.
 */
    output_file = fopen(output_name,"w");
    if (output_file == (FILE *) NULL) {
       printf("Cannot open output file %s\n",output_name);
       exit(5);
    }
/*
 *  Write out the header, the prolog, and the color table.
 */
    begin_picture(output_file);
    fflush(output_file);

/*
 *  Write out the picture body, pict_start+i is the start
 *  position for picture i.
 *  
 */
    picture_body(ifile, output_file, pict_start+i);
    fflush(output_file);

/*
 *  Write out the picture termination.
 */
    end_picture(output_file);
    fflush(output_file);

/*
 *  Close the output file.
 */
    fclose(output_file);
  }

/*
 *  Free memory.
 */
  free(pict_start);
  free(pnum);
  free(output_name);

/*
 *  Close the input file.
 */
  fclose(ifile);
}
コード例 #15
0
ファイル: main.c プロジェクト: Taifuru/winapi_sozluk
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//Degiskenlere degerleri atilarak islemin yapilip yapilmadigiyla alakali kontroller
	//ve kullaniciya geri bildirim
	if( (sozluk = fopen("sozluk.txt", "r") ) == NULL)
	{
		printf("sozluk.txt Açýlamadý! \n");
		getch();
		exit(0);
	}
	else if( ( turkce = fopen("turkce.txt", "w") ) == NULL)
	{
		printf("turkce.txt Açýlamadý! \n");
		getch();
		exit(0);
	}
	else if ( ( ingilizce = fopen("ingilizce.txt", "w") ) == NULL )
	{
		printf("ingilizce.txt Açýlamadý! \n");
		getch();
		exit(0);
	}
	
	//sozluk.txt deki imleç konumu daha sonra kullanýlmak üzere alýnýyor
	fgetpos(sozluk, &konum);
	
	//Dosya okumasý sonrasý kaç kelime okunduðu tutuluyor
	int uzunluk = dosyaOkuma(sozluk, siralanacakKelimeler);
	
	//Türkçe kelimelerin alfabetik sýraya konulmasý
	siralama(siralanacakKelimeler, uzunluk);
	
	//Alfabetik sýraya dizilen kelimelerin ait olduklarý dosyalara yazýlmasý
	dosyaYazma(sozluk,  turkce,  ingilizce, siralanacakKelimeler,&konum, uzunluk);
	
	//Dosyalar yazma modunda açýlmýþtý. O nedenle kapatýp tekrar okuma modunda açýyoruz
	fclose(turkce);
	fclose(ingilizce);
	fclose(sozluk);
	
	if( ( turkce = fopen("turkce.txt", "r") ) == NULL)
	{
		printf("turkce.txt Açýlamadý! \n");
		exit(0);
	}
	else if ( ( ingilizce = fopen("ingilizce.txt", "r") ) == NULL )
	{
		printf("ingilizce.txt Açýlamadý! \n");
		exit(0);
	}
	
	//Dosyalardaki konumlar daha sonra tekrar kullanýlmak üzere alýnýyor
	fgetpos(ingilizce, &konumIng);
	fgetpos(turkce, &konumTr);
	
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND anaPencere; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG Msg; /* A temporary location for all messages */

	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+25);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	anaPencere = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Türkçe-Ýngilizce / Ýngilizce-Türkçe",WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		300, /* height */
		NULL,NULL,hInstance,NULL);

	if(anaPencere == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produce unreasonably high CPU usage
	*/
	while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&Msg); /* Translate key codes to chars if present */
		DispatchMessage(&Msg); /* Send it to WndProc */
	}
	
	fclose(sozluk);
	fclose(turkce);
	fclose(ingilizce);
	
	return Msg.wParam;
}
コード例 #16
0
void SetCookie(FILE *fp)
{
  int     iCookie;
  long    lSave_offset;
  char    buffer[256];
  char    *keystr;
  char    *retbuf;
  fpos_t  filepos;
  char    *mc_pos;
  char    *mc_clrpos;
  int     i;
  int     size;

  /**************************************************************************/
  /* Save file pointer initial setting                                      */
  /**************************************************************************/
  lSave_offset = ftell (fp);

  /**************************************************************************/
  /* Set file pointer to beginning of file                                  */
  /**************************************************************************/
  fseek(fp, 0, SEEK_SET);

  /**************************************************************************/
  /* Calculate magic cookie                                                 */
  /**************************************************************************/
  iCookie = CalculateMagicCookie(fp);
#ifdef DEBUG
  printf("Magic Cookie: %d\n", iCookie);
#endif

  /**************************************************************************/
  /* Find location in header and store cookie                               */
  /**************************************************************************/
  keystr = (char *)NULL;
  retbuf = buffer;
  i = 0;
  while ( !keystr && retbuf && (i < MAX_HEADER_LINES) ) {
    fgetpos(fp, &filepos);
    retbuf = fgets(buffer, 80, fp);
    keystr = strstr (buffer, "$KEY:");
    i++;
  }

  if (!keystr) {
#ifdef DEBUG
     printf("No '$KEY' keyword in file. Magic Cookie not written to file.\n");
#endif
     return;
  }
  mc_pos = strchr(keystr, ':');
  if (mc_pos) {
     mc_pos += 2;
     sprintf(mc_pos, "%i", iCookie);
     size = strlen(mc_pos);
     if (size < MAX_KEY_SIZE) {
        mc_clrpos = mc_pos + size;
        for (i=size; i < MAX_KEY_SIZE; i++) {
            *mc_clrpos = ' ';
            mc_clrpos++;
        }
     }
  }
  fsetpos (fp, &filepos);
  fputs (buffer, fp);

  /**************************************************************************/
  /* Set file pointer back to initial setting                               */
  /**************************************************************************/
  fseek (fp, lSave_offset, SEEK_SET);
  return;
}
コード例 #17
0
JNIEXPORT void JNICALL 
Java_xxl_core_io_raw_NativeRawAccess_open(JNIEnv *env, jobject obj, jstring jfilename) {
	FILE *jfilep; 			// int of the file/device
	jlong sectors;
	fpos_t length;
	// BOOL isDevice=FALSE;
	const jbyte *filename;		// Java needs UTF-coded strings, c needs ASCII
	jint sectorSize;
	
	DEBUG_OUTPUT("Enter open",0);
	init(env, obj);
	
	// do not call get methods before init!
	jfilep = (FILE*) getfilep(env, obj);
	sectorSize = getsectorSize(env, obj);
	// Converts utf to ASCII
	filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
	// got it?
	
	if (filename==NULL) {
		reportError(env,obj,"Filename NULL");
		return;
	}
	
	// Already a device open?
	if (jfilep!=0) {
		reportError(env,obj,"File already open");
		return;
	}

  	DEBUG_OUTPUT("Filename: %s",filename);
  	
  	//if ( (filename[0]=='\\') && (filename[1]=='\\') && (filename[2]=='.') && (filename[3]=='\\') )
  	//	isDevice=TRUE;
  		
	jfilep = fopen(filename,"rwbc");

	// Open failed?
	if (jfilep==NULL) {
		reportError(env,obj,"Open failed - file not found");
		return;
	}

	// Set the int inside the java object
	setfilep(env, obj, (jlong) jfilep);
  	DEBUG_OUTPUT("Filepointer: %d",(long) jfilep);

	setbuf(jfilep, NULL);

	if (fseek(jfilep,0,SEEK_END)!=0) {
		reportError(env,obj,"seek failed");
		return;
	}

	fgetpos(jfilep,&length);
	if (length==-1) {
		reportError(env,obj,"Size returned 0");
		return;
	}
	else  
		sectors = length/sectorSize;
		
  	DEBUG_OUTPUT("Sektoren: %d\n", (long) sectors);
	setsectors(env, obj, (jlong) sectors);
}
コード例 #18
0
ファイル: vsgen.c プロジェクト: anthcp/evalvid-2.7
int main(int cn, char *cl[])
{
  FILE *f1, *f2;
  fpos_t pos = { 0 };
  unsigned grab = 1, sizeS = 0, sizeD = 0, num = 0, off = 0, ins = 0, r = 0, w = 0;
  unsigned width = 352, height = 288, size = 152064, yuv = 0, loop = 0, n = 0;
  char *p = 0, *buf = 0, *dst = 0;
  enum scale scale = NONE;

  if (cn < 7) {
U:  puts("vsgen [options] <src.yuv> <dst.yuv>");
    puts("options:");
    puts("  -size n\tframe size [bytes]");
    puts("  -grab n\tgrab every n-th frame");
    puts("  -insert n\twrite each grabbed frame n times");
    puts("  -offset n\tdo not write first n frames");
    puts("  -num n\tnumber of frames to write (input is looped if required)");
    puts("  -[up|down]\tscales frame size (up = double size, down = half size");
    puts("  -to [422|420]\tconvert from YUV 4:2:0 to YUV 4:2:2 or vice versa");
    return EXIT_FAILURE;
  }

  while (++n < cn - 2) {
    p = cl[n];
    if (*p++ != '-') break;
    switch(*p) {
      case 's': if ((size = sizeS = strtoul(cl[++n], 0, 10)) == 0) goto U; break;
      case 'g': if ((grab = strtoul(cl[++n], 0, 10)) == 0) goto U; break;
      case 'i': ins = strtoul(cl[++n], 0, 10); break;
      case 'o': off = strtoul(cl[++n], 0, 10); break;
      case 'n': if ((num = strtoul(cl[++n], 0, 10)) == 0) goto U; break;
      case 'u': if (scale == DOWN) goto U; scale = UP; break;
      case 'd': if (scale == UP) goto U; scale = DOWN; break;
      case 't': if (420 != (yuv = strtoul(cl[++n], 0, 10)) && 422 != yuv) goto U; break;
      default : goto U;
    }
  }
  if (!sizeS || !num) goto U;

  if (scale || yuv) {
    if (sizeS == 704 * 288 * 3 / 2) {
      width = 704;
      height = 288;
    } else {
      width = sqrt(sizeS * 22 / 27);
      height = sqrt(sizeS * 6 / 11);
    }
    sizeD = scale == DOWN ? sizeS / 4 : sizeS * 4;
  }

  if ((f1 = fopen(cl[n], "rb")) == 0) goto A;
  if ((f2 = fopen(cl[n + 1], "wb")) == 0) goto B;
  if ((buf = malloc(sizeS)) == 0) goto E;
  if (scale) if ((dst = malloc(sizeD)) == 0) goto E;
  if (yuv == 422) {
    size = 4 * sizeS / 3;
    if ((buf = realloc(buf, size)) == 0) goto E;
  }

  fgetpos(f1, &pos);
  while (w < num) {
    if (1 != fread(buf, sizeS, 1, f1)) {
      if (ferror(f1)) goto C;
      if (feof(f1)) {
        if (0 != fsetpos(f1, &pos)) { perror("fsetpos"); break; }
        loop++;
        continue;
      }
    }
    if ((r++ % grab) == 0) {
      if (scale) scale_420(buf, width, height, dst, scale);
      if (yuv == 422) to422(buf, width, height);
      for (n = 0; n < ins + 1 && w < num; n++) {
        if (off == 0) {
          if (1 != fwrite(scale ? dst : buf, scale ? sizeD : size, 1, f2)) goto D;
	  w++;
	} else {
          off--;
	}
        printf("Frames: %u grabbed, %u written (input looped: %u times)\r", r, w, loop);
      }
    }
  }
  puts("");

  fclose(f1);
  fclose(f2);

  return 0;

A: fprintf(stderr, "\nError opening source video file.\n"); goto X;
B: fprintf(stderr, "\nError opening destination video file.\n"); goto X;
C: fprintf(stderr, "\nError reading source video file.\n"); goto X;
D: fprintf(stderr, "\nError writing destination video file.\n"); goto X;
E: fprintf(stderr, "\nNot enough memory.\n");

X: return EXIT_FAILURE;
}
コード例 #19
0
ファイル: batch.c プロジェクト: TijmenW/FreeDOS
char *readbatchline(int *eflag, char *textline, int size)
{
  /*
   * Read and return the next executable line from the current batch file
   *
   * If no batch file is current or no further executable lines are found
   * return NULL.
   *
   * Here we also look out for FOR bcontext structures which trigger the
   * FOR expansion code.
   *
   * Set eflag to 0 if line is not to be echoed else 1
   */

  char *first;
  char *ip;

  if (bc == 0)               /* No batch */
    return 0;

  dprintf(("readbatchline ()\n"));
  assert(textline);
  assert(size > 1);
  assert(eflag);

  ip = "";                      /* make sure ip != NULL in the first
  									iteration of the loop */
  while (bc)
  {
    first = 0;               /* by default return "no file" */

    if (bc->forvar)             /* If its a FOR context... */
    {
      int forvarlen;
      char
       *fv1,
       *sp,      /* pointer to prototype command */
       *dp,          /* Place to expand protoype */
       *fv;				       /* Next list element */

      if (chkCBreak(BREAK_FORCMD) || bc->shiftlevel > bc->numParams)
        /* End of list or User break so... */
      {
        exit_batch();           /* just exit this context */
        continue;
      }

      fv1 = fv = getArgCur(0);

	if (bc->ffind) {          /* First already done fo do next */
		if(FINDNEXT(bc->ffind) != 0) {		/* no next file */
          free(bc->ffind);      /* free the buffer */
          bc->ffind = 0;
          bc->shiftlevel++;     /* On to next list element */
          continue;
        }
	  fv = bc->ffind->ff_name;
	} else
	{
      if (strpbrk(fv, "?*") == 0) {      /* element is not wild file */
        bc->shiftlevel++;       /* No -> use it and shift list */
        fv1 = "";				/* No additional info */
      } else
        /* Wild file spec, find first (or next) file name */
      {

	  /*  For first find, allocate a find first block */
          if ((bc->ffind = (struct ffblk *)malloc(sizeof(struct ffblk)))
           == 0)
          {
            error_out_of_memory();
            exit_batch();		/* kill this FOR context */
            break;
          }

         if(FINDFIRST(fv, bc->ffind, FA_NORMAL) == 0) {
         	/* found a file */
         	*dfnfilename(fv) = '\0';	/* extract path */
        	fv = bc->ffind->ff_name;
         } else {			/* if not found use the string itself */
#if 0
			/* To use the pattern is not compatible with MS COMMAND */
			++bc->shiftlevel;
			fv1 = "";				/* No additional info */
#else
          free(bc->ffind);      /* free the buffer */
          bc->ffind = 0;
          bc->shiftlevel++;     /* On to next list element */
          continue;
#endif
        }

      }
      }

      /* At this point, fv points to parameter string */
      /* fv1 is the string usually set to the path to the
      	found file, otherwise it points to "" */

       sp = bc->forproto;      /* pointer to prototype command */
       dp = textline;          /* Place to expand protoype */

       assert(sp);
       assert(bc->forvar);

      forvarlen = strlen(bc->forvar);
      while (*sp)
      {
        if (memcmp(sp, bc->forvar, forvarlen) == 0)
          dp = stpcpy(stpcpy(dp, fv1), fv), sp += forvarlen;
        else
          *dp++ = *sp++;        /* Else just copy */
      }

      *dp = '\0';

      assert(dp - textline <= size);

      *eflag = echo;

      first = textline;
      break;
    }

    if (!bc->bfile)
    {                           /* modifyable batchfiles */
      if ((bc->bfile = fopen(bc->bfnam, "rt")) == 0)
      {
        error_bfile_vanished(bc->bfnam);
        exit_batch();
        continue;
      }
      bc->bclose = 1;
      if (bc->brewind)
      {
        bc->brewind = 0;        /* fopen() position at start of file */
        bc->blinecnt = 0;
      }
      else if (fsetpos(bc->bfile, &bc->bpos))
      {                         /* end of file reached */
        /* so says MS COMMAND */
        exit_batch();
        continue;
      }
    }
    else if(bc->brewind) {
    	rewind(bc->bfile);
    	bc->brewind = 0;
    	bc->blinecnt = 0;
    }

    assert(ip != 0);
    ++bc->blinecnt;
    if (chkCBreak(BREAK_BATCHFILE)      /* User break */
        || fgets(textline, size, bc->bfile) == 0     /* End of file.... */
        || (ip = textlineEnd(textline, size)) == 0)  /* line too long */
    {
      if (!ip)
        error_long_batchline(bc->bfnam, bc->blinecnt);

      exit_batch();

      continue;
    }

    /* Strip leading spaces and trailing space/control chars */
    rtrimsp(textline);
    first = ltrimcl(textline);

    assert(first);

    /* ignore empty lines */
    if (!*first)
      continue;

    if (*first == ':')
    {
      /* if a label is searched for test if we reached it */
      if(bc->blabel) {
        /* label: the 1st word immediately following the colon ':' */
		for(ip = ++first; isgraph(*ip); ++ip)
			;
        *ip = '\0';
        if (stricmp(first, bc->blabel) == 0)
        {                       /* OK found */
          free(bc->blabel);
          bc->blabel = 0;
        }
      }
      continue;                 /* ignore label */
    }

    if (bc->blabel)
      continue;                 /* we search for a label! */

    if (*first == '@')          /* don't echo this line */
    {
    	first = ltrimcl(first + 1);
      *eflag = 0;
    }
    else
      *eflag = echo;

    break;
  }

  if (bc && bc->bclose)
  {                             /* modifyable batchfiles - ska */
    fgetpos(bc->bfile, &bc->bpos);
    fclose(bc->bfile);
    bc->bfile = 0;
    bc->bclose = 0;
  }

  return first;
}
コード例 #20
0
ファイル: std.c プロジェクト: haijiaoqihao/cppcheck
void nullpointer(int value) {
    int res = 0;
    FILE *fp;

    // cppcheck-suppress nullPointer
    clearerr(0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    feof(0);
    // cppcheck-suppress nullPointer
    fgetc(0);
    // cppcheck-suppress nullPointer
    fclose(0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    ferror(0);
    // cppcheck-suppress nullPointer
    ftell(0);
    // cppcheck-suppress nullPointer
    puts(0);
    // cppcheck-suppress nullPointer
    fp=fopen(0,0);
    fclose(fp);
    fp = 0;
    // No FP
    fflush(0);
    // No FP
    // cppcheck-suppress redundantAssignment
    fp = freopen(0,"abc",stdin);
    fclose(fp);
    fp = 0;
    // cppcheck-suppress nullPointer
    fputc(0,0);
    // cppcheck-suppress nullPointer
    fputs(0,0);
    // cppcheck-suppress nullPointer
    fgetpos(0,0);
    // cppcheck-suppress nullPointer
    frexp(1.0,0);
    // cppcheck-suppress nullPointer
    fsetpos(0,0);
    // cppcheck-suppress nullPointer
    itoa(123,0,10);
    putchar(0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strchr(0,0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strlen(0);
    // cppcheck-suppress nullPointer
    strcpy(0,0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strspn(0,0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strcspn(0,0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strcoll(0,0);
    // cppcheck-suppress nullPointer
    strcat(0,0);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strcmp(0,0);
    // cppcheck-suppress nullPointer
    strncpy(0,0,1);
    // cppcheck-suppress nullPointer
    strncat(0,0,1);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strncmp(0,0,1);
    // cppcheck-suppress ignoredReturnValue
    // cppcheck-suppress nullPointer
    strstr(0,0);
    // cppcheck-suppress nullPointer
    strtoul(0,0,0);
    // cppcheck-suppress nullPointer
    strtoull(0,0,0);
    // cppcheck-suppress nullPointer
    strtol(0,0,0);

    // #6100 False positive nullPointer - calling mbstowcs(NULL,)
    res += mbstowcs(0,"",0);
    // cppcheck-suppress unreadVariable
    res += wcstombs(0,L"",0);

    strtok(NULL,"xyz");

    strxfrm(0,"foo",0);
    // TODO: error message
    strxfrm(0,"foo",42);

    snprintf(NULL, 0, "someformatstring"); // legal
    // cppcheck-suppress nullPointer
    snprintf(NULL, 42, "someformatstring"); // not legal
}
コード例 #21
0
ファイル: dat.c プロジェクト: TijmenW/FreeDOS
dat_t *
dat_fread (FILE *stream, int *count)
{
  /* reads a dat file, into an array pointed to by dat_ary.
     The number of elements in the array (0 may indicate an 
     error) is placed in count, returns dat_ary, the array
     which the user should free() when no longer needed. 
  */

  char str[STRLEN];				/* for reading the line */
  char *s;					/* temporary pointer */
  dat_t *dat_ary;				/* array to hold data */
  int dat_size = 0;			/* holds needed malloc'd size */
  fpos_t iFilePosition;             /* stores initial file position */

  /* Read the file until end of file */

  *count = 0;

  /* Do two reads, 1st time through just count how many there are */

  /* 1st get file position, so can reset after 1st time through,
     we could just seek to the beginning, but there is guarentee
     the stream is at the beginning.
  */
  if (fgetpos(stream, &iFilePosition)) return NULL;

  /* Do 1st pass on the file data, getting just the count */
  while (fgets (str, STRLEN, stream) != NULL) {

    /* Ignore the line if 1st character is a # */
    if (*str == '#') continue;

    /* Ignore blank lines */
    if ( (*str == '\0') || ((*str == '\n') && (*(str+1) == '\0')) ) continue;

    /* Increment counter */
    dat_size++;

  } /* while */

  /* Reset file position */
  if (fsetpos(stream, &iFilePosition)) return NULL;

  /* Allocate necessary space (should be all space that is needed) */
  if ( (dat_size == 0) || ((dat_ary = (dat_t *)malloc(sizeof(dat_t) * dat_size)) == NULL) )
    return NULL;

  /* Actually read in the data */
  while (fgets (str, STRLEN, stream) != NULL) {

    /* Ignore the line if 1st character is a # */
    if (*str == '#') continue;

    /* Ignore blank lines */
    if ( (*str == '\0') || ((*str == '\n') && (*(str+1) == '\0')) ) continue;

    /* Break up the string into tokens */

    s = strtok (str, " :");

    if (s == NULL)
      {
	strncpy (dat_ary[*count].name, "", 1);
      }

    else
      {
	strncpy (dat_ary[*count].name, s, DAT_NAME_LEN);
      }

    s = strtok (NULL, " :");

    if (s == NULL)
      {
	dat_ary[*count].rank = '?';
      }

    else
      {
	dat_ary[*count].rank = s[0];
      }
    
    /* Check on the length of the array */

    *count += 1;

    if (*count > dat_size) {
        printf("INTERNAL ERROR: 2nd processing of data file yielded extra entries!\n");
        exit(1);
    }
  } /* while */

  /* Return */

  return (dat_ary);
}
コード例 #22
0
ファイル: GenSection.c プロジェクト: AshleyDeSimone/edk2
STATUS
GenSectionCommonLeafSection (
  char    **InputFileName,
  int     InputFileNum,
  UINTN   SectionType,
  FILE    *OutFile
  )
/*++
        
Routine Description:
           
  Generate a leaf section of type other than EFI_SECTION_VERSION
  and EFI_SECTION_USER_INTERFACE. Input file must be well formed.
  The function won't validate the input file's contents. For
  common leaf sections, the input file may be a binary file.
  The utility will add section header to the file.
            
Arguments:
               
  InputFileName  - Name of the input file.
                
  InputFileNum   - Number of input files. Should be 1 for leaf section.

  SectionType    - A valid section type string

  OutFile        - Output file handle

Returns:
                       
  STATUS_ERROR            - can't continue
  STATUS_SUCCESS          - successful return

--*/
{
  UINT64                    InputFileLength;
  FILE                      *InFile;
  UINT8                     *Buffer;
  INTN                      TotalLength;
  EFI_COMMON_SECTION_HEADER CommonSect;
  STATUS                    Status;

  if (InputFileNum > 1) {
    Error (NULL, 0, 0, "invalid parameter", "more than one input file specified");
    return STATUS_ERROR;
  } else if (InputFileNum < 1) {
    Error (NULL, 0, 0, "no input file specified", NULL);
    return STATUS_ERROR;
  }
  //
  // Open the input file
  //
  InFile = fopen (InputFileName[0], "rb");
  if (InFile == NULL) {
    Error (NULL, 0, 0, InputFileName[0], "failed to open input file");
    return STATUS_ERROR;
  }

  Status  = STATUS_ERROR;
  Buffer  = NULL;
  //
  // Seek to the end of the input file so we can determine its size
  //
  fseek (InFile, 0, SEEK_END);
  fgetpos (InFile, &InputFileLength);
  fseek (InFile, 0, SEEK_SET);
  //
  // Fill in the fields in the local section header structure
  //
  CommonSect.Type = (EFI_SECTION_TYPE) SectionType;
  TotalLength     = sizeof (CommonSect) + (INTN) InputFileLength;
  //
  // Size must fit in 3 bytes
  //
  if (TotalLength >= MAX_SECTION_SIZE) {
    Error (NULL, 0, 0, InputFileName[0], "file size (0x%X) exceeds section size limit(%dM).", TotalLength, MAX_SECTION_SIZE>>20);
    goto Done;
  }
コード例 #23
0
ファイル: tables.c プロジェクト: 119/aircam-openwrt
/** Write this table.
 *  @param out the file writer
 *  @param td table data to be written
 *  @return -1 on error, or bytes written on success.
 */
int yytbl_data_fwrite (struct yytbl_writer *wr, struct yytbl_data *td)
{
	int  rv;
	flex_int32_t bwritten = 0;
	flex_int32_t i, total_len;
	fpos_t  pos;

	if ((rv = yytbl_write16 (wr, td->td_id)) < 0)
		return -1;
	bwritten += rv;

	if ((rv = yytbl_write16 (wr, td->td_flags)) < 0)
		return -1;
	bwritten += rv;

	if ((rv = yytbl_write32 (wr, td->td_hilen)) < 0)
		return -1;
	bwritten += rv;

	if ((rv = yytbl_write32 (wr, td->td_lolen)) < 0)
		return -1;
	bwritten += rv;

	total_len = yytbl_calc_total_len (td);
	for (i = 0; i < total_len; i++) {
		switch (YYTDFLAGS2BYTES (td->td_flags)) {
		case sizeof (flex_int8_t):
			rv = yytbl_write8 (wr, yytbl_data_geti (td, i));
			break;
		case sizeof (flex_int16_t):
			rv = yytbl_write16 (wr, yytbl_data_geti (td, i));
			break;
		case sizeof (flex_int32_t):
			rv = yytbl_write32 (wr, yytbl_data_geti (td, i));
			break;
		default:
			flex_die (_("invalid td_flags detected"));
		}
		if (rv < 0) {
			flex_die (_("error while writing tables"));
			return -1;
		}
		bwritten += rv;
	}

	/* Sanity check */
	if (bwritten != (int) (12 + total_len * YYTDFLAGS2BYTES (td->td_flags))) {
		flex_die (_("insanity detected"));
		return -1;
	}

	/* add padding */
	if ((rv = yytbl_write_pad64 (wr)) < 0) {
		flex_die (_("pad64 failed"));
		return -1;
	}
	bwritten += rv;

	/* Now go back and update the th_hsize member */
	if (fgetpos (wr->out, &pos) != 0
	    || fsetpos (wr->out, &(wr->th_ssize_pos)) != 0
	    || yytbl_write32 (wr, wr->total_written) < 0
	    || fsetpos (wr->out, &pos)) {
		flex_die (_("get|set|fwrite32 failed"));
		return -1;
	}
	else
		/* Don't count the int we just wrote. */
		wr->total_written -= sizeof (flex_int32_t);
	return bwritten;
}
コード例 #24
0
ファイル: readterm.c プロジェクト: edmcman/yap-6.3
static xarg *setReadEnv(Term opts, FEnv *fe, struct renv *re, int inp_stream) {
  CACHE_REGS
  LOCAL_VarTable = NULL;
  LOCAL_AnonVarTable = NULL;
  fe->cmod = CurrentModule;
  fe->enc = GLOBAL_Stream[inp_stream].encoding;
  xarg *args = Yap_ArgListToVector(opts, read_defs, READ_END);
  if (args == NULL) {
    return NULL;
  }

  re->bq = getBackQuotesFlag();
  if (args[READ_MODULE].used) {
    CurrentModule = args[READ_MODULE].tvalue;
  }
  if (args[READ_BACKQUOTED_STRING].used) {
    if (!setBackQuotesFlag(args[READ_BACKQUOTED_STRING].tvalue))
      return false;
  }
  if (args[READ_QUASI_QUOTATIONS].used) {
    fe->qq = args[READ_QUASI_QUOTATIONS].tvalue;
  } else {
    fe->qq = 0;
  }
  if (args[READ_COMMENTS].used) {
    fe->tcomms = args[READ_COMMENTS].tvalue;
    if (fe->tcomms == TermProlog)
      fe->tcomms = PROLOG_MODULE;
  } else {
    fe->tcomms = 0;
  }
  if (args[READ_TERM_POSITION].used) {
    fe->tp = args[READ_TERM_POSITION].tvalue;
  } else {
    fe->tp = 0;
  }
  if (args[READ_SINGLETONS].used) {
    fe->sp = args[READ_SINGLETONS].tvalue;
  } else {
    fe->sp = 0;
  }
  if (args[READ_SYNTAX_ERRORS].used) {
    re->sy = args[READ_SYNTAX_ERRORS].tvalue;
  } else {
    re->sy = TermError; // getYapFlag( MkAtomTerm(AtomSyntaxErrors) );
  }
  if (args[READ_VARIABLES].used) {
    fe->vp = args[READ_VARIABLES].tvalue;
  } else {
    fe->vp = 0;
  }
  if (args[READ_VARIABLE_NAMES].used) {
    fe->np = args[READ_VARIABLE_NAMES].tvalue;
  } else {
    fe->np = 0;
  }
  if (args[READ_CHARACTER_ESCAPES].used ||
      Yap_CharacterEscapes(CurrentModule)) {
    fe->ce = true;
  } else {
    fe->ce = false;
  }
  re->seekable = (GLOBAL_Stream[inp_stream].status & Seekable_Stream_f) != 0;
  if (re->seekable) {
#if HAVE_FGETPOS
    fgetpos(GLOBAL_Stream[inp_stream].file, &re->rpos);
#else
    re->cpos = GLOBAL_Stream[inp_stream].charcount;
#endif
  }
  if (args[READ_PRIORITY].used) {
    re->prio = IntegerOfTerm(args[READ_PRIORITY].tvalue);
    if (re->prio > GLOBAL_MaxPriority) {
      Yap_Error(DOMAIN_ERROR_OPERATOR_PRIORITY, opts,
                "max priority in Prolog is %d, not %ld", GLOBAL_MaxPriority,
                re->prio);
    }
  } else {
    re->prio = LOCAL_default_priority;
  }
  return args;
}
コード例 #25
0
ファイル: DevControl.cpp プロジェクト: crashatom/phoebemail
LONG CDevControl::StartUpgrade(LONG lLoginID, char *pchFileName, fUpgradeCallBack cbUpgrade, DWORD dwUser)
{
    if (m_pManager->IsDeviceValid((afk_device_s*)lLoginID) < 0)
    {
		m_pManager->SetLastError(NET_INVALID_HANDLE);
        return 0;
    }

	if (!pchFileName)
	{
		m_pManager->SetLastError(NET_ILLEGAL_PARAM);
		return 0;
	}

    afk_device_s *device = (afk_device_s*)lLoginID;

    st_Upgrade_Info* pUI = new st_Upgrade_Info;
	if (!pUI)
	{
		m_pManager->SetLastError(NET_SYSTEM_ERROR);
		return 0;
	}

	afk_channel_s *pchannel = 0;
	afk_upgrade_channel_param_s upgradechannel = {0};

	FILE *file = fopen(pchFileName, "rb");
    if (file)
    {
        fpos_t pos;
        fseek(file, 0, SEEK_END);
        fgetpos(file, &pos);
#ifdef WIN32
        upgradechannel.size = pos;
#else	//linux
        upgradechannel.size = pos.__pos;
#endif
    }
    else
    {
		delete pUI;
		m_pManager->SetLastError(NET_ERROR);
        return 0;
    }

	//判断升级文件类型,应用程序也应该做这部分工作,以免升错文件
	//...新协议中不区分升级类型,升级类型由设备去判断。升级包的头两个字节("DH"和"PK")
	int ret = 0;
	int nUpgradeFlag = 0;
	char tmpbuf[3];
	memset(tmpbuf, 0, 3);
	fseek(file, 0, SEEK_SET);
	fread(tmpbuf, 1, 2, file);
	if ((tmpbuf[0] == 'D' && tmpbuf[1] == 'H') || (tmpbuf[0] == 'P' && tmpbuf[1] == 'K'))
	{
		nUpgradeFlag = 1;	//bios
	}
	else if (tmpbuf[0] == 'U')	
	{
		nUpgradeFlag = 2;	//Web
	}
	else if (tmpbuf[0] == (char)0xC0 && tmpbuf[1] == (char)0x80)
	{
		nUpgradeFlag = 3;//boot
		if (upgradechannel.size <= 1202180) 
		{
			 //boot
		}
		else
		{
			//boot and bios
		}
	}
	else
	{
		goto e_clearup;
	}
	fclose(file);
	file = NULL;
	
	upgradechannel.filetype = nUpgradeFlag;
	upgradechannel.type = AFK_CHANNEL_UPLOAD_UPGRADE;

	pUI->pcsLock = new DEVMutex;
	if (!pUI->pcsLock)
	{
		m_pManager->SetLastError(NET_SYSTEM_ERROR);
		goto e_clearup;
	}
	
	ret = CreateEventEx(pUI->hRecEvent, TRUE, FALSE);
	if (ret < 0)
	{
		m_pManager->SetLastError(NET_SYSTEM_ERROR);
		goto e_clearup;
	}
	
	InterlockedSetEx(&pUI->life, 1);

	pUI->device = device;
	pUI->channel = 0;
	pUI->cbUpgrade = cbUpgrade;
	pUI->dwUser = dwUser;

    upgradechannel.base.func = UpgradeFunc; 
    upgradechannel.base.udata = pUI;
    strcpy(upgradechannel.filename, pchFileName);

    pchannel = (afk_channel_s*)device->open_channel(device, 
        AFK_CHANNEL_TYPE_UPGRADE, &upgradechannel);
    if (pchannel)
    {
		DWORD dwRet = WaitForSingleObjectEx(pUI->hRecEvent, 10*WAIT_TIME);
        ResetEventEx(pUI->hRecEvent);
        if (dwRet == WAIT_OBJECT_0)
        {
            pUI->channel = pchannel;

			m_csUI.Lock();
			m_lstUI.push_back(pUI);
			m_csUI.UnLock();
        }
		else
		{
			goto e_clearup;
		}
    }
	else
	{
		goto e_clearup;
	}

    return (LONG)pchannel;

e_clearup:
	if (file)
	{
		fclose(file);
		file = 0;
	}

	if (pchannel)
	{
		pchannel->close(pchannel);
		pchannel = 0;
	}
	
	if (pUI)
	{
		if (pUI->pcsLock)
		{
			delete pUI->pcsLock;
			pUI->pcsLock = 0;
		}
		
		CloseEventEx(pUI->hRecEvent);
		
		delete pUI;
		pUI = 0;
	}

	return 0;
}
コード例 #26
0
ファイル: key_utils.c プロジェクト: cawka/PyNDN
int
read_key_pem(FILE *fp, PyObject **py_private_key_ndn,
             PyObject **py_public_key_ndn, PyObject **py_public_key_digest,
             int *public_key_digest_len,
             char *password)
{
        struct ndn_pkey *private_key = NULL;
	PyObject *py_private_key = NULL, *py_public_key = NULL;
	unsigned long err, reason;
	fpos_t fpos;
	int r;
	int public_only;

	r = fgetpos(fp, &fpos);
	JUMP_IF_NEG(r, errno_error);

        private_key = (struct ndn_pkey *)PEM_read_PrivateKey(fp, NULL, NULL, password);
	if (private_key) {
		public_only = 0;
		goto success;
	}

	err = ERR_get_error();
	reason = ERR_GET_REASON(err);

	/* 108 was meaning that start line isn't recognized */
	if (reason == 108) {
		r = fsetpos(fp, &fpos);
		JUMP_IF_NEG(r, errno_error);

		private_key = (struct ndn_pkey *)PEM_read_PUBKEY (fp, NULL, NULL, NULL);
		if (private_key) {
			public_only = 1;
			goto success;
		}

		err = ERR_get_error();
		reason = ERR_GET_REASON(err);
	}

	{
		char buf[256];

		ERR_error_string_n(err, buf, sizeof(buf));
		PyErr_Format(g_PyExc_NDNKeyError, "Unable to read Private Key: %s",
				buf);
		goto error;
	}

success:

	r = ndn_keypair(public_only, private_key, py_private_key_ndn,
			py_public_key_ndn);
	JUMP_IF_NEG(r, error);

	r = create_public_key_digest(private_key, py_public_key_digest,
			public_key_digest_len);
	JUMP_IF_NEG(r, error);

	return 0;

errno_error:
	PyErr_SetFromErrno(PyExc_IOError);
error:
	Py_XDECREF(py_private_key);
	Py_XDECREF(py_public_key);
	if (private_key)
		EVP_PKEY_free((EVP_PKEY *)private_key);
	return -1;
}
コード例 #27
0
ファイル: kernel-dens.c プロジェクト: miguel-porto/ecoSpace
JNIEXPORT jint JNICALL Java_pt_floraon_ecospace_nativeFunctions_computeKernelDensities(JNIEnv *env, jclass obj, jstring filename, jstring outfilename, jintArray variables,jint freqthresh,jfloat sigmapercent,jboolean downweight) {
// NOTE: "variables" must be in increasing order! 0 is latitude, 1 is longitude, 2... are the other climatic variables
	const char *pfilename=(*env)->GetStringUTFChars(env, filename , NULL );
	const char *poutfilename=(*env)->GetStringUTFChars(env, outfilename , NULL );
	
	FILE *varsfile,*densfile,*freqfile;
	int nvars,i,j,k,*freqs,ntaxafiltered=0,*mapIDs,*outIDs,d1,d2,d1from,d1to,d2from,d2to,d3from,d3to,d1kern,d2kern,sidesq;
	int lastID=-1,d2p,kernelhalfside,kernelside,kernelsidesq;
	register int d3p,d3;
	register float *d3kern;
	jsize nrecs;
	jint ntaxa,*pIDs;
	VARIABLE *vararray;
	VARIABLEHEADER *varheader;
	int nvarstouse=(int)(*env)->GetArrayLength(env,variables);		// how many vars will be used for kernel density
	jint *pvariables=(*env)->GetIntArrayElements(env, variables, 0);
	float sigma;
	float *kernel,*tmpdens;
	unsigned long *weight;
	DENSITY *densities;
	bool anythingtosave=false,skiprec;
	size_t dummy;
	
	varsfile=fopen(STANDARDVARIABLEFILE(pfilename),"r");
	dummy=fread(&nrecs,sizeof(jsize),1,varsfile);
	dummy=fread(&ntaxa,sizeof(jint),1,varsfile);
	dummy=fread(&nvars,sizeof(int),1,varsfile);
//	fseek(varsfile,nvars*sizeof(tmp.filename)+sizeof(long)*ntaxa,SEEK_CUR);
	fseek(varsfile,sizeof(long)*ntaxa,SEEK_CUR);	// skip index

	{	// redirect stdout to file
		int fd;
		fpos_t pos;
		fflush(stdout);
		fgetpos(stdout, &pos);
		fd = dup(fileno(stdout));
		FILE *dummy=freopen("logfile.txt", "a", stdout);
	}


	vararray=malloc(nvarstouse*nrecs*sizeof(VARIABLE));
	varheader=malloc(nvarstouse*sizeof(VARIABLEHEADER));
	pIDs=malloc(nrecs*sizeof(jint));
	dummy=fread(pIDs,sizeof(jint),nrecs,varsfile);
	fseek(varsfile,2*sizeof(jfloat)*nrecs,SEEK_CUR);	// skip original coordinates
	weight=malloc(sizeof(long)*nrecs);
	dummy=fread(weight,sizeof(long),nrecs,varsfile);

	for(i=0;i<nvarstouse;i++) {
		fseek(varsfile,(sizeof(VARIABLEHEADER) + sizeof(VARIABLE)*nrecs)*(pvariables[i]-(i==0 ? 0 : (pvariables[i-1]+1))),SEEK_CUR);
		dummy=fread(&varheader[i],sizeof(VARIABLEHEADER),1,varsfile);
		printf("Variable %d: min %f max %f\n",pvariables[i],varheader[i].min,varheader[i].max);
		dummy=fread(&vararray[i*nrecs],sizeof(VARIABLE),nrecs,varsfile);
	}
	
// count the # of records of each taxon
	freqs=calloc(ntaxa,sizeof(int));
	for(i=0;i<nrecs;i++) freqs[pIDs[i]]++;
// write out frequencies in a text file (for java)
	freqfile=fopen(FREQANALYSISFILE(pfilename),"w");
	for(i=0;i<ntaxa;i++) fprintf(freqfile,"%d\n",freqs[i]);
	fclose(freqfile);
// count the # of taxa after filtering out rarest	
	for(i=0;i<ntaxa;i++) if(freqs[i] >= freqthresh) ntaxafiltered++;
// create a mapping of IDs: because some IDs were removed, make them sequential without holes (remember that memory is the limiting factor here!)
	mapIDs=malloc(ntaxa*sizeof(int));
	for(i=0;i<ntaxa;i++) mapIDs[i]=-1;
	for(i=0,j=0;i<nrecs;i++) {
		if(freqs[pIDs[i]] >= freqthresh) {
			if(mapIDs[pIDs[i]]==-1) {
				mapIDs[pIDs[i]]=j;
				j++;
			}
		}
	}
//for(i=0;i<ntaxa;i++) printf("%d ",mapIDs[i]);
	
// compute the resolution of the multidimensional space so that not too much memory is occupied
	side=(int)pow((float)(MAXMEMORYPERBATCH)/(float)ntaxafiltered,(float)1/nvarstouse);
	if(side>MAXSIDE) side=MAXSIDE;
//side=40;
	sidesq=side*side;
	sigma=side*sigmapercent;
	arraysize=(int)pow(side,nvarstouse);

	printf("Using a grid with a side of %d cells, in %d variables (dimensions).\n",side,nvarstouse);
	printf("Reading %d variables of %d records of %d taxa (after filtering out those with less than %d occurrences)...\n",nvars,nrecs,ntaxafiltered,freqthresh);
// build the kernel for this case
	kernel=buildKernel(side,sigma,nvarstouse,&kernelhalfside,&kernelside,&kernelsidesq);

// compute densities
// allocate N multidimensional arrays (multidimensional grids to compute kernel densities in each cell)
	densities=malloc(ntaxafiltered*sizeof(DENSITY));
	outIDs=calloc(ntaxafiltered,sizeof(int));

	for(i=0;i<ntaxafiltered;i++) {
		densities[i].density=malloc(arraysize);
		memset(densities[i].density,0,arraysize);
	}

	printf("Computing kernel densities");fflush(stdout);

// scale the variables to the size of the grid
	for(i=0;i<nrecs;i++) {	
		for(k=0;k<nvarstouse;k++) {
			if(vararray[i+nrecs*k]!=RASTERNODATA) vararray[i+nrecs*k]=(vararray[i+nrecs*k]*side)/10000;
		}
	}
	
	#pragma omp parallel private(i,k,skiprec,tmpdens,anythingtosave,d1from,d1to,d1,d1kern,d2,d2from,d2to,d2p,d2kern,d3,d3from,d3to,d3p,d3kern)
	{
		tmpdens=malloc(arraysize*sizeof(float));
		memset(tmpdens,0,arraysize*sizeof(float));

		#pragma omp for
		for(j=0;j<ntaxa;j++) {		// NOTE: this loop doesn't need the records to be sorted, that's why it takes much longer
// TODO: we might have an index here, i.e. for each taxon, a list of tthe respective records, but it's so fast that maybe it's not a big issue, we're talking about a few thousands of taxa only.
			if(freqs[j]<freqthresh) continue;
			anythingtosave=false;
			for(i=0;i<nrecs;i++) {	// iterate through all records in search for this taxon ACK!! give me an index
				if(pIDs[i]!=j) continue;
				for(k=0,skiprec=false;k<nvarstouse;k++) {	// check if any one of the variables is NA. if it is, skip this record
					if(vararray[i+nrecs*k] == RASTERNODATA) {
						skiprec=true;
						break;
					}
				}
				if(skiprec) continue;		// skip NAs
	// now yeah, create density surface by summing the kernels record by record			
	// since it is not computationally feasible more than 3 dimensions, just make the optimized code for each case...
				switch(nvarstouse) {
					case 1:
						d1from=(vararray[i]-kernelhalfside)<0 ? 0 : (vararray[i]-kernelhalfside);
						d1to=(vararray[i]+kernelhalfside+1>side ? side : vararray[i]+kernelhalfside+1);
					
						for(d1=d1from,d1kern=vararray[i]-kernelhalfside<0 ? kernelhalfside-vararray[i] : 0;d1<d1to;d1++,d1kern++) {
							tmpdens[d1]+=kernel[d1kern] * (downweight ? ((float)weight[i]/MULTIPLIER) : 1);
						}
						anythingtosave=true;
					break;
				
					case 2:
						d1from=(vararray[i]-kernelhalfside)<0 ? 0 : (vararray[i]-kernelhalfside);
						d1to=(vararray[i]+kernelhalfside+1>side ? side : vararray[i]+kernelhalfside+1);
						d2from=(vararray[i+nrecs]-kernelhalfside)<0 ? 0 : (vararray[i+nrecs]-kernelhalfside);
						d2to=(vararray[i+nrecs]+kernelhalfside+1>side ? side : vararray[i+nrecs]+kernelhalfside+1);
					
						for(d1=d1from,d1kern=vararray[i]-kernelhalfside<0 ? kernelhalfside-vararray[i] : 0;d1<d1to;d1++,d1kern++) {
							for(d2=d2from,d2p=d1+d2from*side,d2kern=d1kern+((vararray[i+nrecs]-kernelhalfside)<0 ? (kernelhalfside-vararray[i+nrecs])*kernelside : 0);d2<d2to;d2++,d2p+=side,d2kern+=kernelside) {
								tmpdens[d2p]+=kernel[d2kern] * (downweight ? ((float)weight[i]/MULTIPLIER) : 1);
							}
						}
						anythingtosave=true;
					break;
				
					case 3:
						d1from=(vararray[i]-kernelhalfside)<0 ? 0 : (vararray[i]-kernelhalfside);
						d1to=(vararray[i]+kernelhalfside+1>side ? side : vararray[i]+kernelhalfside+1);
						d2from=(vararray[i+nrecs]-kernelhalfside)<0 ? 0 : (vararray[i+nrecs]-kernelhalfside);
						d2to=(vararray[i+nrecs]+kernelhalfside+1>side ? side : vararray[i+nrecs]+kernelhalfside+1);
						d3from=(vararray[i+nrecs*2]-kernelhalfside)<0 ? 0 : (vararray[i+nrecs*2]-kernelhalfside);
						d3to=(vararray[i+nrecs*2]+kernelhalfside+1>side ? side : vararray[i+nrecs*2]+kernelhalfside+1);
						for(d1=d1from,d1kern=vararray[i]-kernelhalfside<0 ? kernelhalfside-vararray[i] : 0;
							d1<d1to;
							d1++,d1kern++) {
							for(d2=d2from,d2p=d1+d2from*side,d2kern=d1kern+((vararray[i+nrecs]-kernelhalfside)<0 ? (kernelhalfside-vararray[i+nrecs])*kernelside : 0)
								;d2<d2to
								;d2++,d2p+=side,d2kern+=kernelside) {
								for(d3=d3from,d3p=d2p+d3from*sidesq
										,d3kern=&kernel[d2kern+((vararray[i+nrecs*2]-kernelhalfside)<0 ? (kernelhalfside-vararray[i+nrecs*2])*kernelsidesq : 0)]
									;d3<d3to
									;d3++,d3p+=sidesq,d3kern+=kernelsidesq) {
									tmpdens[d3p]+=*d3kern * (downweight ? ((float)weight[i]/MULTIPLIER) : 1);
									//kernel[d3kern];
								}
							}
						}
						anythingtosave=true;
					break;
				}
			}	// end record loop
			if(anythingtosave) {
				saveKernelDensity(tmpdens,freqs[j],&densities[mapIDs[j]]);		// save kernel density of previous taxon
				outIDs[mapIDs[j]]=j;
			} else {
				saveKernelDensity(NULL, freqs[j], &densities[mapIDs[j]]);		// save kernel density of previous taxon
				outIDs[mapIDs[j]]=j;
//				outIDs[mapIDs[j]]=-1;
			}
			anythingtosave = false;
			memset(tmpdens,0,arraysize*sizeof(float));
			printf(".");
			fflush(stdout);
		}	// end taxon loop
	}
/*	THIS is the working code. Above still developing.
	for(i=0;i<nrecs;i++) {	// iterate through all records IMPORTANT: records must be sorted by taxon ID
		if(freqs[pIDs[i]]>=freqthresh) {
			if(pIDs[i]!=lastID) {	// this record is already a different species
				if(anythingtosave) {
					saveKernelDensity(tmpdens,freqs[lastID],&densities[mapIDs[lastID]]);		// save kernel density of previous taxon
					outIDs[mapIDs[lastID]]=lastID;
				} else outIDs[mapIDs[lastID]]=-1;
				anythingtosave=false;
				memset(tmpdens,0,arraysize*sizeof(float));
				lastID=pIDs[i];
				printf(".");
				fflush(stdout);
			}
// scale the variables to the size of the grid
			for(j=0,skiprec=false;j<nvarstouse;j++) {
				if(vararray[i+nrecs*j]==RASTERNODATA) {
					skiprec=true;
					continue;
				} else vararray[i+nrecs*j]=(vararray[i+nrecs*j]*side)/10000;
			}
			if(skiprec) continue;		// skip NAs
// now yeah, create density surface by summing the kernels record by record			
// since it is not computationally feasible more than 3 dimensions, just make the optimized code for each case...
			switch(nvarstouse) {
				case 1:
					d1from=(vararray[i]-kernelhalfside)<0 ? 0 : (vararray[i]-kernelhalfside);
					d1to=(vararray[i]+kernelhalfside+1>side ? side : vararray[i]+kernelhalfside+1);
					
					for(d1=d1from,d1kern=vararray[i]-kernelhalfside<0 ? kernelhalfside-vararray[i] : 0;d1<d1to;d1++,d1kern++) {
						tmpdens[d1]+=kernel[d1kern] * (downweight ? ((float)weight[i]/MULTIPLIER) : 1);
					}
					anythingtosave=true;
				break;
				
				case 2:
					d1from=(vararray[i]-kernelhalfside)<0 ? 0 : (vararray[i]-kernelhalfside);
					d1to=(vararray[i]+kernelhalfside+1>side ? side : vararray[i]+kernelhalfside+1);
					d2from=(vararray[i+nrecs]-kernelhalfside)<0 ? 0 : (vararray[i+nrecs]-kernelhalfside);
					d2to=(vararray[i+nrecs]+kernelhalfside+1>side ? side : vararray[i+nrecs]+kernelhalfside+1);
					
					for(d1=d1from,d1kern=vararray[i]-kernelhalfside<0 ? kernelhalfside-vararray[i] : 0;d1<d1to;d1++,d1kern++) {
						for(d2=d2from,d2p=d1+d2from*side,d2kern=d1kern+((vararray[i+nrecs]-kernelhalfside)<0 ? (kernelhalfside-vararray[i+nrecs])*kernelside : 0);d2<d2to;d2++,d2p+=side,d2kern+=kernelside) {
							tmpdens[d2p]+=kernel[d2kern] * (downweight ? ((float)weight[i]/MULTIPLIER) : 1);
						}
					}
					anythingtosave=true;
				break;
				
				case 3:
					d1from=(vararray[i]-kernelhalfside)<0 ? 0 : (vararray[i]-kernelhalfside);
					d1to=(vararray[i]+kernelhalfside+1>side ? side : vararray[i]+kernelhalfside+1);
					d2from=(vararray[i+nrecs]-kernelhalfside)<0 ? 0 : (vararray[i+nrecs]-kernelhalfside);
					d2to=(vararray[i+nrecs]+kernelhalfside+1>side ? side : vararray[i+nrecs]+kernelhalfside+1);
					d3from=(vararray[i+nrecs*2]-kernelhalfside)<0 ? 0 : (vararray[i+nrecs*2]-kernelhalfside);
					d3to=(vararray[i+nrecs*2]+kernelhalfside+1>side ? side : vararray[i+nrecs*2]+kernelhalfside+1);
					for(d1=d1from,d1kern=vararray[i]-kernelhalfside<0 ? kernelhalfside-vararray[i] : 0;
						d1<d1to;
						d1++,d1kern++) {
						for(d2=d2from,d2p=d1+d2from*side,d2kern=d1kern+((vararray[i+nrecs]-kernelhalfside)<0 ? (kernelhalfside-vararray[i+nrecs])*kernelside : 0)
							;d2<d2to
							;d2++,d2p+=side,d2kern+=kernelside) {
							for(d3=d3from,d3p=d2p+d3from*sidesq
									,d3kern=&kernel[d2kern+((vararray[i+nrecs*2]-kernelhalfside)<0 ? (kernelhalfside-vararray[i+nrecs*2])*kernelsidesq : 0)]
								;d3<d3to
								;d3++,d3p+=sidesq,d3kern+=kernelsidesq) {
								tmpdens[d3p]+=*d3kern * (downweight ? ((float)weight[i]/MULTIPLIER) : 1);
								//kernel[d3kern];
							}
						}
					}
					anythingtosave=true;
				break;
			}
		}
	}
	saveKernelDensity(tmpdens,freqs[lastID],&densities[mapIDs[lastID]]);		// save kernel density of the last taxon
	outIDs[mapIDs[lastID]]=lastID;
*/

// scale all densities to the absolute maximum
/*	unsigned long maxmax=0;
	float factor;
	for(i=0;i<ntaxafiltered;i++) if(maxmax<densities[i].max) maxmax=densities[i].max;
	for(i=0;i<ntaxafiltered;i++) {
		factor=(float)densities[i].max/maxmax;
		for(j=0;j<arraysize;j++) densities[i].density[j]=(unsigned char)((float)densities[i].density[j]*factor);
	}
	*/
// now write to output file
	printf("\nWriting file...\n");
	densfile=fopen(DENSITYFILE(pfilename,poutfilename),"w");
	fwrite(&ntaxafiltered,sizeof(int),1,densfile);		// how many densities in file
	fwrite(outIDs,sizeof(int),ntaxafiltered,densfile);	// the real taxon IDs of each density
	fwrite(&side,sizeof(int),1,densfile);				// the size of the grid
	fwrite(&nvarstouse,sizeof(int),1,densfile);			// the number of variables
	for(k=0;k<ntaxafiltered;k++) {						// now the densities!
		fwrite(&densities[k],sizeof(DENSITY),1,densfile);	// of course, the pointer will be meaningless
		fwrite(densities[k].density,arraysize,1,densfile);
	}
	fclose(densfile);
#ifdef VERBOSE	
	for(k=3;k<4;k++) {	//ntaxafiltered
		printf("************* Taxon Nrecs: %d ************\n",densities[k].nrecords);
		switch(nvarstouse) {
			case 2:
				for(d1=0;d1<side;d1++) {
					for(d2=0;d2<side;d2++) {
							printf("%3d",densities[k].density[d1+d2*side]);
					}
					printf("\n");
				}
			break;
			
			case 3:
				for(d1=0;d1<5;d1++) {
					for(d2=0;d2<side;d2++) {
						for(d3=0;d3<side;d3++) {
							printf("%3d",densities[k].density[d1+d2*side+d3*sidesq]);
						}
						printf("\n");
					}
					printf("*****************\n");
				}
			break;
		}
		printf("********SUM: %f NRECS: %d*********\n",sum,densities[k].nrecords);
	}
	for(k=0;k<ntaxafiltered;k++) {
		for(d1=0,sum=0;d1<arraysize;d1++) sum+=(float)densities[k].density[d1]*densities[k].max/255.0;
		printf("SUM: %f NRECS: %d\n",sum,densities[k].nrecords);
	}
#endif
	
	
	fclose(varsfile);
	free(pIDs);
	free(freqs);
	free(vararray);
	free(varheader);
	free(kernel);
	free(mapIDs);
	free(tmpdens);
	for(i=0;i<ntaxafiltered;i++) free(densities[i].density);
	free(densities);
	free(outIDs);
	(*env)->ReleaseStringUTFChars(env,filename,pfilename);
	(*env)->ReleaseStringUTFChars(env,outfilename,poutfilename);
	return 1;
}
コード例 #28
0
void STATEMENT()
{
    //initialization
    if(TOKEN == identsym)
    {
        //stores the name of the identifier that will be initialized
        char name[12];
        strcpy(name, IDENTIFIER);

        //if identifier is not a variable, produce error
        if(getSymbol(IDENTIFIER).kind != 2)
        {
            ERROR("Error number 12, assignment to constant or procedure not allowed.");
        }

        GETTOKEN();
        if(TOKEN != becomessym)
        {
            ERROR("Error number 13, assignment operator expected.");
        }
        GETTOKEN();
        EXPRESSION();

        symbol current = getSymbol(name);
        //STO 0 M
        printToFile(4,current.level,current.addr);
        lines++;
    }
    //procedure call (not in tiny PL/0)
    else if(TOKEN == callsym)
    {
        GETTOKEN();
        if(TOKEN != identsym)
        {
            ERROR("Error number 14, call must be followed by an identifier.");
        }

        //if the identifier is not a procedure, produce an error
        if(getSymbol(IDENTIFIER).kind != 3)
        {
            ERROR("Error number 15, call of a constant or variable is meaningless.");
        }

        GETTOKEN();
    }
    //a group of statements
    else if(TOKEN == beginsym)
    {
        GETTOKEN();
        STATEMENT();
        while(TOKEN == semicolonsym)
        {
            GETTOKEN();
            STATEMENT();
        }
        if(TOKEN != endsym)
        {
            ERROR("Error number 26, end is expected.");
        }
        GETTOKEN();
    }
    else if(TOKEN == ifsym)
    {
        GETTOKEN();
        CONDITION();
//top of the stack has whether it is true or false
        if(TOKEN != thensym)
        {
            ERROR("Error number 16, then expected.");
        }

        //after the condition, count how many instructions are written
        int currentLines = lines;
        inConditional++;
        fpos_t filePos;
        fgetpos(ifp, &filePos);

        //loop ensures this is done twice
        int i;
        for(i = 0; i < 2; i++)
        {
            if(i == 1)
            {
                inConditional--;
//make branch here (lines contains the line that you jump to if the condition is not met)
//printToFile()
                //JPC 0 M = lines
                printToFile(8,0,lines);

                //returns the file to the previous position
                fsetpos(ifp, &filePos);
                lines = currentLines;
                //Lines increment for prinToFile used in for loop
                //lines++;
            }
            lines++;

            GETTOKEN();
            STATEMENT();
        }

    }
    else if(TOKEN == whilesym)
    {
        int jumpBackLine = lines;
        GETTOKEN();
        CONDITION();
//top of the stack has whether it is true or false
        if(TOKEN != dosym)
        {
            ERROR("Error number 18, do expected.");
        }

        //after the condition, count how many instructions are written
        int currentLines = lines;
        inConditional++;
        fpos_t filePos;
        fgetpos(ifp, &filePos);

        //loop ensures this is done twice
        int i;
        for(i = 0; i < 2; i++)
        {
            if(i == 1)
            {
                inConditional--;
//make branch here (lines + 1 contains the line that you jump to if the condition is not met)
//printToFile()
                //JPC 0 M = l
                printToFile(8,0,lines + 1);

                //returns the file to the previous position
                fsetpos(ifp, &filePos);
                lines = currentLines;
                //Lines increment for the printToFile used in for loop
                //lines++;
            }
            //the line for the branch is added
            lines++;

            GETTOKEN();
            STATEMENT();
        }
        //JMP 0 M = jumpBackLines
        printToFile(7,0,jumpBackLine);
        lines++;
    }
}
コード例 #29
0
ファイル: aes.cpp プロジェクト: ChronoMonochrome/Rijndael
int aes::AES_do_decrypt_from_file(char *infile, char *outfile, unsigned long *CifKey)
{
	BYTE* in = new BYTE[4*Nb];
        printf("Decoding...\n");
        GenPowerTab();
        GenSubBytesTab();

        FILE* stream_in;
        FILE* stream_out;
        if ( !(stream_in = fopen(infile, "rb")))
        {
            printf("File in: %s cannot be read", infile);
            return -1;
        }
        if ( !(stream_out = fopen(outfile, "wb")) )
        {
            printf("File out: %s cannot be read", outfile);
            return -1;
        }

        fpos_t flen;
        //   
        fseek(stream_in, 0, SEEK_END);
        fgetpos(stream_in, &flen); 
        unsigned long rlen = file_len(flen);
        //   
        fseek(stream_in, 0, SEEK_SET);

        WORD ExpKey[Nb*(Nr+1)]; 
        //WORD CifKey[Nk] = { 0x00010203,    0x04050607,
        //    0x08090a0b,    0x0c0d0e0f};
        KeyExpansion(CifKey, ExpKey);

        while(rlen > 0 && !feof(stream_in))
        {  
            unsigned long len =
                (unsigned long)fread(in, 1, 4*Nb, stream_in);
            if (rlen < 4*Nb)
                for (int i = rlen; i < 4*Nb; i++)
                    in[i] = 0;
            rlen -= len;
            //if (len != 4*Nb)

            #ifdef LOGit
            printf("\nNew block\n");
            for(int i=0; i<4; i++)
            {
                printf("%02x %02x %02x %02x\n", in[i], in[4+i],
                    in[8+i], in[12+i]);
            }
            #endif

            AddRoundKey((WORD*)in, &ExpKey[4*Nr]);
            InvCipher((WORD*)in, ExpKey);

            if (rlen == 1)
            {
                BYTE* out = new BYTE[1];
                fread(out, sizeof(BYTE), 1, stream_in);
                len = out[0];
                rlen = 0;
            }

            int nWritten = fwrite(in, sizeof(BYTE), len, stream_out);
        }

        fclose(stream_out);
}
コード例 #30
0
static enum nss_status
internal_getgrent_r (ent_t *ent, char *buffer, size_t buflen, const char *user,
		     gid_t group, long int *start, long int *size,
		     gid_t **groupsp, long int limit, int *errnop)
{
  struct parser_data *data = (void *) buffer;
  struct group grpbuf;

  if (!ent->files)
    return getgrent_next_nss (ent, buffer, buflen, user, group,
			      start, size, groupsp, limit, errnop);

  while (1)
    {
      fpos_t pos;
      int parse_res = 0;
      char *p;

      do
	{
	  /* We need at least 3 characters for one line.  */
	  if (__builtin_expect (buflen < 3, 0))
	    {
	    erange:
	      *errnop = ERANGE;
	      return NSS_STATUS_TRYAGAIN;
	    }

	  fgetpos (ent->stream, &pos);
	  buffer[buflen - 1] = '\xff';
	  p = fgets_unlocked (buffer, buflen, ent->stream);
	  if (p == NULL && feof_unlocked (ent->stream))
	    return NSS_STATUS_NOTFOUND;

	  if (p == NULL || __builtin_expect (buffer[buflen - 1] != '\xff', 0))
	    {
	    erange_reset:
	      fsetpos (ent->stream, &pos);
	      goto erange;
	    }

	  /* Terminate the line for any case.  */
	  buffer[buflen - 1] = '\0';

	  /* Skip leading blanks.  */
	  while (isspace (*p))
	    ++p;
	}
      while (*p == '\0' || *p == '#' ||	/* Ignore empty and comment lines. */
	     /* Parse the line.  If it is invalid, loop to
	        get the next line of the file to parse.  */
	     !(parse_res = _nss_files_parse_grent (p, &grpbuf, data, buflen,
						   errnop)));

      if (__builtin_expect (parse_res == -1, 0))
	/* The parser ran out of space.  */
	goto erange_reset;

      if (grpbuf.gr_name[0] != '+' && grpbuf.gr_name[0] != '-')
	/* This is a real entry.  */
	break;

      /* -group */
      if (grpbuf.gr_name[0] == '-' && grpbuf.gr_name[1] != '\0'
	  && grpbuf.gr_name[1] != '@')
	{
	  blacklist_store_name (&grpbuf.gr_name[1], ent);
	  continue;
	}

      /* +group */
      if (grpbuf.gr_name[0] == '+' && grpbuf.gr_name[1] != '\0'
	  && grpbuf.gr_name[1] != '@')
	{
	  if (in_blacklist (&grpbuf.gr_name[1],
			    strlen (&grpbuf.gr_name[1]), ent))
	    continue;
	  /* Store the group in the blacklist for the "+" at the end of
	     /etc/group */
	  blacklist_store_name (&grpbuf.gr_name[1], ent);
	  if (nss_getgrnam_r == NULL)
	    return NSS_STATUS_UNAVAIL;
	  else if (nss_getgrnam_r (&grpbuf.gr_name[1], &grpbuf, buffer,
				   buflen, errnop) != NSS_STATUS_SUCCESS)
	    continue;

	  check_and_add_group (user, group, start, size, groupsp,
			       limit, &grpbuf);

	  return NSS_STATUS_SUCCESS;
	}

      /* +:... */
      if (grpbuf.gr_name[0] == '+' && grpbuf.gr_name[1] == '\0')
	{
	  ent->files = FALSE;
	  return getgrent_next_nss (ent, buffer, buflen, user, group,
				    start, size, groupsp, limit, errnop);
	}
    }

  check_and_add_group (user, group, start, size, groupsp, limit, &grpbuf);

  return NSS_STATUS_SUCCESS;
}