Ejemplo n.º 1
0
int prot_x (int connfd)
{
	XDR xdrs_r, xdrs_w;
	//char buf[MAXBUFL];
	//int nread;
	int op1 = 0;
	int op2 = 0;
	int res;

	//xdrmem_create(xdrs, buf, MAXBUFL, XDR_DECODE);
	FILE *stream_socket_r = fdopen(connfd, "r");
	if (stream_socket_r == NULL)
		err_sys ("(%s) error - fdopen() failed", prog_name);
	xdrstdio_create(&xdrs_r, stream_socket_r, XDR_DECODE);

	FILE *stream_socket_w = fdopen(connfd, "w");
	if (stream_socket_w == NULL)
		err_sys ("(%s) error - fdopen() failed", prog_name);
	xdrstdio_create(&xdrs_w, stream_socket_w, XDR_ENCODE);

	trace( err_msg("(%s) - waiting for operands ...", prog_name) );

	/* get the operands */
	if ( ! xdr_int(&xdrs_r, &op1) ) {
		trace( err_msg("(%s) - cannot read op1 with XDR", prog_name) );
	} else {
		trace( err_msg("(%s) - read op1 = %d", prog_name, op1) );
	}

	if ( ! xdr_int(&xdrs_r, &op2) ) {
		trace( err_msg("(%s) - cannot read op2 with XDR", prog_name) );
	} else {
		trace( err_msg("(%s) - read op2 = %d", prog_name, op2) );
	}

	/* do the operation */
	res = op1 + op2;

	xdr_destroy(&xdrs_r);

	trace( err_msg("(%s) --- result of the sum: %d", prog_name, res) );

	/* send the result */
	xdr_int(&xdrs_w, &res);
	fflush(stream_socket_w);

	xdr_destroy(&xdrs_w);
	fclose(stream_socket_w);

	/* NB: Close read streams only after writing operations have also been done */
	fclose(stream_socket_r);


	trace( err_msg("(%s) --- result just sent back", prog_name) );

	return 0;
}
Ejemplo n.º 2
0
int
mbbs_rdversion(FILE *fp, int *version)
{
	XDR xdrs;

	if (sizeof(int) < 4)
		return BS_BADARCH;

	bs_iobytecnt= 0;

	xdrstdio_create(&xdrs, fp, XDR_DECODE);

	if (!xdr_int(&xdrs, version))
		return BS_FAILURE;
	bs_iobytecnt+= 4;
	switch (*version) {
	case MR1_VERSION_1_0:
	case MR1_VERSION_2_0:
	case BS_VERSION_1_0:
	case BS_VERSION_1_1:
	case BS_VERSION_1_2:
	case BS_VERSION_1_3:
	case BS_VERSION_1_4:
		break;
	default:
		return BS_BADDATA;
	}

	xdr_destroy(&xdrs);

	return BS_SUCCESS;
}
Ejemplo n.º 3
0
main ()
{
        FILE *fp;                  /*указатель файла */
        XDR xdrs;                  /*дескpиптоp XDR */
        long val1=10;              /*целое */
        float val2=4.456789;       /*с плавающей точкой */
        char val3s[] = "qwerty123456uvwxyz"; /* 18+1 bytes null terminated string */
        char *val3;
        int r;
        val3 = malloc(20);
        strcpy(val3,val3s);
/*откpытие файла на запись */
        fp = fopen(FIC, "w");
/*      создание потока XDR для кодиpования */
        xdrstdio_create(&xdrs, fp, XDR_ENCODE);
/*запись целого */
        xdr_long(&xdrs, &val1);
/*запись числа с плавающей точкой */
        xdr_float(&xdrs, &val2);
/* write string */
        r = xdr_string(&xdrs, &val3, strlen(val3));
        printf("r=%d\n",r);
        free(val3);
        fclose(fp);
        exit(0);
}
Ejemplo n.º 4
0
static void *
read_nis_obj (const char *name, iofct_t readfct, freefct_t freefct,
	      size_t objsize)
{
  FILE *in = fopen (name, "rce");
  if (in == NULL)
    return NULL;

  void *obj = calloc (1, objsize);

  if (obj != NULL)
    {
      XDR xdrs;
      xdrstdio_create (&xdrs, in, XDR_DECODE);
      bool_t status = readfct (&xdrs, obj);
      xdr_destroy (&xdrs);

      if (!status)
	{
	  freefct (obj);
	  obj = NULL;
	}
    }

  fclose (in);

  return obj;
}
Ejemplo n.º 5
0
static bool_t
write_struct(char *filename, xdrproc_t structproc, void *list)
{
	FILE *fp;
	XDR xdrs;
	mode_t omask;

	omask = umask(077);
	fp = fopen(filename, "w");
	if (fp == NULL) {
		int i;

		for (i = 0; i < 10; i++)
			close(i);
		fp = fopen(filename, "w");
		if (fp == NULL) {
			syslog(LOG_ERR,
				"cannot open file = %s for writing", filename);
			syslog(LOG_ERR, "cannot save any registration");
			return (FALSE);
		}
	}
	(void) umask(omask);
	xdrstdio_create(&xdrs, fp, XDR_ENCODE);

	if (structproc(&xdrs, list) == FALSE) {
		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
		fclose(fp);
		return (FALSE);
	}
	XDR_DESTROY(&xdrs);
	fclose(fp);
	return (TRUE);
}
Ejemplo n.º 6
0
/* opens test file for read or write */
bool_t
xdrstdio_create_cb (XDR *xdrs, enum xdr_op op, void * data)
{
  bool_t rVal = TRUE;
  xdrstdio_creation_data* xdrstdio_data = (xdrstdio_creation_data*)data;

  switch (op)
    {
      case XDR_DECODE:
      case XDR_FREE:
        xdrstdio_data->f = fopen (xdrstdio_data->fullname, FOPEN_RB);
        break;
      case XDR_ENCODE:
        xdrstdio_data->f = fopen (xdrstdio_data->fullname, FOPEN_WB);
        break;
    }

  if (!xdrstdio_data->f)
    {
      log_msg (xdrstdio_data->o->log, XDR_LOG_NORMAL,
               "could not open data file: %s\n", xdrstdio_data->fullname);
      rVal = FALSE;
    }
  else
    {
      xdrstdio_create (xdrs, xdrstdio_data->f, op);
      xdrstdio_data->finish_guard = 1;
    }
  return rVal;
}
Ejemplo n.º 7
0
nis_object *
nis_read_obj (const char *name)
{
  XDR xdrs;
  FILE *in;
  bool_t status;
  nis_object *obj;

  in = fopen (name, "rb");
  if (in == NULL)
    return NULL;

  obj = calloc (1, sizeof (nis_object));
  if (obj == NULL)
    {
      fclose (in);
      return NULL;
    }

  xdrstdio_create (&xdrs, in, XDR_DECODE);
  status =_xdr_nis_object (&xdrs, obj);
  xdr_destroy (&xdrs);
  fclose (in);

  if (status)
    return obj;
  else
    {
      nis_free_object (obj);
      return NULL;
    }
}
Ejemplo n.º 8
0
void *restore_data(void *data){


//Deserialization

  /*This way I can actually recover the data in the same process*/
  container *old_data;
  old_data=(container *) malloc(sizeof(container));
  FILE *fp;
  XDR xdrs;
  fp=fopen(ser_process_var,"r");
  xdrstdio_create(&xdrs,fp, XDR_DECODE); 
  if(!xdr_container(&xdrs,old_data)) printf("Deserialization error\n"); //Do sth!!! 
  //else printf("Data restored\n"); 

  xdr_destroy (&xdrs);
  fclose (fp);
//  printf("From old version:\nage %f    name %s   address %s   option %d\n",old_data->age,old_data->name, old_data->address, old_data->option);
  
  container_2 *new_data;
  new_data=(container_2 *) malloc(sizeof(container_2));
  
  new_data->name=old_data->name;
  new_data->num_executions=(int)old_data->num_executions;
//  printf("Restored :\nage %d    name %s   address %s   option %d\n",new_data->age,new_data->name, new_data->address, new_data->option);
  
  free(old_data);
  return (void *)new_data;

}
Ejemplo n.º 9
0
directory_obj *
readColdStartFile (void)
{
  XDR xdrs;
  FILE *in;
  bool_t status = TRUE;
  directory_obj *obj;

  in = fopen (cold_start_file, "rb");
  if (in == NULL)
    return NULL;

  obj = calloc (1, sizeof (directory_obj));

  if (obj != NULL)
    {
      xdrstdio_create (&xdrs, in, XDR_DECODE);
      status = _xdr_directory_obj (&xdrs, obj);
      xdr_destroy (&xdrs);

      if (!status)
	{
	  nis_free_directory (obj);
	  obj = NULL;
	}
    }

  fclose (in);

  return obj;
}
Ejemplo n.º 10
0
int mda_dump( char *file)
{
  XDR xdrstream;

  FILE *input;

  int extraflag;


  if( (input = fopen( file, "rb")) == NULL)
    {
      fprintf(stderr, "Can't open file!\n");
      return 1;
    }


  xdrstdio_create(&xdrstream, input, XDR_DECODE);
  

  extraflag = mda_dump_header( &xdrstream);
  mda_dump_scan( &xdrstream);
  if( extraflag)
    mda_dump_extra( &xdrstream);


  xdr_destroy( &xdrstream);

  fclose(input);

  return 0;
}
Ejemplo n.º 11
0
long *kdReadLongArray(char *filename, int bAscii, char *arrayName)
// Reads a tipsy array file
{
  XDR xdrs;
  FILE *fp;
  long np;
  long *arr, temp;
  int i;
  char arrFile[256];

  strcpy(arrFile, filename);
  strcat(arrFile, ".");
  strcat(arrFile, arrayName);

  fprintf(stderr, "array = %s\n", arrFile);

  if (!bAscii) {
    assert(sizeof(Real)==sizeof(float)); /* Otherwise, this XDR stuff
					    ain't gonna work */
    
    fp = fopen(arrFile, "r");
    xdrstdio_create(&xdrs, fp, XDR_DECODE);
    xdr_long(&xdrs, &np);
    arr = malloc(sizeof(float)*np);
    for(i=0;i<np;i++) xdr_long(&xdrs,&temp);
  }
  fclose(fp);
  
  return arr; 
}
Ejemplo n.º 12
0
main()
{
  xdrstdio_create(&xdrs, stdin, XDR_DECODE);
  forever {
	if(xdr_header() != 1)
	  break;

	if(gas_particles != NULL) free(gas_particles);
	if(header.nsph != 0) {
	    gas_particles = (struct gas_particle *)
				malloc(header.nsph*sizeof(*gas_particles));
	    if(gas_particles == NULL) {
		printf("<sorry, no memory for gas particles, master>\n") ;
		return ;
	    }
	}
	else
	  gas_particles = NULL;

	if(dark_particles != NULL) free(dark_particles);
	if(header.ndark != 0) {
	    dark_particles = (struct dark_particle *)
				malloc(header.ndark*sizeof(*dark_particles));
	    if(dark_particles == NULL) {
		printf("<sorry, no memory for dark particles, master>\n") ;
		return ;
	    }
	}
	else
	  dark_particles = NULL;

	if(star_particles != NULL) free(star_particles);
	if(header.nstar != 0) {
	    star_particles = (struct star_particle *)
				malloc(header.nstar*sizeof(*star_particles));
	    if(star_particles == NULL) {
		printf("<sorry, no memory for star particles, master>\n") ;
		return ;
	    }
	}
	else
	  star_particles = NULL;

	xdr_gas();
	xdr_dark();
	xdr_star();
	
	fwrite((char *)&header,sizeof(header),1,stdout) ;
	fwrite((char *)gas_particles,sizeof(struct gas_particle),
	      header.nsph, stdout) ;
	fwrite((char *)dark_particles,sizeof(struct dark_particle),
	       header.ndark,stdout) ;
	fwrite((char *)star_particles,sizeof(struct star_particle),
	      header.nstar, stdout) ;
  
	fprintf(stderr, "read time %lf\n",header.time) ;
      }
  xdr_destroy(&xdrs);
}
Ejemplo n.º 13
0
int
main ()
{
  XDR xdr;
  stringlist strings;
  stringentry *entry;
  FILE *fp;

  fp = fopen ("test1.out", "w");
  xdrstdio_create (&xdr, fp, XDR_ENCODE);

  strings = malloc (sizeof (struct stringentry));
  strings->item = strdup ("hello");
  strings->next = malloc (sizeof (struct stringentry));
  strings->next->item = strdup ("goodbye");
  strings->next->next = NULL;

  if (!xdr_stringlist (&xdr, &strings)) {
    fprintf (stderr, "test1: could not encode\n");
    exit (1);
  }

  xdr_free ((xdrproc_t) xdr_stringlist, (char *) &strings);
  xdr_destroy (&xdr);
  fclose (fp);

  fp = fopen ("test1.out", "r");
  xdrstdio_create (&xdr, fp, XDR_DECODE);

  strings = NULL;
  if (!xdr_stringlist (&xdr, &strings)) {
    fprintf (stderr, "test1: could not decode\n");
    exit (1);
  }

  fclose (fp);

  for (entry = strings; entry; entry = entry->next)
    printf ("entry->item = %s\n", entry->item);

  xdr_free ((xdrproc_t) xdr_stringlist, (char *) &strings);
  xdr_destroy (&xdr);

  exit (0);
}
Ejemplo n.º 14
0
/** 
 * Write a SAC data file from memory to disk is XDR (portable) format
 * 
 * @param idfl 
 *    Data file list index number
 * @param kname 
 *    Name of file to write
 * @param kname_s 
 *    Length of \p kname
 * @param ldta 
 *    - TRUE to write the data and header
 *    - FALSE to write only the header, not data
 * @param nerr 
 *    Error Return Flag
 *    - 0 on Success
 *    - ERROR_WRITING_XDR_FILE
 *    - ERROR_ENCODING_XDR_FILE
 *
 * @date   010496:  Original version.
 *
 */
void 
wrxdr(int   idfl, 
      char *kname, 
      int   kname_s, 
      int   ldta, 
      int  *nerr) {

	int jcomp, ncerr, nlcmem, nptwr; 
        FILE *nun;
        XDR xdrs;

	*nerr = 0;

        if( !ldta ){
          *nerr = ERROR_WRITING_XDR_FILE;
          return;
	}

	/* create a file */
	znfiles(&nun, kname, kname_s, "TEXT", 5, nerr);
	if( *nerr != 0 )
	    return;

	/* create a stream for the XDR conversions */
	xdrstdio_create(&xdrs, nun, XDR_ENCODE);

	/* - Write the header to disk. */
	nlcmem = Ndxhdr[idfl];

	xdrhdr(xdrs, cmmem.sacmem[nlcmem],nerr);
	if( *nerr != 0 )
	    goto L_8888;

	/* - Write each data component, if requested. */
	if( ldta ){
	    for( jcomp = 0; jcomp < Ncomp[idfl]; jcomp++ ){
		nlcmem = cmdfm.ndxdta[idfl - 1][jcomp];
		nptwr = Nlndta[idfl];

		if( !xdr_array(&xdrs, (caddr_t *)&cmmem.sacmem[nlcmem],
		    (u_int *)&nptwr, (u_int)nptwr, sizeof(float), xdr_float)){
		    *nerr = ERROR_ENCODING_XDR_FILE;
		    goto L_8888;
		}
	    }
	}

/* - Close disk file. */

L_8888:
        xdr_destroy(&xdrs);
	zcloses( &nun, &ncerr );

	return;

} 
Ejemplo n.º 15
0
void mint_1_readfh(mint_1 *dest, FILE *fh)
{
	XDR xdrs;

	xdrstdio_create(&xdrs, fh, XDR_DECODE);
	if (!xdr_mint_1(&xdrs, dest))
		panic("error decoding MINT input file");
	xdr_destroy(&xdrs);

	mint_1_check(dest);
}
Ejemplo n.º 16
0
/* aoi_readfh() uses rpcgen-created routes to read in AOI.
*/
void aoi_readfh(aoi *dest, FILE *fh)
{
    XDR xdrs;

    xdrstdio_create(&xdrs, fh, XDR_DECODE);
    if (!xdr_aoi(&xdrs, dest))
        panic("error decoding AOI input file");
    xdr_destroy(&xdrs);

    aoi_check(dest);
}
Ejemplo n.º 17
0
Archivo: ssio.c Proyecto: jpcoles/ZM
int
ssioOpen(const char *filename,SSIO *ssio,const u_int mode) {
    const char type[][3] = {"r","w","r+"};
    const enum xdr_op op[] = {XDR_DECODE,XDR_ENCODE,XDR_ENCODE};

    assert(filename != NULL && ssio != NULL);
    assert(mode == SSIO_READ || mode == SSIO_WRITE || mode == SSIO_UPDATE);
    if (!(ssio->fp = fopen(filename,type[mode])))
	return 1;
    xdrstdio_create(&ssio->xdrs,ssio->fp,op[mode]);
    return 0;
    }
Ejemplo n.º 18
0
void writeInput(void)
{
  const char routineName[] = "writeInput";

  bool_t  result=TRUE, PRD_angle_dep, XRD, big_endian;
  FILE   *fp_out;
  XDR     xdrs;

  if (!strcmp(INPUT_DOT_OUT, "none")) return;

  if ((fp_out = fopen(INPUT_DOT_OUT, "w")) == NULL) {
    sprintf(messageStr, "Unable to open output file %s",
	    INPUT_DOT_OUT);
    Error(ERROR_LEVEL_1, routineName, messageStr);
    return;
  }
  xdrstdio_create(&xdrs, fp_out, XDR_ENCODE);

  PRD_angle_dep = (input.PRD_angle_dep != PRD_ANGLE_INDEP  &&  atmos.NPRDactive > 0);
  XRD           = (input.XRD  &&  atmos.NPRDactive > 0);

  /* --- Write various input parameters to file --     -------------- */      

  result &= xdr_bool(&xdrs, &input.magneto_optical);
  result &= xdr_bool(&xdrs, &PRD_angle_dep);
  result &= xdr_bool(&xdrs, &XRD);

  result &= xdr_enum(&xdrs, (enum_t *) &input.startJ);
  result &= xdr_enum(&xdrs, (enum_t *) &input.StokesMode);

  result &= xdr_double(&xdrs, &input.metallicity);

  result &= xdr_bool(&xdrs, &input.backgr_pol);

  /* --- Write Endianness of compute architecture so that J can be
         read properly in the analysis --              -------------- */

  big_endian = is_big_endian();
  result &= xdr_bool(&xdrs, &big_endian);


  if (!result) {
    sprintf(messageStr, "Unable to write proper amount to output file %s",
	    INPUT_DOT_OUT);
    Error(ERROR_LEVEL_1, routineName, messageStr);
  }
  xdr_destroy(&xdrs);
  fclose(fp_out);
}
Ejemplo n.º 19
0
static bool_t
write_nis_obj (const char *name, const void *obj, iofct_t writefct)
{
  FILE *out = fopen (name, "wce");
  if (out == NULL)
    return FALSE;

  XDR xdrs;
  xdrstdio_create (&xdrs, out, XDR_ENCODE);
  bool_t status = writefct (&xdrs, (void *) obj);
  xdr_destroy (&xdrs);
  fclose (out);

  return status;
}
Ejemplo n.º 20
0
void gmx_fio_rewind(t_fileio* fio)
{
    gmx_fio_lock(fio);

    if (fio->xdr)
    {
        xdr_destroy(fio->xdr);
        frewind(fio->fp);
        xdrstdio_create(fio->xdr, fio->fp, fio->xdrmode);
    }
    else
    {
        frewind(fio->fp);
    }
    gmx_fio_unlock(fio);
}
Ejemplo n.º 21
0
int save_data(void *data){
  
  container_2 *old_data;
  old_data=(container_2 *)data;
  //printf("Before serialization: %s\n",old_data->name);
  XDR xdrs;
  //Serialization
  FILE *fp;
  fp=fopen(ser_process_var,"w");
  xdrstdio_create(&xdrs,fp, XDR_ENCODE);
  if(!xdr_container(&xdrs,old_data)) {printf("Serialization error\n"); return 1;}
  //else printf("Data saved\n"); 
  xdr_destroy (&xdrs);
  fclose (fp);
  return 0;
}
Ejemplo n.º 22
0
//read decode from ./w and encode
int
main(int argc, char *argv[])
{
    XDR xdr;
    long j;

    xdrstdio_create(&xdr, stdin, XDR_DECODE);
    for (int i =0; i < 8; ++i)
    {
        if ( !xdr_long(&xdr, &j) ) {
            fprintf(stderr, "failed");
            exit(1);
        }
        printf("%ld\n", j);
    }
    printf("\n");
    return 0;
}
Ejemplo n.º 23
0
bool_t
writeColdStartFile (const directory_obj *obj)
{
  XDR xdrs;
  FILE *out;
  bool_t status;

  out = fopen (cold_start_file, "wb");
  if (out == NULL)
    return FALSE;

  xdrstdio_create (&xdrs, out, XDR_ENCODE);
  status = _xdr_directory_obj (&xdrs, (directory_obj *) obj);
  xdr_destroy (&xdrs);
  fclose (out);

  return status;
}
Ejemplo n.º 24
0
bool_t
nis_write_obj (const char *name, const nis_object *obj)
{
  XDR xdrs;
  FILE *out;
  bool_t status;

  out = fopen (name, "wb");
  if (out == NULL)
    return FALSE;

  xdrstdio_create (&xdrs, out, XDR_ENCODE);
  status = _xdr_nis_object (&xdrs, (nis_object *) obj);
  xdr_destroy (&xdrs);
  fclose (out);

  return status;
}
Ejemplo n.º 25
0
static bool_t
read_struct(char *filename, xdrproc_t structproc, void *list)
{
	FILE *fp;
	XDR xdrs;
	struct stat sbuf;

	if (stat(filename, &sbuf) != 0) {
		fprintf(stderr,
		"rpcbind: cannot stat file = %s for reading\n", filename);
		goto error;
	}
	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
	    (sbuf.st_mode & S_IRWXO)) {
		fprintf(stderr,
		"rpcbind: invalid permissions on file = %s for reading\n",
			filename);
		goto error;
	}
	fp = fopen(filename, "r");
	if (fp == NULL) {
		fprintf(stderr,
		"rpcbind: cannot open file = %s for reading\n", filename);
		goto error;
	}
	xdrstdio_create(&xdrs, fp, XDR_DECODE);

	if (structproc(&xdrs, list) == FALSE) {
		fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename);
		fclose(fp);
		goto error;
	}
	XDR_DESTROY(&xdrs);
	fclose(fp);
	return (TRUE);

error:	fprintf(stderr, "rpcbind: will start from scratch\n");
	return (FALSE);
}
Ejemplo n.º 26
0
int
main()
{
    struct gas_particle gas;
    struct dark_particle dark;
    struct star_particle star;
    struct dump header;
    int i;
    XDR xdrs;

  setvbuf(stdin, NULL, _IOFBF, 32*4096);
  setvbuf(stdout, NULL, _IOFBF, 32*4096);
  xdrstdio_create(&xdrs, stdout, XDR_ENCODE);
  forever {
	if(fread((char *)&header,sizeof(header),1,stdin) != 1)
	  break;
	fprintf(stderr, "reading time %f\n",header.time) ;
	xdr_header(&xdrs, &header);

	for(i = 0; i < header.nsph; i++) {
	    fread((char *)&gas,sizeof(struct gas_particle), 1, stdin) ;
	    xdr_gas(&xdrs, &gas);
	}
	for(i = 0; i < header.ndark; i++) {
	    fread((char *)&dark,sizeof(struct dark_particle), 1, stdin) ;
	    xdr_dark(&xdrs, &dark);
	}
	for(i = 0; i < header.nstar; i++) {
	    fread((char *)&star,sizeof(struct star_particle), 1, stdin) ;
	    xdr_star(&xdrs, &star);
	}
	
	fprintf(stderr, "read time %f\n",header.time) ;
      }
  xdr_destroy(&xdrs);
  return 0;
}
Ejemplo n.º 27
0
main()
{
 XDR xdr;
 FILE* fp = fopen("test", "r+");

 if(fp)
 {
   xdrstdio_create(&xdr, fp, XDR_DECODE);
 
   RWXDRistream rw_xdr(&xdr);
   int data;
   for(int i=0; i<10; ++i)
     {
       rw_xdr >> data;      // decode integer data

       if(data == i)
         cout << data << endl;
       else
         cout << "Bad input value" << endl;
     }

    fclose(fp);
  }
  else
Ejemplo n.º 28
0
void XdrMGF::init (XdrMGF::XdrIO_TYPE t, const char* fn, const char*, int)
{
  m_type=t;

  // Close old file if necessary
  if (mp_fp) this->fini();


  // Open file
  switch (m_type)
    {

#ifdef LIBMESH_HAVE_XDR

    case (XdrMGF::ENCODE):
    case (XdrMGF::DECODE):
      {
        mp_fp = fopen (fn, (m_type == ENCODE) ? "w" : "r");

        // Make sure the file is ready for use
        if (!mp_fp)
          libmesh_error_msg("XDR Error: Accessing file: " << fn << " failed.");

        // Create the XDR handle
        mp_xdr_handle = new XDR;
        xdrstdio_create(mp_xdr_handle,
                        mp_fp,
                        ((m_type == ENCODE) ? XDR_ENCODE : XDR_DECODE));

        break;
      }

#endif

    case (XdrMGF::R_ASCII):
      {
        mp_in.open(fn, std::ios::in);

        // Make sure it opened correctly
        if (!mp_in.good())
          libmesh_file_error(fn);

        break;
      }

    case (XdrMGF::W_ASCII):
      {
        mp_out.open(fn, std::ios::out);

        // Make sure it opened correctly
        if (!mp_out.good())
          libmesh_file_error(fn);

        break;
      }

    default:
      libmesh_error_msg("Unrecognized file access type!");
    }





  // Read/Write the file signature
  const int  bufLen = 12;
  char       buf[bufLen+1];

  switch (m_type)
    {

#ifdef LIBMESH_HAVE_XDR

    case (XdrMGF::ENCODE):
      {
        char* p = &buf[0];
        const LegacyXdrIO::FileFormat orig = this->get_orig_flag();

        std::ostringstream name;
        if (orig == LegacyXdrIO::DEAL)
          name << "DEAL 003:003";

        else if (orig == LegacyXdrIO::MGF)
          name << "MGF  002:000";

        else if (orig == LegacyXdrIO::LIBM)
          name << "LIBM " << this->get_num_levels();

        else
          libmesh_error_msg("Unknown orig " << orig);

        // Fill the buffer
        std::sprintf(&buf[0], "%s", name.str().c_str());

        xdr_string(mp_xdr_handle, &p, bufLen);  // Writes binary signature

        break;
      }

    case (XdrMGF::DECODE):
      {
        char* p = &buf[0];
        xdr_string(mp_xdr_handle, &p, bufLen); // Reads binary signature

        // Set the number of levels used in the mesh
        this->tokenize_first_line(p);

        break;
      }

#endif

    case (XdrMGF::W_ASCII):
      {
        const LegacyXdrIO::FileFormat orig = this->get_orig_flag();

        if (orig == LegacyXdrIO::DEAL)
          std::sprintf(&buf[0], "%s %03d:%03d", "DEAL", 3, 3);

        else if (orig == LegacyXdrIO::MGF)
          std::sprintf(&buf[0], "%s %03d:%03d", "MGF ", 2, 0);

        else if (orig == LegacyXdrIO::LIBM)
          std::sprintf(&buf[0], "%s %d", "LIBM", this->get_num_levels());

        mp_out << buf << '\n';

        break;
      }

    case (XdrMGF::R_ASCII):
      {

#ifdef __HP_aCC
        // weirdly, _only_ here aCC
        // is not fond of mp_in.getline()
        // however, using mp_in.getline()
        // further below is ok...
        std::string buf_buf;
        std::getline (mp_in, buf_buf, '\n');
        libmesh_assert_less_equal (buf_buf.size(), bufLen);

        buf_buf.copy (buf, std::string::npos);
#else

        // Here we first use getline() to grab the very
        // first line of the file into a char buffer.  Then
        // this line is tokenized to look for:
        // 1.) The name LIBM, which specifies the new Mesh style.
        // 2.) The number of levels in the Mesh which is being read.
        // Note that "buf" will be further processed below, here we
        // are just attempting to get the number of levels.
        mp_in.getline(buf, bufLen+1);

#endif

        // Determine the number of levels in this mesh
        this->tokenize_first_line(buf);

        break;
      }

    default:
      libmesh_error_msg("Unknown m_type" << m_type);
    }



  // If you are reading or decoding, process the signature
  if ((m_type == R_ASCII) || (m_type == DECODE))
    {
      char name[5];
      std::strncpy(name, &buf[0], 4);
      name[4] = '\0';

      if (std::strcmp (name, "DEAL") == 0)
        {
          this->orig_flag = LegacyXdrIO::DEAL; // 0 is the DEAL identifier by definition
        }
      else if (std::strcmp (name, "MGF ") == 0)
        {
          this->orig_flag = LegacyXdrIO::MGF; // 1 is the MGF identifier by definition
        }
      else if (std::strcmp (name, "LIBM") == 0)
        {
          this->orig_flag = LegacyXdrIO::LIBM; // the New and Improved XDA
        }

      else
        libmesh_error_msg("ERROR: No originating software can be determined for header string '" << name);
    }

}
Ejemplo n.º 29
0
int main(int argc, char *argv[])
{
  register int k, l, n, la;

  int     Nspace, result, NmaxIter, Ngdelay, Ngorder, Ngperiod, btype[3],
          inputs[N_INPUTS], nwrite, Nx, Nz;
  double  iterLimit, *lambda, Adamp;

  stats.printCPU = TRUE;
  commandline.quiet = FALSE;
  commandline.logfile = stderr;
  input.Eddington = FALSE;

  getCPU(0, TIME_START, NULL);
  SetFPEtraps();

  fpin  = fopen("2dinput.dat", "r");
  xdrstdio_create(&xdrs, fpin, XDR_DECODE);

  result = xdr_vector(&xdrs, (char *) inputs, N_INPUTS,
		      sizeof(int), (xdrproc_t) xdr_int);

  atmos.angleSet.set = (enum angleset) inputs[0];
  if (atmos.angleSet.set == SET_GL) {
     result = xdr_int(&xdrs, &atmos.angleSet.Ninclination);
     result = xdr_int(&xdrs, &atmos.angleSet.Nazimuth);
  }
  result = xdr_double(&xdrs, &iterLimit);
  result = xdr_double(&xdrs, &Adamp);

  Nx = geometry.Nx = inputs[1];
  Nz = geometry.Nz = inputs[2];
  NmaxIter = inputs[3];
  Ngdelay  = inputs[4];  Ngorder = inputs[5];  Ngperiod = inputs[6];
  Nlambda  = inputs[7];
  Nspace   = atmos.Nspace = geometry.Nx * geometry.Nz;

  result = xdr_vector(&xdrs, (char *) btype, 3,
		      sizeof(int), (xdrproc_t) xdr_int);
  geometry.hboundary = (enum boundary) btype[0];
  for (n = 0;  n < 2;  n++)
    geometry.bvalue[n] = (enum boundval) btype[1+n];

  /* --- Check the validity of boundary conditions and values -- ---- */ 

  switch (geometry.hboundary) {
  case FIXED:     break;
  case PERIODIC:  break;
  default:
    sprintf(messageStr, "Invalid horizontal boundary condition: %d",
	    geometry.hboundary);
    Error(ERROR_LEVEL_2, argv[0], messageStr);
    break;
  }

  switch (geometry.bvalue[TOP]) {
  case IRRADIATED:   break;
  case ZERO:         break;
  case THERMALIZED:  break;
  default:
    sprintf(messageStr, "Invalid boundary value at TOP: %d\n",
	    geometry.bvalue[TOP]);
    Error(ERROR_LEVEL_2, argv[0], messageStr);
    break;
  }
  switch (geometry.bvalue[BOTTOM]) {
  case IRRADIATED:   break;
  case ZERO:         break;
  case THERMALIZED:  break;
  default:
    sprintf(messageStr, "Invalid boundary value at BOTTOM: %d",
	    geometry.bvalue[BOTTOM]);
    Error(ERROR_LEVEL_2, argv[0], messageStr);
    break;
  }

  /* --- Get increments in x, store and check for monotonicity -- --- */

  geometry.dx = (double *) malloc(Nx * sizeof(double));
  result = xdr_vector(&xdrs, (char *) geometry.dx, Nx,
		       sizeof(double), (xdrproc_t) xdr_double);
  for (l = 0;  l < ((geometry.hboundary == PERIODIC) ? Nx : Nx-1);  l++) {
    geometry.dx[l] *= KM_TO_M;
    if (geometry.dx[l] <= 0.0) {
      sprintf(messageStr, "At l = %d:\n x does not increase strictly "
              "monotonically towards the right", l);
      Error(ERROR_LEVEL_2, argv[0], messageStr);
    } 
  }
  geometry.x = (double *) malloc(Nx * sizeof(double));
  geometry.x[0] = 0.0;
  for (l = 0;  l < Nx-1;  l++)
    geometry.x[l+1] = geometry.x[l] + geometry.dx[l];

  /* --- Get vertical grid --                          -------------- */

  geometry.z = (double *) malloc(Nz * sizeof(double));
  result = xdr_vector(&xdrs, (char *) geometry.z, Nz,
		      sizeof(double), (xdrproc_t) xdr_double);
  for (k = 0;  k < Nz;  k++) geometry.z[k] *= KM_TO_M;

  geometry.dz = (double *) malloc(Nz * sizeof(double));
  for (k = 0;  k < Nz-1;  k++) {
    geometry.dz[k] = geometry.z[k] - geometry.z[k+1];
    if (geometry.dz[k] <= 0.0) {
      sprintf(messageStr, "At k = %d:\n z does not decrease strictly "
              "monotonically towards the bottom", k);
      Error(ERROR_LEVEL_2, argv[0], messageStr);
    }
  }
  geometry.dz[Nz-1] = 0.0;


  chi = (double *) malloc(Nspace * sizeof(double));
  S   = (double *) malloc(Nspace * sizeof(double));
  Bp  = (double *) malloc(Nspace * sizeof(double));
  epsilon = (double *) malloc(Nspace * sizeof(double));

  result = xdr_vector(&xdrs, (char *) chi, Nspace,
		       sizeof(double), (xdrproc_t) xdr_double);
  result = xdr_vector(&xdrs, (char *) Bp, Nspace,
		       sizeof(double), (xdrproc_t) xdr_double);
  result = xdr_vector(&xdrs, (char *) epsilon, Nspace,
		       sizeof(double), (xdrproc_t) xdr_double);

  getBoundary(&atmos, &geometry);
  getAngleQuadr(&geometry);
  atmos.Nrays = geometry.Nrays;
  atmos.wmu   = geometry.wmu;
  fillMesh(&geometry);

  lambda = (double *) malloc(Nlambda * sizeof(double));
  phi    = (double *) malloc(Nlambda * sizeof(double));
  wlamb  = (double *) malloc(Nlambda * sizeof(double));

  result = xdr_vector(&xdrs, (char *) lambda, Nlambda,
		      sizeof(double), (xdrproc_t) xdr_double);
  wlamb[0] = (lambda[1] - lambda[0]);
  for (la = 1;  la < Nlambda-1;  la++)
    wlamb[la] = (lambda[la+1] - lambda[la-1]);
  wlamb[Nlambda-1] = (lambda[Nlambda-1] - lambda[Nlambda-2]);

  wphi = 0.0;
  for (la = 0;  la < Nlambda;  la++) {
    phi[la] = Voigt(Adamp, lambda[la], NULL, RYBICKI)/SQRTPI;
    wphi   += wlamb[la]*phi[la];
  }
  wphi = 1.0/wphi;

  xdr_destroy(&xdrs);
  fclose(fpin);

  for (k = 0;  k < Nspace;  k++)  S[k] = Bp[k];
  Ng_S = NgInit(Nspace, Ngdelay, Ngorder, Ngperiod, S);

  Iterate(NmaxIter, iterLimit);

  nwrite = fwrite(S, sizeof(double), Nspace, stdout);
  printTotalCPU();
}
Ejemplo n.º 30
0
int
mbbs_splitfile(char *dirnm, char *bsfnm0, char *bsfnm1, int pngid, char *logprefix)
{
	int chngdir, origdirfd, err;
	char tmpfilenm[80], prefix[80], newlogtail[120];
	char *lp, *tailstr;
	FILE *ifp, *ofp;
	XDR xdri, xdro;
	BSFile bsfi, bsfo;

	if (sizeof(int) < 4)
		return BS_BADARCH;

	if ((bsfnm0 == (char *) 0) ||
	    ((int) strlen(bsfnm0) == 0) ||
	    (bsfnm1 == (char *) 0) ||
	    ((int) strlen(bsfnm1) == 0) ||
	    (pngid < 0))
		return BS_BADARG;

	if ((dirnm == (char *) 0) ||
	    ((int) strlen(dirnm) == 0))
		chngdir= 0;
	else
		chngdir= 1;

	if (chngdir) {
		if ((origdirfd= open(".", O_RDONLY)) < 0)
			return BS_OPEN;
		if (chdir(dirnm) < 0) {
			(void) close(origdirfd);
			return BS_CHDIR;
		}
	}

	/* rename original file to temporary name */
	(void) strcpy(tmpfilenm, "BSLIBsplittmp");
	if (access(tmpfilenm, F_OK) == 0) {
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return BS_ACCESS;
	}
	if (rename(bsfnm0, tmpfilenm) < 0) {
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return BS_RENAME;
	}

	if ((ifp= fopen(tmpfilenm, "r")) == (FILE *) 0) {
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return BS_OPEN;
	}
	xdrstdio_create(&xdri, ifp, XDR_DECODE);
	if ((err= mbbs_rdbsfhdr(&bsfi, &xdri)) != BS_SUCCESS) {
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return err;
	}
	if (pngid >= bsfi.bsf_count) {
		(void) mbbs_freebsfmem(&bsfi);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return BS_BADARG;
	}
	MemCopy(&bsfi, &bsfo, sizeof(BSFile));

	/* these strings now belong to the output header! */
	bsfi.bsf_srcfilenm= (char *) 0;
	bsfi.bsf_log= (char *) 0;

	if ((logprefix == (char *) 0) ||
	    ((int) strlen(logprefix) == 0)) {
		(void) sprintf(prefix, "BSLIB::bs_split()");
		lp= prefix;
	}
	else if ((int) strlen(logprefix) > 50) {
		(void) strncpy(prefix, logprefix, 47);
		prefix[47]= '\0';
		(void) strcat(prefix, "...");
		lp= prefix;
	}
	else
		lp= logprefix;
	if ((int) strlen(bsfo.bsf_log) > 0)
		(void) sprintf(newlogtail, "\n%s [ BreakFile @ Ping%1d HEAD ] ;", lp, pngid);
	else
		(void) sprintf(newlogtail, "%s [ BreakFile @ Ping%1d HEAD ] ;", lp, pngid);
	if ((err= mbbs_appendstr(&(bsfo.bsf_log), newlogtail)) != BS_SUCCESS) {
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return err;
	}

	/* eventually we will replace the "HEAD ] ;" substring just
	   appended to the log with "TAIL ] ;", so locate it now */
	tailstr= bsfo.bsf_log;
	tailstr+= (int) strlen(bsfo.bsf_log);
	for ( ; *tailstr != 'H'; tailstr--);

	/* copy first part of original file */
	if ((ofp= fopen(bsfnm0, "w")) == (FILE *) 0) {
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return BS_OPEN;
	}
	xdrstdio_create(&xdro, ofp, XDR_ENCODE);
	bsfo.bsf_count= pngid;
	if ((err= mbbs_wrbsfhdr(&bsfo, &xdro)) != BS_SUCCESS) {
		(void) fclose(ofp);
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return err;
	}
	if ((err= mbbs_copypng(bsfo.bsf_count, &xdri, &xdro, bsfi.bsf_version)) != BS_SUCCESS) {
		(void) fclose(ofp);
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return err;
	}
	xdr_destroy(&xdro);
	(void) fclose(ofp);

	/* copy second part of original file */
	if ((ofp= fopen(bsfnm1, "w")) == (FILE *) 0) {
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return BS_OPEN;
	}
	xdrstdio_create(&xdro, ofp, XDR_ENCODE);
	bsfo.bsf_count= bsfi.bsf_count-pngid;
	(void) strcpy(tailstr, "TAIL ] ;");
	if ((err= mbbs_wrbsfhdr(&bsfo, &xdro)) != BS_SUCCESS) {
		(void) fclose(ofp);
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return err;
	}
	if ((err= mbbs_copypng(bsfo.bsf_count, &xdri, &xdro, bsfi.bsf_version)) != BS_SUCCESS) {
		(void) fclose(ofp);
		(void) mbbs_freebsfmem(&bsfo);
		(void) fclose(ifp);
		(void) rename(tmpfilenm, bsfnm0);
		if (chngdir) {
			(void) fchdir(origdirfd);
			(void) close(origdirfd);
		}
		return err;
	}
	xdr_destroy(&xdro);
	(void) fclose(ofp);

	(void) mbbs_freebsfmem(&bsfo);
	(void) fclose(ifp);
	(void) unlink(tmpfilenm);
	if (chngdir) {
		if (fchdir(origdirfd) != 0) {
			(void) close(origdirfd);
			return BS_CHDIR;
		}
		(void) close(origdirfd);
	}

	return BS_SUCCESS;
}