void align_reads(vector<pair<string, string> >& reference, string& read_file, string& sam_file, vector<reference_index>& refindex)
{
	time_t tstrt, tbgn, tnd;
	time(&tstrt);
		
	/*//Can be used for analyzing the difference between LAST and NanoBLASTer
	ifstream fp_nano;
	string nano_input;
	string nano_file = "last_but_not_nano.txt";
	char *nano = new char[nano_file.length() + 1];
	strcpy(nano, nano_file.c_str());
	fp_nano.open(nano, ifstream::in);
	unordered_map<string, int> nano_read;

	while(getline(fp_nano, nano_input))
	{
		//cout << "" << nano_input << endl;
		nano_read[nano_input] = 1;
		//continue;
	}
	cout << "Total Size of Nano Read = " << nano_read.size() << endl << endl;
	fp_nano.close();
	delete [] nano;
	*/
	
	ifstream fp_read;
	ofstream fp_sam;
	
	char *read = new char[read_file.length() + 1];
	strcpy(read, read_file.c_str());

	char *sam = new char[sam_file.length() + 1];
	strcpy(sam, sam_file.c_str());

	fp_read.open(read, ifstream::in);
	fp_sam.open(sam, ofstream::out | ofstream::app);

	string input, read_name, ref_name;
	string readseq, refgenome;
	string slash = "/";
	int map = 0;
	int count = 0;
	int cant_map = 0;
	int invalid_count = 0;

	fp_csv << "cnt, red_nam, red_len, red_dir, ref_nam, ref_len, ref_pos, score, span, " <<
				"percent, aln_len, spn_rat, aln_tim, tot_tim" << endl;

	getline(fp_read, input);
	while(!fp_read.eof())
	{
		//int find = input.find(slash);
		//if(find != string::npos)
		//	read_name = input.substr(1, find - 1);
		//else
		//	read_name = input.substr(1);
		read_name = input.substr(1);

		readseq = "";
		while(getline(fp_read, input))
		{
			if(input.length() == 0)
				continue;
			if(input.at(0) == '>')
				break;

			readseq += input;
		}
		
		//getline(fp_read, input);
		//getline(fp_read, input);

		//ratio problem with channel_46_read_98_1406145606_2D
		//if(read_name.find("channel_407_read_0_1405093831_2D") == std::string::npos)//to optimize the output
		//if(read_name.find("channel_17_read_24_1405524767_2D") == std::string::npos)//small read to optimize
		//if(read_name.find("channel_201_read_10_1405541481_2D") == std::string::npos)//to compare version 1 and 2
		//if(read_name.find("channel_64_read_7_1403826200_template") == std::string::npos)//max length reads analysis
		//if(read_name.find("channel_424_read_1_1403566249_template") == std::string::npos)//found in last but not in nano
		//if(read_name.find("2D") == std::string::npos)//03-09-2015
		//if(read_name.find("channel_237_read_42_1406145606_2D") == std::string::npos)
		//if(read_name.find("channel_322_read_11_1405524767_template") == std::string::npos)

		//if(read_name.find("channel_171_read_2_1403855963_2D") == std::string::npos)//20 times higher than last
		//if(read_name.find("channel_82_read_0_1403855963_2D") == std::string::npos)//20 times higher than last
		//if(read_name.find("channel_221_read_19_1406145606_2D") == std::string::npos)//has maximul length of deletion
		//if(read_name.find("channel_415_read_6_1406242409_template") == std::string::npos)//has 5 times less length than last
		//if(read_name.find("channel_167_read_19_1403811400_2D") == std::string::npos)//analyze output validity
		//if(read_name.find("channel_474_read_32_1405524767_template") == std::string::npos)//found in last and nano repeat
		//if(read_name.find("channel_468_read_12_1403811400_complement") == std::string::npos)//cause exception in nano repeat
		//if(read_name.find("channel_345_read_7_1403811400_2D") == std::string::npos)//max length increased

		//if(read_name.find("channel_104_read_1_1403551548_template") == std::string::npos)//different in edit not lis
		//if(read_name.find("channel_216_read_0_1403551548_template") == std::string::npos)//different in lis not edit	
		//if(read_name.find("channel_118_read_6_1403551548_template") == std::string::npos)//different in lis and edit	
		//if(read_name.find("channel_486_read_0_1403566249_template") == std::string::npos)//reverse problem
		//if(nano_read.find(read_name) == nano_read.end())
		//if(read_name.find("channel_352_read_34_1405541481_template") == std::string::npos)//Why there are multiple results
		//if(read_name.find("channel_68_read_22_1405541481_template") == std::string::npos)//multiple results, boundary problem
		//if(read_name.find("channel_261_read_39_1405541481_template") == std::string::npos)//multiple result indexing

		//if(read_name.find("channel_302_read_2_1403855963_2D") == std::string::npos)//found in mms not in ssg = align length
		//if(read_name.find("channel_243_read_0_1403595798_template") == std::string::npos)//found in 40655 not in lis+edit
		//if(read_name.find("channel_452_read_46_1405541481_template") == std::string::npos)//same problem as above
		//if(read_name.find("channel_431_read_2_1403915857_template") == std::string::npos)//require top 40 tuple list to solve
		//readseq = readseq.substr(readseq.length() / 2, readseq.length() - readseq.length() / 2);
		//if(read_name.find("channel_199_read_0_1403841073_template") == std::string::npos)//solved
		//if(read_name.find("channel_480_read_91_1406242409_template") == std::string::npos)//in last and not in nano
		//if(read_name.find("channel_389_read_57_1406242409_template") == std::string::npos)//solved
		//if(read_name.find("channel_56_read_1_1403826200_template") == std::string::npos)
		//if(read_name.find("channel_356_read_29_1406242409_template") == std::string::npos)// < 40 in nano very weird
		//if(read_name.find("channel_131_read_5_1403826200_template") == string::npos)// < 100 in nano seems weird
		//if(read_name.find("channel_75_read_80_1406145606_template") == std::string::npos)//80% last not found now solved
		//	continue;


		if(count >= MAXREAD && MAXREAD != 0) break;
		count += 1;

		//if(count < 11762) continue;
		cout << count << ") " << read_name << endl;
		if(readseq.length() < MINREADLEN || readseq.length() > MAXREADLEN)//03-09-2015
		{
			cout << "Invalid String Found" << endl;
			invalid_count += 1;
			count -= 1;
			fp_sam << read_name << "\t4\t*\t0\t0\t*\t*\t0\t0\t" << readseq << "\t*" << endl;
			time(&tnd);
			//fp_csv << count << ", " << readseq.length() << ", 0, 0, 0, " <<
			//		"0, 0, 0, 0, 0, 0, 0, " << difftime(tnd, tstrt) << endl;
			continue;
		}
		
		if(count <= MINREAD) continue;
		//if(count < 318) continue;	
	
		time(&tbgn);
		if(DEBUG == 99)	
			fp_csv << count << ", " << read_name << ", " << readseq.length() << ", ";

		upper_case(readseq);		
		//reverse_str(readseq);
		//readseq = reverse_complement(readseq);
		
		int match_info, global_match = -1, indpos;
		int match, max_match = 0, match_index, dir;
		vector<vector<string> > list_final_result;
		
		//time_t start, end;
		//clock_t t_start, t_end;

		//for(int i = 0; i < reference.size(); i++)
		{

			//vector<pair<int, pair<int, int> > > kmer_ref;
			vector<pair<int, vector<pair<int, int> > > > kmer_ref;
			//cout << "Analysis for forward:" << endl;
			//
			//time(&start);
			//t_start = clock();

			read_vs_reference(readseq, read_name, FF, refindex, kmer_ref);

			//t_end = clock();
			//time(&end);
			//cout << "Total time taken for calling forward read_vs_ref = " << difftime(end, start) << endl;
			
			//t_lookup += t_end - t_start;
			//align(readseq, read_name, FF, refindex, kmer_ref, final_result);

			//cout << "Data for reverse:" << endl;
			//time(&start);
			//t_start = clock();
		
			if(SINGLE == 1)
			{	
				string reverse = reverse_complement(readseq);
				read_vs_reference(reverse, read_name, FR, refindex, kmer_ref);
			}
			//t_end = clock();
			//time(&end);
			//cout << "Total time taken for calling reverse read_vs_ref = " << difftime(end, start) << endl;
			
			//t_lookup += t_end - t_start;
			//cout << endl <<endl;

			//uncomment here for aligninng read
			list_final_result.clear();
			align(readseq, read_name, FR, refindex, kmer_ref, list_final_result);
			
			
			if(list_final_result.size() == 0)
			{
				cant_map += 1;
				kmer_ref.clear();
				//time(&tnd);
				//fp_csv << difftime(tnd, tbgn) << ", " << difftime(tnd, tstrt) << endl;
				fp_sam << read_name << "\t4\t*\t0\t0\t*\t*\t0\t0\t" << readseq << "\t*" << endl;
				continue;
			}

			kmer_ref.clear();
		}
	
		for(int i = 0; i < list_final_result.size(); i++)
		{
			vector<string>& final_result = list_final_result[i];
			fp_sam << final_result[0];
			for(int k = 1; k < final_result.size(); k++)
                	{
                        	fp_sam << "\t" << final_result[k];
                        	//cout << i << ": " << output[k] << endl;
                	}
		
			fp_sam << endl;
			map += 1;
			final_result.clear();
		}
		/*
		if(list_final_result.size() == 0 && SAM_FORMAT == 1)
		{
			fp_sam << read_name << "\t4\t*\t0\t0\t*\t*\t0\t0\t" << readseq << "\t*" << endl;
		}
		*/
		//time(&tnd);
		list_final_result.clear();
		if(DEBUG == 99)
			fp_csv << endl;//difftime(tnd, tbgn) << ", " << difftime(tnd, tstrt) << endl;
		//cout << "\nTime taken to process " << count << "th read = " << difftime(tnd, tstrt) << "\n" << endl;	
		//break;	
	}

	cout << endl;
	cout << "Overall Statistics - " << endl;
	cout << "total reference size = " << reference.size() << endl << endl;
	cout << "Total read = " << count << endl << endl;
	cout << "Total read mapped = " << map << endl << endl;
	cout << "Total unmapped read = " << cant_map << endl << endl;
	cout << "Out of range read (< 100 or > 15000) = " << invalid_count << endl << endl; 
	//cout << "Total MAX_MATCHED (= " << MAX_MATCHED << ") Reads = " << MAX_SCORED << endl << endl;

	fp_read.close();
	fp_sam.close();

	delete [] read;
	delete [] sam;

}
Exemple #2
0
void ShaderBuild::parseLog(std::string* errStr, std::string log, int baseFile, bool readLineNumbers)
{
	std::string line, first, second;
	std::stringstream reader(log);
	int prevFile = -1;
	int prevLine = -1;
	while (getline(reader, line))
	{
		//attempt to read two ints - file ane line numbers
		int end = extractInt(line, first);
		if (readLineNumbers && end > 0)
			end = extractInt(line.substr(end), second);
		
		//if found, replace the error line with the file info
		if (end > 0)
		{
			//read/convert line numbers
			int filenum = stringToInt(first);
			//I could swear nvidia's compiler is reporting bad line numbers when #line is used
			int linenum = stringToInt(second);
			
			//stupid ATI drivers don't like #line before #version, so the root file never has a #line
			//this workaround replaces file 0 with the REAL file. thanks, AMD. real helpful
			if (filenum == 0)
				filenum = baseFile;
			
			//extract error message
			end = (int)line.find(":", end);
			std::string errormsg;
			if (end >= 0)
				errormsg = line.substr(end + 1);
			else
				errormsg = line;
			
			//add file/line number info if available
			line = "";
			if (filenum >= 0 && filenum < (int)allfilebits.size())
				line += allfilebits[filenum].filename + ":";
			else
				line += "<unknown file " + intToString(filenum) + ">";
			if (readLineNumbers && linenum >= 0) line += intToString(linenum) + ":";
			
			//if both are available and valid, attempt to include the source line from the file
			int tabw = (int)line.size();
			std::string faultline;
			if (filenum >= 0 && linenum >= 0 && filenum < (int)allfilebits.size())
			{
				std::string sourceLine;
				if (!getFileLine(allfilebits[filenum].filename, linenum, sourceLine))
					sourceLine = "<I'm stupid and can't read the source>";
				if (sourceLine.size() == 0)
					sourceLine = "<empty line>";
				faultline = " " + sourceLine;
			}
			else
				faultline = " <line not given>";
			line += faultline;
			
			//add the actual error message to the end
			if  (prevLine == linenum && prevFile == filenum)
				line = std::string(tabw, ' ') + errormsg;
			else
				line += "\n" + std::string(tabw, ' ') + errormsg;
			prevLine = linenum;
			
			//if this is the first time the file was mentioned, give the include path
			//FIXME: this is incorrect. each occurance of an include should be marked
			//       as a separate file so as to distinguish the include path!!!
			if (prevFile != filenum)
			{
				if (filenum >= 0 && filenum < (int)allfilebits.size())
					line = printPath(allfilebits[filenum].path) + line;
				else
					line = "<error in file number>\n" + line;
				prevFile = filenum;
			}
		}
		
		//print the line
		if (errStr)
			*errStr += line + "\n";
		else
			std::cout << line << std::endl;
	}
}
Exemple #3
0
void Scanner::run()
{
  std::vector<double> kernelWidth;
  std::vector<double> windowSize;
  std::vector<double> peakThreshold;
  std::vector<double> floorThreshold;
  std::vector<int> signalFindingIterations;
  std::vector<double> reclusterThreshold;
  std::ostringstream dispstr;
  dispstr << "--------------------SCAN PARAMETERS:\n--------------------\n\n";
  
  //----- Read configuration file -----
  std::ifstream phil(m_configFile);
  if(!(phil.is_open())){
    hide();
    return;
  }
  std::string line;
  boost::char_separator<char> sep(":");
  while(getline(phil,line)){
    dispstr << line << "\n";
    boost::tokenizer< boost::char_separator<char> > tokens(line,sep);
    boost::tokenizer< boost::char_separator<char> >::iterator tit = tokens.begin();
    std::string param = *tit;
    tit++;
    boost::char_separator<char> sep2(",");
    boost::tokenizer< boost::char_separator<char> > tokens2(*tit,sep2);
    if(param.compare("kernel width") == 0){
      for(const auto& t2 : tokens2) kernelWidth.push_back(boost::lexical_cast<double>(t2));
    }
    else if(param.compare("window size") == 0){
      for(const auto& t2 : tokens2) windowSize.push_back(boost::lexical_cast<double>(t2));
    }
    else if(param.compare("peak threshold") == 0){
      for(const auto& t2 : tokens2) peakThreshold.push_back(boost::lexical_cast<double>(t2));
    }
    else if(param.compare("floor threshold") == 0){
      for(const auto& t2 : tokens2) floorThreshold.push_back(boost::lexical_cast<double>(t2));
    }
    else if(param.compare("signal finding iterations") == 0){
      for(const auto& t2 : tokens2) signalFindingIterations.push_back(boost::lexical_cast<int>(t2));
    }
    else if(param.compare("recluster threshold") == 0){
      for(const auto& t2 : tokens2) reclusterThreshold.push_back(boost::lexical_cast<double>(t2));
    }
  }
  phil.close();
  //-----------------------------------

  dispstr << "\n--------------------\nINPUT FILES:\n--------------------\n\n";

  //----- Load images -----
  int tot_tasks = 0;
  std::vector<int> series_ntasks;
  std::vector< std::vector<ImRecord*>* > templateRecords;
  m_fileManager = new FileManager();
  for(std::vector<std::string>::iterator fit = m_inputFiles.begin(); fit != m_inputFiles.end(); fit++){
    dispstr << *fit << "\n";
    ImageAnalysisToolkit tmp;
    std::vector<ImRecord*>* recs = new std::vector<ImRecord*>();
    recs->clear();
    FileManager::input_file infile = FileConverter::read(m_fileManager,&tmp,recs,*fit,nia::niaVersion);
    templateRecords.push_back(recs);
    int itasks = infile.np * infile.nt;
    series_ntasks.push_back(itasks);
    tot_tasks += itasks;
    m_fileManager->addInputFile(infile);
  }

  m_textBuffer->set_text(dispstr.str());
  show_all_children();

  tot_tasks *= kernelWidth.size()*windowSize.size()*peakThreshold.size()*floorThreshold.size()*reclusterThreshold.size()*signalFindingIterations.size();
  m_progressWindow.launch(tot_tasks);
  m_fileManager->reset();
  ImSeries* m_data = m_fileManager->loadNext();
  int seriesID = 0;
  int scanID = 0;
  while(m_data){
    for(std::vector<double>::iterator kwit = kernelWidth.begin(); kwit != kernelWidth.end(); kwit++){
      for(std::vector<double>::iterator wsit = windowSize.begin(); wsit != windowSize.end(); wsit++){
	for(std::vector<double>::iterator ptit = peakThreshold.begin(); ptit != peakThreshold.end(); ptit++){
	  for(std::vector<double>::iterator ftit = floorThreshold.begin(); ftit != floorThreshold.end(); ftit++){
	    for(std::vector<double>::iterator rtit = reclusterThreshold.begin(); rtit != reclusterThreshold.end(); rtit++){
	      for(std::vector<int>::iterator sfit = signalFindingIterations.begin(); sfit != signalFindingIterations.end(); sfit++){
		ImageAnalysisToolkit* toolkit = new ImageAnalysisToolkit();
		toolkit->setSaturationThreshold(65534);
		toolkit->setKernelWidth(*kwit);
		toolkit->setLocalWindow(*wsit);
		toolkit->setPeakThreshold(*ptit);
		toolkit->setFloorThreshold(*ftit);
		toolkit->setReclusterThreshold(*rtit);
		toolkit->setMaxSignalFindingIterations(*sfit);
		std::vector<ImRecord*>* scanRecs = new std::vector<ImRecord*>();
		scanRecs->clear();
		for(int i = 0; i < series_ntasks[seriesID]; i++) scanRecs->push_back(templateRecords[seriesID]->at(i)->emptyCopy());
		m_records.push_back(scanRecs);
		for(int i = 0; i < scanRecs->at(0)->nSynapseCollections(); i++) toolkit->addSynapseDefinition(scanRecs->at(0)->getSynapseCollection(i)->emptyCopy());
		m_ntasks.push_back(series_ntasks[seriesID]);
		for(int ps = 0; ps < m_data->npos(); ps++){
		  for(int tm = 0; tm < m_data->nt(); tm++){
		    while(m_activeThreads == m_maxThreads) boost::this_thread::sleep(boost::posix_time::millisec(60000));
		    m_mtx.lock();
		    m_threadpool.create_thread(boost::bind(&Scanner::run_analysis, this, m_data, toolkit, scanID, ps, tm, seriesID));
		    m_activeThreads++;
		    boost::this_thread::sleep(boost::posix_time::millisec(1000));
		    m_mtx.unlock();
		  }
		}
		boost::this_thread::sleep(boost::posix_time::millisec(1000));
		scanID++;
	      }
	    }
	  }
	}
      }
    }
    m_threadpool.join_all();
    nia::nout << "Finished parameter scan for sample " << m_fileManager->getName(seriesID) << "\n";
    delete m_data;
    m_data = m_fileManager->loadNext();
    seriesID++;
  }
  delete m_fileManager;
  hide();
  //-----------------------
}
Exemple #4
0
Table* read_from_geoplaces2() {


  
  ifstream myfile ("geoplaces2.csv");
  if (myfile.is_open())
  {

	Table::ColumnList columns;

	String placeID;
	getline( myfile, placeID, ',' );
    columns.push_back(make_pair(placeID, Table::integer));

	String latitude;
	getline( myfile, latitude, ',' );
	columns.push_back(make_pair(latitude, Table::floating));

	String longitude;
	getline( myfile, longitude, ',' );
	columns.push_back(make_pair(longitude, Table::floating));

	String the_geom_meter;
	getline( myfile, the_geom_meter, ',' );
	columns.push_back(make_pair(the_geom_meter, Table::varchar));

	String name;
	getline( myfile, name, ',' );
    columns.push_back(make_pair(name, Table::varchar));

	String address;
	getline( myfile, address, ',' );
    columns.push_back(make_pair(address, Table::varchar));

	String city;
	getline( myfile, city, ',' );
    columns.push_back(make_pair(city, Table::varchar));

	String state;
	getline( myfile, state, ',' );
    columns.push_back(make_pair(state, Table::varchar));

	String country;
	getline( myfile, country, ',' );
    columns.push_back(make_pair(country, Table::varchar));

	String fax;
	getline( myfile, fax, ',' );
    columns.push_back(make_pair(fax, Table::varchar));

	String zip;
	getline( myfile, zip, ',' );
    columns.push_back(make_pair(zip, Table::varchar));

	String alcohol;
	getline( myfile, alcohol, ',' );
    columns.push_back(make_pair(alcohol, Table::varchar));

	String smoking_area;
	getline( myfile, smoking_area, ',' );
    columns.push_back(make_pair(smoking_area, Table::varchar));

	String dress_code;
	getline( myfile, dress_code, ',' );
    columns.push_back(make_pair(dress_code, Table::varchar));

	String accessibility;
	getline( myfile, accessibility, ',' );
    columns.push_back(make_pair(accessibility, Table::varchar));

	String price;
	getline( myfile, price, ',' );
    columns.push_back(make_pair(price, Table::varchar));

	String url;
	getline( myfile, url, ',' );
    columns.push_back(make_pair(url, Table::varchar));

	String Rambience;
	getline( myfile, Rambience, ',' );
    columns.push_back(make_pair(Rambience, Table::varchar));

	String franchise;
	getline( myfile, franchise, ',' );
    columns.push_back(make_pair(franchise, Table::varchar));

	String area;
	getline( myfile, area, ',' );
    columns.push_back(make_pair(area, Table::varchar));

	String other_services;
	getline( myfile, other_services, '\n' );
    columns.push_back(make_pair(other_services, Table::varchar));

	

	Table* geo = new Table(columns); // Create geo using the defined columns
	


    while ( myfile.good() )
    {
	  vector<pair<string, string> > entries; // Entries for the record to be placed in the table
	  
	  string placeID;
	  getline( myfile, placeID, ',' );
	  pair <string,string> pair0  ("placeID",placeID);   // value init
	  entries.push_back(pair0);

	  string latitude;
	  getline( myfile, latitude, ',' );
	  pair <string,string> pair1  ("latitude",latitude);   // value init
	  entries.push_back(pair1);

	  string longitude;
	  getline( myfile, longitude, ',' );
	  pair <string,string> pair2  ("longitude",longitude);   // value init
	  entries.push_back(pair2);

	  string the_geom_meter;
	  getline( myfile, the_geom_meter, ',' );
	  pair <string,string> pair3  ("the_geom_meter",the_geom_meter);   // value init
	  entries.push_back(pair3);

	  string name;
	  getline( myfile, name, ',' );
	  pair <string,string> pair4  ("name",name);   // value init
	  entries.push_back(pair4);

	  string address;
	  getline( myfile, address, ',' );
	  pair <string,string> pair5  ("address",address);   // value init
	  entries.push_back(pair5);

	  string city;
	  getline( myfile, city, ',' );
	  pair <string,string> pair6  ("city",city);   // value init
	  entries.push_back(pair6);

	  string state;
	  getline( myfile, state, ',' );
	  pair <string,string> pair7  ("state",state);   // value init
	  entries.push_back(pair7);

	  string country;
	  getline( myfile, country, ',' );
	  pair <string,string> pair8  ("country",country);   // value init
	  entries.push_back(pair8);

	  string fax;
	  getline( myfile, fax, ',' );
	  pair <string,string> pair9  ("fax",fax);   // value init
	  entries.push_back(pair9);

	  string zip;
	  getline( myfile, zip, ',' );
	  pair <string,string> pair10  ("zip",zip);   // value init
	  entries.push_back(pair10);

	  string alcohol;
	  getline( myfile, alcohol, ',' );
	  pair <string,string> pair11  ("alcohol",alcohol);   // value init
	  entries.push_back(pair11);

	  string smoking_area;
	  getline( myfile, smoking_area, ',' );
	  pair <string,string> pair12  ("smoking_area",smoking_area);   // value init
	  entries.push_back(pair12);

	  string dress_code;
	  getline( myfile, dress_code, ',' );
	  pair <string,string> pair13  ("dress_code",dress_code);   // value init
	  entries.push_back(pair13);

	  string accessibility;
	  getline( myfile, accessibility, ',' );
	  pair <string,string> pair14  ("accessibility",accessibility);   // value init
	  entries.push_back(pair14);

	  string price;
	  getline( myfile, price, ',' );
	  pair <string,string> pair15 ("price",price);   // value init
	  entries.push_back(pair15);

	  string url;
	  getline( myfile, url, ',' );
	  pair <string,string> pair16  ("url",url);   // value init
	  entries.push_back(pair16);

	  string Rambience;
	  getline( myfile, Rambience, ',' );
	  pair <string,string> pair17  ("Rambience",Rambience);   // value init
	  entries.push_back(pair17);

	  string franchise;
	  getline( myfile, franchise, ',' );
	  pair <string,string> pair18  ("franchise",franchise);   // value init
	  entries.push_back(pair18);

	  string area;
	  getline( myfile, area, ',' );
	  pair <string,string> pair19  ("area",area);   // value init
	  entries.push_back(pair19);

	  string other_services;
	  getline( myfile, other_services, '\n' );
	  pair <string,string> pair20  ("other_services",other_services);   // value init
	  entries.push_back(pair20);

	  Record add(entries); // Create record to add to table

	  geo->insert(add);  // Insert add record into geo
	  
	}
    
	myfile.close();
	return geo;

  }

}
void Player::start_playing(const char* filename, string name, int score)
{
	/* handle file */
	ifstream file;
	file.open(filename, ios::app | ios::in);

	if(Base::file_is_empty(file)) {
		cout << "File is empty.Suggest you to manage the file, import entries and try again to play." << endl;
		return;
	}
	file.close();

	// vector<string> rest_meaning_example;
	file.open(filename, ios::app | ios::in);
	string temp;

	// transfer file entries in vector
	while(!file.eof())
	{
		getline(file, temp, '\n');
		entry.push_back(temp);
	}

	construct_prefix_meaning_vectors(entry);

	string choice;
	do{
		cout << "Make a choice: " << endl;
		cout << "1.Guess the meaning given the prefix." << endl;
		cout << "2.Guess the prefix given the meaning." << endl;
		cout << "3.Exit" << endl;

		// convert string to int
		getline(cin, choice);
		int ch = atoi(choice.c_str());
		const char* cstr = choice.c_str();
		//.. checks for invalid input: such as: 2 3
		for(unsigned int i = 0; i < choice.length(); i++) {
			if(cstr[i] == ' ') {
				for(unsigned int j = i; j < choice.length(); j++) {
					if(cstr[j] != ' ') {
						cout << "INVALID INPUT!!!" << endl;
						ch = 100;
					}
				}
			}
		}
		switch(ch)
		{
			case 1:
			{
				cout << "1.Guess the meaning given the prefix." << endl;
				this->score = 0;
				this->find_meaning();
				cout << "Play again? " << endl;
				break;
			}
			case 2:
			{
				cout << "2.Guess the prefix given the meaning." << endl;
				this->score = 0;
				this->find_prefix();
				cout << "Play again? " << endl;
				break;
			}
			case 3:
			{
				break;
			}
			default:
			{
				break;
			}
		}
	}while(choice.compare("3") != 0);
}
bool indexEngine::open()
{
    if(0 == dict_pth_.length())
        return false;
    std::string termId_pth =  dict_pth_ + "/termId.v";
    std::string queryDat_pth = dict_pth_ + "/queryDat.v";

    ifstream finTermsId;
    ifstream finQueryDat;

    std::string sLine = "";
    std::string s = "";

    //open term id dictionary if exist , load data
    finTermsId.open(termId_pth.c_str());
    if(finTermsId.is_open())
    {
        vector<std::size_t> queryIdVector;
        vector<std::string> context;
        std::size_t termID;
        //std::size_t queryID;
        while(getline(finTermsId,sLine))
        {
            queryIdVector.clear();
            context.clear();
            if(0 >= sLine.length())
                continue;
            boost::split(context,sLine,boost::is_any_of("\t"));
            if(2 > context.size())
                continue;
            termID = boost::lexical_cast<std::size_t>(context[0]);
            for(std::size_t it = 1; it < context.size()-1; ++it)
            {
                std::size_t id = boost::lexical_cast<std::size_t>(context[it]);
                queryIdVector.push_back(id);
            }
            terms2qIDs_.insert(make_pair(termID,queryIdVector));
        }
    }
//	std::cout << "读入terms的大小:" << terms2qIDs_.size() << std::endl;
    finTermsId.close();
    //open query data exist if exist , load data
    finQueryDat.open(queryDat_pth.c_str());
    if(finQueryDat.is_open())
    {
        QueryData qDat;
        std::size_t queryID;
        std::size_t tID;
        sLine = "";
        s = "";
        vector<std::size_t> termsIdVector;
        vector<std::string> context;
        while(getline(finQueryDat,sLine))
        {
            termsIdVector.clear();
            qDat.tid.clear();
            context.clear();
            if(0>= sLine.length())
                continue;
            boost::split(context,sLine,boost::is_any_of("\t"));
            if( 5 > context.size())
                continue;
            queryID = boost::lexical_cast<std::size_t>(context[0]);
            qDat.text = context[1];
            qDat.hits = boost::lexical_cast<std::size_t>(context[2]);
            qDat.counts = boost::lexical_cast<std::size_t>(context[3]);
            for(vector<std::string>::iterator it = context.begin()+4; it != context.end(); ++it)
            {
                tID = boost::lexical_cast<std::size_t>(*it);
                termsIdVector.push_back(tID);
            }
            qDat.tid = termsIdVector;
            queryIdata_.insert(make_pair(queryID,qDat));
        }

    }
//	std::cout << "读入query id 的大小:" << queryIdata_.size() << std::endl;
    finQueryDat.close();

    //load forbid keywords list
    ifstream finForbid;
    std::string forbid_pth = dict_pth_ + "/forbidden.v";
    finForbid.open(forbid_pth.c_str());
    if(finForbid.is_open())
    {
        sLine = "";
        forbidList_.clear();
        while(getline(finForbid,sLine))
        {
            if(0 == sLine.size())
                continue;
            forbidList_.insert(sLine);
        }
    }
    finForbid.close();

    //load rskeywords form TaoBao
    ifstream finRskeywords;
    string rs_pth = dict_pth_ + "/HotRskeywords.v";
    finRskeywords.open(rs_pth.c_str());
    if(finRskeywords.is_open()) {
        vector<std::string> context;
        vector<std::string> rskeywords;
        sLine = "";
        std::size_t hsId;
        while(getline(finRskeywords,sLine))
        {
            rskeywords.clear();
            if(0 >= sLine.length())
                continue;
            boost::split(context,sLine,boost::is_any_of("\t"));
            if(2 > context.size())
                continue;
            hsId = boost::lexical_cast<std::size_t>(context[0]);
            boost::unordered_map<std::size_t,vector<std::string> >::iterator rsIter;
            //std::cout << "key:" << context[0] << "\t hash:" << hsId << std::endl;
            rsIter = rsKeyTaoBao_.find(hsId);
            if(rsIter != rsKeyTaoBao_.end())
                continue;
            for(std::size_t i = 1; i < context.size(); ++i)
                if(context[i].size() != 0)
                    rskeywords.push_back(context[i]);
            rsKeyTaoBao_.insert(make_pair(hsId,rskeywords));
        }
    }
    finRskeywords.close();
    std::cout << "load rskeywords size:" << rsKeyTaoBao_.size() << std::endl;

    //load category dictonary
    ifstream finQuery2Cate;
    string cate_pth = dict_pth_ + "/query2Cate.v";
    finQuery2Cate.open(cate_pth.c_str());
    if(finQuery2Cate.is_open())
    {
        vector<std::string> context;
        vector<std::string> cate;
        sLine = "";
        std::size_t qID;
        while(getline(finQuery2Cate,sLine))
        {
            cate.clear();
            context.clear();
            if(0 >= sLine.length())
                continue;
            std::size_t pos = sLine.find('\t');
            if(std::string::npos == pos)
            {
                continue;
            }
            else
            {
                string ss = sLine.substr(0,pos);
                sLine.assign(sLine,pos+1,sLine.length()-1);
                qID = boost::lexical_cast<std::size_t>(ss);
                if( std::string::npos != sLine.find(","))
                {
                    boost::split(context,sLine,boost::is_any_of(","));
                    // if(1 > context.size())
                    //    continue;
                    for(std::size_t id = 0; id < context.size(); ++id)
                        cate.push_back(context[id]);
                }
                else
                    cate.push_back(sLine);
            }
            query2Cate_.insert(make_pair(qID,cate));
        }

    }
    finQuery2Cate.close();
    std::cout << "读入的category size:" << query2Cate_.size() <<  std::endl;

    LOG(INFO) << "Load terms dictionary size:" << terms2qIDs_.size()
              << "\tLoad keywords size:" << queryIdata_.size()
              << "\tLoad category size:" << query2Cate_.size()
              << "\tLoad forbidden keywords size:" << forbidList_.size();

    if(0 == terms2qIDs_.size() || 0 == queryIdata_.size())
    {
        isNeedflush = true;
        return true;
    }
    else
        return false; // load dictionary successfully!
}
Exemple #7
0
Table* read_from_rating_final() {

  ifstream myfile ("rating_final.csv");
  if (myfile.is_open())
  {

	Table::ColumnList columns;

	String placeID;
	getline( myfile, placeID, ',' );
    columns.push_back(make_pair(placeID, Table::varchar));

	String latitude;
	getline( myfile, latitude, ',' );
	columns.push_back(make_pair(latitude, Table::integer));

	String longitude;
	getline( myfile, longitude, ',' );
	columns.push_back(make_pair(longitude, Table::integer));

	String the_geom_meter;
	getline( myfile, the_geom_meter, ',' );
	columns.push_back(make_pair(the_geom_meter, Table::integer));

	String name;
	getline( myfile, name, '\n' );
    columns.push_back(make_pair(name, Table::integer));


	Table* geo = new Table(columns); // Create geo using the defined columns
	


    while ( myfile.good() )
    {
	  vector<pair<string, string> > entries; // Entries for the record to be placed in the table
	  
	  string placeID;
	  getline( myfile, placeID, ',' );
	  pair <string,string> pair0  ("userID",placeID);   // value init
	  entries.push_back(pair0);

	  string latitude;
	  getline( myfile, latitude, ',' );
	  pair <string,string> pair1  ("placeID",latitude);   // value init
	  entries.push_back(pair1);

	  string longitude;
	  getline( myfile, longitude, ',' );
	  pair <string,string> pair2  ("rating",longitude);   // value init
	  entries.push_back(pair2);

	  string the_geom_meter;
	  getline( myfile, the_geom_meter, ',' );
	  pair <string,string> pair3  ("food_rating",the_geom_meter);   // value init
	  entries.push_back(pair3);

	  string name;
	  getline( myfile, name, '\n' );
	  pair <string,string> pair4  ("service_rating",name);   // value init
	  entries.push_back(pair4);


	  Record add(entries); // Create record to add to table

	  geo->insert(add); // Insert add record into geo
	  
	}
    myfile.close();
	return geo;
  }
}
Exemple #8
0
JabberBotSession::JabberBotSession(const std::string &host, unsigned short port, bool isSecure, const std::string &userid, const std::string &password, const std::string &resource, const std::string &email, const std::string &affirmation_path, const std::string &db_path) : m_subscriptionDb(NULL, 0) {
  openlog("affirmations-bot", LOG_PID|LOG_CONS, LOG_DAEMON);

  Dbc *cursor;
  m_subscriptionDb.open(NULL, db_path.c_str(), NULL, DB_BTREE, DB_CREATE, 0);

  m_subscriptionDb.cursor(NULL, &cursor, 0);
  if (NULL != cursor) {
    Dbt key, data;
    time_t now = time(NULL);
    time_t bod = now - (now % 86400);
    m_eod = bod + 86400;

    while (0 == cursor->get(&key, &data, DB_NEXT)) {
      std::string user((char *)key.get_data());
      time_t last = *((time_t*) data.get_data());
      if (last < bod) {
	// He hasn't gotten an affirmation today
	time_t t1 = random() % ((m_eod - now)/2);
	time_t t2 = random() % ((m_eod - now)/2);
	
	Upcoming newEntry(user, now + t1 + t2);
	m_upcomingQueue.push(newEntry);
      }
      std::ostringstream message;
      message << "User " << user << " last got an affirmation at " << last;
      syslog(LOG_NOTICE, message.str().c_str());
    }
    cursor->close();
  }

  if (m_handlers.begin() == m_handlers.end()) {
    CommandHandler handler;
    handler.method = &JabberBotSession::Subscribe;
    m_handlers.insert(CommandHandlerMap::value_type("subscribe", handler));
    handler.method = &JabberBotSession::Unsubscribe;
    m_handlers.insert(CommandHandlerMap::value_type("unsubscribe", handler));
    handler.method = &JabberBotSession::Affirmation;
    m_handlers.insert(CommandHandlerMap::value_type("affirmation", handler));
    handler.method = &JabberBotSession::Status;
    m_handlers.insert(CommandHandlerMap::value_type("status", handler));
  }
  if (m_affirmations.empty()) {
    srand(time(NULL));
    std::string line;

    std::ifstream a(affirmation_path.c_str());
    while (getline(a, line)) { 
      std::ostringstream message;
      message << "The line is \"" << line << "\"" << std::endl;
      syslog(LOG_DEBUG, message.str().c_str());
      m_affirmations.push_back(line);
    }
  }

  m_session = new JabberSession(host, port, isSecure);

  // SYZYGY
  m_session->Register(this, HandlePresenceRequest);
  m_session->Register(this, HandleMessageRequest);
  m_session->Register(this, HandlePingRequest, "urn:xmpp:ping");
  JabberIqAuth login_stanza(userid, password, resource);
  const Stanza *response = m_session->SendMessage(login_stanza, true);
  std::ostringstream message;
  message << "The first login response is " << *(response->render(NULL)) << std::endl;
  syslog(LOG_DEBUG, message.str().c_str());
  if (200 != response->Error()) {
    message << "The first login response is " << response->ErrorMessage() << std::endl;
    syslog(LOG_NOTICE, message.str().c_str());
    JabberIqRegister register_stanza(userid, password, email);
    response = m_session->SendMessage(register_stanza, true);
    message << "The register response is " << *(response->render(NULL)) << std::endl;
    syslog(LOG_NOTICE, message.str().c_str());
    if (200 != response->Error()) {
      message << "The register response is " << response->ErrorMessage() << std::endl;
      syslog(LOG_NOTICE, message.str().c_str());
    }
    else {
      JabberIqAuth login2_stanza(userid, password, resource);
      response = m_session->SendMessage(login2_stanza, true);
      message << "The second login response is " << *(response->render(NULL)) << std::endl;
      syslog(LOG_NOTICE, message.str().c_str());
      if (200 != response->Error()) {
	message << "The second login failed with a message of " << response->ErrorMessage() << std::endl;
	syslog(LOG_NOTICE, message.str().c_str());
      }
    }
  }
  delete response;

  m_id = 0;
  m_continueRunning = true;
}
/*
 * Loads text(s) from an XML file to memory.
 * */
TextToClassify* Classifier::load_texts(string input_fpath) {
	if(!file_exists(input_fpath, false)){
		printf("Error: input XML file '%s' doesn't exist!\n", input_fpath.c_str());
		return NULL;
	}

	// Read XML file to memory
	string xml_string;
	ifstream xml_file;
	xml_file.open(input_fpath.c_str());
	string line;
	getline(xml_file,line);
	while (xml_file) {
		xml_string += line + "\n";
		getline(xml_file,line);
	}
	if (xml_string.length() <= 0){
		printf("Error: Input XML file %s is void!\n", input_fpath.c_str());
		return NULL;
	}

	// Parse XML content
	char* xml_text = (char*)xml_string.c_str();
	xml_document<> document;
	document.parse<0>(xml_text);
	//regex remove_parse_pattern("[^ ]+#[^# ]+#", regex_constants::icase|regex_constants::perl);
	//regex space_token_pattern("\\s+#[^# ]+#\\s+", regex_constants::icase|regex_constants::perl);
	regex whitespace_pattern("\\s+");

	TextToClassify* text_beg = 0;
	TextToClassify* text_prev = 0;
	TextToClassify* text_cur = 0;
	for (xml_node<>* node = document.first_node(ROOT_TAG)->first_node(TEXT_TAG); node; node = node->next_sibling()) {
		// Create a structure for current XML element
		text_cur = create_text();

		// Read class attribute
		xml_attribute<> *attr = node->first_attribute(CLASS_ATT);
		string label(attr->value());
		if(label == POSITIVE_CLASS_ATT) text_cur->cClass = POSITIVE_CLASS_I;
		else if(label == NEGATIVE_CLASS_ATT) text_cur->cClass = NEGATIVE_CLASS_I;
		else{
			printf("Wrong  class attribute value %s\n", label.c_str());
			continue;
		}

		// Load original string
		char* filename_original = node->first_node(ORIGINAL_TAG)->value();
		text_cur->sText = new char[strlen(filename_original)+1];
		sprintf(text_cur->sText, "%s", filename_original);

		// Load tokens
		string tokens_str(node->first_node(LEMMAS_TAG)->value());
		sregex_token_iterator token_it(tokens_str.begin(), tokens_str.end(), whitespace_pattern, -1);
		sregex_token_iterator end_it;
		TokenToClassify* token_cur = 0;
		TokenToClassify* token_prev = 0;
		string token;
		while(token_it != end_it) {
			// Create a token
			token_cur = create_token();
			token = *token_it;

			// Fill the token with data
			stringstream iss(token);
			string tmp;
			if (!getline(iss, tmp, '#')) {token_it++; continue;}
			algorithm::to_lower(tmp);
			token_cur->sSurface = new char[strlen(tmp.c_str())+1];
			sprintf(token_cur->sSurface, "%s", tmp.c_str());

			if (!getline(iss, tmp, '#')) {token_it++; continue;}
			token_cur->sPOS = new char[strlen(tmp.c_str())+1];
			sprintf(token_cur->sPOS, "%s", tmp.c_str());

			if (!getline(iss, tmp, '#')) {token_it++; continue;}
			algorithm::to_lower(tmp);
			token_cur->sLemma = new char[strlen(tmp.c_str())+1];
			sprintf(token_cur->sLemma, "%s", tmp.c_str());

			// Save the token
			if(text_cur->pToken == NULL) text_cur->pToken = token_cur; // First token -> previous is null
			else token_prev->pNext = token_cur; // Not first token -> set previous
			token_prev = token_cur;

			// Move to the next token
			token_it++;
		}

		// Set a link between previous and current structures
		if(text_beg == NULL) text_beg = text_cur;
		else text_prev->pNext = text_cur;
		text_prev = text_cur;
	}

	return text_beg;
}
MainWindow::MainWindow() :
        ui(new Ui::MainWindow),refreshGap(50000)
{
    ui->setupUi(this);
    ret=0;
    ifstream fin;
    ofstream fout;
    int currentUserId;
    string AccessToken, AccessSecret, Verifier, UserId;

    sina = ManagerFactory::getInstance()->getProvider(PROVIDER_SINA);

    /* 从文件读取登录信息,如果存在,则无需再登录,否则登录,并保存登录状态文件 */
    fin.open("login.data", ios::in);
    fin>>AccessToken
        >>AccessSecret
        >>Verifier
        >>UserId;
    fin.close();
    cout<<AccessToken<<" "<<AccessSecret<<" "<<Verifier<<" "<<UserId<<endl;
    if (AccessToken == "" || AccessSecret == "" || Verifier == "" || UserId == "") {
        ret = sina->OauthStep1(&url);
        cout << "LoginStep 1 result:" << ret << "\nUrl:" << url << endl;
        cout << "Please Enter Pin code :";
        getline(cin, pin);
        ret = sina->OauthStep2(pin);
        cout << "LoginStep 2 result:" << ret << endl;

        /* 将登录信息导出到文件 */
        sina->ExportLoginData(&AccessToken, &AccessSecret, &Verifier, &UserId);
        fout.open("login.data", ios::out);
        fout<<AccessToken<<" "<<AccessSecret<<" "<<Verifier<<" "<<UserId<<endl;
        fout.close();
    }
    else {
        sina->RevertLoginStatus(AccessToken, AccessSecret, Verifier, UserId);
    }

    // 设置当前用户
    currentUserId = atoi(UserId.c_str());
    cout<<currentUserId<<endl;
    this->currentUser = sina->getUser(currentUserId);
    cout<<currentUserId<<endl;

    this->ui->webView_Main->setPage(new QWebPage(this));
    //this->timer = new QTimer(this);// timer
    //ui->toolButton_UserName->setText(tr("GoDefine"));
    cout<<"UseId"<<currentUserId<<endl;

    ui->toolButton_Friends->setText(tr("微博数 %1").arg(currentUser->getStatuses_count()));
    ui->toolButton_Follower->setText(tr("粉丝数 %1").arg(currentUser->getFollowers_count()));
    ui->toolButton_WeiboCount->setText(tr("收藏数 %1").arg(currentUser->getFavourites_count()));
    ui->toolButton_UserName->setText(currentUser->getScreen_name());
    //ui->toolButton_Logo->setIcon(currentUser->getProfile_image_url());

    ui->webView_Main->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

    /*新建一个定时器,并连接到timeHere(),以一定时间反复的触发*/
    mainTimer = new QTimer(this);
    QObject::connect( mainTimer, SIGNAL(timeout()), this, SLOT(timeHere()) );
    mainTimer->start(refreshGap);

    QObject::connect(ui->toolButton_HomePage, SIGNAL(clicked()), this, SLOT(homePageButtonClicked()) );
    QObject::connect(ui->toolButton_MyWeiboPage, SIGNAL(clicked()), this, SLOT(myWeiboPageButtonClicked()) );
    QObject::connect(ui->toolButton_AtMePage, SIGNAL(clicked()), this, SLOT(mentionMePageButtonClicked()) );
    QObject::connect(ui->toolButton_CommentPage, SIGNAL(clicked()), this, SLOT(commentPageButtonClicked()) );
    QObject::connect(ui->toolButton_NewWeiBo, SIGNAL(clicked()), this, SLOT(newWeiboButtonClicked()));
    QObject::connect(ui->webView_Main, SIGNAL(linkClicked(QUrl)), this, SLOT(linkButtonClicked(QUrl)));

    QFile basicHtmlFile(":/UI/res/homepage.html");
    if(!basicHtmlFile.open(QFile::ReadOnly))
    {
        qDebug(basicHtmlFile.errorString().toAscii());
    }
    QTextStream stream(&basicHtmlFile);
    basicHtml = stream.readAll();
    basicHtmlFile.close();

    // 初始化status模板HTML
    this->statusHtml="<div style='background-color:#B1D0D9;margin-bottom:3px;'>"\
                        "<div style='font-weight:bold;color:blue;font-size:14px;background-color:#B1D0D9;border-bottom:solid 1px grey'>%1 说:</div>"\
                        "<div style='font-weight:normal;font-family:楷体;font-size:13px;margin:5px 10px 0px 10px;line-height:15px;'>%2</div>"\
                        "<div style='font-weight:normal;font-family:楷体;font-size:13px;margin:5px 10px 0px 10px;text-align:right;'>"\
                        "<a href='REPLY:%3:%4'>回复</a> <a href='REPOST:%5:%6'>转发</a>"
                        "</div></div>";
    this->showHomePage();
}
Exemple #11
0
void fastscan() {
    struct methodchain *methodstack=NULL;
    struct namer *namer=allocatenamer();
    struct genhashtable *calltable=genallocatehashtable((int (*)(void *)) &hashmethod, (int (*)(void *,void *)) &comparemethod);
    struct genhashtable *statictable=genallocatehashtable((int (*)(void *)) &hashmethod, (int (*)(void *,void *)) &comparemethod);

    while(1) {
    char *line=getline();

#ifdef DEBUG
    printf("------------------------------------------------------\n");
#endif

    if (line==0) {
	outputinfo(namer, calltable,statictable);
	return;
    }
#ifdef DEBUG
    printf("[%s]\n",line);
#endif
    switch(line[0]) {
    case 'C': 
	break;
    case 'O':
	break;
    case 'N':
      {
	/* Natively created object...may not have pointer to it*/
	char buf[1000];
	sscanf(line,"NI: %s",buf);

	getclass(namer,buf);
      }
      break;
    case 'U':
      {
	/* New object*/

	char buf[1000];
	sscanf(line,"UI: %s",buf);
	getclass(namer,buf);
      }
      break;
    case 'K':
      break; 
    case 'L':
      /* Do Load */
      {
	struct localvars * lv=(struct localvars *) calloc(1, sizeof(struct localvars));
	long long uid, objuid;
	char fieldname[600], classname[600], fielddesc[600];

	sscanf(line,"LF: %s %ld %s %lld %s %s %s %lld",lv->name,&lv->linenumber, lv->sourcename, &objuid, classname, fieldname, fielddesc, &uid);
	getfield(namer,classname, fieldname,fielddesc);
	
      }
      break;
    case 'G':
      /* Do Array Load */
      break;
    case 'M':
      /* Mark Local*/
      break;
    case 'I':
      /* Enter Method*/
      {
	struct methodchain* methodchain=(struct methodchain *) calloc(1,sizeof(struct methodchain));
	char classname[600], methodname[600],signature[600];
	int isStatic;
	sscanf(line,"IM: %s %s %s %d", classname, methodname, signature, &isStatic);
	methodchain->method=getmethod(namer, classname, methodname, signature);
	methodchain->caller=methodstack;
	if (!gencontains(statictable, methodchain->method)) {
	    int * staticflag=(int *)malloc(sizeof (int));
	    *staticflag=isStatic;
	    genputtable(statictable, methodchain->method, staticflag);
	}

	if (methodstack!=NULL) {
	  if (!gencontains(calltable, methodchain->method)) {
	    struct methodchain *mc=(struct methodchain *) calloc(1,sizeof(struct methodchain));
	    mc->method=methodstack->method;
	    genputtable(calltable, methodchain->method, mc);
	  } else {
	    struct methodchain *tosearch=(struct methodchain *)gengettable(calltable, methodchain->method);
	    while(tosearch->method!=methodstack->method) {
	      if (tosearch->caller==NULL) {
		struct methodchain *mc=(struct methodchain *) calloc(1,sizeof(struct methodchain));
		mc->method=methodstack->method;
		tosearch->caller=mc;
		break;
	      }
	      tosearch=tosearch->caller;
	    }
	  }
	}
	methodstack=methodchain;
      }

      break;
    case 'R':
      /* Return from method */
      {
	struct methodchain* caller=methodstack->caller;
        free(methodstack);
        methodstack=caller;
      }
      break;
    case 'F':
      /* Field Assignment */
      {
	long long suid;
	long long duid;
	char classname[1000];
	char fieldname[1000];
	char descname[1000];
	sscanf(line,"FA: %lld %s %s %s %lld", &suid, classname, fieldname, descname, &duid);
	getfield(namer, classname, fieldname, descname);
      }
      break;
    case 'A':
      /* Array Assignment */
      {
      	long long suid;
	long long duid;
	long index;
	sscanf(line,"AA: %lld %ld %lld", &suid, &index, &duid);
      }
      break;
    }
    free(line);
  }
}
Exemple #12
0
Tup::Tup(string filename, int home, int venue, int gamma) : home_restriction_(home), venue_restriction_(venue), gamma_(gamma)
{
	//read the config file and create parameters
	ifstream infile(filename);
	string line;
	int rowCounter = 0; //for counting the rows of distance and schedule matrix
	while (getline(infile, line)) {
		if(line[0]  == 'n') {
			//reading the number of teams
			try {
				string to_parse_nteams = line.substr(7,string::npos);
				number_of_teams_ = stoi(to_parse_nteams, nullptr);
				number_of_umpires_ = number_of_teams_ / 2;
				number_of_slots_ = 2*number_of_teams_ - 2;
				//resize matrix of distances and schedule
				teams_schedule_.resize(extents[number_of_umpires_][number_of_slots_]);
				distance_matrix_.resize(extents[number_of_teams_][number_of_teams_]);
				inverted_distance_matrix_.resize(extents[number_of_teams_][number_of_teams_]);
			} catch (const invalid_argument& ia) {
				infile.close();
				throw "Error reading instance file: the number of teams is not a number";
			}
		}
			if(line[0] == '[') {
				//reading a row from the distance matrix
				tokenizer<> tok(line);
				int columnCounter = 0;
				for(auto elem = tok.begin(); elem != tok.end(); ++elem) {
					try {
						distance_matrix_[rowCounter][columnCounter] = stod(*elem, nullptr);
						inverted_distance_matrix_[rowCounter][columnCounter] =
						    1/distance_matrix_[rowCounter][columnCounter];
						columnCounter++;
					} catch (const invalid_argument& ia) {
						throw "Error reading distance matrix: the number in row " + to_string(rowCounter) +
						          " and column " + to_string(columnCounter) + " is not a number";
					}
				}
				rowCounter = (rowCounter == number_of_teams_ -1) ? 0 : rowCounter + 1;
			} if(line[3] == '[') {
				//reading a row from opponents matrix
				int columnCounter = 0; //OBS: in this time column gets inverted with row!
				string buf;
				stringstream ss(line);
				vector<string> tok;
				while (ss >> buf)
					tok.push_back(buf);
				vector<int> lecture;
				vector<bool> readed;
				for(auto elem = tok.begin(); elem != tok.end(); ++elem) {
					try {
						if((*elem).compare(tok.front()) == 0)
							lecture.push_back(stoi((*elem).substr(1), nullptr));
						else
							lecture.push_back(stoi(*elem, nullptr));
						readed.push_back(false);
					} catch (const invalid_argument& ia) {
						throw "Error reading opponents matrix: the number in row " + to_string(columnCounter) +
						          " and column " + to_string(rowCounter) + " is not a number";
					}
				}
				//insert into the opponents matrix
				for(std::vector<int>::size_type i = 0; i != lecture.size(); i++) {
					int local = (lecture[i] > 0) ? i+1 : abs(lecture[i]);
					int visit = (lecture[i] < 0) ? i+1 : lecture[i];
					if(!readed[visit-1] && !readed[local-1]){					
						Game game {local, visit, columnCounter};
						teams_schedule_[columnCounter][rowCounter] = game;
						readed[visit-1] = readed[local-1] = true;
						columnCounter++;
					}
				}
				rowCounter++;
		}
	}
Exemple #13
0
static std::string loadFromTextFile(std::istream &is) 
{
	std::string temp;
	getline(is, temp);
	return temp;
}
/**
*
* This function Loads a .ini file 
*
* @Author Carsten Scholz
* @param _pcFilename is the name of the file which will be loading
* @This function goes through the file and scans through it looking at the sections and the values.
* 
*/
bool
CINIParser::LoadIniFile(const char* _pcFilename)
{
	bool bResult = true;

	//Our string values for section and the current line.
	std::string strLine;
	std::string strSection;
	std::string strMapKey;

	//Setting the filename string
	std::string strFilename(_pcFilename);
	m_strFilename = strFilename + ".ini";;
 
	m_filestream.open(m_strFilename.c_str());

	if (m_filestream.is_open())
	{
		while (!m_filestream.eof())
		{
			bool bReadingKey = true;
			std::string strKey = "";
			std::string strValue = "";

			getline( m_filestream, strLine );

			if (strLine[0] == ';')
			{
				//this line is a comment, ignore it
				continue;
			}
			else if (strLine[0] == '[')
			{
				//Its a section, read it
				strSection = "";

				for(int32 i = 0; i < static_cast<int32>(strLine.size()); ++i)
				{
					if (strLine[i] == '[' || strLine[i] == ']')
					{
						continue;
					}
					
					strSection += strLine[i];
				}

				continue;
			}
			else if (strLine.empty())
			{
				//string is empty, read next line
				continue;
			}

			for(int32 i = 0; i < static_cast<int32>(strLine.size()); ++i)
			{
				if (strLine[i] == ' ')
				{
					continue;
				}
				else if (strLine[i] == '"')
				{
					continue;
				}
				else if (strLine[i] == '=')
				{
					bReadingKey = false;
					continue;
				}
				else if (strLine[i] == ';')
				{
					//the rest of the line are comments
					break;
				}

				if (bReadingKey)
				{
					strKey += strLine[i];
				}
				else
				{
					strValue += strLine[i];
				}
			
			}
			strMapKey = strSection + "|" + strKey;
			m_mapPairs[strMapKey] = strValue;

		}

		m_filestream.close();
	}
	else
	{
		bResult = false;
	}

	return (bResult);
}
Exemple #15
0
bool load_files( Dot &thisDot, Uint32 &bg )
{
    //Load the dot image
    dot = load_image( "dot.png" );

    //If there was a problem in loading the dot
    if( dot == NULL )
    {
        return false;
    }

    //Open a file for reading
    std::ifstream load( "game_save" );

    //If the file loaded
    if( load != NULL )
    {
        //The offset
        int offset;

        //The level name
        std::string level;

        //Set the x offset
        load >> offset;
        thisDot.set_x( offset );

        //Set the y offset
        load >> offset;
        thisDot.set_y( offset );

        //If the x offset is invalid
        if( ( thisDot.get_x() < 0 ) || ( thisDot.get_x() > SCREEN_WIDTH - DOT_WIDTH ) )
        {
            return false;
        }

        //If the y offset is invalid
        if( ( thisDot.get_y() < 0 ) || ( thisDot.get_y() > SCREEN_HEIGHT - DOT_HEIGHT ) )
        {
            return false;
        }

        //Skip past the end of the line
        load.ignore();

        //Get the next line
        getline( load, level );

        //If an error occurred while trying to read the data
        if( load.fail() == true )
        {
            return false;
        }

        //If the level was white
        if( level == "White Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF );
        }
        //If the level was red
        else if( level == "Red Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 );
        }
        //If the level was green
        else if( level == "Green Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0x00, 0xFF, 0x00 );
        }
        //If the level was blue
        else if( level == "Blue Level" )
        {
            //Set the background color
            bg = SDL_MapRGB( screen->format, 0x00, 0x00, 0xFF );
        }

        //Close the file
        load.close();
    }
void makecache(unsigned char flag){
	int cache = open(CACHE_FILE, O_CREAT|O_RDWR, 00644);
	long offset = 0;
	int tmp = 0;
	if(cache < 0){
		printf("\nНе могу создать кеш-файл\n");
		exit(7);
	}
	if(flag == UPDATE){
		if((tmp = lseek(cache, -sizeof(s_cache), SEEK_END)) > 0){
			if(read(cache, &s_cache, sizeof(s_cache)) > 0){
				offset = s_cache.offset;
			}
		}
	}
	size_t len = 0;
	char *string = NULL;
	struct stat filestat;
	FILE *log = fopen(LOG_FILE, "r");
	if(!log){
		printf("\nНе могу открыть " LOG_FILE " \n");
		exit(8);
	}
	printf("\nСоздаю кеш\n");
	if(stat(LOG_FILE, &filestat) != 0){
		printf("\nОшибка, " LOG_FILE ": не могу сделать stat\n");
		exit(10);
	}
	if(fseek(log, offset, SEEK_SET) != 0){
		printf("\nВнимание: " LOG_FILE " устарел, обновляю кеш полностью\n");
		offset = 0;
	}
	s_cache.offset = offset;
	if(getline(&string, &len, log) < 1){
		printf("\nОшибка: " LOG_FILE "пуст\n");
		exit(9);
	}
	long dataportion = filestat.st_size / 100;
	int indpos = 1;
	int frac = 0;
	if(offset > 0) frac = atoi(string) / TIME_INTERVAL;
	do{
		
		if( ( tmp = ((s_cache.time = atoi(string)) / TIME_INTERVAL)) != frac ){
			write(cache, &s_cache, sizeof(s_cache));
#ifdef DEBUG
			printf("очередная строка, время: %d, смещение: %d\n", s_cache.time, s_cache.offset);
#endif
			frac = tmp;
		}
		s_cache.offset = ftell(log);
		if( (tmp = s_cache.offset / dataportion) > indpos ){
			if( (tmp % 10) == 0) printf(" %d%% ", tmp);
			else printf(".");
			indpos = tmp;
			fflush(stdout);
		}
	} while(getline(&string, &len, log) > 0);
	printf("\nГотово!\n");
	free(string);
	close(cache);
	fclose(log);
}
void indexEngine::indexing(const std::string& corpus_pth)
{
    ifstream ifOrigin_data; //file stream to load data from corpus
    ifOrigin_data.open(corpus_pth.c_str());
    if(!ifOrigin_data.is_open())
        //std::cerr << "Open corpus files failed!" << std::endl;
        LOG(ERROR) << "Open indexing corpus files error!";

    std::string sLine = "";
    std::string sData = "";
    vector<string> splitDat;

    while(getline(ifOrigin_data,sLine))
    {
        splitDat.clear();

        //normalize
        Normalize::normalize(sLine);

        //get detail data from original corpus
        for(unsigned int i = 0; i < 3; ++i)
        {
            std::size_t pos = sLine.find('\t');
            if(std::string::npos != pos)
            {
                sData = sLine.substr(0,pos);
                sLine.assign(sLine,pos+1,sLine.length()-1);
                splitDat.push_back(sData);
            }
        }
        splitDat.push_back(sLine);

        //check data
        if(4 != splitDat.size())
            continue;
        QueryData qDat;
        qDat.text = splitDat[0]; //query text
        qDat.hits = atoi(splitDat[2].c_str()); //query times
        qDat.counts = atoi(splitDat[3].c_str());//results numbers

        //get term id
        StrToIntMap termsVector;
        tokenTerms(qDat.text,termsVector);

        std::size_t queryID = izenelib::util::izene_hashing(qDat.text);
        vector<std::size_t> queryIdVector;
        vector<std::size_t> termsIdVector;

        IdToQListIter termsQueryIter;
        queryIdVector.push_back(queryID);
        StrToIntMapIter termsIter;
        //assign hash id for every terms
        for(termsIter = termsVector.begin(); termsIter != termsVector.end(); ++termsIter)
        {
            termsIdVector.push_back(termsIter->second);
            //find terms in dictornary,termsID.v
            termsQueryIter = terms2qIDs_.find(termsIter->second);
            if(termsQueryIter != terms2qIDs_.end())
            {
                termsQueryIter->second.push_back(queryID);
            }
            else
            {
                terms2qIDs_.insert(make_pair(termsIter->second,queryIdVector));
            }
        }

        //complete data for one query
        qDat.tid = termsIdVector;
        boost::unordered_map<std::size_t,QueryData>::iterator queryIter;
        queryIdata_.insert(make_pair(queryID,qDat));

        //flush to disk file
        /* ofQueryDat << queryID << "\t" <<qDat.text << "\t" << qDat.hits
           << "\t" << qDat.counts;
         for(unsigned int i = 0; i < qDat.tid.size(); ++i)
         {
        	ofQueryDat << "\t" << qDat.tid[i];
        	//cout << "terms id :" << qDat.tid[i] << ",";
         }
         ofQueryDat << std::endl;


        //merge the searching times
        boost::unordered_map<std::size_t,QueryData>::iterator queryIter;
        queryIter = queryIdata_.find(queryID);
        if(queryIdata_.end() != queryIter && queryIter->second.text == qDat.text)
        {
        	std::cout << "queryIter->seoncd.text:" << queryIter->second.text <<
        		"\t qDat.text:" << qDat.text << std::endl;
        	std::cout << "queryID:" << queryID << "\tqueryIter->first:" << queryIter->first << std::endl;
        	std::cout << "\t text:" << qDat.text << std::endl;
        	queryIter->second.hits += qDat.hits;
        	std::cout  << "test--hits:" << queryIter->second.hits << "\t qdat.hits:" << qDat.hits << std::endl;
        }
        else
        	queryIdata_.insert(make_pair(queryID,qDat));
        queryIdata_.insert(make_pair(queryID,qDat));*/
    }
    ifOrigin_data.close();//file stream close
    //ofQueryDat.close();
}
Exemple #18
0
// shows a prompt to the user, gets an input line,
// calls parseCommand, then determines and calls
// the appropriate function for the command
void promptUser() {

    for (;;) {
        char *line = NULL;
        char **argv = NULL;
        size_t line_size = 0;
        size_t n = 0;

        printf("prompt>");

        getline(&line, &line_size, stdin);
        int argc = parseCommand(line, &n, &argv);
        free(line);
        // printf("%d tokens parsed\n", argc);
        if (argc > 0) {
            // int i;
            // for (i = 0 ; i < argc ; i++) {
            //     printf("argv %d: %s\n", i, argv[i]);
            // }
            if (!strcmp(argv[0], "allocate")) {
                // first check that there's an appropriate number of arguments
                if (argc == 2) {
                    // int allocate(int number_of_bytes)
                    int number_of_bytes = atoi(argv[1]);
                    if (number_of_bytes > 0)
                        allocate(number_of_bytes);
                    else
                        printf("Cannot allocate < 1 byte.\n");
                }
                else {
                    printf("Usage: allocate [number_of_bytes]\n");
                }
            }
            else if (!strcmp(argv[0], "free")) {
                if (argc == 2) {
                    // void deallocate(int block_number)
                    int block_number = atoi(argv[1]);
                    if (block_number > 0)
                        deallocate(block_number);
                    else
                        printf("Block number can not be less than 1.\n");
                }
                else {
                    printf("Usage: free [block_number]\n");
                }
            }
            else if (!strcmp(argv[0], "blocklist")) {
                // void blocklist()
                blocklist();
            }
            else if (!strcmp(argv[0], "writeheap")) {
                if (argc == 4) {
                    // void writeheap(int the_block_number, char CTW, int copies)
                    int the_block_number = atoi(argv[1]);
                    char CTW = argv[2][0];
                    int copies = atoi(argv[3]);
                    if ( (the_block_number > 0) && (copies > 0) )
                        writeheap(the_block_number, CTW, copies);
                    else if (the_block_number < 1)
                        printf("Block number can not be less than 1.\n");
                    else
                        printf("Must write at least 1 byte.\n");
                }
                else {
                    printf("Usage: writeheap [block_number] [char] [amount]\n");
                }
            }
            else if (!strcmp(argv[0], "printheap")) {
                if (argc == 3) {
                    // void printheap(int block_number, int number_of_bytes)
                    int block_number = atoi(argv[1]);
                    int number_of_bytes = atoi(argv[2]);
                    if ( (block_number > 0) && (number_of_bytes) > 0)
                        printheap(block_number, number_of_bytes);
                    else if (block_number < 1)
                        printf("Block number can not be less than 1.\n");
                    else
                        printf("Must read at least 1 byte.\n");
                }
                else {
                    printf("Usage: printheap [block_number] [amount]\n");
                }
            }
            else if (!strcmp(argv[0], "quit")) {
                // void quit()
                quit();
                break;
            }
            else {
                printf("Invalid Command\n");
            }
        }
        if (argc != -1) {
            int i;
            for (i = 0 ; i < argc ; i++) {
                free(argv[i]);
            }
        }
        free(argv);
    }
}
int main(int argc, char** argv){
	// Get arguments
	if (argc==1) {
		print_usage(argv[0]);
		return -1;
	}
	init_args();
	int result;
	while((result=getopt(argc,argv,"hc:i:o:"))!=-1){
		switch(result){
			/* INPUTS */
			case 'c':
				strcpy(m_config_file,optarg);
				printf("config file: %s\n",m_config_file);
				break;
			case 'i':
				strcpy(m_input_file,optarg);
				printf("input file: %s\n",m_input_file);
				break;
			case 'o':
				strcpy(m_output_file,optarg);
				printf("output file: %s\n",m_output_file);
				break;
			case '?':
				printf("Wrong option! optopt=%c, optarg=%s\n", optopt, optarg);
				break;
			case 'h':
			default:
				print_usage(argv[0]);
				return 1;
		}
	}

	// wire info
	std::vector<std::string> m_layer_name;
	std::vector<double> m_layer_radius;
	std::vector<std::string> m_layer_nWire;
	std::vector<std::string> m_layer_nWire_string;
	std::vector<std::vector<std::string> > m_layer_nWire_paras;
	std::vector<std::string> m_layer_phi;
	std::vector<std::string> m_layer_phi_string;
	std::vector<std::vector<std::string> > m_layer_phi_paras;

	std::stringstream buf;
	std::string temp;
	// Read from configure file
	std::ifstream fin_config(m_config_file);
	if(!fin_config){
		std::cout<<"Cannot open "<<m_config_file<<" in ifstream format"<<std::endl;
		return -1;
	}
	while(getline(fin_config,temp)){
		buf.str("");
		buf.clear();
		buf<<temp;
		double value;
		std::string identifier;
		std::string name;
		std::string trash;
		buf>>identifier>>name>>trash>>value;
		m_para_value.push_back(value);
		m_para_identifier.push_back(identifier);
		m_para_name.push_back(name);
	}

	// Read from input file
	std::ifstream fin_input(m_input_file);
	if(!fin_input){
		std::cout<<"Cannot open "<<m_input_file<<" in ifstream format"<<std::endl;
		return -1;
	}
	bool inWireLines = false;
	while(getline(fin_input,temp)){
		//std::cout<<temp<<std::endl;
		buf.str("");
		buf.clear();
		buf<<temp;
		double double1,double2;
		std::string seg1,seg2,seg3,seg4;
		std::string trash;
		buf>>seg1>>seg2;
		bool isParameter = false;
		int i_para = -1;
		for ( int i = 0; i < m_para_value.size(); i++ ){
			//std::cout<<"m_para_identifier["<<i<<"] = "<<m_para_identifier[i]<<", m_para_name["<<i<<"] = "<<m_para_name[i]<<std::endl;
			if ( seg1 == m_para_identifier[i] && seg2 == m_para_name[i] ){
				isParameter = true;
				i_para = i;
				break;
			}
		}
		if (isParameter){
			buf>>trash>>m_para_value[i_para];
			continue;
		}
		else if ( seg1 == "ROWS" && seg2 == "Polar" ) {
			inWireLines = true;
			continue;
		}
		if ( inWireLines ){
			if (seg1.size()==0){
				inWireLines = false;
				break;
			}
			else{
				buf>>trash>>double1>>seg3;
				m_layer_name.push_back(seg1);
				m_layer_radius.push_back(double1);
				std::vector<std::string> vec;
				vec.clear();
				m_layer_nWire_string.push_back(seg2);
				m_layer_nWire.push_back("");
				m_layer_nWire_paras.push_back(vec);
				get_formula(seg2,m_layer_nWire[m_layer_nWire.size()-1],m_layer_nWire_paras[m_layer_nWire_paras.size()-1]);
				m_layer_phi_string.push_back(seg3);
				m_layer_phi.push_back("");
				m_layer_phi_paras.push_back(vec);
				get_formula(seg3,m_layer_phi[m_layer_phi.size()-1],m_layer_phi_paras[m_layer_phi_paras.size()-1]);
			}
		}
	}
Exemple #20
0
//Basic Init, create the font, backbuffer, etc
WINDOW *initscr(void)
{
   // _windows = new WINDOW[20];         //initialize all of our variables
    BITMAPINFO bmi;
    lastchar=-1;
    inputdelay=-1;
    std::string typeface;
char * typeface_c;
std::ifstream fin;
fin.open("data\\FONTDATA");
 if (!fin.is_open()){
     MessageBox(WindowHandle, "Failed to open FONTDATA, loading defaults.",
                NULL, NULL);
     fontheight=16;
     fontwidth=8;
 } else {
     getline(fin, typeface);
     typeface_c= new char [typeface.size()+1];
     strcpy (typeface_c, typeface.c_str());
     fin >> fontwidth;
     fin >> fontheight;
     if ((fontwidth <= 4) || (fontheight <=4)){
         MessageBox(WindowHandle, "Invalid font size specified!",
                    NULL, NULL);
        fontheight=16;
        fontwidth=8;
     }
 }
    halfwidth=fontwidth / 2;
    halfheight=fontheight / 2;
    WindowWidth=80*fontwidth;
    WindowHeight=25*fontheight;
    WindowX=(GetSystemMetrics(SM_CXSCREEN) / 2)-WindowWidth/2;    //center this
    WindowY=(GetSystemMetrics(SM_CYSCREEN) / 2)-WindowHeight/2;   //sucker
    WinCreate();    //Create the actual window, register it, etc
    CheckMessages();    //Let the message queue handle setting up the window
    WindowDC = GetDC(WindowHandle);
    backbuffer = CreateCompatibleDC(WindowDC);
    ZeroMemory(&bmi, sizeof(BITMAPINFO));
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = WindowWidth;
    bmi.bmiHeader.biHeight = -WindowHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount=8;
    bmi.bmiHeader.biCompression = BI_RGB;   //store it in uncompressed bytes
    bmi.bmiHeader.biSizeImage = WindowWidth * WindowHeight * 1;
    bmi.bmiHeader.biClrUsed=16;         //the number of colors in our palette
    bmi.bmiHeader.biClrImportant=16;    //the number of colors in our palette
    backbit = CreateDIBSection(0, &bmi, DIB_RGB_COLORS, (void**)&dcbits, NULL, 0);
    DeleteObject(SelectObject(backbuffer, backbit));//load the buffer into DC

 int nResults = AddFontResourceExA("data\\termfont",FR_PRIVATE,NULL);
   if (nResults>0){
    font = CreateFont(fontheight, fontwidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
                      ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                      PROOF_QUALITY, FF_MODERN, typeface_c);   //Create our font

  } else {
      MessageBox(WindowHandle, "Failed to load default font, using FixedSys.",
                NULL, NULL);
       font = CreateFont(fontheight, fontwidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
                      ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                      PROOF_QUALITY, FF_MODERN, "FixedSys");   //Create our font
   }
    //FixedSys will be user-changable at some point in time??
    SetBkMode(backbuffer, TRANSPARENT);//Transparent font backgrounds
    SelectObject(backbuffer, font);//Load our font into the DC
//    WindowCount=0;

    delete typeface_c;
    mainwin = newwin(25,80,0,0);
    return mainwin;   //create the 'stdscr' window and return its ref
};
Exemple #21
0
Table* read_from_userprofile() {
	ifstream myfile ("userprofile.csv");
  if (myfile.is_open())
  {

	Table::ColumnList columns;

	String userID;
	getline( myfile, userID, ',' );
    columns.push_back(make_pair(userID, Table::varchar));

	String latitude;
	getline( myfile, latitude, ',' );
	columns.push_back(make_pair(latitude, Table::floating));

	String longitude;
	getline( myfile, longitude, ',' );
	columns.push_back(make_pair(longitude, Table::integer));

	String smoker;
	getline( myfile, smoker, ',');
    columns.push_back(make_pair(smoker, Table::varchar));

	String drink_level;
	getline( myfile, drink_level, ',');
    columns.push_back(make_pair(drink_level, Table::varchar));

	String dress_preference;
	getline( myfile, dress_preference, ',' );
    columns.push_back(make_pair(dress_preference, Table::varchar));

	String ambience;
	getline( myfile, ambience, ',' );
    columns.push_back(make_pair(ambience, Table::varchar));

	String transport;
	getline( myfile, transport, ',' );
    columns.push_back(make_pair(transport, Table::varchar));

	String marital_status;
	getline( myfile, marital_status, ',' );
    columns.push_back(make_pair(marital_status, Table::varchar));

	String hijos;
	getline( myfile, hijos, ',');
    columns.push_back(make_pair(hijos, Table::varchar));

	String birth_year;
	getline( myfile, birth_year, ',' );
    columns.push_back(make_pair(birth_year, Table::integer));

		String interest;
	getline( myfile, interest, ',' );
    columns.push_back(make_pair(interest, Table::varchar));

		String personality;
	getline( myfile, personality, ',' );
    columns.push_back(make_pair(personality, Table::varchar));

		String religion;
	getline( myfile, religion, ',' );
    columns.push_back(make_pair(religion, Table::varchar));

		String activity;
	getline( myfile, activity, ',' );
    columns.push_back(make_pair(activity, Table::varchar));

		String color;
	getline( myfile, color, ',' );
    columns.push_back(make_pair(color, Table::varchar));

		String weight;
	getline( myfile, weight, ',' );
    columns.push_back(make_pair(weight, Table::integer));

		String budget;
	getline( myfile, budget, ',' );
    columns.push_back(make_pair(budget, Table::varchar));

		String height;
	getline( myfile, height, '\n' );
	columns.push_back(make_pair(height, Table::floating));


	Table* geo = new Table(columns); // Create geo using the defined columns
	


    while ( myfile.good() )
    {

	  vector<pair<string, string> > entries; // Entries for the record to be placed in the table
	  
	  string userID;
	  getline( myfile, userID, ',' );
	  pair <string,string> pair0  ("userID",userID);   // value init
	  entries.push_back(pair0);

	  string latitude;
	  getline( myfile, latitude, ',' );
	  pair <string,string> pair1  ("latitude",latitude);   // value init
	  entries.push_back(pair1);

	  string longitude;
	  getline( myfile, longitude, ',' );
	  pair <string,string> pair2  ("longitude",longitude);   // value init
	  entries.push_back(pair2);

	  string smoker;
	  getline( myfile, smoker, ',' );
	  pair <string,string> pair3  ("smoker",smoker);   // value init
	  entries.push_back(pair3);

	  string drink_level;
	  getline( myfile, drink_level, ',' );
	  pair <string,string> pair4  ("drink_level",drink_level);   // value init
	  entries.push_back(pair4);

	  string dress_preference;
	  getline( myfile, dress_preference, ',' );
	  pair <string,string> pair5  ("dress_preference",dress_preference);   // value init
	  entries.push_back(pair5);

	  string ambience;
	  getline( myfile, ambience, ',' );
	  pair <string,string> pair6  ("ambience",ambience);   // value init
	  entries.push_back(pair6);

	  string transport;
	  getline( myfile, transport, ',' );
	  pair <string,string> pair7  ("transport",transport);   // value init
	  entries.push_back(pair7);

	  string marital_status;
	  getline( myfile, marital_status, ',' );
	  pair <string,string> pair8  ("marital_status",marital_status);   // value init
	  entries.push_back(pair8);

	  string hijos;
	  getline( myfile, hijos, ',' );
	  pair <string,string> pair9  ("hijos",hijos);   // value init
	  entries.push_back(pair9);

	  string birth_year;
	  getline( myfile, birth_year, ',' );
	  pair <string,string> pair10  ("birth_year",birth_year);   // value init
	  entries.push_back(pair10);

	  string interest;
	  getline( myfile, interest, ',' );
	  pair <string,string> pair11  ("interest",interest);   // value init
	  entries.push_back(pair11);

	  string personality;
	  getline( myfile, personality, ',' );
	  pair <string,string> pair12  ("personality",personality);   // value init
	  entries.push_back(pair12);

	  string religion;
	  getline( myfile, religion, ',' );
	  pair <string,string> pair13  ("religion",religion);   // value init
	  entries.push_back(pair13);

	  string activity;
	  getline( myfile, activity, ',' );
	  pair <string,string> pair14  ("activity",activity);   // value init
	  entries.push_back(pair14);

	  string color;
	  getline( myfile, color, ',' );
	  pair <string,string> pair15 ("color",color);   // value init
	  entries.push_back(pair15);

	  string weight;
	  getline( myfile, weight, ',' );
	  pair <string,string> pair16  ("weight",weight);   // value init
	  entries.push_back(pair16);

	  string budget;
	  getline( myfile, budget, ',' );
	  pair <string,string> pair17  ("budget",budget);   // value init
	  entries.push_back(pair17);

	  string height;
	  getline( myfile, height, '\n' );
	  pair <string,string> pair18  ("height",height);   // value init
	  entries.push_back(pair18);



	  Record add(entries); // Create record to add to table

	  geo->insert(add); // Insert add record into geo
	  
	}
    myfile.close();
	return geo;
  }
}
/** The method parse the configfile for attributes and 
 * radius server, the attributes are copied to the
 * member variables.
 * @param configfile The name of the configfile.
 * @return An integer, 0 if everything is ok 
 * or PARSING_ERROR or BAD_FILE if something is wrong.*/
int Config::parseConfigFile(const char * configfile)
{
	string line;
	
	ifstream file;
	file.open(configfile, ios::in);
	if (file.is_open())
	{
		while (file.eof()==false)
		{
			getline(file,line);
			this->deletechars(&line);
			if(line.empty()==false)
			{
				if (strncmp(line.c_str(),"subnet=",7)==0)
				{
					if((line.size()-7)>15)
					{
						return BAD_FILE;
					}
					line.copy(this->subnet,line.size()-7,7);
					
				}
				if (strncmp(line.c_str(),"p2p=",4)==0)
				{
					if((line.size()-4)>15)
					{
						return BAD_FILE;
					}
					line.copy(this->p2p,line.size()-4,4);
				}
				if (strncmp(line.c_str(),"p2p6=",5)==0)
				{
					if((line.size()-5)>39)
					{
						return BAD_FILE;
					}
					line.copy(this->p2p6,line.size()-5,5);
				}
				if (strncmp(line.c_str(),"vsascript=",10)==0)
				{
					this->vsascript=line.substr(10,line.size()-10);
				}
				if (strncmp(line.c_str(),"vsanamedpipe=",13)==0)
				{
					this->vsanamedpipe=line.substr(13,line.size()-13);
				}
							
				if (strncmp(line.c_str(),"OpenVPNConfig=",14)==0)
				{
					this->openvpnconfig=line.substr(14,line.size()-14);
				}
				if (strncmp(line.c_str(),"overwriteccfiles=",17)==0)
				{
					
					string stmp=line.substr(17,line.size()-17);
					deletechars(&stmp);
					if(stmp == "true") this->overwriteccfiles=true;
					else if (stmp =="false") this->overwriteccfiles=false;
					else return BAD_FILE;
						
				}
                                if (strncmp(line.c_str(),"useauthcontrolfile=",19)==0)
				{
					
					string stmp=line.substr(19,line.size()-19);
					deletechars(&stmp);
					if(stmp == "true") this->useauthcontrolfile=true;
					else if (stmp =="false") this->useauthcontrolfile=false;
					else return BAD_FILE;
						
				}
                                if (strncmp(line.c_str(),"useclientconnectdeferfile=",26)==0)
				{

					string stmp=line.substr(26,line.size()-26);
					deletechars(&stmp);
					if(stmp == "true") this->useclientconnectdeferfile=true;
					else if (stmp =="false") this->useclientconnectdeferfile=false;
					else return BAD_FILE;

				}
				if (strncmp(line.c_str(),"accountingonly=",15)==0)
				{
					
					string stmp=line.substr(15,line.size()-15);
					deletechars(&stmp);
					if(stmp == "true") this->accountingonly=true;
					else if (stmp =="false") this->accountingonly=false;
					else return BAD_FILE;
						
				}
				if (strncmp(line.c_str(),"nonfatalaccounting=",19)==0)
				{
					
					string stmp=line.substr(19,line.size()-19);
					deletechars(&stmp);
					if(stmp == "true") this->nonfatalaccounting=true;
					else if (stmp =="false") this->nonfatalaccounting=false;
					else return BAD_FILE;
						
				}
				if (strncmp(line.c_str(),"defacctinteriminterval=",23)==0)
				{

					string stmp=line.substr(23,line.size()-23);
					deletechars(&stmp);
					char *stemp;
					long defacctinteriminterval = 0;
					defacctinteriminterval = strtol(stmp.c_str(), &stemp, 0);
					if (stemp == stmp.c_str() || *stemp != '\0' || defacctinteriminterval < 0)
						return BAD_FILE;
					this->defacctinteriminterval=(int)defacctinteriminterval;
				}
			}
			
		}
		file.close();
		// if the main files contains references to other config files
		// we don't need to care about recursive includes, OpenVPN does it already
		list<string> configfiles; 
		configfiles.push_back(this->openvpnconfig);
		//open OpenVPN config
		while(configfiles.size() > 0)
		{
		  ifstream file2;
		  string filename=configfiles.front();
		  file2.open(filename.c_str(), ios::in);
		  char const* delims = " \t\r\n\0";
		  if (file2.is_open())
		  {
			  while(file2.eof()==false)
			  {
				  getline(file2,line);
				  
				  if(line.empty()==false)
				  {
					  string param=line;
					  // trim leading whitespace
					  string::size_type  pos = param.find_first_not_of(delims);
					  if (pos != string::npos) param.erase(0,pos );
					  pos=param.find_first_of(delims);
					  if (pos != string::npos) param.erase(pos);
					  if (param == "client-cert-not-required")
					  {
						  this->deletechars(&line);
						  if (line == "client-cert-not-required")
						  {
							  this->clientcertnotrequired=true;
						  }
					  }
					  if (param == "username-as-common-name")
					  {
						  this->deletechars(&line);
						  if (line == "username-as-common-name")
						  {
							  this->usernameascommonname=true;
						  }
					  }
					  if (param == "client-config-dir")
					  {
						  this->deletechars(&line);
						  line.erase(0, 17);
						  this->setCcdPath(line);
					  }
					  if (param == "config")
					  {
						  this->deletechars(&line);
						  line.erase(0, 6);
						  configfiles.push_back(line);
					  }
					  if (param == "status")
					  {
						  //method deletechars don't work, entry has formet: status <file> [time]
						  pos  = line.find_first_of("#");
						  if (pos != string::npos) 
						  {
							  line.erase(pos);
						  }
						  // trim leading whitespace
						  pos = line.find_first_not_of(delims);
						  if (pos != string::npos) line.erase(0,pos);
						  line.erase(0, 6);
						  // trim leading whitespace again
						  pos = line.find_first_not_of(" \t");
						  if (pos != string::npos) line.erase(0,pos);
						  //delete the trailing version of status if there
						  pos = line.find_first_of(delims);
						  if (pos != string::npos) line.erase(pos);
						  this->deletechars(&line);
						  if(!line.empty())
						  {
							    
						    this->statusfile=line;
						  }
					  }	
				  }
			  }
			  file.close();
			  configfiles.remove(filename);
		  }
		  else
		  {
			  return BAD_FILE;
		  }
		}
	}
	else
	{
		return BAD_FILE;
	}
	return 0;
}
Exemple #23
0
Scraper::Scraper(const char* scriptDir)
    : scriptPath(scriptDir), imageDataPath(scriptDir)
{
    imageDataPath.append("\\ImageFiles.data");

    // Read image data
    // Open file
    ifstream f (imageDataPath);
    if (!f.is_open())
    {
        char err[500];
        strerror_s(err, 500, errno);

        string s("Error opening " + imageDataPath + ": " + err);

        logger->WriteLog(s);
        return;
    }

    // Read the file
    ImageGroup newImageGroup;

    string line("");
    while (getline(f, line))
    {
        vector<string> elems;
        split(line, ' ', elems);

        // Comment
        if (line.empty() || elems[0].find("//") != string::npos)
        {
            continue;
        }

        // Find image class
        bool foundImageType = false;
        for (int i = 0; i < imageClassCount; i++)
        {
            if (elems[0].find(imageClasses[i])!=string::npos)
            {
                newImageGroup.iType = (imageType) i;
                foundImageType = true;
                break;
            }
        }

        // Found the right group?
        if (!foundImageType)
        {
            string s("Image group " + elems[0] + " is unrecognized");
            logger->WriteLog(s);
        }

        else
        {
            newImageGroup.imagePaths.clear();
            newImageGroup.mats.clear();
            newImageGroup.imagePaths.resize(elems.size()-1);
            newImageGroup.mats.resize(elems.size()-1);
            imageGroups.push_back(newImageGroup);
            int lastGroup = imageGroups.size()-1;

            // Create a Mat for each path in the group
            for (int i = 1; i < (int) elems.size(); i++)
            {
                imageGroups[lastGroup].imagePaths[i-1] = elems[i];

                string path(scriptPath + "\\Images\\" + elems[i]);
                wstring ws = utf8_decode(path);
                imageGroups[lastGroup].mats[i-1] = CGdiPlus::ImgRead(ws.c_str());
            }

            char s[500];
            sprintf_s(s, 500, "Loaded %d images for image group %s.", elems.size()-1, elems[0].c_str());
            logger->WriteLog(s);
        }
    }

    // Handle read error
    if (f.bad())
    {
        char err[500];
        strerror_s(err, 500, errno);

        string s("Error reading " + imageDataPath + ": " + err);

        logger->WriteLog(s);
    }

    f.close();

    logger->WriteLog("Images loaded");
}
void fullPedestalAnalysis(string inputDIR, string outputDIR, string inputCablingMap, string outputFileName){

  gROOT->ProcessLine("gErrorIgnoreLevel = 1");
  
  // open the file and prepare the cluster tree, adding the other trees as frined --> memory consuming                                                                                                
  std::cout<<"##################################"<<std::endl;
  std::cout<<"###### fullPedestalAnalysis ######"<<std::endl;
  std::cout<<"##################################"<<std::endl;

  clock_t tStart = clock();

  // prepare style and load macros                                                                                                                                                                    
  setTDRStyle();
  gROOT->SetBatch(kTRUE);

  system(("mkdir -p "+outputDIR).c_str());
  ifstream file;

  std::cout<<"### Make input file list"<<std::endl;
  system(("find "+inputDIR+" -name \"*.root\" > file.temp").c_str());
  std::ifstream infile;
  string line;
  vector<string> fileList;
  infile.open("file.temp",ifstream::in);
  if(infile.is_open()){
    while(!infile.eof()){
      getline(infile,line);
      if(line != "" and TString(line).Contains(".root") and line !="\n"){
        fileList.push_back(line);
      }
    }
  }
  system("rm file.temp");
  std::sort(fileList.begin(),fileList.end());

  TFile* cablingFile = TFile::Open(inputCablingMap.c_str(),"READ");
  cablingFile->cd();
  TTree* readoutMap = (TTree*) cablingFile->FindObjectAny("readoutMap");
  TTreeReader reader(readoutMap);
  TTreeReaderValue<uint32_t> detid    (reader,"detid");
  TTreeReaderValue<uint16_t> fecCrate (reader,"fecCrate");
  TTreeReaderValue<uint16_t> fecSlot  (reader,"fecSlot");
  TTreeReaderValue<uint16_t> fecRing  (reader,"fecRing");
  TTreeReaderValue<uint16_t> ccuAdd   (reader,"ccuAdd");
  TTreeReaderValue<uint16_t> ccuChan  (reader,"ccuChan");
  TTreeReaderValue<uint16_t> lldChannel  (reader,"lldChannel");
  TTreeReaderValue<uint16_t> fedId  (reader,"fedId");
  TTreeReaderValue<uint16_t> fedCh  (reader,"fedCh");

  // output tree
  TFile* ouputTreeFile = new TFile((outputDIR+"/"+outputFileName).c_str(),"RECREATE");
  ouputTreeFile->cd();
  ouputTreeFile->SetCompressionLevel(0);
  TTree* outputTree = new TTree("pedestalFullNoise","pedestalFullNoise");
  
  // branches
  uint32_t detid_,fedKey_;
  uint16_t fecCrate_,fecSlot_, fecRing_, ccuAdd_, ccuChan_, lldChannel_, fedId_, fedCh_, apvId_, stripId_;
  float    noiseMean_,noiseRMS_, noiseSkewness_, noiseKurtosis_;
  float    fitChi2_, fitChi2Probab_, fitStatus_;
  float    fitGausMean_, fitGausSigma_, fitGausNormalization_;
  float    fitGausMeanError_, fitGausSigmaError_, fitGausNormalizationError_;
  float    noiseIntegral3Sigma_, noiseIntegral3SigmaFromFit_;
  float    noiseIntegral4Sigma_, noiseIntegral4SigmaFromFit_;
  float    noiseIntegral5Sigma_, noiseIntegral5SigmaFromFit_;
  float    kSValue_, kSProbab_, jBValue_, jBProbab_, aDValue_, aDProbab_;
  vector<float> noiseDistribution_, noiseDistributionError_;
  float xMin_, xMax_, nBin_ ;

  outputTree->Branch("detid",&detid_,"detid/i");
  outputTree->Branch("fedKey",&fedKey_,"fedKey/i");
  outputTree->Branch("fecCrate",&fecCrate_,"fecCrate/s");
  outputTree->Branch("fecSlot",&fecSlot_,"fecSlot/s");
  outputTree->Branch("fecRing",&fecRing_,"fecRing/s");
  outputTree->Branch("ccuAdd",&ccuAdd_,"ccuAdd/s");
  outputTree->Branch("ccuChan",&ccuChan_,"ccuChan/s");
  outputTree->Branch("lldChannel",&lldChannel_,"lldChannel/s");
  outputTree->Branch("fedId",&fedId_,"fedId/s");
  outputTree->Branch("fedCh",&fedCh_,"fedCh/s");
  outputTree->Branch("apvId",&apvId_,"apvId/s");
  outputTree->Branch("stripId",&stripId_,"stripId/s");

  outputTree->Branch("noiseMean",&noiseMean_,"noiseMean/F");
  outputTree->Branch("noiseRMS",&noiseRMS_,"noiseRMS/F");
  outputTree->Branch("noiseSkewness",&noiseSkewness_,"noiseSkewness/F");
  outputTree->Branch("noiseKurtosis",&noiseKurtosis_,"noiseKurtosis/F");
  outputTree->Branch("fitGausNormalization",&fitGausNormalization_,"fitGausNormalization/F");
  outputTree->Branch("fitGausMean",&fitGausMean_,"fitGausMean/F");
  outputTree->Branch("fitGausSigma",&fitGausSigma_,"fitGausSigma/F");
  outputTree->Branch("fitGausNormalizationError",&fitGausNormalizationError_,"fitGausNormalizationError/F");
  outputTree->Branch("fitGausMeanError",&fitGausMeanError_,"fitGausMeanError/F");
  outputTree->Branch("fitGausSigmaError",&fitGausSigmaError_,"fitGausSigmaError/F");
  outputTree->Branch("fitChi2",&fitChi2_,"fitChi2/F");
  outputTree->Branch("fitChi2Probab",&fitChi2Probab_,"fitChi2Probab/F");
  outputTree->Branch("fitStatus",&fitStatus_,"fitStatus_F");
  outputTree->Branch("noiseIntegral3Sigma",&noiseIntegral3Sigma_,"noiseIntegral3Sigma/F");
  outputTree->Branch("noiseIntegral3SigmaFromFit",&noiseIntegral3SigmaFromFit_,"noiseIntegral3SigmaFromFit/F");
  outputTree->Branch("noiseIntegral4Sigma",&noiseIntegral4Sigma_,"noiseIntegral4Sigma/F");
  outputTree->Branch("noiseIntegral4SigmaFromFit",&noiseIntegral4SigmaFromFit_,"noiseIntegral4SigmaFromFit/F");
  outputTree->Branch("noiseIntegral5Sigma",&noiseIntegral4Sigma_,"noiseIntegral5Sigma/F");
  outputTree->Branch("noiseIntegral5SigmaFromFit",&noiseIntegral4SigmaFromFit_,"noiseIntegral5SigmaFromFit/F");
  outputTree->Branch("kSValue",&kSValue_,"kSValue/F");
  outputTree->Branch("jBValue",&jBValue_,"jBValue/F");
  outputTree->Branch("aDValue",&aDValue_,"aDValue/F");
  outputTree->Branch("kSProbab",&kSProbab_,"kSProbab/F");
  outputTree->Branch("jBProbab",&jBProbab_,"jBProbab/F");
  outputTree->Branch("aDProbab",&aDProbab_,"aDProbab/F");
  outputTree->Branch("xMin",&xMin_,"xMin/F");
  outputTree->Branch("xMax",&xMax_,"xMax/F");
  outputTree->Branch("nBin",&nBin_,"nBin/F");

  bool histoBranches = false;

  // Loop on the file list to extract each histogram 2D DQM histo with full noise distribution  
  TH1F* histoNoiseStrip = NULL;
  TF1*  fitFunc = NULL;
  TH1F* randomHisto = NULL;
  TFitResultPtr result;
  for(auto file : fileList){
    cout<<"input file: "<<file<<endl;
    TFile* inputFile = TFile::Open(file.c_str(),"READ");
    inputFile->cd();
    // take into account that the DQM file structure for strips is always the same --> use cabling map to browse the histograms
    reader.SetEntry(0);
    TH2* histoNoise = NULL;
    long int iChannel = 0;
    int noFitResult = 0;
    while(reader.Next()){
      cout.flush();
      if(iChannel %10 == 0) cout<<"\r"<<"iChannel "<<100*double(iChannel)/(readoutMap->GetEntries()/reductionFactor)<<" % ";
      if(iChannel > double(readoutMap->GetEntries())/reductionFactor) break;
      iChannel++;
      TString objName;
      uint32_t fedKey =  SiStripFedKey(*fedId,SiStripFedKey::feUnit(*fedCh),SiStripFedKey::feChan(*fedCh)).key();
      std::stringstream stream;
      stream << std::hex << fedKey;
      string fedKeyStr = stream.str();
      if(fedKeyStr.size() == 4)
	objName = Form("DQMData/SiStrip/ControlView/FecCrate%d/FecSlot%d/FecRing%d/CcuAddr%d/CcuChan%d/ExpertHisto_PedsFullNoise_FedKey0x0000%s_LldChannel%d_Noise2D",*fecCrate,*fecSlot,*fecRing,*ccuAdd,*ccuChan,fedKeyStr.c_str(),*lldChannel);      
      else if(fedKeyStr.size() == 5)
	objName = Form("DQMData/SiStrip/ControlView/FecCrate%d/FecSlot%d/FecRing%d/CcuAddr%d/CcuChan%d/ExpertHisto_PedsFullNoise_FedKey0x000%s_LldChannel%d_Noise2D",*fecCrate,*fecSlot,*fecRing,*ccuAdd,*ccuChan,fedKeyStr.c_str(),*lldChannel);      
      else
	cerr<<"hex number to short "<<fedKeyStr<<" --> please check "<<endl;

      inputFile->GetObject(objName.Data(),histoNoise);
      // extract single strip noise histogram --> loop on the y-axis
      uint16_t apvID = 0;
      uint16_t stripID = 0;       
      if(histoNoiseStrip == 0 or histoNoiseStrip == NULL){
	histoNoiseStrip = new TH1F ("histoNoiseStrip","",histoNoise->GetNbinsX(),histoNoise->GetXaxis()->GetXmin(),histoNoise->GetXaxis()->GetXmax());
	histoNoiseStrip->Sumw2();
      }
      for(int iBinY = 0; iBinY < histoNoise->GetNbinsY(); iBinY++){
	histoNoiseStrip->Reset();
	histoNoiseStrip->SetDirectory(0);
	// two multiplexed APV per line
	if(iBinY < histoNoise->GetNbinsY()/2) apvID = 1;
	else apvID = 2;
	// strip id
	stripID++;
	if(stripID > 128) stripID = 1;
	// loop on x-axis bin
	for(int iBinX = 0; iBinX < histoNoise->GetNbinsX(); iBinX++){
	  histoNoiseStrip->SetBinContent(iBinX+1,histoNoise->GetBinContent(iBinX+1,iBinY+1));
	  histoNoiseStrip->SetBinError(iBinX+1,histoNoise->GetBinError(iBinX+1,iBinY+1));	    
	}
     	
	// to initialize branches
	detid_ = 0; fedKey_ = 0; fecCrate_ = 0; fecSlot_ = 0; fecRing_ = 0; ccuAdd_ = 0; ccuChan_ = 0; lldChannel_ = 0; fedId_ = 0; fedCh_ = 0; apvId_ = 0; stripId_ = 0; 
	noiseMean_ = 0.; noiseRMS_ =  0.; noiseSkewness_ = 0.; noiseKurtosis_ = 0.; 
	fitGausMean_ = 0.; fitGausSigma_ = 0.;fitGausNormalization_ = 0.;
	fitGausMeanError_ = 0.; fitGausSigmaError_ = 0.;fitGausNormalizationError_ = 0.;	  	  
	fitChi2_ = 0.; fitChi2Probab_ = 0.; fitStatus_ = -1.; 
	noiseIntegral3Sigma_ = 0.; noiseIntegral3SigmaFromFit_ = 0.; 
	noiseIntegral4Sigma_ = 0.; noiseIntegral4SigmaFromFit_ = 0.; 
	noiseIntegral5Sigma_ = 0.; noiseIntegral5SigmaFromFit_ = 0.; 
	kSProbab_ = 0.; jBProbab_ = 0.;
	kSValue_ = 0.; jBValue_ = 0.; 
	aDValue_= 0.; aDProbab_ = 0.;
	nBin_ = 0.; xMin_ = 0.; xMax_ = 0.;
	
	// basic info
	detid_ = *detid;
	fedKey_ = fedKey;
	fecCrate_ = *fecCrate;
	fecSlot_ = *fecSlot;
	fecRing_ = *fecRing;
	ccuAdd_  = *ccuAdd;
	ccuChan_ = *ccuChan;
	lldChannel_ = *lldChannel;
	fedId_   = *fedId;
	fedCh_   = *fedCh;
	apvId_   = apvID;
	stripId_ = stripID;
	
	// basic info of nioise distribution
	noiseMean_ = histoNoiseStrip->GetMean();
	noiseRMS_  = histoNoiseStrip->GetRMS();
	noiseSkewness_ = histoNoiseStrip->GetSkewness();
	noiseKurtosis_ = histoNoiseStrip->GetKurtosis();
	float integral = histoNoiseStrip->Integral();	
	noiseIntegral3Sigma_ = (histoNoiseStrip->Integral(histoNoiseStrip->FindBin(noiseMean_+noiseRMS_*3),histoNoiseStrip->GetNbinsX()+1) + histoNoiseStrip->Integral(0,histoNoiseStrip->FindBin(noiseMean_-noiseRMS_*3)))/integral;
	noiseIntegral4Sigma_ = (histoNoiseStrip->Integral(histoNoiseStrip->FindBin(noiseMean_+noiseRMS_*4),histoNoiseStrip->GetNbinsX()+1) + histoNoiseStrip->Integral(0,histoNoiseStrip->FindBin(noiseMean_-noiseRMS_*4)))/integral;
	noiseIntegral5Sigma_ = (histoNoiseStrip->Integral(histoNoiseStrip->FindBin(noiseMean_+noiseRMS_*5),histoNoiseStrip->GetNbinsX()+1) + histoNoiseStrip->Integral(0,histoNoiseStrip->FindBin(noiseMean_-noiseRMS_*5)))/integral;
	
	// make a gaussian fit	  	
	if(fitFunc == NULL or fitFunc == 0){
	  fitFunc = new TF1 ("fitFunc","gaus(0)",histoNoise->GetXaxis()->GetXmin(),histoNoise->GetXaxis()->GetXmax());
	}
	fitFunc->SetRange(histoNoise->GetXaxis()->GetXmin(),histoNoise->GetXaxis()->GetXmax());
	fitFunc->SetParameters(histoNoiseStrip->Integral(),noiseMean_,noiseRMS_);
	result = histoNoiseStrip->Fit(fitFunc,"QSR");

	if(result.Get()){
	    fitStatus_     = result->Status();
	    fitGausNormalization_  = fitFunc->GetParameter(0);
	    fitGausMean_   = fitFunc->GetParameter(1);
	    fitGausSigma_  = fitFunc->GetParameter(2);
	    fitGausNormalizationError_  = fitFunc->GetParError(0);
	    fitGausMeanError_  = fitFunc->GetParError(1);
	    fitGausSigmaError_ = fitFunc->GetParError(2);
	    fitChi2_           = result->Chi2();
	    fitChi2Probab_     = result->Prob();

	    noiseIntegral3SigmaFromFit_ = (histoNoiseStrip->Integral(histoNoiseStrip->FindBin(noiseMean_+fitGausSigma_*3),histoNoiseStrip->GetNbinsX()+1) + histoNoiseStrip->Integral(0,histoNoiseStrip->FindBin(noiseMean_-fitGausSigma_*3)))/histoNoiseStrip->Integral();
	    noiseIntegral4SigmaFromFit_ = (histoNoiseStrip->Integral(histoNoiseStrip->FindBin(noiseMean_+fitGausSigma_*4),histoNoiseStrip->GetNbinsX()+1) + histoNoiseStrip->Integral(0,histoNoiseStrip->FindBin(noiseMean_-fitGausSigma_*4)))/histoNoiseStrip->Integral();
	    noiseIntegral5SigmaFromFit_ = (histoNoiseStrip->Integral(histoNoiseStrip->FindBin(noiseMean_+fitGausSigma_*5),histoNoiseStrip->GetNbinsX()+1) + histoNoiseStrip->Integral(0,histoNoiseStrip->FindBin(noiseMean_-fitGausSigma_*5)))/histoNoiseStrip->Integral();
	    
	    jBValue_   = (histoNoiseStrip->Integral()/6)*(noiseSkewness_*noiseSkewness_+(noiseKurtosis_*noiseKurtosis_)/4);	  
	    jBProbab_  = ROOT::Math::chisquared_cdf_c(jBValue_,2);

	    if(randomHisto == 0 or randomHisto == NULL)
	      randomHisto = (TH1F*) histoNoiseStrip->Clone("randomHisto");	    	    
	    randomHisto->Reset();
	    randomHisto->SetDirectory(0);     
	
      
	    if(integral != 0){	      
	      if(generateRandomDistribution){
		randomHisto->FillRandom("fitFunc",histoNoiseStrip->Integral());	    
		kSValue_  = histoNoiseStrip->KolmogorovTest(randomHisto,"MN");
		kSProbab_ = histoNoiseStrip->KolmogorovTest(randomHisto,"N");	    
		aDValue_  = histoNoiseStrip->AndersonDarlingTest(randomHisto,"T");
		aDProbab_ = histoNoiseStrip->AndersonDarlingTest(randomHisto);
	      }
	      else{
		
		randomHisto->Add(fitFunc);		
		kSValue_  = histoNoiseStrip->KolmogorovTest(randomHisto,"MN"); 
		kSProbab_ = histoNoiseStrip->KolmogorovTest(randomHisto,"N");
		// AD test
		ROOT::Fit::BinData data1;
		ROOT::Fit::BinData data2;
		ROOT::Fit::FillData(data1,histoNoiseStrip,0);
		data2.Initialize(randomHisto->GetNbinsX()+1,1);
		for(int ibin = 0; ibin < randomHisto->GetNbinsX(); ibin++){ 
		  if(histoNoiseStrip->GetBinContent(ibin+1) != 0 or randomHisto->GetBinContent(ibin+1) >= 1)
		    data2.Add(randomHisto->GetBinCenter(ibin+1),randomHisto->GetBinContent(ibin+1),randomHisto->GetBinError(ibin+1));
		}
	  
		double probab;
		double value;
		ROOT::Math::GoFTest::AndersonDarling2SamplesTest(data1,data2,probab,value);
		aDValue_ = value;
		aDProbab_ = probab;
	      }
	    }
	}
	else
	  noFitResult++;
	
	if(not histoBranches){
	  noiseDistribution_.clear();
	  noiseDistributionError_.clear();
	  outputTree->Branch("noiseDistribution","vector<float>",&noiseDistribution_);
	  outputTree->Branch("noiseDistributionError","vector<float>",&noiseDistributionError_);
	  histoBranches = true;
	}
    
	// set histogram
	noiseDistribution_.clear();
	noiseDistributionError_.clear();
	for(int iBin = 0; iBin < histoNoiseStrip->GetNbinsX(); iBin++){
	  noiseDistribution_.push_back(histoNoiseStrip->GetBinContent(iBin+1));
	  noiseDistributionError_.push_back(histoNoiseStrip->GetBinError(iBin+1));	      
	}
    
	nBin_ = histoNoiseStrip->GetNbinsX();
	xMin_ = histoNoise->GetXaxis()->GetBinLowEdge(1);
	xMax_ = histoNoise->GetXaxis()->GetBinLowEdge(histoNoise->GetNbinsX()+1);

	// fill all branches for each strip
	ouputTreeFile->cd();
	outputTree->Fill();
      }
    }
    inputFile->Close();
    std::cout<<std::endl;
    cout<<"No fit results found for "<<100*double(noFitResult)/iChannel<<endl;
  }
  outputTree->BuildIndex("detid");
  outputTree->Write(outputTree->GetName(),TObject::kOverwrite);
  ouputTreeFile->Close();
  cablingFile->Close();

  /* Do your stuff here */
  cout<<"Time taken: "<<(double)(clock() - tStart)/CLOCKS_PER_SEC<<endl;  
}
void BEP::read(const char *fileName, WCSP *wcsp)
{
  Cost top = MIN_COST;

  // open the file
  ifstream file(fileName);
  if (!file) {
    cerr << "Could not open file " << fileName << endl;
    exit(EXIT_FAILURE);
  }
  string str;
  getline(file,str);
  getline(file,str);
  file >> size;
  duration.resize(size);
  earliest.resize(size);
  latest.resize(size);
  revenue.resize(size);
  delay.resize(size*size);
  getline(file,str);
  for (int i=0; i<size; i++) {
	int pos;
	file >> pos;
	pos--;
	int x;
	file >> x;
	int y;
	file >> y;
	file >> duration[pos];
	file >> earliest[pos];
	earliest[pos] = max(0,earliest[pos]);
	file >> latest[pos];
	latest[pos] -= duration[pos];
	file >> revenue[pos];
	top += revenue[pos] * (size-1);
  }
  for (int i=0; i<size; i++) {
	for (int j=0; j<size; j++) {
	  file >> delay[i*size+j];
	}
  }

  wcsp->updateUb(top);

  /* create variables */
  for (int i=0; i<size; i++) {
	string varname = "t";
	varname += to_string(i+1);
	if (BAC) wcsp->makeIntervalVariable(varname, earliest[i],latest[i]+1);
	else {
	  wcsp->makeEnumeratedVariable(varname, 0, latest[i]+1);
	  wcsp->increase(i, earliest[i]);
	}
  }

  /* create binary special disjunction */
  for (int i=0; i<size; i++) {
	for (int j=i+1; j<size; j++) {
	  if (BAC) {
		wcsp->postSpecialDisjunction(i,j,duration[i]+delay[i*size+j],duration[j]+delay[j*size+i],latest[i]+1,latest[j]+1,revenue[i],revenue[j]);
	  } else {
		vector<Cost> costs((latest[i]+2) * (latest[j]+2), 0);
		for (int a=earliest[i];a<=latest[i]+1;a++) {
		  for (int b=earliest[j];b<=latest[j]+1;b++) {
			if (a>latest[i] && b>latest[j]) costs[a * (latest[j]+2) + b] = revenue[i]+revenue[j];
			else if (a>latest[i]) costs[a * (latest[j]+2) + b] = revenue[i];
			else if (b>latest[j]) costs[a * (latest[j]+2) + b] = revenue[j];
			else costs[a * (latest[j]+2) + b] = (((a>=b+duration[j]+delay[j*size+i])||(b>=a+duration[i]+delay[i*size+j]))?0:wcsp->getUb()*MEDIUM_COST);
		  }
		}
		wcsp->postBinaryConstraint(i,j,costs);
	  }
	}
  }
  wcsp->sortConstraints();
  
  if (ToulBar2::verbose >= 0) {
    cout << "Read BEP with " << size << " photographs and total gain " << top/(size-1) << endl;
  }
}
Exemple #26
0
int main(int argc, char *argv[])
{
  char *configfile = NULL;
  FILE *fin, *bin;

  char *linebuf = NULL;
  size_t buflen = 0;

  int iterations = 3;
  int mode = 3;

  int c;
  double d;
  double *loglik;
  double p;
  int i, j, k;
  opterr = 0;


  while ((c = getopt(argc, argv, "c:n:hp:")) != -1) {
    switch (c) {
    case 'c':
      configfile = optarg;
      break;
    case 'h':
      usage();
      exit(EXIT_SUCCESS);
    case 'n':
      iterations = atoi(optarg);
      break;
    case 'p':
      mode = atoi(optarg);
      if (mode != 1 && mode != 2 && mode != 3) {
        fprintf(stderr, "illegal mode: %d\n", mode);
        exit(EXIT_FAILURE);
      }
      break;
    case '?':
      fprintf(stderr, "illegal options\n");
      exit(EXIT_FAILURE);
    default:
      abort();
    }
  }

  if (configfile == NULL) {
    fin = stdin;
  } else {
    fin = fopen(configfile, "r");
    if (fin == NULL) {
      handle_error("fopen");
    }
  }
  
  i = 0;
  while ((c = getline(&linebuf, &buflen, fin)) != -1) {
    if (c <= 1 || linebuf[0] == '#')
      continue;
    
    if (i == 0) {
      if (sscanf(linebuf, "%d", &nstates) != 1) {
        fprintf(stderr, "config file format error: %d\n", i);
        freeall();
        exit(EXIT_FAILURE);
      }

      prior = (double *) malloc(sizeof(double) * nstates);
      if (prior == NULL) handle_error("malloc");

      trans = (double *) malloc(sizeof(double) * nstates * nstates);
      if (trans == NULL) handle_error("malloc");

      xi = (double *) malloc(sizeof(double) * nstates * nstates);
      if (xi == NULL) handle_error("malloc");

      pi = (double *) malloc(sizeof(double) * nstates);
      if (pi == NULL) handle_error("malloc");

    } else if (i == 1) {
      if (sscanf(linebuf, "%d", &nobvs) != 1) {
        fprintf(stderr, "config file format error: %d\n", i);
        freeall();
        exit(EXIT_FAILURE);
      }

      obvs = (double *) malloc(sizeof(double) * nstates * nobvs);
      if (obvs == NULL) handle_error("malloc");

      gmm = (double *) malloc(sizeof(double) * nstates * nobvs);
      if (gmm == NULL) handle_error("malloc");

    } else if (i == 2) {
      /* read initial state probabilities */ 
      bin = fmemopen(linebuf, buflen, "r");
      if (bin == NULL) handle_error("fmemopen");
      for (j = 0; j < nstates; j++) {
        if (fscanf(bin, "%lf", &d) != 1) {
          fprintf(stderr, "config file format error: %d\n", i);
          freeall();
          exit(EXIT_FAILURE);
        }
        prior[j] = log(d);
      }
      fclose(bin);

    } else if (i <= 2 + nstates) {
      /* read state transition  probabilities */ 
      bin = fmemopen(linebuf, buflen, "r");
      if (bin == NULL) handle_error("fmemopen");
      for (j = 0; j < nstates; j++) {
        if (fscanf(bin, "%lf", &d) != 1) {
          fprintf(stderr, "config file format error: %d\n", i);
          freeall();
          exit(EXIT_FAILURE);
        }
        trans[IDX((i - 3),j, nstates)] = log(d);
      }
      fclose(bin);
    } else if (i <= 2 + nstates * 2) {
      /* read output probabilities */
      bin = fmemopen(linebuf, buflen, "r");
      if (bin == NULL) handle_error("fmemopen");
      for (j = 0; j < nobvs; j++) {
        if (fscanf(bin, "%lf", &d) != 1) {
          fprintf(stderr, "config file format error: %d\n", i);
          freeall();
          exit(EXIT_FAILURE);
        }
        obvs[IDX((i - 3 - nstates),j,nobvs)] = log(d);
      }
      fclose(bin);
    } else if (i == 3 + nstates * 2) {
      if (sscanf(linebuf, "%d %d", &nseq, &length) != 2) {
        fprintf(stderr, "config file format error: %d\n", i);
        freeall();
        exit(EXIT_FAILURE);
      }
      data = (int *) malloc (sizeof(int) * nseq * length);
      if (data == NULL) handle_error("malloc");
    } else if (i <= 3 + nstates * 2 + nseq) {
      /* read data */
      bin = fmemopen(linebuf, buflen, "r");
      if (bin == NULL) handle_error("fmemopen");
      for (j = 0; j < length; j++) {
        if (fscanf(bin, "%d", &k) != 1 || k < 0 || k >= nobvs) {
          fprintf(stderr, "config file format error: %d\n", i);
          freeall();
          exit(EXIT_FAILURE);
        }
        data[(i - 4 - nstates * 2) * length + j] = k;
      }
      fclose(bin);
    }

    i++;
  }
  fclose(fin);
  if (linebuf) free(linebuf);

  if (i < 4 + nstates * 2 + nseq) {
    fprintf(stderr, "configuration incomplete.\n");
    freeall();
    exit(EXIT_FAILURE);
  }

  if (mode == 3) {
    loglik = (double *) malloc(sizeof(double) * nseq);
    if (loglik == NULL) handle_error("malloc");
    for (i = 0; i < iterations; i++) {
      init_count();
      for (j = 0; j < nseq; j++) {
        loglik[j] = forward_backward(data + length * j, length, 1);
      }
      p = sum(loglik, nseq);

      update_prob();

      printf("iteration %d log-likelihood: %.4lf\n", i + 1, p);
      printf("updated parameters:\n");
      printf("# initial state probability\n");
      for (j = 0; j < nstates; j++) {
        printf(" %.4f", exp(prior[j]));
      }
      printf("\n");
      printf("# state transition probability\n");
      for (j = 0; j < nstates; j++) {
        for (k = 0; k < nstates; k++) {
          printf(" %.4f", exp(trans[IDX(j,k,nstates)]));
        }
        printf("\n");
      }
      printf("# state output probility\n");
      for (j = 0; j < nstates; j++) {
        for (k = 0; k < nobvs; k++) {
          printf(" %.4f", exp(obvs[IDX(j,k,nobvs)]));
        }
        printf("\n");
      }
      printf("\n");
    }
    free(loglik);
  } else if (mode == 2) {
    for (i = 0; i < nseq; i++) {
      viterbi(data + length * i, length);
    }
  } else if (mode == 1) {
    loglik = (double *) malloc(sizeof(double) * nseq);
    if (loglik == NULL) handle_error("malloc");
    for (i = 0; i < nseq; i++) {
      loglik[i] = forward_backward(data + length * i, length, 0);
    }
    p = sum(loglik, nseq);
    for (i = 0; i < nseq; i++)
      printf("%.4lf\n", loglik[i]);
    printf("total: %.4lf\n", p);
    free(loglik);
  }

  freeall();
  return 0;
}
int main(int argc, char* argv[]) {


  std::string line;
  std::ifstream pdbfile;
  std::ofstream translatefile;
  std::vector<double> d;

  if(argc == 3){

    pdbfile.open(argv[1], std::ios::in);
    translatefile.open(argv[2], std::ios::out);
    d.assign(3,1.000);

  }else if(argc == 6){

    pdbfile.open(argv[1], std::ios::in);
    translatefile.open(argv[2], std::ios::out);
  
    d.push_back(std::stod(argv[3]));
    d.push_back(std::stod(argv[4]));
    d.push_back(std::stod(argv[5]));

  }else{

    std::cout << "Wrong amount of Parameters\n\n Useage: HandlingPDB inFile outFile\n";
    return 1;

  }

  //Basic idea:
  // Checking ATOM
  // Grabbing Coordinates based on columns
  // Parsing to double, adding offset
  // And the c++ way to parsing double to string with fixed size and precision is to set up an outsream correctly
  // Important: If you want a precision of 3, i.e 3 digits after the dot, you need an outstream.precision of 4. Otherwise
  // it get rounded to early.
  // Lastly, replacing the new string in the selected line
  if (pdbfile.is_open()){
    while (getline(pdbfile,line)){
      
      //std::string buf;
      //std::stringstream ss(line);
      //std::vector<std::string> items;
      
      /*
      while (ss >> buf){
        items.push_back(buf);
      }*/
      
      //std::vector<int>::size_type sz = items.size();
      
      if (line.substr(0,4) == "ATOM"){

        double xCoord = stod(line.substr(30,8));
        double yCoord = stod(line.substr(38,8));
        double zCoord = stod(line.substr(46,8));


        xCoord += d[0];
        yCoord += d[1];
        zCoord += d[2];

        std::ostringstream xCoordStrs;
        std::ostringstream yCoordStrs;
        std::ostringstream zCoordStrs;

        xCoordStrs.width(8);
        xCoordStrs.precision(4);
        yCoordStrs.width(8);
        yCoordStrs.precision(4);
        zCoordStrs.width(8);
        zCoordStrs.precision(4);

        xCoordStrs << xCoord;
        yCoordStrs << yCoord;
        zCoordStrs << zCoord;

        line.replace(30,8,xCoordStrs.str());
        line.replace(38,8,yCoordStrs.str());
        line.replace(46,8,zCoordStrs.str());
      }

      translatefile << line << '\n';

    }

  }else{

    std::cout << "Unable to open file";

  }
      /*

	for (unsigned i = 0; i < sz; i++){
	  // positions 6,7,8 contain x,y,z for an atom line in a pdb file
	  if (i == 6){
	    // translate x position
	    translatefile << stof(items[i]) + d[0] << ' ';
	    i++;
	    // translate y position
	    translatefile << stof(items[i]) + d[1] << ' ';
	    i++;
	    // translate z position
	    translatefile << stof(items[i]) + d[2] << ' ';
	  }
	  else{
	    translatefile << line;
	  }
	}
	translatefile << std::endl;
      }
      
      else{
	// write non atom line
	for (unsigned i = 0; i < sz; i++){
	  translatefile << items[i] << ' ';
	}
	translatefile << std::endl;
      }
      
    }
    
    pdbfile.close();
    translatefile.close();

  }

  else std::cout << "Unable to open file"; 
*/

  return 0;
}
Exemple #28
0
Parser::Parser(string fileName) {

    //INITIALIZTIONS
    ifstream dataFile;
    string line;
    string data;
    start_idx = 0;
    element_counter=0;

    elements[17]=new Element(">$<");
    elements[18]=new Element(">$<");


    for(int i=0 ; i < 141; i++) locations[i] = NULL; //Thanks Charlie

    int section = 0;

    dataFile.open(fileName.c_str());

    if(!dataFile.eof()){
        while(getline(dataFile,line)){
            data += line + END_LINE;

            int first_number = getFirstNumberOf(line);

            if(SEPARATOR == first_number){
               section++;
            }

            switch(section){
                case LONG_DESC_SECTION:
                    parse_locations(line, first_number);
                    break;
                case SHORT_DESC_SECTION:
                    break;
                case TRAVEL_TABLE:
                    parse_travel_table(line, first_number);
                    break;
                case VOCABULARY:
                    parse_vocabulary(line);
                    break;
                case ELEMENT_DESC:
                    parse_element_desc(line);
                    break;
                case ELEMENT_LOCATION:
                    parse_element_location(line);
                    break;
                case ABBR_MSG:
                    parse_abbr_msg(line);
                    break;
                case ACTIONS:
                    parse_actions(line);
                    break;
                case LIQUID_ASSET:
                    break;
                case CLASS_MSG:
                    parse_player_classification(line);
                    break;
                case HINTS:
                    break;
                case MAGIC_WORDS:
                    break;
            }//endSwitch
        }//endWhile
        dataFile.close();
    }

}
Exemple #29
0
int THaCrateMap::init(TString the_map)
{
  // initialize the crate-map according to the lines in the string 'the_map'
  // parse each line separately, to ensure that the format is correct

  // be certain the_map ends with a '\0' so we can make a stringstream from it
  the_map += '\0';
  ISSTREAM s(the_map.Data());

  int linecnt = 0;
  string line;
  int crate; // current CRATE
  int slot;

  typedef string::size_type ssiz_t;

  for(crate=0; crate<MAXROC; crate++) {
    crdat[crate].nslot = 0;
    crdat[crate].crate_used = false;
    crdat[crate].bank_structure = false;
    setCrateType(crate,"unknown"); //   crate_type[crate] = "unknown";
    crdat[crate].minslot=MAXSLOT;
    crdat[crate].maxslot=0;
    for(slot=0; slot<MAXSLOT; slot++) {
      crdat[crate].slot_used[slot] = false;
      crdat[crate].model[slot] = 0;
      crdat[crate].header[slot] = 0;
      crdat[crate].slot_clear[slot] = true;
      crdat[crate].bank[slot] = -1;
    }
  }

  crate=-1; // current CRATE

  while ( getline(s,line).good() ) {
    linecnt++;
    ssiz_t l = line.find_first_of("!#");    // drop comments
    if (l != string::npos ) line.erase(l);

    if ( line.length() <= 0 ) continue;

    if ( line.find_first_not_of(" \t") == string::npos ) continue; // nothing useful

    char ctype[21];

// Make the line "==== Crate" not care about how many "=" chars or other
// chars before "Crate", but lines beginning in # are still a comment
    ssiz_t st = line.find("Crate", 0, 5);
    if (st != string::npos) {
      string lcopy = line;
      line.replace(0, lcopy.length(), lcopy, st, st+lcopy.length());
    }

// set the next CRATE number and type

    if ( sscanf(line.c_str(),"Crate %d type %20s",&crate,ctype) == 2 ) {
      if ( setCrateType(crate,ctype) != CM_OK )  {
        cout << "THaCrateMap:: fatal ERROR 2  setCrateType "<<endl;
	return CM_ERR;
      }
      // for a scaler crate, get the 'name' or location as well
      if ( crdat[crate].crate_code == kScaler ) {
	if (sscanf(line.c_str(),"Crate %*d type %*s %20s",ctype) != 1) {
          cout << "THaCrateMap:: fatal ERROR 3   "<<endl;
	  return CM_ERR;
	}
	TString scaler_name(ctype);
	scaler_name.ReplaceAll("\"",""); // drop extra quotes
	setScalerLoc(crate,scaler_name);
      }
      continue; // onto the next line
    }

    // The line is of the format:
    //        slot#  model#  [clear header  mask  nchan ndata ]
    // where clear, header, mask, nchan and ndata are optional interpretted in
    // that order.
    // Another option is "bank decoding" :  all data in this CODA bank
    // belongs to this slot and model.  The line has the format
    //        slot#  model#  bank#

    // Default values:
    int imodel, cword=1;
    unsigned int mask=0, iheader=0, ichan=MAXCHAN, idata=MAXDATA;
    int nread;
    // must read at least the slot and model numbers
    if ( crate>=0 &&
	 (nread=
	  sscanf(line.c_str(),"%d %d %d %x %x %u %u",
		 &slot,&imodel,&cword,&iheader,&mask,&ichan,&idata)) >=2 ) {
      if (nread>=6)
	setModel(crate,slot,imodel,ichan,idata);
      else
	setModel(crate,slot,imodel);

      if (nread==3) 
        setBank(crate, slot, cword);
      if (nread>3)
	setClear(crate,slot,cword);
      if (nread>=4)
	setHeader(crate,slot,iheader);
      if (nread>=5)
	setMask(crate,slot,mask);
      continue;
    }

    // unexpected input
    cout << "THaCrateMap:: fatal ERROR 4   "<<endl<<"Bad line "<<endl<<line<<endl;
    cout << "    Warning: a bad line could cause wrong decoding !"<<endl;

    return CM_ERR;
  }

  for(crate=0; crate<MAXROC; crate++) {
    Int_t imin=MAXSLOT;
    Int_t imax=0;
    for(slot=0; slot<MAXSLOT; slot++) {
      if (crdat[crate].bank[slot]>=0) crdat[crate].bank_structure=true;
      if (crdat[crate].slot_used[slot]) {
	if (slot < imin) imin=slot;
        if (slot > imax) imax=slot;
      }
    }
    crdat[crate].minslot=imin;
    crdat[crate].maxslot=imax;
  }

  return CM_OK;
}
Exemple #30
0
BipedManager::BipedManager(std::string const& rootPath,
    std::string const& bname, ProgramFactory& programFactory,
    SkinController::Updater const& postUpdate)
{
    // Vertex format shared by the two skins.
    VertexFormat vformat;
    vformat.Bind(VA_POSITION, DF_R32G32B32_FLOAT, 0);
    vformat.Bind(VA_NORMAL, DF_R32G32B32_FLOAT, 0);
    vformat.Bind(VA_TEXCOORD, DF_R32G32_FLOAT, 0);

    // Create the texture effects for the two skins.
    std::shared_ptr<Texture2> texture[2];
    std::shared_ptr<Texture2Effect> effect[2];
    for (int i = 0; i < 2; ++i)
    {
        std::string name = rootPath + "Skins/Skins" + std::to_string(i) + ".texture.png";
        texture[i].reset(WICFileIO::Load(name, true));
        texture[i]->AutogenerateMipmaps();
        effect[i] = std::make_shared<Texture2Effect>(programFactory,
            texture[i], SamplerState::MIN_L_MAG_L_MIP_L, SamplerState::WRAP,
            SamplerState::WRAP);
    }

    PreSpatialArray preSpatialArray;
    PreSkinArray preSkinArray;
    SpatialMap spatialMap;

    std::string filename = rootPath + bname + ".txt";
    std::ifstream inFile(filename);
    while (!inFile.eof())
    {
        std::string line;
        getline(inFile, line);
        if (line == "")
        {
            // The file contains no blank lines, except for the very last one.
            break;
        }

        // Strip off initial white space.
        std::string::size_type begin = line.find_first_not_of(" ");
        if (begin > 0)
        {
            line = line.substr(begin);
        }

        std::string::size_type end;
        std::string name;
        if (line.find("Node") == 0)
        {
            // Load the node.
            begin = line.find("<");
            end = line.find(">");
            name = line.substr(begin + 1, end - 1 - begin);
            if (name.back() == 'X')
            {
                // TODO: These nodes are not necessary.  Remove them from the
                // data sets.
                continue;
            }

            PreSpatial* preSpatial = LoadNode(rootPath, name);
            preSpatialArray.push_back(preSpatial);
            Node* node = reinterpret_cast<Node*>(preSpatial->Associate);
            spatialMap[name] = node;

            // Load the transform controllers.
            NodeCtrl nc;
            nc.first = node;

            nc.second.reset(LoadTransformController(rootPath, name, "Idle"));
            mIdleArray.push_back(nc);

            nc.second.reset(LoadTransformController(rootPath, name, "Walk"));
            mWalkArray.push_back(nc);

            nc.second.reset(LoadTransformController(rootPath, name, "Run"));
            mRunArray.push_back(nc);
        }
        else if (line.find("TriMesh") == 0)
        {
            // Load the mesh.
            begin = line.find("<");
            end = line.find(">");
            name = line.substr(begin + 1, end - 1 - begin);
            int suffix = name[name.length() - 1] - '0';
            PreSpatial* preSpatial = LoadMesh(rootPath, name, vformat, effect[suffix]);
            preSpatialArray.push_back(preSpatial);
            spatialMap[name] = preSpatial->Associate;

            // Load the skin controller.
            PreSkin* preSkin = LoadSkinController(rootPath, name, postUpdate);
            preSkinArray.push_back(preSkin);

            // Attach the skin controller to the mesh.
            preSpatial->Associate->AttachController(preSkin->Associate);
        }
    }

    // Resolve the bone links.
    for (auto preSkin : preSkinArray)
    {
        SkinController* ctrl = preSkin->Associate;
        Node** bones = ctrl->GetBones();
        int i = 0;
        for (auto const& boneName : preSkin->BoneNames)
        {
            auto iter = spatialMap.find(boneName);
            bones[i] = reinterpret_cast<Node*>(iter->second);
            ++i;
        }
    }

    // Assemble the biped hierarchy.
    for (auto preSpatial : preSpatialArray)
    {
        Node* node = dynamic_cast<Node*>(preSpatial->Associate);
        if (node)
        {
            for (auto const& childName : preSpatial->ChildNames)
            {
                if (childName.back() == 'X')
                {
                    // TODO: These nodes are not necessary.  Remove them from
                    // the data sets.
                    continue;
                }

                auto iter = spatialMap.find(childName);
                Visual* mesh = dynamic_cast<Visual*>(iter->second);
                if (mesh)
                {
                    std::shared_ptr<Visual> visual(mesh);
                    node->AttachChild(visual);
                    if (visual->GetEffect().get() == effect[0].get())
                    {
                        mSubscribers[0].first = visual;
                        mSubscribers[0].second = effect[0]->GetPVWMatrixConstant();
                    }
                    else
                    {
                        mSubscribers[1].first = visual;
                        mSubscribers[1].second = effect[1]->GetPVWMatrixConstant();
                    }
                }
                else
                {
                    node->AttachChild(std::shared_ptr<Spatial>(iter->second));
                }
            }
        }
    }

    mRoot.reset(reinterpret_cast<Node*>(preSpatialArray[0]->Associate));

    for (auto preSpatial : preSpatialArray)
    {
        delete preSpatial;
    }

    for (auto preSkin : preSkinArray)
    {
        delete preSkin;
    }

    // Create the blend controllers.
    int const numControllers = static_cast<int>(mIdleArray.size());
    mIdleWalkArray.resize(numControllers);
    mWalkRunArray.resize(numControllers);
    for (int i = 0; i < numControllers; ++i)
    {
        NodeCtrl const& nc0 = mIdleArray[i];
        NodeCtrl const& nc1 = mWalkArray[i];
        NodeCtrl const& nc2 = mRunArray[i];

        mIdleWalkArray[i].first = nc0.first;
        mIdleWalkArray[i].second = std::make_shared<BlendTransformController>(
            nc0.second, nc1.second, true, false);

        mWalkRunArray[i].first = nc0.first;
        mWalkRunArray[i].second = std::make_shared<BlendTransformController>(
            nc1.second, nc2.second, true, false);
    }
}