Ejemplo n.º 1
0
HeaderParams& Codec20::writeHeader(
    Transport& transport, HeaderParams& params) const
{
    return writeHeader(transport, params, HotRodConstants::VERSION_20);
}
Ejemplo n.º 2
0
unsigned long CSysSolve::ConjugateGradient(const CSysVector & b, CSysVector & x, CMatrixVectorProduct & mat_vec,
                                           CPreconditioner & precond, double tol, unsigned long m, bool monitoring) {
	
int rank = 0;

#ifndef NO_MPI
#ifdef WINDOWS
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#else
	rank = MPI::COMM_WORLD.Get_rank();
#endif
#endif
  
  /*--- Check the subspace size ---*/
  if (m < 1) {
    if (rank == 0) cerr << "CSysSolve::ConjugateGradient: illegal value for subspace size, m = " << m << endl;
#ifdef NO_MPI
    exit(1);
#else
#ifdef WINDOWS
	MPI_Abort(MPI_COMM_WORLD,1);
    MPI_Finalize();
#else
    MPI::COMM_WORLD.Abort(1);
    MPI::Finalize();
#endif
#endif
  }
  
  CSysVector r(b);
  CSysVector A_p(b);
  
  /*--- Calculate the initial residual, compute norm, and check if system is already solved ---*/
  mat_vec(x,A_p);
  
  r -= A_p; // recall, r holds b initially
  double norm_r = r.norm();
  double norm0 = b.norm();
  if ( (norm_r < tol*norm0) || (norm_r < eps) ) {
    if (rank == 0) cout << "CSysSolve::ConjugateGradient(): system solved by initial guess." << endl;
    return 0;
  }
  
  double alpha, beta, r_dot_z;
  CSysVector z(r);
  precond(r, z);
  CSysVector p(z);
  
  /*--- Set the norm to the initial initial residual value ---*/
  norm0 = norm_r;
  
  /*--- Output header information including initial residual ---*/
  int i = 0;
  if ((monitoring) && (rank == 0))  {
    writeHeader("CG", tol, norm_r);
    writeHistory(i, norm_r, norm0);
  }
  
  /*---  Loop over all search directions ---*/
  for (i = 0; i < m; i++) {
    
    /*--- Apply matrix to p to build Krylov subspace ---*/
    mat_vec(p, A_p);
    
    /*--- Calculate step-length alpha ---*/
    r_dot_z = dotProd(r, z);
    alpha = dotProd(A_p, p);
    alpha = r_dot_z / alpha;
    
    /*--- Update solution and residual: ---*/
    x.Plus_AX(alpha, p);
    r.Plus_AX(-alpha, A_p);
    
    /*--- Check if solution has converged, else output the relative residual if necessary ---*/
    norm_r = r.norm();
    if (norm_r < tol*norm0) break;
    if (((monitoring) && (rank == 0)) && ((i+1) % 5 == 0)) writeHistory(i+1, norm_r, norm0);
    
    precond(r, z);
    
    /*--- Calculate Gram-Schmidt coefficient beta,
		 beta = dotProd(r_{i+1}, z_{i+1}) / dotProd(r_{i}, z_{i}) ---*/
    beta = 1.0 / r_dot_z;
    r_dot_z = dotProd(r, z);
    beta *= r_dot_z;
    
    /*--- Gram-Schmidt orthogonalization; p = beta *p + z ---*/
    p.Equals_AX_Plus_BY(beta, p, 1.0, z);
  }
  

  
  if ((monitoring) && (rank == 0))  {
    cout << "# Conjugate Gradient final (true) residual:" << endl;
    cout << "# Iteration = " << i << ": |res|/|res0| = "  << norm_r/norm0 << endl;
  }
  
//  /*--- Recalculate final residual (this should be optional) ---*/
//  mat_vec(x, A_p);
//  r = b;
//  r -= A_p;
//  double true_res = r.norm();
//  
//  if (fabs(true_res - norm_r) > tol*10.0) {
//    if (rank == 0) {
//      cout << "# WARNING in CSysSolve::ConjugateGradient(): " << endl;
//      cout << "# true residual norm and calculated residual norm do not agree." << endl;
//      cout << "# true_res - calc_res = " << true_res - norm_r << endl;
//    }
//  }
	
	return i;
  
}
Ejemplo n.º 3
0
unsigned long CSysSolve::BCGSTAB(const CSysVector & b, CSysVector & x, CMatrixVectorProduct & mat_vec,
                                 CPreconditioner & precond, double tol, unsigned long m, bool monitoring) {
	
  int rank = 0;
#ifndef NO_MPI
#ifdef WINDOWS
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#else
	rank = MPI::COMM_WORLD.Get_rank();
#endif
#endif
  
  /*--- Check the subspace size ---*/
  if (m < 1) {
    if (rank == 0) cerr << "CSysSolve::BCGSTAB: illegal value for subspace size, m = " << m << endl;
#ifdef NO_MPI
    exit(1);
#else
#ifdef WINDOWS
	MPI_Abort(MPI_COMM_WORLD,1);
    MPI_Finalize();
#else
    MPI::COMM_WORLD.Abort(1);
    MPI::Finalize();
#endif
#endif
  }
	
  CSysVector r(b);
  CSysVector r_0(b);
  CSysVector p(b);
	CSysVector v(b);
  CSysVector s(b);
	CSysVector t(b);
	CSysVector phat(b);
	CSysVector shat(b);
  CSysVector A_x(b);
  
  /*--- Calculate the initial residual, compute norm, and check if system is already solved ---*/
	mat_vec(x,A_x);
  r -= A_x; r_0 = r; // recall, r holds b initially
  double norm_r = r.norm();
  double norm0 = b.norm();
  if ( (norm_r < tol*norm0) || (norm_r < eps) ) {
    if (rank == 0) cout << "CSysSolve::BCGSTAB(): system solved by initial guess." << endl;
    return 0;
  }
	
	/*--- Initialization ---*/
  double alpha = 1.0, beta = 1.0, omega = 1.0, rho = 1.0, rho_prime = 1.0;
	
  /*--- Set the norm to the initial initial residual value ---*/
  norm0 = norm_r;
  
  /*--- Output header information including initial residual ---*/
  int i = 0;
  if ((monitoring) && (rank == 0)) {
    writeHeader("BCGSTAB", tol, norm_r);
    writeHistory(i, norm_r, norm0);
  }
	
  /*---  Loop over all search directions ---*/
  for (i = 0; i < m; i++) {
		
		/*--- Compute rho_prime ---*/
		rho_prime = rho;
		
		/*--- Compute rho_i ---*/
		rho = dotProd(r, r_0);
		
		/*--- Compute beta ---*/
		beta = (rho / rho_prime) * (alpha /omega);
		
		/*--- p_{i} = r_{i-1} + beta * p_{i-1} - beta * omega * v_{i-1} ---*/
		double beta_omega = -beta*omega;
		p.Equals_AX_Plus_BY(beta, p, beta_omega, v);
		p.Plus_AX(1.0, r);
		
		/*--- Preconditioning step ---*/
		precond(p, phat);
		mat_vec(phat, v);
    
		/*--- Calculate step-length alpha ---*/
    double r_0_v = dotProd(r_0, v);
    alpha = rho / r_0_v;
    
		/*--- s_{i} = r_{i-1} - alpha * v_{i} ---*/
		s.Equals_AX_Plus_BY(1.0, r, -alpha, v);
		
		/*--- Preconditioning step ---*/
		precond(s, shat);
		mat_vec(shat, t);
    
		/*--- Calculate step-length omega ---*/
    omega = dotProd(t, s) / dotProd(t, t);
    
		/*--- Update solution and residual: ---*/
    x.Plus_AX(alpha, phat); x.Plus_AX(omega, shat);
		r.Equals_AX_Plus_BY(1.0, s, -omega, t);
    
    /*--- Check if solution has converged, else output the relative residual if necessary ---*/
    norm_r = r.norm();
    if (norm_r < tol*norm0) break;
    if (((monitoring) && (rank == 0)) && ((i+1) % 5 == 0) && (rank == 0)) writeHistory(i+1, norm_r, norm0);
    
  }
	  
  if ((monitoring) && (rank == 0)) {
    cout << "# BCGSTAB final (true) residual:" << endl;
    cout << "# Iteration = " << i << ": |res|/|res0| = "  << norm_r/norm0 << endl;
  }
	
//  /*--- Recalculate final residual (this should be optional) ---*/
//	mat_vec(x, A_x);
//  r = b; r -= A_x;
//  double true_res = r.norm();
//  
//  if ((fabs(true_res - norm_r) > tol*10.0) && (rank == 0)) {
//    cout << "# WARNING in CSysSolve::BCGSTAB(): " << endl;
//    cout << "# true residual norm and calculated residual norm do not agree." << endl;
//    cout << "# true_res - calc_res = " << true_res <<" "<< norm_r << endl;
//  }
	
	return i;
}
Ejemplo n.º 4
0
//--------------------------------
// 
//--------------------------------
void AflMapData::writeHeader(FILE* pFile,LPCSTR pHeader,int nData)
{
	writeHeader(pFile,pHeader,&nData,sizeof(int));
}
Ejemplo n.º 5
0
void* CDRATSServer::Entry()
{
	wxLogMessage(wxT("Starting the D-RATS Server thread for %s"), m_callsign.c_str());

	bool sending = false;
	unsigned int id = 0U;

	unsigned char seqNo = 0U;
	unsigned int  sent = 0U;

	wxStopWatch time;

	try {
		while (!m_stopped) {
			serviceSocket();

			if (m_readEnd && !sending) {
				id = CHeaderData::createId();

				// Write header
				CHeaderData header;
				header.setMyCall1(m_callsign);
				header.setMyCall2(wxT("DATA"));
				header.setYourCall(wxT("CQCQCQ  "));
				header.setId(id);

#if defined(LOOPBACK)
				writeHeader(header);
#else
				m_handler->process(header, DIR_INCOMING, AS_DRATS);
#endif

				m_readState = SS_FIRST;
				m_readPos   = 0U;
				sending     = true;
				seqNo       = 0U;
				sent        = 0U;

				time.Start();
			}

			if (m_readEnd && sending) {
				unsigned int needed = time.Time() / DSTAR_FRAME_TIME_MS;

				while (sent < needed && sending) {
					// Write AMBE data
					CAMBEData data;
					data.setId(id);

					unsigned char buffer[DV_FRAME_LENGTH_BYTES];
					::memcpy(buffer + 0U, NULL_AMBE_DATA_BYTES, VOICE_FRAME_LENGTH_BYTES);

					// Insert sync bytes when the sequence number is zero, slow data otherwise
					if (seqNo == 0U) {
						::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES);
						m_readState = SS_FIRST;
					} else {
						if (m_readState == SS_FIRST) {
							unsigned char readText[3U];
							::memset(readText, 'f', 3U);

							unsigned int length = m_readLength - m_readPos;
							unsigned char bytes = 5U;
							if (length < 5U)
								bytes = length;

							readText[0U] = SLOW_DATA_TYPE_GPS | bytes;

							for (unsigned int i = 0U; i < 2U && m_readPos < m_readLength; i++)
								readText[i + 1U] = m_readBuffer[m_readPos++];

							readText[0U] ^= SCRAMBLER_BYTE1;
							readText[1U] ^= SCRAMBLER_BYTE2;
							readText[2U] ^= SCRAMBLER_BYTE3;

							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, readText, DATA_FRAME_LENGTH_BYTES);

							m_readState = SS_SECOND;
						} else {
							unsigned char readText[3U];
							::memset(readText, 'f', 3U);

							for (unsigned int i = 0U; i < 3U && m_readPos < m_readLength; i++)
								readText[i] = m_readBuffer[m_readPos++];

							readText[0U] ^= SCRAMBLER_BYTE1;
							readText[1U] ^= SCRAMBLER_BYTE2;
							readText[2U] ^= SCRAMBLER_BYTE3;

							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, readText, DATA_FRAME_LENGTH_BYTES);				

							m_readState = SS_FIRST;
						}
					}

					data.setSeq(seqNo);
					data.setData(buffer, DV_FRAME_LENGTH_BYTES);
					sent++;

#if defined(LOOPBACK)
					writeData(data);
#else
					m_handler->process(data, DIR_INCOMING, AS_DRATS);
#endif

					if (m_readPos == m_readLength) {
						if (m_readState == SS_SECOND) {
							seqNo++;
							if (seqNo == 21U)
								seqNo = 0U;

							unsigned char readText[3U];
							readText[0U] = 'f' ^ SCRAMBLER_BYTE1;
							readText[1U] = 'f' ^ SCRAMBLER_BYTE2;
							readText[2U] = 'f' ^ SCRAMBLER_BYTE3;

							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, readText, DATA_FRAME_LENGTH_BYTES);

							data.setSeq(seqNo);
							data.setData(buffer, DV_FRAME_LENGTH_BYTES);
							sent++;

#if defined(LOOPBACK)
							writeData(data);
#else
							m_handler->process(data, DIR_INCOMING, AS_DRATS);
#endif
						}

						seqNo++;
						if (seqNo == 21U)
							seqNo = 0U;

						if (seqNo == 0U)
							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, DATA_SYNC_BYTES, DATA_FRAME_LENGTH_BYTES);
						else
							::memcpy(buffer + VOICE_FRAME_LENGTH_BYTES, NULL_SLOW_DATA_BYTES, DATA_FRAME_LENGTH_BYTES);

						data.setData(buffer, DV_FRAME_LENGTH_BYTES);
						data.setSeq(seqNo);
						data.setEnd(true);
						sent++;

#if defined(LOOPBACK)
						writeData(data);
#else
						m_handler->process(data, DIR_INCOMING, AS_DRATS);
#endif

						m_readLength = 0U;
						m_readPos    = 0U;
						m_readEnd    = false;
						sending      = false;
						sent         = 0U;
					}

					seqNo++;
					if (seqNo == 21U)
						seqNo = 0U;
				}
			}

			// 50ms
			Sleep(50UL);
		}

		if (m_socket != NULL)
			m_socket->stop();
	}
	catch (std::exception& e) {
		wxString message(e.what(), wxConvLocal);
		wxLogError(wxT("Exception raised in the D-RATS Server thread - \"%s\""), message.c_str());
	}
	catch (...) {
		wxLogError(wxT("Unknown exception raised in the D-RATS Server thread"));
	}

	wxLogMessage(wxT("Stopping the D-RATS Server thread for %s"), m_callsign.c_str());

	return NULL;
}
Ejemplo n.º 6
0
void
SoSwitch::write(SoWriteAction *action)
//
////////////////////////////////////////////////////////////////////////
{
    SoOutput *out = action->getOutput();

    // When writing out a switch that is in a path, we always want to
    // write out ALL children of the switch. If we did the default
    // thing of writing out just those children that affect the nodes
    // in the path, we could screw up. Consider a switch that has two
    // child separators and whichChild set to 1. If a path goes
    // through the switch to the second child, the first child, being
    // a separator, does not affect the path. But if we don't write
    // out the separator, the whichChild will reference a
    // nonexistent child. So we always write out all children.

    // NOTE: SoChildList::traverse() checks the current path code and
    // skips children off the path that do not affect the
    // state. Because of this we have to avoid calling it. Instead, we
    // do its work here.

    // This code is stolen and modified from SoGroup::write() and
    // SoChildList::traverse()

    int lastChild = getNumChildren() - 1;
    SoAction::PathCode pc = action->getCurPathCode();

    // In write-reference counting phase
    if (out->getStage() == SoOutput::COUNT_REFS) {

	// Increment our write reference count
	addWriteReference(out);

	// If this is the first reference (i.e., we don't now have
	// multiple references), also count all appropriate children
	if (! hasMultipleWriteRefs()) {
	    for (int i = 0; i <= lastChild; i++) {
		action->pushCurPath(i);
		action->traverse(getChild(i));
		action->popCurPath(pc);
	    }
	}
    }

    // In writing phase, we have to do some more work
    else if (! writeHeader(out, TRUE, FALSE)) {

	// Write fields
	const SoFieldData *fieldData = getFieldData();
	fieldData->write(out, this);

	// We KNOW that all children should be written, so don't
	// bother calling shouldWrite()

	// If writing binary format, write out number of children
	// that are going to be written
	if (out->isBinary())
	    out->write(getNumChildren());

	for (int i = 0; i <= lastChild; i++) {
	    action->pushCurPath(i);
	    action->traverse(getChild(i));
	    action->popCurPath(pc);
	}

	// Write post-children stuff
	writeFooter(out);
    }
}
Ejemplo n.º 7
0
//--------------------------------
// 
//--------------------------------
void AflMapData::writeHeader(FILE* pFile,LPCSTR pHeader,LPCSTR pData)
{
	writeHeader(pFile,pHeader,pData,strlen(pData)+1);
}
Ejemplo n.º 8
0
	void CsvSerializer::open(const Record &record) {
		this->record = &record;
		writeHeader();
	}
Ejemplo n.º 9
0
/**
 * trains the model using the patterns returned by the given query or the default query if none is given
 */
double Trainer::train(Optimizer& optimizer, ErrorFunction& errFct, size_t iterations) throw(MachineLearningException) {
	double error = 0;

	if(TRAINING_OUTPUT)
		writeHeader("Normal trainer", optimizer, errFct, iterations);

//		std::cout << "Query: \n" << query << std::endl;
	try {
		if(nFeatures() != model.getInputDimension() && model.iterativeTraining()) {
			std::cerr << "Number of features: " << nFeatures() << "\nModel input size: " << model.getInputDimension() << std::endl;
			throw MachineLearningException("Number of selected features is not equal to the model's input size");
		}

		Array<double> in, target;
		Array<double> out(model.getOutputDimension());

		size_t nRows = readDatabase(in, target);

		// do the actual training
		optimizer.init(model.getModel());

		// check if we are dealing with an svm, in this case the iterations argument is ignored
		if(SVM_Optimizer* svmOpt = dynamic_cast<SVM_Optimizer*>(&optimizer)){
			MyMultiClassSVM* csvm = dynamic_cast<MyMultiClassSVM*>(&model);
			MyEpsilon_SVM* esvm = dynamic_cast<MyEpsilon_SVM*>(&model);
			if(csvm) {
				optimizeDistorted(*svmOpt, csvm->getSVM(), errFct, in, target);
			} else if(esvm) {
				optimizeDistorted(*svmOpt, esvm->getSVM(), errFct, in, target);
			}
			assert_true(esvm || csvm) << "Trying to call SVM_Optimizer with a non Epsilon- or MultiClassSVM model";


			error = errFct.error(model.getModel(), in, target);
		}  else if(iterations != 0) {
			for(size_t i = 0; i < iterations; ++i) {
				optimizeDistorted(optimizer, model.getModel(), errFct, in, target);
				if(TRAINING_OUTPUT)
					writeStatistics(i, in, target, errFct, -1.0);
			}

			error = errFct.error(model.getModel(), in, target);
		} else
//			error = this->earlyStopping(optimizer, errFct, in, target, 10);
			error = this->myEarlyStopping(optimizer, errFct, in, target, 10, std::max(static_cast<size_t>(1),nRows/1000));

		size_t misClass = 0;
		for(size_t i = 0; i < in.dim(0); ++i ) {
			model.model(in.subarr(i,i), out);
//std::cout << in.subarr(i,i) << round(out(0)) << std::endl;
//std::cout << oneOfNtoIdx(target.subarr(i,i)) << " - " <<  oneOfNtoIdx(out) << std::endl;
			if(model.usesOneOfNCoding()) {
				if(oneOfNtoIdx(target.subarr(i,i)) != oneOfNtoIdx(out))
					++misClass;
			} else if(target.subarr(i,i)(0) != round(out(0)))
				++misClass;
		}
		LOG(INFO) << "Missclassification rate: " << (double(misClass)/in.dim(0)) * 100.0 << "%\n";

	} catch(Kompex::SQLiteException& sqle) {
		const std::string err = "\nSQL query for training data failed\n" ;
		LOG(ERROR) << err << std::endl;
		sqle.Show();
		throw ml::MachineLearningException(err);
	} catch(SharkException& se) {
		LOG(ERROR) << se.what() << std::endl;
		throw ml::MachineLearningException("Shark error");
	}catch (std::exception& exception) {
		const std::string err = "\nQuery for data failed\n" ;
		LOG(ERROR) << err << exception.what() << std::endl;
		throw ml::MachineLearningException(err);
	}

	return error;
}
Ejemplo n.º 10
0
void createResponse(bufferevent *pBufferEvent)
{
    struct evbuffer* pInputBuffer = bufferevent_get_input(pBufferEvent);
    struct evbuffer* pOutputBuffer = bufferevent_get_output(pBufferEvent);

    char *line;
    size_t size = 0;
    line = evbuffer_readln(pInputBuffer, &size, EVBUFFER_EOL_CRLF);

    std::string Request = std::string(line);
    //std::cout << Request << "\n";

    //read method and path
    size_t nMethodEnd = Request.find(" ");
    std::string sMethod = std::string(Request, 0, nMethodEnd);
    //std::cout << sMethod << "\n";

    size_t nPathEnd = Request.find(" ", nMethodEnd+1);
    std::string sRawPath = std::string(Request, nMethodEnd+1, nPathEnd-nMethodEnd-1);
    //std::cout << sRawPath << "\n";

    size_t nDelimPos = sRawPath.find('?');
    std::string sHalfRawPath = sRawPath.substr(0, nDelimPos);
    //std::cout << sHalfRawPath << "\n";
    char* sTempPath = new char[sHalfRawPath.length()+1]; // at most the same size + term\0
    urlDecode(sTempPath, sHalfRawPath.c_str());
    std::string sPath = std::string(sTempPath);
    //std::cout << sPath << "\n";

    if (Request.find(" ", nPathEnd+1) != std::string::npos) { // extra spaces in request
        //std::cout << "ERROR: extra spaces\n";
        writeHeader(pOutputBuffer, STATUS_BAD_REQUEST, TYPE_HTML, MASSAGE_LENGTH_BAD_REQUEST);
        evbuffer_add(pOutputBuffer, MASSAGE_BAD_REQUEST, MASSAGE_LENGTH_BAD_REQUEST);
        return;
    }

    //Validate path
    if(!validatePath(sPath))
    {
        //std::cout << "Warning: forbidden path\n";
        writeHeader(pOutputBuffer, STATUS_FORBIDDEN, TYPE_HTML, MASSAGE_LENGTH_FORBIDDEN);
        evbuffer_add(pOutputBuffer, MASSAGE_FORBIDDEN, MASSAGE_LENGTH_FORBIDDEN);
        return;
    }
    //std::cout << "Ok: path ok\n";



    //find target file
    std::string sRealPath = std::string(DOCUMENT_ROOT);
    sRealPath.append(std::string(sPath));

    bool bIndex = false;
    if (sRealPath[sRealPath.length()-1] == '/') {
        //std::cout << "Ok: index\n";
        sRealPath.append("index.html");
        bIndex = true;
    }

    int fFile = open(sRealPath.c_str(), O_NONBLOCK|O_RDONLY);
    if (fFile == -1) {
        //std::cout << "Warning: file not opened\n";
        if (bIndex) {
            writeHeader(pOutputBuffer, STATUS_FORBIDDEN, TYPE_HTML, MASSAGE_LENGTH_FORBIDDEN);
            evbuffer_add(pOutputBuffer, MASSAGE_FORBIDDEN, MASSAGE_LENGTH_FORBIDDEN);
        } else {
            writeHeader(pOutputBuffer, STATUS_NOT_FOUND, TYPE_HTML, MASSAGE_LENGTH_NOT_FOUND);
            evbuffer_add(pOutputBuffer, MASSAGE_NOT_FOUND, MASSAGE_LENGTH_NOT_FOUND);
        }
        return;
    }
    //std::cout << "Ok: file opened\n";

    struct stat FileStats;
    fstat(fFile, &FileStats);
    if(!strcmp(sMethod.c_str(), METHOD_GET)) {
        //std::cout << "Ok: method \"get\"\n";
        writeHeader(pOutputBuffer, STATUS_OK, getContentType(sRealPath), FileStats.st_size);
        evbuffer_add_file(pOutputBuffer, fFile, 0, FileStats.st_size);
    } else if(!strcmp(sMethod.c_str(), METHOD_HEAD)){
        //std::cout << "Ok: method \"head\"\n";
        // ctime gives only /n so we'll add proper CRLF
        writeHeader(pOutputBuffer, STATUS_OK, getContentType(sRealPath), FileStats.st_size);
        evbuffer_add(pOutputBuffer, CRLF, 2);
    } else {
        writeHeader(pOutputBuffer, STATUS_BAD_REQUEST, TYPE_HTML, MASSAGE_LENGTH_BAD_REQUEST);
        evbuffer_add(pOutputBuffer, MASSAGE_BAD_REQUEST, MASSAGE_LENGTH_BAD_REQUEST);
    }
    return;
}
Ejemplo n.º 11
0
int QgsGridFileWriter::writeFile( QgsFeedback *feedback )
{
  QFile outputFile( mOutputFilePath );

  if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
  {
    return 1;
  }

  if ( !mInterpolator )
  {
    outputFile.remove();
    return 2;
  }

  QTextStream outStream( &outputFile );
  outStream.setRealNumberPrecision( 8 );
  writeHeader( outStream );

  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
  double currentXValue;
  double interpolatedValue;

  for ( int i = 0; i < mNumRows; ++i )
  {
    currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
    for ( int j = 0; j < mNumColumns; ++j )
    {
      if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
      {
        outStream << interpolatedValue << ' ';
      }
      else
      {
        outStream << "-9999 ";
      }
      currentXValue += mCellSizeX;
    }
    outStream << endl;
    currentYValue -= mCellSizeY;

    if ( feedback )
    {
      if ( feedback->isCanceled() )
      {
        outputFile.remove();
        return 3;
      }
      feedback->setProgress( 100.0 * i / static_cast< double >( mNumRows ) );
    }
  }

  // create prj file
  QgsInterpolator::LayerData ld;
  ld = mInterpolator->layerData().at( 0 );
  QgsVectorLayer *vl = ld.vectorLayer;
  QString crs = vl->crs().toWkt();
  QFileInfo fi( mOutputFilePath );
  QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
  QFile prjFile( fileName );
  if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) )
  {
    return 1;
  }
  QTextStream prjStream( &prjFile );
  prjStream << crs;
  prjStream << endl;
  prjFile.close();

  return 0;
}
Ejemplo n.º 12
0
/*
 * Save a directory and all of its files to the tar file.
 */
static void
saveDirectory(const char * dirName, const struct stat * statbuf)
{
	DIR *		dir;
	struct dirent *	entry;
	BOOL		needSlash;
	char		fullName[PATH_LEN];

	/*
	 * Construct the directory name as used in the tar file by appending
	 * a slash character to it.
	 */
	strcpy(fullName, dirName);
	strcat(fullName, "/");

	/*
	 * Write out the header for the directory entry.
	 */
	writeHeader(fullName, statbuf);

	/*
	 * Open the directory.
	 */
	dir = opendir(dirName);

	if (dir == NULL)
	{
		fprintf(stderr, "Cannot read directory \"%s\": %s\n",
			dirName, strerror(errno));

		return;
	}

	/*
	 * See if a slash is needed.
	 */
	needSlash = (*dirName && (dirName[strlen(dirName) - 1] != '/'));

	/*
	 * Read all of the directory entries and check them,
	 * except for the current and parent directory entries.
	 */
	while (!intFlag && !errorFlag && ((entry = readdir(dir)) != NULL))
	{
		if ((strcmp(entry->d_name, ".") == 0) ||
			(strcmp(entry->d_name, "..") == 0))
		{
			continue;
		}

		/*
		 * Build the full path name to the file.
		 */
		strcpy(fullName, dirName);

		if (needSlash)
			strcat(fullName, "/");

		strcat(fullName, entry->d_name);

		/*
		 * Write this file to the tar file, noticing whether or not
		 * the file is a symbolic link.
		 */
		saveFile(fullName, TRUE);
	}

	/*
	 * All done, close the directory.
	 */
	closedir(dir);
}
Ejemplo n.º 13
0
/*
 * Save a regular file to the tar file.
 */
static void
saveRegularFile(const char * fileName, const struct stat * statbuf)
{
	BOOL		sawEof;
	int		fileFd;
	int		cc;
	int		dataCount;
	long		fullDataCount;
	char		data[TAR_BLOCK_SIZE * 16];

	/*
	 * Open the file for reading.
	 */
	fileFd = open(fileName, O_RDONLY);

	if (fileFd < 0)
	{
		perror(fileName);

		return;
	}

	/*
	 * Write out the header for the file.
	 */
	writeHeader(fileName, statbuf);

	/*
	 * Write the data blocks of the file.
	 * We must be careful to write the amount of data that the stat
	 * buffer indicated, even if the file has changed size.  Otherwise
	 * the tar file will be incorrect.
	 */
	fullDataCount = statbuf->st_size;
	sawEof = FALSE;

	while (!intFlag && (fullDataCount > 0))
	{
		/*
		 * Get the amount to write this iteration which is
		 * the minumum of the amount left to write and the
		 * buffer size.
		 */
		dataCount = sizeof(data);

		if (dataCount > fullDataCount)
			dataCount = (int) fullDataCount;

		/*
		 * Read the data from the file if we haven't seen the
		 * end of file yet.
		 */
		cc = 0;

		if (!sawEof)
		{
			cc = fullRead(fileFd, data, dataCount);

			if (cc < 0)
			{
				perror(fileName);

				(void) close(fileFd);
				errorFlag = TRUE;

				return;
			}

			/*
			 * If the file ended too soon, complain and set
			 * a flag so we will zero fill the rest of it.
			 */
			if (cc < dataCount)
			{
				fprintf(stderr,
					"%s: Short read - zero filling",
					fileName);

				sawEof = TRUE;
			}
		}

		/*
		 * Zero fill the rest of the data if necessary.
		 */
		if (cc < dataCount)
			memset(data + cc, 0, dataCount - cc);

		/*
		 * Write the buffer to the TAR file.
		 */
		writeTarBlock(data, dataCount);

		fullDataCount -= dataCount;
	}

	/*
	 * Close the file.
	 */
	if (close(fileFd) < 0)
		fprintf(stderr, "%s: close: %s\n", fileName, strerror(errno));
}
Ejemplo n.º 14
0
   uint16 requestedLanguage,
   uint32 regionsInMatches,
   StringTable::countryCode topRegion )
{
   if ( numMapID == 1 ) {
      RequestPacket::setMapID( mapID[ 0 ] );
   }
   DEBUG8(
      for ( uint32 w = 0 ; w < numMapID ; w++ ) {
         mc2dbg << "VanillaEncode MapID = " << mapID[w] << endl;         
      }
      );
   int position = writeHeader( sortingType, numMapID, mapID, zipCode, 
                               nbrLocations,
                               locations, locationType,
                               nbrCategories, categories,
                               nbrMasks, masks, maskItemIDs,
                               maskNames,
                               dbMask, regionsInMatches, topRegion,
                               vector<MC2BoundingBox>());
   incWriteString( position, searchString);
   incWriteByte(   position, nbrHits );
   incWriteByte(   position, matchType );
   incWriteByte(   position, stringPart );
   incWriteByte(   position, sortingType );
   incWriteShort(  position, categoryType );
   incWriteShort(  position, requestedLanguage );
   setLength(position); 
}


void UserSearchRequestPacket::encodeRequest(
Ejemplo n.º 15
0
static int msDBFWriteAttribute(DBFHandle psDBF, int hEntity, int iField, void * pValue )
{
    unsigned int          nRecordOffset;
    int  i, j;
    uchar *pabyRec;
    char  szSField[40], szFormat[12];

    /* -------------------------------------------------------------------- */
    /*  Is this a valid record?             */
    /* -------------------------------------------------------------------- */
    if( hEntity < 0 || hEntity > psDBF->nRecords )
        return( MS_FALSE );

    if( psDBF->bNoHeader )
        writeHeader(psDBF);

    /* -------------------------------------------------------------------- */
    /*      Is this a brand new record?                                     */
    /* -------------------------------------------------------------------- */
    if( hEntity == psDBF->nRecords ) {
        flushRecord( psDBF );

        psDBF->nRecords++;
        for( i = 0; i < psDBF->nRecordLength; i++ )
            psDBF->pszCurrentRecord[i] = ' ';

        psDBF->nCurrentRecord = hEntity;
    }

    /* -------------------------------------------------------------------- */
    /*      Is this an existing record, but different than the last one     */
    /*      we accessed?                                                    */
    /* -------------------------------------------------------------------- */
    if( psDBF->nCurrentRecord != hEntity ) {
        flushRecord( psDBF );

        nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;

        safe_fseek( psDBF->fp, nRecordOffset, 0 );
        fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );

        psDBF->nCurrentRecord = hEntity;
    }

    pabyRec = (uchar *) psDBF->pszCurrentRecord;

    /* -------------------------------------------------------------------- */
    /*      Assign all the record fields.                                   */
    /* -------------------------------------------------------------------- */
    switch( psDBF->pachFieldType[iField] ) {
    case 'D':
    case 'N':
    case 'F':
        if( psDBF->panFieldDecimals[iField] == 0 ) {
            snprintf( szFormat, sizeof(szFormat), "%%%dd", psDBF->panFieldSize[iField] );
            snprintf(szSField, sizeof(szSField), szFormat, (int) *((double *) pValue) );
            if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
                szSField[psDBF->panFieldSize[iField]] = '\0';
            strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) );
        } else {
            snprintf( szFormat, sizeof(szFormat), "%%%d.%df", psDBF->panFieldSize[iField], psDBF->panFieldDecimals[iField] );
            snprintf(szSField, sizeof(szSField), szFormat, *((double *) pValue) );
            if( (int) strlen(szSField) > psDBF->panFieldSize[iField] )
                szSField[psDBF->panFieldSize[iField]] = '\0';
            strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),  szSField, strlen(szSField) );
        }
        break;

    default:
        if( (int) strlen((char *) pValue) > psDBF->panFieldSize[iField] )
            j = psDBF->panFieldSize[iField];
        else
            j = strlen((char *) pValue);

        strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), (char *) pValue, j );
        break;
    }

    psDBF->bCurrentRecordModified = MS_TRUE;
    psDBF->bUpdated = MS_TRUE;

    return( MS_TRUE );
}
Ejemplo n.º 16
0
void  msDBFClose(DBFHandle psDBF)
{
  /* -------------------------------------------------------------------- */
  /*      Write out header if not already written.                        */
  /* -------------------------------------------------------------------- */
    if( psDBF->bNoHeader )
        writeHeader( psDBF );

    flushRecord( psDBF );

    /* -------------------------------------------------------------------- */
    /*      Update last access date, and number of records if we have       */
    /*	write access.                					    */ 
    /* -------------------------------------------------------------------- */
    if( psDBF->bUpdated && psDBF->fp)
    {
	uchar		abyFileHeader[32];

	fseek( psDBF->fp, 0, 0 );
	fread( abyFileHeader, 32, 1, psDBF->fp );

	abyFileHeader[1] = 95;			/* YY */
	abyFileHeader[2] = 7;			/* MM */
	abyFileHeader[3] = 26;			/* DD */

	abyFileHeader[4] = psDBF->nRecords % 256;
	abyFileHeader[5] = (psDBF->nRecords/256) % 256;
	abyFileHeader[6] = (psDBF->nRecords/(256*256)) % 256;
	abyFileHeader[7] = (psDBF->nRecords/(256*256*256)) % 256;

	fseek( psDBF->fp, 0, 0 );
	fwrite( abyFileHeader, 32, 1, psDBF->fp );
    }

    /* -------------------------------------------------------------------- */
    /*      Close, and free resources.                                      */
    /* -------------------------------------------------------------------- */
    if (psDBF->fp) {
      fclose( psDBF->fp );
      psDBF->fp = 0;
    }
    if (psDBF->zfp) {
      zzip_fclose( psDBF->zfp );
      psDBF->zfp = 0;
    }

    if( psDBF->panFieldOffset != NULL )
    {
        free( psDBF->panFieldOffset );
        free( psDBF->panFieldSize );
        free( psDBF->panFieldDecimals );
        free( psDBF->pachFieldType );
    }

    free( psDBF->pszHeader );
    free( psDBF->pszCurrentRecord );

    if(psDBF->pszStringField) free(psDBF->pszStringField);

    free( psDBF );
}
Ejemplo n.º 17
0
void Compression::huffmanEncode(const char* inputFile)
{
    FILE * src = fopen(inputFile, "rb");

    char outputPath[1000];
    const char * fileExtension = ".bin";
    outputFilePath(inputFile, outputPath, fileExtension);
    FILE * dest = fopen(outputPath, "wb");

    if (src == NULL || dest == NULL)
    {
        printf("Не удается найти файл.");
        exit(EXIT_FAILURE);
    }

    unsigned int fileSize;
    fileSize = getFileSize(src);

    unsigned int * freqList;
    freqList = (unsigned int *)calloc(CHARS_LIMIT, sizeof(unsigned int));
    calcCharFreq(src, freqList);

    unsigned int numOfFreq;
    numOfFreq = calcNumOfFreq(freqList);

    HuffNode * nodeList = NULL;
    buildNodeList(&nodeList, freqList);

    buildHuffTree(&nodeList);
    HuffNode * treeRoot = nodeList;

    unsigned int i;
    HuffCode newCode;
    HuffCode * huffCodeTable;
    huffCodeTable = (HuffCode *)calloc(CHARS_LIMIT, sizeof(HuffCode));
    for(i=0; i<CHARS_LIMIT; i++)
    {
        if(freqList[i] > 0)
        {
            newCode.length = 0;
            buildHuffCode(treeRoot, &newCode, i);
            huffCodeTable[i] = newCode;
        }
    }

    HuffHeader hHeader;
    writeHeader(dest, hHeader, numOfFreq, fileSize);

    HuffFreq hFreq;
    writeFreq(dest, freqList, hFreq);

    writeEncodedData(src, dest, huffCodeTable, fileSize);

    freeHuffTree(treeRoot);
    treeRoot = NULL;
    free(huffCodeTable);
    free(freqList);

    fclose(src);
    fclose(dest);
}
Ejemplo n.º 18
0
bool ADM_audioWriteWav::init(ADM_audioStream *stream, const char *fileName)
{
    if(false==ADM_audioWrite::init(stream,fileName)) return false;
    return writeHeader(stream);
}
Ejemplo n.º 19
0
bool NetDemo::startRecording(const std::string &filename)
{
	this->filename = filename;

	if (isPlaying() || isPaused())
	{
		error("Cannot record a netdemo while not connected to a server.");
		return false;
	}

	// Already recording so just ignore the command
	if (isRecording())
		return true;

	if (demofp != NULL)		// file is already open for some reason
	{
		fclose(demofp);
		demofp = NULL;
	}

	demofp = fopen(filename.c_str(), "wb");
	if (!demofp)
	{
		//error("Unable to create netdemo file " + filename + ".");
		I_Warning("Unable to create netdemo file %s", filename.c_str());
		return false;
	}

	memset(&header, 0, sizeof(header));
	// Note: The header is not finalized at this point.  Write it anyway to
	// reserve space in the output file for it and overwrite it later.
	if (!writeHeader())
	{
		error("Unable to write netdemo header.");
		return false;
	}

	state = NetDemo::st_recording;
	header.starting_gametic = gametic;
	Printf(PRINT_HIGH, "Recording netdemo %s.\n", filename.c_str());

	if (connected)
	{
		// write a simulation of the connection sequence since the server
		// has already sent it to the client and it wasn't captured
		static buf_t tempbuf(MAX_UDP_PACKET);

		// Fake the launcher query response
		SZ_Clear(&tempbuf);
		writeLauncherSequence(&tempbuf);
		capture(&tempbuf);
		writeMessages();
		
		// Fake the server's side of the connection sequence
		SZ_Clear(&tempbuf);
		writeConnectionSequence(&tempbuf);
		capture(&tempbuf);
		writeMessages();

		// Record any additional messages (usually a full update if auto-recording))
		capture(&net_message);
		writeMessages();
		
		SZ_Clear(&tempbuf);
		MSG_WriteMarker(&tempbuf, svc_netdemoloadsnap);
		capture(&tempbuf);
		writeMessages();
	}

	return true;
}
Ejemplo n.º 20
0
int QgsGridFileWriter::writeFile( bool showProgressDialog )
{
  QFile outputFile( mOutputFilePath );

  if ( !outputFile.open( QFile::WriteOnly ) )
  {
    return 1;
  }

  if ( !mInterpolator )
  {
    outputFile.remove();
    return 2;
  }

  QTextStream outStream( &outputFile );
  outStream.setRealNumberPrecision( 8 );
  writeHeader( outStream );

  double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
  double currentXValue;
  double interpolatedValue;

  QProgressDialog* progressDialog = 0;
  if ( showProgressDialog )
  {
    progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 );
    progressDialog->setWindowModality( Qt::WindowModal );
  }

  for ( int i = 0; i < mNumRows; ++i )
  {
    currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
    for ( int j = 0; j < mNumColumns; ++j )
    {
      if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 )
      {
        outStream << interpolatedValue << ' ';
      }
      else
      {
        outStream << "-9999 ";
      }
      currentXValue += mCellSizeX;
    }
    outStream << endl;
    currentYValue -= mCellSizeY;

    if ( showProgressDialog )
    {
      if ( progressDialog->wasCanceled() )
      {
        outputFile.remove();
        return 3;
      }
      progressDialog->setValue( i );
    }
  }

  // create prj file
  QgsInterpolator::LayerData ld;
  ld = mInterpolator->layerData().first();
  QgsVectorLayer* vl = ld.vectorLayer;
  QString crs = vl->crs().toWkt();
  QFileInfo fi( mOutputFilePath );
  QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj";
  QFile prjFile( fileName );
  if ( !prjFile.open( QFile::WriteOnly ) )
  {
    return 1;
  }
  QTextStream prjStream( &prjFile );
  prjStream << crs;
  prjStream << endl;
  prjFile.close();

  delete progressDialog;
  return 0;
}
Ejemplo n.º 21
0
//--------------------------------
// 
//--------------------------------
void AflMapData::writeHeader(FILE* pFile,LPCSTR pHeader,LPCWSTR pData)
{
	CHAR cBuff[1024];
	sprintf(cBuff,"%lc",pData);
	writeHeader(pFile,pHeader,cBuff);
}
Ejemplo n.º 22
0
//
// Maya calls this method to have the translator write out a file.
//
MStatus maTranslator::writer(
		const MFileObject& file,
		const MString& /* options */,
		MPxFileTranslator::FileAccessMode mode
)
{
	//
	// For simplicity, we only do full saves/exports.
	//
	if ((mode != kSaveAccessMode) && (mode != kExportAccessMode))
	   	return MS::kNotImplemented;

	//
	// Let's see if we can open the output file.
	//
	fstream	output(file.fullName().asChar(), ios::out | ios::trunc);

	if (!output.good()) return MS::kNotFound;

	//
	// Get some node flags to keep track of those nodes for which we
	// have already done various stages of processing.
	//
	MStatus	status;

	fCreateFlag = MFnDependencyNode::allocateFlag(fPluginName, &status);

	if (status)
		fAttrFlag = MFnDependencyNode::allocateFlag(fPluginName, &status);

	if (status)
		fConnectionFlag = MFnDependencyNode::allocateFlag(fPluginName, &status);

	if (!status)
	{
		MGlobal::displayError(
			"Could not allocate three free node flags."
			"  Try unloading some other plugins."
		);

		return MS::kFailure;
	}

	//
	// Run through all of the nodes in the scene and clear their flags.
	//
	MItDependencyNodes	nodesIter;

	for (; !nodesIter.isDone(); nodesIter.next())
	{
		MObject				node = nodesIter.item();
		MFnDependencyNode	nodeFn(node);

		nodeFn.setFlag(fCreateFlag, false);
		nodeFn.setFlag(fAttrFlag, false);
		nodeFn.setFlag(fConnectionFlag, false);
	}

	//
	// Write out the various sections of the file.
	//
	writeHeader(output, file.name());
	writeFileInfo(output);
	writeReferences(output);
	writeRequirements(output);
	writeUnits(output);
	writeDagNodes(output);
	writeNonDagNodes(output);
	writeDefaultNodes(output);
	writeReferenceNodes(output);
	writeConnections(output);
	writeFooter(output, file.name());

	output.close();

	MFnDependencyNode::deallocateFlag(fPluginName, fCreateFlag);

	return MS::kSuccess;
}
Ejemplo n.º 23
0
//--------------------------------
// 
//--------------------------------
bool AflMapData::saveMap(LPCSTR pFileName)
{
	int i,j,k;
	FILE* pFile;

	pFile = fopen(pFileName,TEXT("wb"));
	if(!pFile)
		return false;

	m_strFileName = pFileName;
	setRelativePath();

	fwrite(m_strMapHeader[MAP_BINHEADER],strlen(m_strMapHeader[MAP_BINHEADER])+1,1,pFile);
	
	//-----------------------------
	writeHeader(pFile,m_strMapHeader[MAP_VERSION],getVersion());
	writeHeader(pFile,m_strMapHeader[MAP_MAPNAME],getMapName());
	writeHeader(pFile,m_strMapHeader[MAP_BITMAP],getImageName(0));
	writeHeader(pFile,m_strMapHeader[MAP_SPRITE],getImageName(1));
	writeHeader(pFile,m_strMapHeader[MAP_WIDTH],getMapWidth());
	writeHeader(pFile,m_strMapHeader[MAP_HEIGHT],getMapHeight());
	writeHeader(pFile,m_strMapHeader[MAP_OPATTERN],getOutTipIndex());
	writeHeader(pFile,m_strMapHeader[MAP_PARTSWIDTH],getTipWidth());
	writeHeader(pFile,m_strMapHeader[MAP_PARTSHEIGHT],getTipHeight());
	writeHeader(pFile,m_strMapHeader[MAP_OBJECT],getObjectFileName());

	//-----------------------------
	//マップデータ
	for(k=0;k<4;k++)
	{
		if(!isZero(k))
		{
			PSHORT pData = m_ptrData[k].get();
			writeHeader(pFile,m_strMapHeader[MAP_MAPDATA0+k],
				pData,getMapWidth()*getMapHeight()*sizeof(short));
		}
	}

	//-----------------------------
	//パーツ接触フラグ
    for(j=0;j<5;j++)
    {
		int nCollide;
		for(nCollide=i=0;i<0xffff/8;i++)
			if(m_byCollide[j][i])
				nCollide = i+1;
		writeHeader(pFile,m_strMapHeader[MAP_PAERSCOLLIDE0+j],m_byCollide,nCollide);
	}
	//-----------------------------

	//-----------------------------
	//マップ個別フラグ
	writeHeader(pFile,m_strMapHeader[MAP_MAPFLAG0],getMapFlag(),getMapWidth()*getMapHeight());
	//-----------------------------
	fclose(pFile);
	return true;
}
Ejemplo n.º 24
0
bool GLDriver::compileVertexShader(VertexShader &vertex, FetchShader &fetch, uint8_t *buffer, size_t size)
{
   auto sq_config = getRegister<latte::SQ_CONFIG>(latte::Register::SQ_CONFIG);
   auto spi_vs_out_config = getRegister<latte::SPI_VS_OUT_CONFIG>(latte::Register::SPI_VS_OUT_CONFIG);
   fmt::MemoryWriter out;
   latte::Shader shader;
   std::string body;
   FetchShader::Attrib *semanticAttribs[32];
   memset(semanticAttribs, 0, sizeof(FetchShader::Attrib *) * 32);

   shader.type = latte::Shader::Vertex;

   if (!latte::decode(shader, gsl::as_span(buffer, size))) {
      gLog->error("Failed to decode vertex shader");
      return false;
   }

   latte::disassemble(shader, vertex.disassembly);

   if (!latte::blockify(shader)) {
      gLog->error("Failed to blockify vertex shader");
      return false;
   }

   if (!glsl::generateBody(shader, body)) {
      gLog->warn("Failed to translate 100% of instructions for vertex shader");
   }

   // Get vertex sampler types
   for (auto i = 0; i < MAX_SAMPLERS_PER_TYPE; ++i) {
      auto sq_tex_resource_word0 = getRegister<latte::SQ_TEX_RESOURCE_WORD0_N>(latte::Register::SQ_TEX_RESOURCE_WORD0_0 + 4 * (latte::SQ_PS_TEX_RESOURCE_0 + i * 7));
      auto sq_tex_resource_word4 = getRegister<latte::SQ_TEX_RESOURCE_WORD4_N>(latte::Register::SQ_TEX_RESOURCE_WORD4_0 + 4 * (latte::SQ_PS_TEX_RESOURCE_0 + i * 7));

      vertex.samplerTypes[i] = getSamplerType(sq_tex_resource_word0.DIM,
                                              sq_tex_resource_word4.NUM_FORMAT_ALL,
                                              sq_tex_resource_word4.FORMAT_COMP_X);
   }

   // Write header
   writeHeader(out);

   // Uniforms
   writeUniforms(out, shader, sq_config, vertex.samplerTypes);
   out << '\n';

   // Vertex Shader Inputs
   for (auto &attrib : fetch.attribs) {
      semanticAttribs[attrib.location] = &attrib;

      out << "in "
          << getGLSLDataFormat(attrib.format, attrib.numFormat, attrib.formatComp)
          << " fs_out_" << attrib.location << ";\n";
   }
   out << '\n';

   // Vertex Shader Exports
   for (auto i = 0u; i <= spi_vs_out_config.VS_EXPORT_COUNT; i++) {
      out << "out vec4 vs_out_" << i << ";\n";
   }
   out << '\n';

   // Program code
   out << "void main()\n"
       << "{\n";

   writeLocals(out, shader);
   writeExports(out, shader);

   // Initialise registers
   if (shader.gprsUsed.find(0) != shader.gprsUsed.end()) {
      // TODO: Check which order of VertexID and InstanceID for r0.x, r0.y
      out << "R[0] = vec4(intBitsToFloat(gl_VertexID), intBitsToFloat(gl_InstanceID), 0.0, 0.0);\n";
   }

   // Assign fetch shader output to our GPR
   for (auto i = 0u; i < 32; ++i) {
      auto sq_vtx_semantic = getRegister<latte::SQ_VTX_SEMANTIC_N>(latte::Register::SQ_VTX_SEMANTIC_0 + i * 4);
      auto id = sq_vtx_semantic.SEMANTIC_ID;

      if (sq_vtx_semantic.SEMANTIC_ID == 0xff) {
         continue;
      }

      auto attrib = semanticAttribs[id];

      if (!attrib) {
         gLog->error("Invalid semantic mapping: {}", id);
         continue;
      }

      out << "R[" << (i + 1) << "] = ";

      auto channels = getDataFormatChannels(attrib->format);

      switch (channels) {
      case 1:
         out << "vec4(fs_out_" << attrib->location << ", 0.0, 0.0, 0.0);\n";
         break;
      case 2:
         out << "vec4(fs_out_" << attrib->location << ", 0.0, 0.0);\n";
         break;
      case 3:
         out << "vec4(fs_out_" << attrib->location << ", 0.0);\n";
         break;
      case 4:
         out << "fs_out_" << attrib->location << ";\n";
         break;
      }
   }

   out << '\n' << body << '\n';

   for (auto exp : shader.exports) {
      switch (exp->exportType) {
      case latte::SQ_EXPORT_POS:
         out << "gl_Position = exp_position_" << (exp->arrayBase - 60) << ";\n";
         break;
      case latte::SQ_EXPORT_PARAM:
         // TODO: Use vs_out semantics?
         out << "vs_out_" << exp->arrayBase << " = exp_param_" << exp->arrayBase << ";\n";
         break;
      case latte::SQ_EXPORT_PIXEL:
         throw std::logic_error("Unexpected pixel export in vertex shader.");
         break;
      }
   }

   out << "}\n";

   vertex.code = out.str();
   return true;
}
Ejemplo n.º 25
0
/* Increase the number of pages in the file by one. 
 * The new last page is filled with zero bytes.
 */
RC
appendEmptyBlock (SM_FileHandle *fHandle)
{
	FILE *fp;
	int totalNumPages, i, h_size;
	char *buff;
	// char zero = '\0';

	if (access(fHandle->fileName, R_OK) < 0) return RC_FILE_NOT_FOUND;

	fp = fopen(fHandle->fileName, "r+");

	buff = (char*)malloc(sizeof(char)*PAGE_SIZE);

	if ((totalNumPages = readHeader(fp)) < 1) return RC_FILE_R_W_ERROR;

	rewind(fp);

	if (writeHeader(fp,totalNumPages+1) < 1) return RC_FILE_R_W_ERROR;

	// printf("\n");
	// printf("APPEND: ftell_pre-fseek %ld\n", ftell(fp));
	
	fseek(fp, 0L, SEEK_CUR);
	
	// printf("APPEND: ftell_post-fseek %ld\n", ftell(fp));

	//read to the end of the page file
	for (i = 0; i < totalNumPages; i++) {
		fread(buff, sizeof(char)*PAGE_SIZE, 1, fp);
		// printf("APPEND: totalNumPages_header %d\n", totalNumPages);
		// printf("APPEND: iteration %d\n", i);
	}
	fseek(fp, 0L, SEEK_CUR);
	// fwrite(&zero, sizeof(char), PAGE_SIZE, fp);

  // printf("Appending page (%d)...\n", totalNumPages);
  memset(buff, 0, sizeof(*buff) * PAGE_SIZE);
  fwrite(buff, sizeof(*buff) * PAGE_SIZE, 1, fp);
  // printf("End of appending page...\n");




	fHandle->totalNumPages++;
	fHandle->curPagePos;

	free(buff);
	fclose(fp);

	// log file info to console
  struct stat fileStat;
  if(stat(fHandle->fileName,&fileStat) < 0)
    return RC_FILE_R_W_ERROR;
	// printf("\n\n**APPEND LOG**\n");
	// printf("************************************\n");
	// printf("File Name: \t\t%s\n",fHandle->fileName);
	// printf("File Size: \t\t%lld bytes\n",fileStat.st_size);
	// printf("Total # Pages: \t%d\n",fHandle->totalNumPages);
  
	return RC_OK;
}
Ejemplo n.º 26
0
bool GLDriver::compilePixelShader(PixelShader &pixel, uint8_t *buffer, size_t size)
{
   auto sq_config = getRegister<latte::SQ_CONFIG>(latte::Register::SQ_CONFIG);
   auto spi_ps_in_control_0 = getRegister<latte::SPI_PS_IN_CONTROL_0>(latte::Register::SPI_PS_IN_CONTROL_0);
   auto spi_ps_in_control_1 = getRegister<latte::SPI_PS_IN_CONTROL_1>(latte::Register::SPI_PS_IN_CONTROL_1);
   auto cb_shader_mask = getRegister<latte::CB_SHADER_MASK>(latte::Register::CB_SHADER_MASK);
   fmt::MemoryWriter out;
   latte::Shader shader;
   std::string body;

   shader.type = latte::Shader::Pixel;

   if (!latte::decode(shader, gsl::as_span(buffer, size))) {
      gLog->error("Failed to decode pixel shader");
      return false;
   }

   latte::disassemble(shader, pixel.disassembly);

   if (!latte::blockify(shader)) {
      gLog->error("Failed to blockify pixel shader");
      return false;
   }

   if (!glsl::generateBody(shader, body)) {
      gLog->warn("Failed to translate 100% of instructions for pixel shader");
   }

   // Get pixel sampler types
   for (auto i = 0; i < MAX_SAMPLERS_PER_TYPE; ++i) {
      auto sq_tex_resource_word0 = getRegister<latte::SQ_TEX_RESOURCE_WORD0_N>(latte::Register::SQ_TEX_RESOURCE_WORD0_0 + 4 * (latte::SQ_PS_TEX_RESOURCE_0 + i * 7));
      auto sq_tex_resource_word4 = getRegister<latte::SQ_TEX_RESOURCE_WORD4_N>(latte::Register::SQ_TEX_RESOURCE_WORD4_0 + 4 * (latte::SQ_PS_TEX_RESOURCE_0 + i * 7));

      pixel.samplerTypes[i] = getSamplerType(sq_tex_resource_word0.DIM,
                                             sq_tex_resource_word4.NUM_FORMAT_ALL,
                                             sq_tex_resource_word4.FORMAT_COMP_X);
   }

   // Write header
   writeHeader(out);

   // Uniforms
   writeUniforms(out, shader, sq_config, pixel.samplerTypes);
   out << '\n';

   // Pixel Shader Inputs
   for (auto i = 0u; i < spi_ps_in_control_0.NUM_INTERP; ++i) {
      auto spi_ps_input_cntl = getRegister<latte::SPI_PS_INPUT_CNTL_0>(latte::Register::SPI_PS_INPUT_CNTL_0 + i * 4);

      if (spi_ps_input_cntl.FLAT_SHADE) {
         out << "flat ";
      }

      out << "in vec4 vs_out_" << spi_ps_input_cntl.SEMANTIC << ";\n";
   }
   out << '\n';

   // Pixel Shader Exports
   auto maskBits = cb_shader_mask.value;

   for (auto i = 0; i < 8; ++i) {
      if (maskBits & 0xf) {
         out << "out vec4 ps_out_" << i << ";\n";
      }

      maskBits >>= 4;
   }
   out << '\n';

   // Program code
   out << "void main()\n"
      << "{\n";

   writeLocals(out, shader);
   writeExports(out, shader);

   // Assign vertex shader output to our GPR
   for (auto i = 0u; i < spi_ps_in_control_0.NUM_INTERP; ++i) {
      auto spi_ps_input_cntl = getRegister<latte::SPI_PS_INPUT_CNTL_0>(latte::Register::SPI_PS_INPUT_CNTL_0 + i * 4);
      auto id = spi_ps_input_cntl.SEMANTIC;

      if (id == 0xff) {
         continue;
      }

      out << "R[" << i << "] = vs_out_" << id << ";\n";
   }

   out << '\n' << body << '\n';

   for (auto exp : shader.exports) {
      switch (exp->exportType) {
      case latte::SQ_EXPORT_PIXEL: {
         auto mask = cb_shader_mask.value >> (4 * exp->arrayBase);

         if (!mask) {
            gLog->warn("Export is masked by cb_shader_mask");
         } else {
            out
               << "ps_out_"
               << exp->arrayBase
               << " = exp_pixel_"
               << exp->arrayBase << '.';

            if (mask & (1 << 0)) {
               out << 'x';
            }

            if (mask & (1 << 1)) {
               out << 'y';
            }

            if (mask & (1 << 2)) {
               out << 'z';
            }

            if (mask & (1 << 3)) {
               out << 'w';
            }

            out << ";\n";
         }
      } break;
      case latte::SQ_EXPORT_POS:
         throw std::logic_error("Unexpected position export in pixel shader.");
         break;
      case latte::SQ_EXPORT_PARAM:
         throw std::logic_error("Unexpected parameter export in pixel shader.");
         break;
      }
   }

   out << "}\n";

   pixel.code = out.str();
   return true;
}
Ejemplo n.º 27
0
unsigned long CSysSolve::FGMRES(const CSysVector & b, CSysVector & x, CMatrixVectorProduct & mat_vec,
                               CPreconditioner & precond, double tol, unsigned long m, bool monitoring) {
	
int rank = 0;

#ifndef NO_MPI
#ifdef WINDOWS
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#else
	rank = MPI::COMM_WORLD.Get_rank();
#endif
#endif
  
  /*---  Check the subspace size ---*/
  if (m < 1) {
    if (rank == 0) cerr << "CSysSolve::FGMRES: illegal value for subspace size, m = " << m << endl;
#ifdef NO_MPI
    exit(1);
#else
#ifdef WINDOWS
	MPI_Abort(MPI_COMM_WORLD,1);
    MPI_Finalize();
#else
    MPI::COMM_WORLD.Abort(1);
    MPI::Finalize();
#endif
#endif
  }

  /*---  Check the subspace size ---*/
  if (m > 1000) {
    if (rank == 0) cerr << "CSysSolve::FGMRES: illegal value for subspace size (too high), m = " << m << endl;
#ifdef NO_MPI
    exit(1);
#else
#ifdef WINDOWS
	MPI_Abort(MPI_COMM_WORLD,1);
    MPI_Finalize();
#else
    MPI::COMM_WORLD.Abort(1);
    MPI::Finalize();
#endif
#endif
  }
  
  /*---  Define various arrays
	 Note: elements in w and z are initialized to x to avoid creating
	 a temporary CSysVector object for the copy constructor ---*/
  vector<CSysVector> w(m+1, x);
  vector<CSysVector> z(m+1, x);
  vector<double> g(m+1, 0.0);
  vector<double> sn(m+1, 0.0);
  vector<double> cs(m+1, 0.0);
  vector<double> y(m, 0.0);
  vector<vector<double> > H(m+1, vector<double>(m, 0.0));
  
  /*---  Calculate the norm of the rhs vector ---*/
  double norm0 = b.norm();
  
  /*---  Calculate the initial residual (actually the negative residual)
	 and compute its norm ---*/
  mat_vec(x,w[0]);
  w[0] -= b;
  
  double beta = w[0].norm();
  
  if ( (beta < tol*norm0) || (beta < eps) ) {
    /*---  System is already solved ---*/
    if (rank == 0) cout << "CSysSolve::FGMRES(): system solved by initial guess." << endl;
    return 0;
  }
  
  /*---  Normalize residual to get w_{0} (the negative sign is because w[0]
	 holds the negative residual, as mentioned above) ---*/
  w[0] /= -beta;
  
  /*---  Initialize the RHS of the reduced system ---*/
  g[0] = beta;
  
  /*--- Set the norm to the initial initial residual value ---*/
  norm0 = beta;

  /*---  Output header information including initial residual ---*/
  int i = 0;
  if ((monitoring) && (rank == 0)) {
    writeHeader("FGMRES", tol, beta);
    writeHistory(i, beta, norm0);
  }
  
  /*---  Loop over all serach directions ---*/
  for (i = 0; i < m; i++) {
    
    /*---  Check if solution has converged ---*/
    if (beta < tol*norm0) break;
    
    /*---  Precondition the CSysVector w[i] and store result in z[i] ---*/
    precond(w[i], z[i]);
    
    /*---  Add to Krylov subspace ---*/
    mat_vec(z[i], w[i+1]);
    
    /*---  Modified Gram-Schmidt orthogonalization ---*/
    modGramSchmidt(i, H, w);
    
    /*---  Apply old Givens rotations to new column of the Hessenberg matrix
		 then generate the new Givens rotation matrix and apply it to
		 the last two elements of H[:][i] and g ---*/
    for (int k = 0; k < i; k++)
      applyGivens(sn[k], cs[k], H[k][i], H[k+1][i]);
    generateGivens(H[i][i], H[i+1][i], sn[i], cs[i]);
    applyGivens(sn[i], cs[i], g[i], g[i+1]);
    
    /*---  Set L2 norm of residual and check if solution has converged ---*/
    beta = fabs(g[i+1]);
    
    /*---  Output the relative residual if necessary ---*/
    if ((((monitoring) && (rank == 0)) && ((i+1) % 100 == 0)) && (rank == 0)) writeHistory(i+1, beta, norm0);
  }

  /*---  Solve the least-squares system and update solution ---*/
  solveReduced(i, H, g, y);
  for (int k = 0; k < i; k++) {
    x.Plus_AX(y[k], z[k]);
  }
  
  if ((monitoring) && (rank == 0)) {
    cout << "# FGMRES final (true) residual:" << endl;
    cout << "# Iteration = " << i << ": |res|/|res0| = " << beta/norm0 << endl;
  }
  
//  /*---  Recalculate final (neg.) residual (this should be optional) ---*/
//  mat_vec(x, w[0]);
//  w[0] -= b;
//  double res = w[0].norm();
//  
//  if (fabs(res - beta) > tol*10) {
//    if (rank == 0) {
//      cout << "# WARNING in CSysSolve::FGMRES(): " << endl;
//      cout << "# true residual norm and calculated residual norm do not agree." << endl;
//      cout << "# res - beta = " << res - beta << endl;
//    }
//  }
	
	return i;
  
}
 void AuthenticationTicketMessage::pack(DataOutput &out)
 {
     serialize(out);
     writeHeader(out);
 }
Ejemplo n.º 29
0
status NeXTFile::update()
{
	writeHeader();
	return AF_SUCCEED;
}
Ejemplo n.º 30
0
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_stm32f4xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f4xx.c file
       */

    //================= Initialisation pin et interruptions =============================

    GPIO_Configuration();
    NVIC_Configuration(); 	/* Interrupt Config SDIO*/
    EXTILine0_Config();		// Interrupt PA0
    Timer2_Config();		//Configure timer2 1[s]
    Init_Uart2(4800);
    Init_Uart3(4800);
    Init_SDCard();			//Mount sd card and use fat32 format

    fileUart2 = initialiserFile();	//Init fifo pile
    fileUart3 = initialiserFile();	//Init fifo pile


    while(1) 	/* Infinite loop */
    {
        switch(etatBtBleu)
        {
        case START:
            enableUsartIt();
            GPIO_SetBits(GPIOD, GPIO_Pin_12); 			//Turn on green led
            led_gr_blink = 1;							//Activate the blink mod in an interruption

            writeHeader(head_txt2, file2_path);		//header for the usart2 file
            writeHeader(head_txt3, file3_path);		//header for the usart3 file

            etatBtBleu = RUN;
            break;

        case STOP:
            disableUsartIt();							//we will read the fifo pile so it's better to inibit usart interrupt
            led_gr_blink = 0;
            GPIO_ResetBits(GPIOD, GPIO_Pin_12); 		//green led	off
            GPIO_SetBits(GPIOD, GPIO_Pin_15);			//blue led on

            writeDynamicTabData(pullN(fileUart2, nbCaractUart2), nbCaractUart2, file2_path);			//write all the fifo's uart2 data
            writeDynamicTabData(pullN(fileUart3, nbCaractUart3), nbCaractUart3, file3_path);			//write all the fifo's uart3 data

            GPIO_ResetBits(GPIOD, GPIO_Pin_15);			//blue led off

            nbCaractUart2 = 0;
            nbCaractUart3 = 0;
            etatBtBleu=WAIT;

            break;

        case RUN:
            //=========== write data when we have NB_CARACT_STOCK into the usart2 fifo pile =============
            if(flag_fileUart2Ready)
            {
                flag_fileUart2Ready=0;
                writeDynamicTabData(pullN(fileUart2, NB_CARACT_STOCK), NB_CARACT_STOCK, file2_path);	//write NB_CARACT_STOCK of uart2 data
                GPIO_ResetBits(GPIOD, GPIO_Pin_15);		//blue led off
            }

            //=========== write data when we have NB_CARACT_STOCK into the usart3 fifo pile =============
            if(flag_fileUart3Ready)
            {
                flag_fileUart3Ready = 0;
                writeDynamicTabData(pullN(fileUart3, NB_CARACT_STOCK), NB_CARACT_STOCK, file3_path);	//write NB_CARACT_STOCK of uart3 data
                GPIO_ResetBits(GPIOD, GPIO_Pin_15);		//blue led off
            }
            break;
        }
    }
}