// IO functions
int readFromBinFile(Matrix * matrix, int matrixToBeRead, char * file)
{
      FILE * fp;
      Matrix temp;
      int i, errore_fine_file = 0;

      if ((fp = fopen(file,"rb"))!=NULL)
      {
          /*
          Per raggiungere la matrix da leggere bisogna passare in rassegna di tutte
          le precedenti, in quanto non si è a conoscenza della loro grandezza
          */
          for(i=0; i<matrixToBeRead; i++)
          {
              fread(&temp,1,sizeof(matrix),fp);
              /*
              Una volta letta una matrix, si può determinare quante celle di memoria saltare
              in base alla sua dimensione
              */
              fseek(fp, readColumns(temp)*readRows(temp)*sizeof(float),SEEK_CUR);
          }
          //Se si è alla fine del file, la matrix non esiste
          if (feof(fp))
          {
             fclose(fp);
             return 3;
          }
          else
          {
              fread(&temp, 1, sizeof(matrix), fp);
              //Reinizializzo la matrix per evitare di sovrascrivere dati
              if (!initMatrix(matrix,readRows(temp),readColumns(temp))) //Si verifica che la matrix sia inizializzabile
              {
                 //Adesso si leggono i values
                 for(i=0; i<readRows(*matrix) && !errore_fine_file;i++)
                 {
                     if (fread(*(matrix->values + i),sizeof(float), readColumns(*matrix),fp)!=readColumns(*matrix))
                        errore_fine_file = 1;
                        //Si controlla che vengano letti il giusto numero di dati.
                 }
                 if (errore_fine_file) return 4;
                 return 0;
              }
              else return 5;
          }
      }
      else return 1;
}
Exemple #2
0
int Patch::read(const string &file)
{
  jRoot = json_load_file(file.c_str(), 0, &jError);
  if (!jRoot) {
    gu_log("[Patch::read] unable to read patch file! Error on line %d: %s\n", jError.line, jError.text);
    return PATCH_UNREADABLE;
  }

  if (!checkObject(jRoot, "root element"))
    return PATCH_INVALID;

  init();

  /* TODO json_decref also when PATCH_INVALID */

  if (!readCommons(jRoot))  return setInvalid();
  if (!readColumns(jRoot))  return setInvalid();
  if (!readChannels(jRoot)) return setInvalid();
#ifdef WITH_VST
  if (!readPlugins(jRoot, &masterInPlugins, PATCH_KEY_MASTER_IN_PLUGINS))   return setInvalid();
  if (!readPlugins(jRoot, &masterOutPlugins, PATCH_KEY_MASTER_OUT_PLUGINS)) return setInvalid();
#endif

  json_decref(jRoot);

  sanitize();

  return PATCH_READ_OK;
}
IpfixDbWriterOracleCfg::IpfixDbWriterOracleCfg(XMLElement* elem)
  : CfgHelper<IpfixDbWriterOracle, IpfixDbWriterOracleCfg>(elem, "ipfixDbWriterOracle"),
    port(0), bufferRecords(30), observationDomainId(0)
{
	msg(MSG_DEBUG, "Starting configuration for Oracle connection");
	if (!elem) return;
	XMLNode::XMLSet<XMLElement*> set = _elem->getElementChildren();
	for (XMLNode::XMLSet<XMLElement*>::iterator it = set.begin(); it != set.end(); it++) {
		XMLElement* e = *it;
		if (e->matches("host")) {
			hostname = e->getFirstText();
		} else if (e->matches("port")) {
			port = getInt("port");
		} else if (e->matches("dbname")) {
			dbname = e->getFirstText();
		} else if (e->matches("username")) {
			user = e->getFirstText();
		} else if (e->matches("password")) {
			password = e->getFirstText();
		} else if (e->matches("bufferrecords")) {
			bufferRecords = getInt("bufferrecords");
		} else if (e->matches("columns")) {
			readColumns(e);
		} else if (e->matches("next")) { // ignore next
		} else {
			msg(MSG_FATAL, "Unknown IpfixDbWriterOracle config statement %s\n", e->getName().c_str());
			continue;
		}
	}
	if (hostname=="") THROWEXCEPTION("IpfixDbWriterOracleCfg: host not set in configuration!");
	if (port==0) THROWEXCEPTION("IpfixDbWriterOracleCfg: port not set in configuration!");
	if (dbname=="") THROWEXCEPTION("IpfixDbWriterOracleCfg: dbname not set in configuration!");
	if (user=="") THROWEXCEPTION("IpfixDbWriterOracleCfg: username not set in configuration!");
	if (password=="") THROWEXCEPTION("IpfixDbWriterOracleCfg: password not set in configuration!");
}
int read(Matrix *matrix)
{
     int i,j,rows,cols;
     float value;

     do
     {
     printf("Inserire numero rows: ");
     scanf("%d",&rows);
     }
     while(rows<=0);
     do
     {
     printf("Inserire numero cols: ");
     scanf("%d",&cols);
     }
     while(cols<=0);

     initMatrix(matrix, rows, cols);

     for(i=0; i<readRows(*matrix); i++)
         for(j=0; j<readColumns(*matrix); j++)
         {
             printf("Inserisci value(%d,%d): ", i, j);
             scanf("%f",&value);
             writeMatrix(matrix, i, j, value);
         }

     return 0;
}
int readMatrix(Matrix matrix, int row, int col, float * value)
{
     if (row < 0 || row >= readRows(matrix) || col < 0 || col >= readColumns(matrix) )
        return 6;
     *value = *(*(matrix.values + row)+col);
     return 0;
}
// Write and read values MATRICE
int writeMatrix(Matrix *matrix, int row, int col, float value)
{
     if (row < 0 || row >= readRows(*matrix) || col < 0 || col >= readColumns(*matrix) )
        return 7;
     *(*(matrix->values + row)+col)=value;
     return 0;
}
int writeToTxtFile(Matrix * matrix, char * file)
{
      FILE * fp;
      int i,j;
      float matrixValue;

      if ((fp = fopen(file,"a"))!=NULL)
      {
          fprintf(fp,"\n%d %d ", readRows(*matrix), readColumns(*matrix));
          for(i=0; i<readRows(*matrix);i++)
              for(j=0; j<readColumns(*matrix);j++)
              {
                  readMatrix(*matrix, i, j, &matrixValue);
                  fprintf(fp,"%f ",matrixValue);
              }
          fprintf(fp,"\n");

          fclose(fp);
          return 0;
      }
      else return 1;
}
Exemple #8
0
void TableEditor::deleteColumnClicked()
{
    if ( listColumns->currentItem() == -1 )
	return;
#ifndef QT_NO_TABLE
    table->setNumCols( table->numCols() - 1 );
    delete listColumns->item( listColumns->currentItem() );
    readColumns();
    if ( listColumns->firstItem() ) {
	listColumns->setCurrentItem( listColumns->firstItem() );
	listColumns->setSelected( listColumns->firstItem(), TRUE );
    }
#endif
}
int readMatrix(Matrix matrix, int row, int col, float * value)
{
    if
        (
            row < 0 ||
            row >= readRows(matrix) ||
            col < 0 ||
            col >= readColumns(matrix)
        )
        return 6;

    *value = matrix.values[col*readRows(matrix)+ row];

    return 0;
}
// Write and read values MATRICE
int writeMatrix(Matrix *matrix, int row, int col, float value)
{
    if
        (
            row < 0 ||
            row >= readRows(*matrix) ||
            col < 0 ||
            col >= readColumns(*matrix)
        )
        return 7;

    matrix->values[col*readRows(*matrix)+ row]=value;
    
    return 0;
}
Exemple #11
0
void TableEditor::columnUpClicked()
{
    if ( listColumns->currentItem() <= 0 ||
	 listColumns->count() < 2 )
	return;
    saveFieldMap();
    int index = listColumns->currentItem() - 1;
    QListBoxItem *i = listColumns->item( listColumns->currentItem() );
    listColumns->takeItem( i );
    listColumns->insertItem( i, index );
    listColumns->setCurrentItem( i );
    readColumns();
    restoreFieldMap();
    currentColumnChanged( i );
}
// GUI
int print(Matrix matrix)
{
     int i,j;
     float matrixValue;

     puts("--------");
     for(i=0; i<readRows(matrix); i++)
     {
         for(j=0; j<readColumns(matrix); j++)
         {
             readMatrix(matrix, i, j, &matrixValue);
             printf("%f ", matrixValue);
         }
     printf("\n");
     }
     puts("--------");
     return 0;
}
int writeToBinFile(Matrix * matrix, char * file)
{
      FILE * fp;
      int i;

      if ((fp = fopen(file,"ab"))!=NULL)
      {
          //Si scrive innanzitutto la struttura della matrix
          fwrite(matrix,sizeof(matrix),1,fp);
          for(i=0; i<readRows(*matrix);i++)
          {
              //Ora si scrivono i values
              fwrite(*(matrix->values + i),sizeof(float),readColumns(*matrix),fp);
          }
          fclose(fp);
          return 0;
      }
      else return 1;
}
Exemple #14
0
int main(int argc,char *argv[])
{
  //////////////////////////////////////////////////////////////
  //PROGRAM VARIBALES
  //////////////////////////////////////////////////////////////
  char datafile[FSIZE]="",derintfile[FSIZE]="";
  int numcols=0,colx=0,coly=0;
  int i,ndata;

  //////////////////////////////////////////////////////////////
  //INITIALIZE
  //////////////////////////////////////////////////////////////
  TITLE(stdout,'*',"COMPUTE DERIVATIVES AND INTEGRAL OF DATA");

  //////////////////////////////////////////////////////////////
  //SET OPTIONS AND USAGE
  //////////////////////////////////////////////////////////////
  SET_OPTIONS(":hvVf:d:n:x:y:");
  SET_USAGE(
"=======================================================================================\n"
"Usage:\n\n"
"\t./program -f <datafile> [-d <derint_file>] -n <numcols> -x <colx> -y <coly>\n"
"\n"
"Take columns <colx> and <coly> from <datafile> and computes the first\n"
"and second derivative but also the integral in the interval.  The\n"
"results are stored in file <derint_file>\n"
"=======================================================================================\n"
);

  //////////////////////////////////////////////////////////////
  //READ OPTIONS
  //////////////////////////////////////////////////////////////
  while(ITEROPTIONS){
    switch(OPTION){
    case 'f':
      strcpy(datafile,optarg);
      break;
    case 'd':
      strcpy(derintfile,optarg);
      break;
    case 'n':
      numcols=atoi(optarg);
      break;
    case 'x':
      colx=atoi(optarg);
      break;
    case 'y':
      coly=atoi(optarg);
      break;
    //========================================
    //COMMON
    //========================================
    case 'v':
      VERBOSITY=1;
      break;
    case 'V':
      VERBOSITY=2;
      break;
    //DETECT ERRORS
    OPTION_ERRORS;
    }
  }

  //////////////////////////////////////////////////////////////
  //VALIDATE OPTIONS
  //////////////////////////////////////////////////////////////
  if(isBlank(datafile)){
    fprintf(stderr,"Error: No datafile was provided\n");
    PRINT_USAGE;
    EXIT;
  }
  if(!fileExists(datafile)){
    fprintf(stderr,"Error: Datafile '%s' does not exist\n",datafile);
    PRINT_USAGE;
    EXIT;
  }
  if(isBlank(derintfile)){
    sprintf(derintfile,"%s.der",datafile);
  }
  if((ndata=countLines(datafile))==0){
    fprintf(stderr,"Error: Datafile '%s' seems empty\n",datafile);
    PRINT_USAGE;
    EXIT;
  }
  if(numcols<1){
    fprintf(stderr,"Error: The number of columns should be different from 0\n");
    PRINT_USAGE;
    EXIT;
  }
  if(colx==0){
    colx=0;
  }
  if(coly==0){
    coly=1;
  }

  //////////////////////////////////////////////////////////////
  //REPORT INPUT INFORMATION
  //////////////////////////////////////////////////////////////
  if(VERBOSE(1)){
    BAR(stdout,'O');
    fprintf(stdout,"Datafile: %s\n",datafile);
    fprintf(stdout,"Der. Int. file: %s\n",derintfile);
    fprintf(stdout,"Number of columns: %d\n",numcols);
    fprintf(stdout,"Columns x,y: %d,%d\n",colx,coly);
    fprintf(stdout,"Number of points in datafile: %d\n",ndata);
    BAR(stdout,'O');
  }

  //////////////////////////////////////////////////////////////
  //PROGRAM
  //////////////////////////////////////////////////////////////

  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  //READING DATA
  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  STPRINTF("Reading %d data points from datafile '%s'...\n",ndata,datafile);
  real *X=readColumns(datafile,ndata,numcols,colx);
  real *Y=readColumns(datafile,ndata,numcols,coly);

  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  //COMPUTING DERIVATIVES
  //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  STPRINTF("Computing derivatives and integral...\n");
  file fd=fileOpen(derintfile,"w");
  fprintf(fd,"#Datafile: %s\n",datafile);
  fprintf(fd,"#NumberColumns: %d\n",numcols);
  fprintf(fd,"#ColX,ColY: %d %d\n",colx,coly);
  fprintf(fd,"%-14s %-14s %-14s %-14s %-14s\n",
	  "#1:X","2:Y","3:dydx","4:d2ydx2","5:integ");

  real dx,dydx,d2ydx2,integ=0;
  for(i=0;i<ndata;i++){
    if(i==0){
      dx=X[i+1]-X[i];
      dydx=(Y[i+1]-Y[i])/dx;
      d2ydx2=(Y[2]-2*Y[1]+Y[0])/((X[2]-X[0])/2*(X[2]-X[0])/2);
      integ+=(Y[i]+Y[i+1])/2*dx;
    }else if(i==ndata-1){
      dx=X[i]-X[i-1];
      dydx=(Y[i]-Y[i-1])/dx;
      d2ydx2=(Y[ndata-1]-2*Y[ndata-2]+Y[ndata-3])/
	((X[ndata-1]-X[ndata-3])/2*(X[ndata-1]-X[ndata-3])/2);
      integ+=(Y[i]+Y[i-1])/2*dx;
    }else{
      dx=X[i+1]-X[i-1];
      dydx=(Y[i+1]-Y[i-1])/dx;
      d2ydx2=(Y[i+1]-2*Y[i]+Y[i-1])/(dx/2*dx/2);
      integ+=(Y[i+1]+Y[i-1])/2*(dx/2);
    }
    fprintf(fd,"%+14.7e %+14.7e %+14.7e %+14.7e %+14.7e\n",
	    X[i],Y[i],dydx,d2ydx2,integ);
  }
  fclose(fd);
  STPRINTF("Done.\n");
  
  return 0;
}