⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spice3.c

📁 this file is contain important facts aboute different programming langueges.
💻 C
📖 第 1 页 / 共 3 页
字号:
   double temp;
   int rc = CVT_NORMAL;

   if( input_type == CVT_BIG_ENDIAN_IEEE_S ) {
      memcpy(buffer,input_val,4);
      if( output_type == CVT_IEEE_S ) {
         t = buffer[0];
         buffer[0] = buffer[3];
         buffer[3] = t;
         t = buffer[1];
         buffer[1] = buffer[2];
         buffer[2] = t;
         memcpy(output_val,buffer,4);
      } else {
         rc = CVT_INVALID_OUTPUT_TYPE;
      }
   } else if ( input_type == CVT_BIG_ENDIAN_IEEE_T ) {
      memcpy(buffer,input_val,8);
      if( output_type == CVT_IEEE_S ) {
         t = buffer[0];
         buffer[0] = buffer[7];
         buffer[7] = t;
         t = buffer[1];
         buffer[1] = buffer[6];
         buffer[6] = t;
         t = buffer[2];
         buffer[2] = buffer[5];
         buffer[5] = t;
         t = buffer[3];
         buffer[3] = buffer[4];
         buffer[4] = t;
         memcpy(&temp,buffer,8);
         *(float *)output_val = (float) temp;
      } else {
         rc = CVT_INVALID_OUTPUT_TYPE;
      }
   } else {
      rc = CVT_INVALID_INPUT_TYPE;
   }

   return rc;
}


#endif

/************************************************************************/
/*    Spice3 Fortran interface                                          */
/*                                                                      */
/*    Note: Some functions return addresses. On 64-bit systems, this    */
/*    MUST be stored into an INTEGER*8 variable (or a POINTER under f90)*/
/*                                                                      */
/************************************************************************/
#if defined(vms) || defined(__vms)
#define FORTRAN_CALL
#include <descrip.h>
#elif defined(_WIN32)
#define FORTRAN_CALL __stdcall
#define fspice3_open           FSPICE3_OPEN
#define fspice3_load_transient FSPICE3_LOAD_TRANSIENT
#define fspice3_close          FSPICE3_CLOSE
#define fspice3_free_data      FSPICE3_FREE_DATA
#define fspice3_get_error_msg  FSPICE3_GET_ERROR_MSG
#define fspice3_num_transient_points FSPICE3_NUM_TRANSIENT_POINTS
#define fspice3_get_quantity_data FSPICE3_GET_QUANTITY_DATA
#define fspice3_get_simulation_type FSPICE3_GET_SIMULATION_TYPE
#define fspice3_get_name_by_index FSPICE3_GET_NAME_BY_INDEX
#define fspice3_num_variables     FSPICE3_NUM_VARIABLES
#define fspice3_get_quantity_units  FSPICE3_GET_QUANTITY_UNITS
#else
#define FORTRAN_CALL
#define fspice3_open           fspice3_open_
#define fspice3_load_transient fspice3_load_transient_
#define fspice3_close          fspice3_close_
#define fspice3_free_data      fspice3_free_data_
#define fspice3_get_error_msg  fspice3_get_error_msg_
#define fspice3_num_transient_points fspice3_num_transient_points_
#define fspice3_get_quantity_data fspice3_get_quantity_data_
#define fspice3_get_simulation_type fspice3_get_simulation_type_
#define fspice3_num_variables     fspice3_num_variables_
#define fspice3_get_name_by_index fspice3_get_name_by_index_
#define fspice3_get_quantity_units  fspice3_get_quantity_units_
#endif

#if defined(vms) || defined(__vms)
void FORTRAN_CALL fspice3_open(struct dsc$descriptor_s *file_name_dsc,
                               Spice3File *f,
                               int *error_code)
{
   char file[256], ext[256], m[5];
   int ilen;

   ilen = lnblnk(file_name_dsc->dsc$a_pointer, file_name_dsc->dsc$w_length);
   memcpy(file, file_name_dsc->dsc$a_pointer, MIN(ilen,255));
   file[ilen] = NUL;
   *f = spice3_open(file, error_code);
}
#else
#if defined(_WIN32)
void FORTRAN_CALL fspice3_open(char *file_name, int len1,
                               Spice3File *f,
                               int  *error_code)
#else
void FORTRAN_CALL fspice3_open(char *file_name,
                               Spice3File *f,
                               int  *error_code,
                               int  len1)
#endif
{
   char file[256];
   int ilen;

   ilen = lnblnk(file_name, len1);
   memcpy(file, file_name, MIN(ilen,255));
   file[ilen] = NUL;
   *f = spice3_open(file, error_code);
}
#endif

void FORTRAN_CALL fspice3_close(Spice3File *f)
{
   spice3_close(*f);
}

void FORTRAN_CALL fspice3_load_transient(Spice3File *f,
                                         int *data_group, int *convert_type,
                                         Spice3Data *d,  int *error_code)
{
   *d = spice3_load_transient(*f, *data_group, *convert_type, error_code);
}

void FORTRAN_CALL fspice3_free_data(Spice3Data *data)
{
   spice3_free_data(*data);
}

#if defined(vms) || defined (__vms)
void FORTRAN_CALL fspice3_get_error_msg(int *error_code,
                                        struct dsc$descriptor_s *msg_dsc)
{
   char *p;
   int ilen, len1;

   p = spice3_get_error_msg(*error_code);
   ilen = strlen(p);

   len1 = msg_dsc->dsc$w_length;
   memset(msg_dsc->dsc$a_pointer,' ',len1); /* Blank fill */
   memcpy(msg_dsc->dsc$a_pointer,p,MIN(ilen,len1));

}
#else
#if defined(_WIN32)
void FORTRAN_CALL fspice3_get_error_msg(int *error_code, char *msg, int len1)
#else
void FORTRAN_CALL fspice3_get_error_msg(int *error_code, char *msg, int len1)
#endif
{
   char *p;
   int ilen;

   p = spice3_get_error_msg(*error_code);
   ilen = strlen(p);

   memset(msg,' ',len1); /* Blank fill */
   memcpy(msg,p,MIN(ilen,len1));
}
#endif

int FORTRAN_CALL fspice3_num_transient_points(Spice3Data *data)
{
   return spice3_num_transient_points(*data);
}

int FORTRAN_CALL fspice3_num_variables(Spice3Data *data)
{
   return spice3_num_variables(*data);
}

#if defined(vms) || defined(__vms)
void FORTRAN_CALL fspice3_get_name_by_index(Spice3Data *data,
                                           int *index,
                                           struct dsc$descriptor_s *name_dsc)
{
   int ilen;
   char *p;

   /* Blank fill target */
   memset(name_dsc->dsc$a_pointer,' ',name_dsc->dsc$w_length);

   p = spice3_get_name_by_index(*data, *index);
   if( p != NULL ) {
      ilen = strlen(p);
      memcpy(name_dsc->dsc$a_pointer,p,MIN(ilen,name_dsc->dsc$w_length));
   }

}
#else
#if defined(_WIN32)
void FORTRAN_CALL fspice3_get_name_by_index(Spice3Data *data,
                                           int *index,
                                           char *name, int len1)
#else
void FORTRAN_CALL fspice3_get_name_by_index(Spice3Data *data,
                                           int *index,
                                           char *name, int len1)
#endif
{
   int ilen;
   char *p;

   /* Blank fill target */
   memset(name,' ',len1);

   p = spice3_get_name_by_index(*data, *index);
   if( p != NULL ) {
      ilen = strlen(p);
      memcpy(name,p,MIN(ilen,len1));
   }

}
#endif

int FORTRAN_CALL fspice3_get_simulation_type(Spice3Data *data)
{
   return spice3_get_simulation_type(*data);
}

#if defined(vms) || defined(__vms)
int FORTRAN_CALL fspice3_get_quantity_data(Spice3Data *data,
                                           struct dsc$descriptor_s *name_dsc,
                                           float *array)
{
   char str[256];
   int ilen;

   /* Create NUL terminated string */
   ilen = lnblnk(name_dsc->dsc$a_pointer, name_dsc->dsc$w_length);
   memcpy(str,name_dsc->dsc$a_pointer,MIN(255,ilen));
   str[ilen] = NUL;
   return spice3_get_quantity_data(*data, str, array);
}
#else
#if defined(_WIN32)
int FORTRAN_CALL fspice3_get_quantity_data(Spice3Data *data, char *name,
                                           int len1,
                                           float *array)
#else
int FORTRAN_CALL fspice3_get_quantity_data(Spice3Data *data, char *name,
                                           float *array,
                                           int len1)
#endif
{
   char str[256];
   int ilen;

   /* Create NUL terminated string */
   ilen = lnblnk(name, len1);
   memcpy(str,name,MIN(255,ilen));
   str[ilen] = NUL;
   return spice3_get_quantity_data(*data, str, array);
}
#endif

#if defined(vms) || defined(__vms)
int FORTRAN_CALL fspice3_get_quantity_units(Spice3Data *data,
                                            struct dsc$descriptor_s *name_dsc,
                                            struct dsc$descriptor_s *units_dsc)
{
   char str[256], units[256];
   int ilen, rc;

   /* Create NUL terminated name string */
   ilen = lnblnk(name_dsc->dsc$a_pointer, name_dsc->dsc$w_length);
   memcpy(str,name_dsc->dsc$a_pointer,MIN(255,ilen));
   str[ilen] = NUL;

   rc = spice3_get_quantity_units(*data, str, units, 255);

   /* Set up blank padded return string*/
   ilen = strlen(units);
   memset(units_dsc->dsc$a_pointer, ' ', units_dsc->dsc$w_length);
   memcpy(units_dsc->dsc$a_pointer, units, MIN(ilen, units_dsc->dsc$w_length));

   return rc;
}
#else
#if defined(_WIN32)
int FORTRAN_CALL fspice3_get_quantity_units(Spice3Data *data,
                                            char *name, int len1,
                                            char *units, int len2)
#else
int FORTRAN_CALL fspice3_get_quantity_units(Spice3Data *data,
                                            char *name, char *units,
                                            int len1, int len2)
#endif
{
   char str[256], str2[255];
   int ilen, rc;

   /* Create NUL terminated string */
   ilen = lnblnk(name, len1);
   memcpy(str,name,MIN(255,ilen));
   str[ilen] = NUL;

   rc = spice3_get_quantity_units(*data, str, str2, 255);

   /* Set up blank padded return string*/
   ilen = strlen(str2);
   memset(units, ' ', len2);
   memcpy(units, str2, MIN(ilen, len2));

   return rc;
}
#endif

/************************************************************************/
/*    Spice3 Sample main routine                                        */
/************************************************************************/
#if DEBUG_SPICE3

/* A sample driver program for these routines */
/* To create: cc -DDEBUG_SPICE3 spice3.c -lm  */

main (int argc, char *argv[]) {

   Spice3File   file;
   Spice3Data   data;
   int          transient_points, i, num_variables;
   float        *time, *quantity;
   char         name[80];
   int          convert_type=SPICE3_LITTLE_ENDIAN;
   int          error_code;

   if (argc < 3) {
      printf("usage: %s spice3_file name\n", argv[0]);
      exit(EXIT_FAILURE);
   }
   if (argc == 4) {
      sscanf(argv[3],"%d",&convert_type);
   }

   file = spice3_open(argv[1], &error_code);
   if (error_code != 0) {
      printf("spice3_open() error: %s\n", spice3_get_error_msg(error_code));
      exit(EXIT_FAILURE);
   }

   data = spice3_load_transient (file, 1, convert_type, &error_code);
   if (error_code != 0) {
      printf("spice3_load_transient() error: %s\n", spice3_get_error_msg(error_code));
      exit(EXIT_FAILURE);
   }

   spice3_close(file);

   num_variables    = spice3_num_variables(data);
   for(i=0; i<num_variables; i++) {
      printf("Variable %d: %s\n",i,spice3_get_name_by_index(data,i));
   }

   transient_points = spice3_num_transient_points(data);
   time     = (float *) malloc( sizeof(float) * transient_points );
   quantity = (float *) malloc( sizeof(float) * transient_points );
   if( time == NULL || quantity == NULL ) {
      printf("Could not malloc storage\n");
      exit(EXIT_FAILURE);
   }
   error_code = spice3_get_quantity_data(data, "time", time);
   if( error_code != 0 ) {
      printf("spice3_get_time_data() error: %s\n", spice3_get_error_msg(error_code));
   }
   strcpy(name,argv[2]);
   error_code = spice3_get_quantity_data(data, name, quantity);
   if( error_code != 0 ) {
      printf("spice3_get_quantity_data() error: %s\n", spice3_get_error_msg(error_code));
   }

   if( error_code == 0 ) {
      for(i=0; i<transient_points; i++) {
         printf("%e %e\n",time[i],quantity[i]);
      }
   }

   spice3_free_data(data);
   free(time);
   free(quantity);

   exit(EXIT_SUCCESS);
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -