예제 #1
0
/** 
 * Write hmmlist (logical-to-physical mapping table) and
 * cdset (pseudo phone set) to file.
 * 
 * @param fp [in] file pointer to write
 * @param hmminfo [in] HMM definition data
 * 
 * @return TRUE on success, FALSE on failure.
 */
boolean
save_hmmlist_bin(FILE *fp, HTK_HMM_INFO *hmminfo)
{
  /* write 4 byte as NULL to auto detect file format at read time */
  /* this mark will be read in init_hmminfo() */
  int x = 0;
  if (myfwrite(&x, sizeof(int), 1, fp) < 1) {
    jlog("Error: save_hmmlist_bin: failed to write hmmlist to binary file\n");
    return FALSE;
  }
  /* write hmmlist */
  if (aptree_write(fp, hmminfo->logical_root, save_hmmlist_callback) == FALSE) {
    jlog("Error: save_hmmlist_bin: failed to write hmmlist to binary file\n");
    return FALSE;
  }
  /* write cdset */
  if (aptree_write(fp, hmminfo->cdset_info.cdtree, save_cdset_callback) == FALSE) {
    jlog("Error: save_hmmlist_bin: failed to write cdset to binary file\n");
    return FALSE;
  }
  return TRUE;
}
예제 #2
0
/** 
 * Binary write function with byte swap (assume file is BIG ENDIAN)
 * 
 * @param fp [in] file pointer
 * @param buf [in] data to write
 * @param unitbyte [in] size of a unit in bytes
 * @param unitnum [in] number of unit to write
 */
static boolean
wrtfunc(FILE *fp, void *buf, size_t unitbyte, size_t unitnum)
{

  if (unitnum == 0) return TRUE;

#ifndef WORDS_BIGENDIAN
  if (unitbyte != 1) {
    swap_bytes((char *)buf, unitbyte, unitnum);
  }
#endif
  if (myfwrite(buf, unitbyte, unitnum, fp) < unitnum) {
    jlog("Error: write_binhmm: failed to write %d bytes", unitbyte * unitnum);
    return FALSE;
  }
#ifndef WORDS_BIGENDIAN
  if (unitbyte != 1) {
    swap_bytes((char *)buf, unitbyte, unitnum);
  }
#endif
  return TRUE;
}
예제 #3
0
/** 
 * Callback to write cdset data into file.
 * 
 * @param data [in] cdset node data
 * @param fp [in] file pointer to write
 * 
 * @return TRUE on success, FALSE on failure.
 */
static boolean
save_cdset_callback(void *data, FILE *fp)
{
  CD_Set *cd = data;
  int len;
  int i, j;

  len = strlen(cd->name) + 1;
  if (myfwrite(&len, sizeof(int), 1, fp) < 1) return FALSE;
  if (myfwrite(cd->name, len, 1, fp) < 1) return FALSE;
  if (myfwrite(&(cd->tr->id), sizeof(int), 1, fp) < 1) return FALSE;
  if (myfwrite(&(cd->state_num), sizeof(unsigned short), 1, fp) < 1) return FALSE;
  for(i=0;i<cd->state_num;i++) {
    if (myfwrite(&(cd->stateset[i].num), sizeof(unsigned short), 1, fp) < 1) return FALSE;
    for(j=0;j<cd->stateset[i].num;j++) {
      if (myfwrite(&(cd->stateset[i].s[j]->id), sizeof(int), 1, fp) < 1) return FALSE;
      
    }
  }
  
  return TRUE;
}
void save_galaxy_tree_finalize(int filenr, int tree)
{
  int i, p, num;
  struct GALAXY_OUTPUT galaxy_output;

  for(i = 0; i < NGalTree; i++)
    {
      GalTree[i].Done = 0;
      GalTree[i].LastProgGal = -1;
      GalTree[i].MainLeaf = -1;
      GalTree[i].TreeRoot = -1;
    }

  GalCount = 0;

  for(num = LastDarkMatterSnapShot; num >= 0; num--)
    {
      for(i = 0; i < NGalTree; i++)
	{
	  if(GalTree[i].SnapNum == num)
	    if(GalTree[i].Done == 0)
		walk_galaxy_tree(i);
	}
    }

  for(i = 0; i < NGalTree; i++)
    {
      p = GalTree[i].FirstProgGal;
      while(p >= 0)
	{
	  GalTree[p].DescendantGal = i;
	  p = GalTree[p].NextProgGal;
	}
    }

  for(i = 0; i < NGalTree; i++)
    {
      if(GalTree[i].FirstProgGal >= 0)
	GalTree[i].FirstProgGal = GalTree[GalTree[i].FirstProgGal].GalID;

      if(GalTree[i].LastProgGal >= 0)
	GalTree[i].LastProgGal = GalTree[GalTree[i].LastProgGal].GalID;

      if(GalTree[i].MainLeaf >= 0)
	GalTree[i].MainLeaf = GalTree[GalTree[i].MainLeaf].GalID;

      if(GalTree[i].TreeRoot >= 0)
	GalTree[i].TreeRoot = GalTree[GalTree[i].TreeRoot].GalID;

      if(GalTree[i].NextProgGal >= 0)
	GalTree[i].NextProgGal = GalTree[GalTree[i].NextProgGal].GalID;

      if(GalTree[i].DescendantGal >= 0)
	GalTree[i].DescendantGal = GalTree[GalTree[i].DescendantGal].GalID;

      if(GalTree[i].FOFCentralGal >= 0)
	GalTree[i].FOFCentralGal = GalTree[GalTree[i].FOFCentralGal].GalID;
    }

  // order GalTree by current order of storage in file (IndexStored)
  qsort(GalTree, NGalTree, sizeof(struct galaxy_tree_data), save_galaxy_tree_compare);
   

  /* Before, the header was a simple integer for number of galaxies. So, the
     code had to jump over an int (used to store the number of galaxies) and
     and over all the galaxies written so far */ 
  // for DB compatible output, pad the first line with the size of one struct.
 
  for(i = 0; i < NGalTree; i++)
    { 
      myfseek(FdGalTree, (1 + TotGalCount + i) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
      myfread(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);

      prepare_galaxy_tree_info_for_output(filenr, tree, &GalTree[i], &galaxy_output);

      myfseek(FdGalTree, (1 + TotGalCount + i) * sizeof(struct GALAXY_OUTPUT), SEEK_SET);
      myfwrite(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, FdGalTree);

    }

  // GL: propose to only reorder file on disk based on input (or Makefile) parameter
  // if DB is fast in ordering, eg using SSDs, we could leave it out here.
  // BTW it is BETTER not to reorder galaxies if SFHBins are not also reordered,
  // if at least both are to be used in light cone post processing.
  // for easier to read
  save_galaxy_tree_reorder_on_disk();

  TotGalCount += NGalTree;

}
예제 #5
0
파일: save.c 프로젝트: boywert/L-Galaxy_RT
void write_sfh_bins()
{
	FILE* SFH_Bins_File;
	struct SFH_Time *sfh_times;
	char buf[1000];

	/*
	sprintf(buf, "%s/SFH_Bins.csv", FinalOutputDir);
	if(!(SFH_Bins_File = fopen(buf, "w")))
	{
	  char sbuf[1000];
	  sprintf(sbuf, "can't open file `%s'\n", buf);
	  terminate(sbuf);
	} else
	  printf("writing sfh bins to %s\n", buf);
*/

  int snap,j, nbins,ibin;
  nbins = 0;
  for(snap = 0; snap < MAXSNAPS; snap++)
  {
	  for(j=0;j < SFH_ibin[snap][0];j++)
	  {
		  nbins++;
		  // TODO writing as CSV is temporary test for checking output in binary
/*
		  fprintf(SFH_Bins_File,"%d,%d,%f,%f,%f,%f,%d,%d\r\n",
     			snap,j,SFH_t[snap][0][j],SFH_dt[snap][0][j],
     	 	    (SFH_t[snap][0][j]+SFH_dt[snap][0][j]/2.-NumToTime(snap))*UnitTime_in_years/Hubble_h,
     	 	    SFH_dt[snap][0][j]*UnitTime_in_years/Hubble_h,
     			SFH_Nbins[snap][0][j],SFH_ibin[snap][0]);
*/
	  }
  }
//  fflush(SFH_Bins_File);
//  fclose(SFH_Bins_File);

  sfh_times = (struct SFH_Time *) mymalloc("sfh_times", sizeof(struct SFH_Time) * nbins);
  ibin = 0;
  for(snap = 0; snap < MAXSNAPS; snap++)
  {
	  for(j=0;j < SFH_ibin[snap][0];j++)
	  {
		  sfh_times[ibin].snapnum = snap;
		  sfh_times[ibin].bin = j;
		  sfh_times[ibin].lookbacktime = (SFH_t[snap][0][j]+SFH_dt[snap][0][j]/2.-NumToTime(snap))*UnitTime_in_years/Hubble_h;
		  sfh_times[ibin].dt=SFH_dt[snap][0][j]*UnitTime_in_years/Hubble_h;
		  sfh_times[ibin].nbins = SFH_Nbins[snap][0][j];
		  ibin++;
	  }
  }
	sprintf(buf, "%s/SFH_Bins", FinalOutputDir);
	if(!(SFH_Bins_File = fopen(buf, "w")))
	{
	  char sbuf[1000];
	  sprintf(sbuf, "can't open file `%s'\n", buf);
	  terminate(sbuf);
	} else
	  printf("writing sfh bins to %s\n", buf);

	// write # bins
    myfwrite(&nbins, sizeof(int), 1, SFH_Bins_File);	// write 1
	// skip header
	myfseek(SFH_Bins_File, sizeof(struct SFH_Time), SEEK_SET);
    myfwrite(sfh_times, sizeof(struct SFH_Time), nbins, SFH_Bins_File);	// size of an output structure (Galaxy_Output)
    fflush(SFH_Bins_File);
    fclose(SFH_Bins_File);


}
예제 #6
0
파일: aptree.c 프로젝트: drv5455/sdes_music
boolean
aptree_write(FILE *fp, APATNODE *root, boolean (*save_data_func)(void *, FILE *fp))
{
  int count_node, count_branch, count_data, maxbit;
  int *left, *right, *value;
  int num, did;
  boolean err;

  if (root == NULL) return TRUE;

  /* count statistics */
  count_branch = count_data = 0;
  maxbit = 0;
  aptree_count(root, &count_branch, &count_data, &maxbit);
  count_node = count_branch + count_data;
  jlog("Stat: aptree_write: %d nodes (%d branch + %d data), maxbit=%d\n", count_node, count_branch, count_data, maxbit);
  /* allocate */
  left = (int *)mymalloc(sizeof(int) * count_node);
  right = (int *)mymalloc(sizeof(int) * count_node);
  value = (int *)mymalloc(sizeof(int) * count_node);
  /* make index */
  did = num = 0;
  aptree_build_index(root, &num, &did, left, right, value);
#if 0
  {
    int i;
    for(i=0;i<count_node;i++) {
      printf("%d: %d %d %d\n", i, left[i], right[i], value[i]);
    }
  }
#endif
  /* write tree to file */
  if (myfwrite(&count_node, sizeof(int), 1, fp) < 1) {
    jlog("Error: aptree_write: fail to write header\n");
    return FALSE;
  }
  if (myfwrite(&count_data, sizeof(int), 1, fp) < 1) {
    jlog("Error: aptree_write: fail to write header\n");
    return FALSE;
  }
  if (myfwrite(left, sizeof(int), count_node, fp) < count_node) {
    jlog("Error: aptree_write: fail to write %d bytes\n", sizeof(int) * count_node);
    return FALSE;
  }
  if (myfwrite(right, sizeof(int), count_node, fp) < count_node) {
    jlog("Error: aptree_write: fail to write %d bytes\n", sizeof(int) * count_node);
    return FALSE;
  }
  if (myfwrite(value, sizeof(int), count_node, fp) < count_node) {
    jlog("Error: aptree_write: fail to write %d bytes\n", sizeof(int) * count_node);
    return FALSE;
  }
  if (save_data_func != NULL) {
    /* write leaf node data */
    err = FALSE;
    aptree_write_leaf(fp, root, save_data_func, &err);
  }
  if (err) {
    jlog("Error: aptree_write: error occured when writing tree leaf data\n");
    return FALSE;
  }

  free(value);
  free(right);
  free(left);

  return TRUE;
}
예제 #7
0
파일: bcreate.c 프로젝트: ageneau/scid
void compress(void)
{
	int i1, i2;
	printit(0);
	clearTimeout(void);
	printf("\nsorting buffer ............ ");

	qsort( A, I, sizeof(tb), bsortkey );

	puts("done");
	printf("compressing buffer ........ ");
	i1=0; i2=1;
	while( i2 != I )
	{
		while( i2 != I
		    && A[i1].hashboard == A[i2].hashboard
		    && A[i1].move == A[i2].move )
		{
			A[i1].n += A[i2].n;
			i2++;
		}
		if( i2 != I )
		{ i1++; A[i1]=A[i2]; i2++; }
	}
	I=i1+1;
	A[I].n = 0;
	printf("done, buffer usage=%i (%i%%)\n",I,100*I/Asize);

	/** Now, take tb1 file and the memory buffer and join them to
        *** the tb2 (rbook.phalanx.tmp).  Rename tb2 to tb1.
	**/
	{
		int i=0;
		int done_m, done_f;
		tb mtb, ftb;

		printf("adding to file ............ ");
		tb1=fopen("rbook.phalanx","rb");
		tb2=fopen("rbook.phalanx.tmp","wb");
		rewind(tb1);

		mtb=A[0]; i=1; done_m=(i==I);
		myfread( &ftb, sizeof(tb), tb1 ); done_f=feof(tb1);

		while( !done_f || !done_m )
		{
			if( !done_m && !done_f && bsortkey(&mtb,&ftb)==0 )
			{
				mtb.n += ftb.n;
				myfwrite(&mtb,sizeof(tb),tb2);
				mtb=A[i]; i++; done_m=(i==I);
				myfread(&ftb,sizeof(tb),tb1); done_f=feof(tb1);
			}
			else
			if( !done_m && ( done_f || bsortkey(&mtb,&ftb)==-1 ) )
			{
				myfwrite(&mtb,sizeof(tb),tb2);
				mtb=A[i]; i++; done_m=(i==I);
			}
			else
			if( !done_f && ( done_m || bsortkey(&mtb,&ftb)==1 ) )
			{
				myfwrite(&ftb,sizeof(tb),tb2);
				myfread(&ftb,sizeof(tb),tb1); done_f=feof(tb1);
			}
		}

		/* good, it's there, close the two files, move newly
		 * joined to the first one and reopen */

		fclose(tb1); fclose(tb2);
		rename("rbook.phalanx.tmp","rbook.phalanx");
		printf("done\n");
	}

	I=0;

	setTimeout(printit, 5);
}
예제 #8
0
파일: bcreate.c 프로젝트: ageneau/scid
int bcreate( int argc, char ** argv )
{
  FILE * f;
  int c;

  while( (c=getopt(argc,argv,"b:p:c:v:g:w:d:l:u:")) != -1 )
  switch(c)
  {
	case 'b':
		if( sscanf( optarg, "%i", &Asize ) == 0 ) wusage();
		if( Asize < 1000 ) wusage();
	break;
	case 'p':
		if( sscanf( optarg, "%i", &MaxBookPly ) == 0 ) wusage();
		if( MaxBookPly < 2 ) wusage();
	break;
	case 'c':
		if( sscanf( optarg, "%i", &MaxComment ) == 0 ) wusage();
		if( MaxComment < 2 ) wusage();
	break;
	case 'v':
		if( sscanf( optarg, "%i", &MinValue ) == 0 ) wusage();
		if( MinValue < 0 ) wusage();
	break;
	case 'g':
		if( sscanf( optarg, "%i", &MinPercentage ) == 0 ) wusage();
		if( MinPercentage < 50 || MinPercentage > 100 ) wusage();
	break;
	case 'w':
		if( sscanf( optarg, "%i", &WIN ) == 0 ) wusage();
		if( WIN < 1 || WIN > 10 ) wusage();
	break;
	case 'd':
		if( sscanf( optarg, "%i", &DRA ) == 0 ) wusage();
		if( DRA < 0 || DRA > 10 ) wusage();
	break;
	case 'l':
		if( sscanf( optarg, "%i", &LOO ) == 0 ) wusage();
		if( LOO < 0 || LOO > 10 ) wusage();
	break;
	case 'u':
		if( sscanf( optarg, "%i", &UNK ) == 0 ) wusage();
		if( UNK < 0 || UNK > 10 ) wusage();
	break;
	default: wusage();
  }

  A=malloc(Asize*sizeof(tb));
  if( A == NULL )
  { printf("cannot alloc %lu bytes of memory\n",
    (unsigned long)(Asize*sizeof(tb))); exit(0);
  }

  {
    struct stat fs;
    if( ! stat("rbook.phalanx",&fs) )
	{ puts("rbook.phalanx exists, skipping stage 1"); goto stage_2; }
    else
	{ tb1 = fopen("rbook.phalanx", "w+"); }
  }

  setvbuf(stdout, (char*)NULL, _IONBF, 0);
  puts(  "creating opening book");
  puts(  "-----------------------------");
  puts(  "   games positions buffer%");
  puts(  "-----------------------------");
  setTimeout(printit, 5);
  while( findgame() )
  { parsegame(); games++; }
  printf("end of file, parsed %i positions in %i games\n",positions,games);
  if( games == 0 )
  { puts("opening book not created"); return 0; }
  compress();
  clearTimeout(void);

 stage_2:;

  if( ( f = fopen( SBOOK_FILE, "wb" ) ) == NULL )
  { printf("cannot create book file %s\n", SBOOK_FILE); return 1; }

 {
  int dots=0;
  struct stat fs;
  int fsize;
  tb M;
  tb1=fopen("rbook.phalanx","rb");
  myfread( &M, sizeof(tb), tb1 );

  stat("rbook.phalanx",&fs); fsize = fs.st_size/sizeof(tb);

  printf("writing book ");

  while(!feof(tb1))
  {
	int i;
	int minn;
	A[0]=M;
	I=0;

	do
	{
		int fpos=ftell(tb1)/sizeof(tb);
		if( fpos % (fsize/30) == 1 )
		{
		  if( dots==3 )
		  { printf("%i%%", (int)(((float)fpos)*100/fsize + 1) ); dots=0; }
		  else printf(".");
		  dots++;
		}
		I++; myfread( A+I, sizeof(tb), tb1 );
	}
	while( !feof(tb1) && M.hashboard==A[I].hashboard );

	M=A[I];

	/* sort them, better first */
	qsort( A, I, sizeof(tb), usortkey );

	/* more variability in frequently played lines of early openings */
	{ unsigned i=100, j=10, p=MinPercentage;
	  while( A[0].n > i && p>10 ) { p-=j; j--; i*=10; }
	  minn=A[0].n*p/100;
	}

	if( minn > A[0].n ) minn = A[0].n;

	if( minn < MinValue ) minn=MinValue;

	for( i=0; i!=I && A[i].n>=minn; i++ ) {}

/*
	printf("got %i of %i moves at hasboard of %08X, next HB %08X\n",
		i, I, A[0].hashboard, M.hashboard );
*/

	I=i;

	for(i=0;i!=I;i++)
	{
		unsigned short m = A[i].move;
		myfwrite( A+i, sizeof(unsigned), f );
		myfwrite( &m, sizeof(unsigned short), f );
	}
  }
  printf(" done\n");
 }

  return 0;
}
예제 #9
0
void save_galaxies(int filenr, int tree)
{
  char buf[MAX_STRING_LEN];
  int i, n;
  struct GALAXY_OUTPUT galaxy_output = {0};
  int OutputGalCount[MAXSNAPS], *OutputGalOrder, nwritten;

  OutputGalOrder = (int*)malloc( NumGals*sizeof(int) );
  if(OutputGalOrder == NULL) {
    fprintf(stderr,"Error: Could not allocate memory for %d int elements in array `OutputGalOrder`\n", NumGals);
    ABORT(10);
  }

  // reset the output galaxy count and order
  for(i = 0; i < MAXSNAPS; i++)
    OutputGalCount[i] = 0;
  for(i = 0; i < NumGals; i++)
    OutputGalOrder[i] = -1;
  
  // first update mergeIntoID to point to the correct galaxy in the output
  for(n = 0; n < NOUT; n++)
    {
      for(i = 0; i < NumGals; i++)
	{
	  if(HaloGal[i].SnapNum == ListOutputSnaps[n])
	    {
	      OutputGalOrder[i] = OutputGalCount[n];
	      OutputGalCount[n]++;
	    }
	}
    }
  
  for(i = 0; i < NumGals; i++)
    if(HaloGal[i].mergeIntoID > -1)
      HaloGal[i].mergeIntoID = OutputGalOrder[HaloGal[i].mergeIntoID];    
  
  // now prepare and write galaxies
  for(n = 0; n < NOUT; n++)
  {
      // only open the file if it is not already open.
      if( !save_fd[n] )
	    {
        snprintf(buf, MAX_STRING_LEN - 1, "%s/%s_z%1.3f_%d", OutputDir, FileNameGalaxies, ZZ[ListOutputSnaps[n]], filenr);
       
        save_fd[n] = fopen(buf, "r+");
        if (save_fd[n] == NULL) 
        {
          fprintf(stderr, "can't open file `%s'\n", buf);
          ABORT(0);
        }
        
        // write out placeholders for the header data.
        size_t size = (Ntrees + 2)*sizeof(int); /* Extra two inegers are for saving the total number of trees and total number of galaxies in this file */
        int* tmp_buf = (int*)malloc( size );
        if (tmp_buf == NULL)
        {
          fprintf(stderr, "Error: Could not allocate memory for header information for file %d\n", n);
          ABORT(10);
        }

        memset( tmp_buf, 0, size );
        nwritten = fwrite( tmp_buf, sizeof(int), Ntrees + 2, save_fd[n] );
        if (nwritten != Ntrees + 2)
        {
          fprintf(stderr, "Error: Failed to write out %d elements for header information for file %d.  Only wrote %d elements.\n", Ntrees + 2, n, nwritten);
        }
        free( tmp_buf );
	    }
      
      for(i = 0; i < NumGals; i++)
	    {
        if(HaloGal[i].SnapNum == ListOutputSnaps[n])
	      {        
          prepare_galaxy_for_output(filenr, tree, &HaloGal[i], &galaxy_output);
   
          nwritten = myfwrite(&galaxy_output, sizeof(struct GALAXY_OUTPUT), 1, save_fd[n]);
          if (nwritten != 1)
          {
            fprintf(stderr, "Error: Failed to write out the galaxy struct for galaxy %d within file %d.  Meant to write 1 element but only wrote %d elements.\n", i, n, nwritten); 
          }
          
          TotGalaxies[n]++;
          TreeNgals[n][tree]++;	      
	      }
	    }
      
  }

  // don't forget to free the workspace.
  free( OutputGalOrder );

}
예제 #10
0
파일: scanner.c 프로젝트: mingpen/OpenNT
/************************************************************************
**  get_real : gathers the real part/exponent of a real number.
**              Input  : ptr to the null terminator of the whole part
**                               pointer to receive value.
**              Output : L_CFLOAT
**
**  ASSUMES whole part is either at Exp_ptr or Reuse_W.
************************************************************************/
token_t    get_real(REG PWCHAR p)
{
    REG int             c;
    token_t     tok;

    c = get_non_eof();
    if(Cross_compile && (Tiny_lexer_nesting == 0)) {
        strcpy (Msg_Text, GET_MSG (4012));
        warning(4012);  /* float constant in cross compilation */
        Cross_compile = FALSE;  /*  only one msg per file */
    }
    /*
**  if the next char is a digit, then we've been called after
**  finding a '.'. if this is true, then
**  we want to find the fractional part of the number.
**  if it's a '.', then we've been called after finding
**  a whole part, and we want the fraction.
*/
    if( LXC_IS_DIGIT((WCHAR)c) || IS_DOT(c) ) {
        do {
            *p++ = (WCHAR)c;
            c = (int)get_non_eof();
        } while( LXC_IS_DIGIT((WCHAR)c) );
    }
    if( IS_E((WCHAR)c) ) {              /*  now have found the exponent  */
        *p++ = (WCHAR)c;                /*  save the 'e'  */
        c = (WCHAR)get_non_eof();       /*  skip it  */
        if( IS_SIGN(c) ) {              /*  optional sign  */
            *p++ = (WCHAR)c;            /*  save the sign  */
            c = (int)get_non_eof();
        }
        if( ! LXC_IS_DIGIT((WCHAR)c)) {
            if( ! Rflag ) {
                if(Tiny_lexer_nesting == 0) {
                    Msg_Temp = GET_MSG (2021);
                    SET_MSG (Msg_Text, sizeof(Msg_Text), Msg_Temp, c);
                    error(2021); /* missing or malformed exponent */
                }
                *p++ = L'0';
            }
        }
        else {
            do {                        /* gather the exponent */
                *p++ = (WCHAR)c;
                c = (int)get_non_eof();
            } while( LXC_IS_DIGIT((WCHAR)c) );
        }
    }
    if( IS_F((WCHAR)c) ) {
        tok = L_CFLOAT;
        if( Prep ) {
            *p++ = (WCHAR)c;
        }
    }
    else if( IS_EL((WCHAR)c) ) {
        tok = L_CLDOUBLE;
        if( Prep ) {
            *p++ = (WCHAR)c;
        }
    }
    else {
        UNGETCH();
        tok = L_CDOUBLE;
    }
    *p = L'\0';
    if( Tiny_lexer_nesting > 0 ) {
        Exp_ptr = p;
        return(L_NOTOKEN);
    }
    else if( Prep ) {
        myfwrite( Reuse_W, (size_t)(p - Reuse_W) * sizeof(WCHAR), 1, OUTPUTFILE);
        return(L_NOTOKEN);
    }
    /*
        ** reals aren't used during preprocessing
        */
    return(tok);
}
예제 #11
0
파일: scanner.c 프로젝트: mingpen/OpenNT
/************************************************************************
 * GETNUM - Get a number from the input stream.
 *
 * ARGUMENTS
 *      radix - the radix of the number to be accumulated.  Can only be 8, 10,
 *                      or 16
 *      pval - a pointer to a VALUE union to be filled in with the value
 *
 * RETURNS - type of the token (L_CINTEGER or L_CFLOAT)
 *
 * SIDE EFFECTS -
 *      does push back on the input stream.
 *      writes into pval by reference
 *  uses buffer Reuse_W
 *
 * DESCRIPTION -
 *      Accumulate the number according to the rules for each radix.
 *      Set up the format string according to the radix (or distinguish
 *      integer from float if radix is 10) and convert to binary.
 *
 * AUTHOR - Ralph Ryan, Sept. 8, 1982
 *
 * MODIFICATIONS - none
 *
 ************************************************************************/
token_t getnum(REG      WCHAR           c)
{
    REG WCHAR   *p;
    WCHAR       *start;
    int         radix;
    token_t     tok;
    value_t     value;

    tok = L_CINTEGER;
    start = (Tiny_lexer_nesting ? Exp_ptr : Reuse_W);
    p = start;
    if( c == L'0' ) {
        c = get_non_eof();
        if( IS_X(c) ) {
            radix = 16;
            if( Prep ) {
                *p++ = L'0';
                *p++ = L'x';
            }
            for(c = get_non_eof(); LXC_IS_XDIGIT(c); c = get_non_eof()) {
                /* no check for overflow? */
                *p++ = c;
            }
            if((p == Reuse_W) && (Tiny_lexer_nesting == 0)) {
                strcpy (Msg_Text, GET_MSG (2153));
                error(2153);
            }
            goto check_suffix;
        }
        else {
            radix = 8;
            *p++ = L'0'; /* for preprocessing or 0.xxx case */
        }
    }
    else {
        radix = 10;
    }

    while( LXC_IS_DIGIT((WCHAR)c) ) {
        *p++ = c;
        c = get_non_eof();
    }

    if( IS_DOT(c) || IS_E(c) ) {
        UNGETCH();
        return(get_real(p));
    }

check_suffix:
    if( IS_EL(c) ) {
        if( Prep ) {
            *p++ = c;
        }
        c = get_non_eof();
        if( IS_U(c) ) {
            if(Prep) {
                *p++ = c;
            }
            tok = L_LONGUNSIGNED;
        }
        else {
            tok = L_LONGINT;
            UNGETCH();
        }
    }
    else if( IS_U(c) ) {
        if( Prep ) {
            *p++ = c;
        }
        c = get_non_eof();
        if( IS_EL(c) ) {
            if( Prep ) {
                *p++ = c;
            }
            tok = L_LONGUNSIGNED;
        }
        else {
            tok = L_CUNSIGNED;
            UNGETCH();
        }
    }
    else {
        UNGETCH();
    }
    *p = L'\0';
    if( start == Exp_ptr ) {
        Exp_ptr = p;
        return(L_NOTOKEN);
    }
    else if( Prep ) {
        myfwrite( Reuse_W, (size_t)(p - Reuse_W) * sizeof(WCHAR), 1, OUTPUTFILE);
        return(L_NOTOKEN);
    }
    value.v_long = matol(Reuse_W,radix);
    switch(tok) {
    case L_CINTEGER:
        tok = (radix == 10)
            ? c_size(value.v_long)
            : uc_size(value.v_long)
            ;
        break;
    case L_LONGINT:
        tok = l_size(value.v_long);
        break;
    case L_CUNSIGNED:
        tok = ul_size(value.v_long);
        break;
    }
    yylval.yy_tree = build_const(tok, &value);
    return(tok);
}
예제 #12
0
파일: scanner.c 프로젝트: mingpen/OpenNT
void   do_newline()
{
    ++Linenumber;
    for(;;) {
        switch(CHARMAP(GETCH())) {
        case LX_BOM:  // ignore Byte Order Mark
            break;
        case LX_CR:
            break;
        case LX_POUND:
            preprocess();
            break;
        case LX_SLASH:
            if( ! skip_comment()) {
                goto leave_do_newline;
            }
            break;
        case LX_NL:
            {
                if ((lCPPTotalLinenumber++ & RC_PREPROCESS_UPDATE) == 0)
                    UpdateStatus(1, lCPPTotalLinenumber);
            }
            Linenumber++;
            // must manually write '\r' with '\n' when writing 16-bit strings
            if( Prep ) {        /*  preprocessing only */
                myfwrite(L"\r", sizeof(WCHAR), 1, OUTPUTFILE);
            }
            /*
                        **  FALLTHROUGH
                        */
        case LX_WHITE:
            if( Prep ) {        /*  preprocessing only, output whitespace  */
                myfwrite(&(PREVCH()), sizeof(WCHAR), 1, OUTPUTFILE);
            }
            else {
                do {
                    ;
                } while(LXC_IS_WHITE(GETCH()));
                UNGETCH();
            }
            break;
        case LX_EOS:
            if(PREVCH() == EOS_CHAR || PREVCH() == CONTROL_Z) {
                if(io_eob()) {          /* leaves us pointing at a valid char */
                    return;
                }
                break;
            }
            if(checknl()) {
                continue;
            }
            /* it's a backslash */
            /*
                        **      FALLTHROUGH
                        */
        default:                /* first non-white is not a '#', leave */

leave_do_newline:

            UNGETCH();
            return;
        }
    }
}