Esempio n. 1
0
int readRegionDefinitions(node_map& regionDefs, string filename){ //node_map maps id to vector<double>
	int nRegions=0; // number of regionDefs read
	bool regKwdFound=false; // look for the keyword "regions" followed by the number of regions to read
	string line, token;
	int ret, done=0;
	char check;
	double a,b,c,d;
	unsigned int nextId;

	std::istringstream strstream;
	ifstream in(filename.c_str());
	if(!in.is_open()) return -1;

	while(in.good()){
		getline(in, line);
		if(line.empty() || line.at(0)=='#') continue; // comments have # in the first column
		strstream.clear();
		strstream.str(line);

		getline(strstream, token, ' ');
		if(!regKwdFound){
			if(token.compare("regions")==0){
				regKwdFound=true;
				getline(strstream, token, ' '); //next token is number of regions to read
				ret=sscanf(token.c_str(), "%u", &nRegions);
				if(ret!=1){
					in.close();
					//printf("invalid ret=%d should be 1 on token %s\n",ret, token.c_str());
					return -1;
				}
			}
		}else if(done<nRegions){ //reading next region until we have plenty
			//printf("processing '%s' done %d\n",line.c_str(),done);
			ret=sscanf(token.c_str(), "%u", &nextId);
			if(ret==1) while(getline(strstream, token, ' ')){ //token is now one condition of the region definition
				//printf("parsing token '%s'",token.c_str());
				ret=sscanf(token.c_str(), "(%lf,%lf,%lf,%lf%c",&a,&b,&c,&d,&check);
				if(ret==5 && check==')'){ // correct format
					regionDefs[nextId].push_back(a);
					regionDefs[nextId].push_back(b);
					regionDefs[nextId].push_back(c);
					regionDefs[nextId].push_back(d);
					//printf(" ... ok\n");
				}
				//else printf(" ... reject ret=%d, check='%c'\n",ret,check);
			}
			++done;
		}
	}
	//printf("\nregionDefs:\n");
	//printMap(regionDefs);
	// finally add an empty region def which will be the default
	if(regionDefs.empty()) nextId=ELEM_BASE_INDEX;
	else nextId = regionDefs.rbegin()->first +1;
	regionDefs[nextId].assign(0,0.0);

	return nRegions;
}