int main() { { testbuf<char> sb1; testbuf<char> sb2; test_ostream<char> os1(&sb1); test_ostream<char> os2(&sb2); os1.swap(os2); assert(os1.rdbuf() == &sb1); assert(os1.tie() == 0); assert(os1.fill() == ' '); assert(os1.rdstate() == os1.goodbit); assert(os1.exceptions() == os1.goodbit); assert(os1.flags() == (os1.skipws | os1.dec)); assert(os1.precision() == 6); assert(os1.getloc().name() == "C"); assert(os2.rdbuf() == &sb2); assert(os2.tie() == 0); assert(os2.fill() == ' '); assert(os2.rdstate() == os2.goodbit); assert(os2.exceptions() == os2.goodbit); assert(os2.flags() == (os2.skipws | os2.dec)); assert(os2.precision() == 6); assert(os2.getloc().name() == "C"); } { testbuf<wchar_t> sb1; testbuf<wchar_t> sb2; test_ostream<wchar_t> os1(&sb1); test_ostream<wchar_t> os2(&sb2); os1.swap(os2); assert(os1.rdbuf() == &sb1); assert(os1.tie() == 0); assert(os1.fill() == ' '); assert(os1.rdstate() == os1.goodbit); assert(os1.exceptions() == os1.goodbit); assert(os1.flags() == (os1.skipws | os1.dec)); assert(os1.precision() == 6); assert(os1.getloc().name() == "C"); assert(os2.rdbuf() == &sb2); assert(os2.tie() == 0); assert(os2.fill() == ' '); assert(os2.rdstate() == os2.goodbit); assert(os2.exceptions() == os2.goodbit); assert(os2.flags() == (os2.skipws | os2.dec)); assert(os2.precision() == 6); assert(os2.getloc().name() == "C"); } }
std::string MobV2SAO::getClientInitializationData() { //infostream<<__FUNCTION_NAME<<std::endl; updateProperties(); std::ostringstream os(std::ios::binary); // version writeU8(os, 0); Settings client_properties; /*client_properties.set("version", "0"); client_properties.updateValue(*m_properties, "pos"); client_properties.updateValue(*m_properties, "yaw"); client_properties.updateValue(*m_properties, "hp");*/ // Just send everything for simplicity client_properties.update(*m_properties); std::ostringstream os2(std::ios::binary); client_properties.writeLines(os2); compressZlib(os2.str(), os); return os.str(); }
void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const { writeU8(os, 1); // version u16 count = 0; std::ostringstream os2(std::ios::binary); for (u32 i = 0; i < m_content_features.size(); i++) { if (i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) continue; const ContentFeatures *f = &m_content_features[i]; if (f->name.empty()) continue; writeU16(os2, i); // Wrap it in a string to allow different lengths without // strict version incompatibilities std::ostringstream wrapper_os(std::ios::binary); f->serialize(wrapper_os, protocol_version); os2<<serializeString(wrapper_os.str()); // must not overflow u16 next = count + 1; FATAL_ERROR_IF(next < count, "Overflow"); count++; } writeU16(os, count); os << serializeLongString(os2.str()); }
void serialize(std::ostream &os, u16 protocol_version) { writeU8(os, 1); // version u16 count = 0; std::ostringstream os2(std::ios::binary); for(u32 i=0; i<m_content_features.size(); i++) { if(i == CONTENT_IGNORE || i == CONTENT_AIR || i == CONTENT_UNKNOWN) continue; ContentFeatures *f = &m_content_features[i]; if(f->name == "") continue; writeU16(os2, i); // Wrap it in a string to allow different lengths without // strict version incompatibilities std::ostringstream wrapper_os(std::ios::binary); f->serialize(wrapper_os, protocol_version); os2<<serializeString(wrapper_os.str()); assert(count + 1 > count); // must not overflow count++; } writeU16(os, count); os<<serializeLongString(os2.str()); }
int main() { { std::ostream os((std::streambuf*)0); std::ostream::sentry s(os); assert(!bool(s)); } { testbuf1<char> sb; std::ostream os(&sb); std::ostream::sentry s(os); assert(bool(s)); } { testbuf1<char> sb; std::ostream os(&sb); testbuf1<char> sb2; std::ostream os2(&sb2); os.tie(&os2); assert(sync_called == 0); std::ostream::sentry s(os); assert(bool(s)); assert(sync_called == 1); } }
value_t Hash::to_s(Hash *self) { RecursionDetector<RecursionType::Hash_to_s, false> rd(self); if(rd.recursion()) return String::get("{...}"); CharArray result = "{"; OnStack<1> os1(self); OnStackString<1> os2(result); HashAccess::each_pair(self, [&](value_t key, value_t value) -> bool { OnStack<1> os(value); result += inspect(key); result += "=>"; result += inspect(value); result += ", "; return true; }); if(result.size() > 1) result.shrink(result.size() - 2); result += "}"; return result.to_string(); }
void Mesh::objFileStructure2(const char* mtlfilename){ cout<<mtlfilename<<endl; ifstream mtl(mtlfilename,ios::in); if(!mtl){ cerr<<"File could not be found."<<endl; exit(1); } string materialLine=""; string materialToken=""; while(!mtl.eof()){ if(materialToken!="newmtl"){ getline(mtl, materialLine); std::stringstream os1(materialLine); os1>>materialToken; } if(materialToken =="newmtl"){ std::stringstream os2(materialLine); os2>>materialToken;//new mtl os2>>materialToken;//material string thematerial = materialToken; float red = 0.0776f; float green = 0.2571f; float blue = 0.2041f; do{ if(mtl.eof()){ break; } if(!mtl.eof()){ getline(mtl, materialLine); // cout<<"lines before newmtl ..."<<materialLine<<endl; std::stringstream os3(materialLine); os3>>materialToken; if(materialToken=="Kd"){ os3>>materialToken; //cout<<"red "<<materialToken<<endl; red = atof(materialToken.c_str()); os3>>materialToken; //cout<<"green "<<materialToken<<endl; green = atof(materialToken.c_str()); os3>>materialToken; // cout<<"blue "<<materialToken<<endl; blue = atof(materialToken.c_str()); } }
/** * Runs rbx from the filesystem. Searches for the Rubinius runtime files * according to the algorithm in find_runtime(). */ void Environment::run_from_filesystem() { int i = 0; state->vm()->set_root_stack(reinterpret_cast<uintptr_t>(&i), VM::cStackDepthMax); std::string runtime = system_prefix() + RBX_RUNTIME_PATH; load_platform_conf(runtime); boot_vm(); start_finalizer(); load_argv(argc_, argv_); start_signals(); state->vm()->initialize_config(); load_tool(); G(rubinius)->set_const(state, "Signature", Integer::from(state, signature_)); if(LANGUAGE_20_ENABLED(state)) { runtime += "/20"; } else if(LANGUAGE_19_ENABLED(state)) { runtime += "/19"; } else { runtime += "/18"; } G(rubinius)->set_const(state, "RUNTIME_PATH", String::create(state, runtime.c_str(), runtime.size())); load_kernel(runtime); shared->finalizer_handler()->start_thread(state); run_file(runtime + "/loader.rbc"); state->vm()->thread_state()->clear(); Object* loader = G(rubinius)->get_const(state, state->symbol("Loader")); if(loader->nil_p()) { rubinius::bug("Unable to find loader"); } OnStack<1> os(state, loader); Object* inst = loader->send(state, 0, state->symbol("new")); if(inst) { OnStack<1> os2(state, inst); inst->send(state, 0, state->symbol("main")); } else { rubinius::bug("Unable to instantiate loader"); } }
/** Save help entry to database */ void HelpOLC::save(void) { QString query; QTextOStream qos(&query); QSqlQuery q; if (_entry == 0) { qos << "insert into helptext (title, keywords, body) values " << endl << "('" << _topic << "', '" << _keywords << "', '" << _body << "');"; } else { qos << "update helptext " << endl << "set title='" << _topic << "', " << endl << "keywords='" << _keywords << "', " << endl << "body='" << Logger::escapeString(_body) << "'" << endl << "where helpid = " << _entry << ";"; } if (q.exec(query)) { if (_entry == 0) { /* Doesn't look like we can get the last insert ID, so we'll query for a * specific match to our title and keywords */ QString q2; QTextOStream os2(&q2); os2 << "select helpid from helptext where " << endl << "title = '" << _topic << "' and keywords = '" << _keywords << "';"; if (q.exec(q2) && q.next()) { _entry = q.value(0).toUInt(); } } } else { QString out; QTextOStream os(&out); os << "Failed save query in help editor: " << query; Logger::msg(out, Logger::LOG_ERROR); } }
int main() { { std::ostream os((std::streambuf*)0); const void* n = 0; os << n; assert(os.bad()); assert(os.fail()); } { testbuf<char> sb1; std::ostream os1(&sb1); int n1; os1 << &n1; assert(os1.good()); std::string s1(sb1.str()); testbuf<char> sb2; std::ostream os2(&sb2); int n2; os2 << &n2; assert(os2.good()); std::string s2(sb2.str()); // %p is implementation defined. Instead of validating the // output, at least ensure that it does not generate an empty // string. Also make sure that given two distinct addresses, the // output of %p is different. assert(!s1.empty()); assert(!s2.empty()); assert(s1 != s2); } { testbuf<char> sb; std::ostream os(&sb); const void* n = &sb; os << n; assert(os.good()); } }
void serialize(std::ostream &os) { writeU8(os, 1); // version u16 count = 0; std::ostringstream os2(std::ios::binary); for(u16 i=0; i<=MAX_CONTENT; i++) { if(i == CONTENT_IGNORE || i == CONTENT_AIR) continue; ContentFeatures *f = &m_content_features[i]; if(f->name == "") continue; writeU16(os2, i); // Wrap it in a string to allow different lengths without // strict version incompatibilities std::ostringstream wrapper_os(std::ios::binary); f->serialize(wrapper_os); os2<<serializeString(wrapper_os.str()); count++; } writeU16(os, count); os<<serializeLongString(os2.str()); }
QuadcopterPlanner::QuadcopterPlanner(): ph_("~"){ pose_pub_ = nh_.advertise<geometry_msgs::PoseStamped>("/app/quadcmdpose",1); status_pub_ = nh_.advertise<actionlib_msgs::GoalStatus>("/app/plannersts",1); bool prmAvail = false; std::string param; if(ph_.getParam("wayPtThreshold",param)){ std::stringstream os1(param.c_str()); os1 >> wayPtThreshold_.x>> wayPtThreshold_.y>> wayPtThreshold_.z; if(ph_.getParam("minThreshold",param)){ std::stringstream os2(param.c_str()); os2 >> min_threshold.x>> min_threshold.y>> min_threshold.z; if(ph_.getParam("step",param)){ std::stringstream os3(param.c_str()); os3 >> step_.x>> step_.y>> step_.z; if(ph_.getParam("proximityThreshold",proxityData_.threshold)){ if(ph_.getParam("avoidStep",collisionData_.avoidStep)){ std::cout << "Parameter read from launch file!" << std::endl; prmAvail=true; } } }
void Run() { { // ver 0 SharedBuffer<u8> fromdata(4); fromdata[0]=1; fromdata[1]=5; fromdata[2]=5; fromdata[3]=1; std::ostringstream os(std::ios_base::binary); compress(fromdata, os, 0); std::string str_out = os.str(); infostream<<"str_out.size()="<<str_out.size()<<std::endl; infostream<<"TestCompress: 1,5,5,1 -> "; for(u32 i=0; i<str_out.size(); i++) { infostream<<(u32)str_out[i]<<","; } infostream<<std::endl; assert(str_out.size() == 10); assert(str_out[0] == 0); assert(str_out[1] == 0); assert(str_out[2] == 0); assert(str_out[3] == 4); assert(str_out[4] == 0); assert(str_out[5] == 1); assert(str_out[6] == 1); assert(str_out[7] == 5); assert(str_out[8] == 0); assert(str_out[9] == 1); std::istringstream is(str_out, std::ios_base::binary); std::ostringstream os2(std::ios_base::binary); decompress(is, os2, 0); std::string str_out2 = os2.str(); infostream<<"decompress: "; for(u32 i=0; i<str_out2.size(); i++) { infostream<<(u32)str_out2[i]<<","; } infostream<<std::endl; assert(str_out2.size() == fromdata.getSize()); for(u32 i=0; i<str_out2.size(); i++) { assert(str_out2[i] == fromdata[i]); } } { // ver HIGHEST SharedBuffer<u8> fromdata(4); fromdata[0]=1; fromdata[1]=5; fromdata[2]=5; fromdata[3]=1; std::ostringstream os(std::ios_base::binary); compress(fromdata, os, SER_FMT_VER_HIGHEST); std::string str_out = os.str(); infostream<<"str_out.size()="<<str_out.size()<<std::endl; infostream<<"TestCompress: 1,5,5,1 -> "; for(u32 i=0; i<str_out.size(); i++) { infostream<<(u32)str_out[i]<<","; } infostream<<std::endl; /*assert(str_out.size() == 10); assert(str_out[0] == 0); assert(str_out[1] == 0); assert(str_out[2] == 0); assert(str_out[3] == 4); assert(str_out[4] == 0); assert(str_out[5] == 1); assert(str_out[6] == 1); assert(str_out[7] == 5); assert(str_out[8] == 0); assert(str_out[9] == 1);*/ std::istringstream is(str_out, std::ios_base::binary); std::ostringstream os2(std::ios_base::binary); decompress(is, os2, SER_FMT_VER_HIGHEST); std::string str_out2 = os2.str(); infostream<<"decompress: "; for(u32 i=0; i<str_out2.size(); i++) { infostream<<(u32)str_out2[i]<<","; } infostream<<std::endl; assert(str_out2.size() == fromdata.getSize()); for(u32 i=0; i<str_out2.size(); i++) { assert(str_out2[i] == fromdata[i]); } } }
int main(int argc, char *argv[]) { ParamProgram *par = new ParamProgram(); char fileName[300]; if(argc != 2){ cout << "ERRORR !! " << endl; cout << "buildDL_LZ's usage requires the Properties File as parameter !! " << endl; cout << "Example for the file 'config.txt' in the same directory: ./buildDL_LZ config.txt" << endl; exit(1); } par->configFile = string(argv[1]); cout << "Congif file: " << par->configFile << endl; ConfigFile cf(par->configFile); TRACE = cf.Value("GLOBALS","TRACE"); TEST = cf.Value("GLOBALS","TEST"); N_REP = cf.Value("GLOBALS","N_REP"); strcpy(par->inputFile, ((string)(cf.Value("DL","inputFile"))).c_str()); par->filesInList = cf.Value("DL","filesInList"); par->boundSymbol = cf.Value("DL","boundSymbol"); par->cutDoc = cf.Value("DL","cutDoc"); par->lowercase = cf.Value("DL","lowercase"); par->levRMQ_range = cf.Value("DL","levRMQ"); strcpy(par->dirStore, ((string)(cf.Value("DL","dirStore"))).c_str()); cout << "buildDL_LZ config parameters..." << endl; cout << "Input File : " << par->inputFile << endl; cout << "File in list : " << par->filesInList << endl; cout << "Boundary Symbol(code) : " << (int)par->boundSymbol << endl; cout << "Cut Doc. Symbol(code) : " << (int)par->cutDoc << endl; cout << "Lowercase : " << par->lowercase << endl; cout << "Range levels with RMQ : " << par->levRMQ_range << endl; cout << "Store Folder : " << par->dirStore << endl; LZDocList64::TRACE = TRACE; LZDocList64::TEST = TEST; par->index = new LZDocList64(par->levRMQ_range, par->inputFile, par->filesInList, par->cutDoc, par->lowercase, par->dirStore, par->boundSymbol, false); par->n = par->index->n; par->EndDocs = par->index->EndDocs; cout << "____________________________________________________" << endl; cout << "*** Index size " << par->index->sizeDS << " bytes = " << (float)par->index->sizeDS*8.0/(float)par->n << " bpc" << endl; cout << "*** SizeUpRange = " << par->index->sizeUpRange << " bytes = " << (float)par->index->sizeUpRange*8.0/(float)par->n << " bpc" << endl; cout << "====================================================" << endl; par->index->saveDS(true); { // create the FMI to execute test... cout << "____________________________________________________" << endl; cout << " Make the FMI ..." << endl; strcpy(fileName, ""); strcpy(fileName, par->inputFile); cout << " Reading... " << fileName << endl; strcat(fileName, "_copy.txt"); construct(par->index->fmi, fileName, 1); // generate index cout << " ** FMI size " << size_in_bytes(par->index->fmi) << " bytes = " << (float)size_in_bytes(par->index->fmi)/(float)par->n << "|T|" << endl; cout << " ** FMI length " << par->index->fmi.size() << endl; if (par->index->fmi.size() != par->n){ cout << "ERROR. FMI length != n = " << par->n << endl; exit(1); } strcpy(fileName, ""); strcpy(fileName, par->dirStore); strcat(fileName, "fmi.test"); store_to_file(par->index->fmi, fileName); } if (TEST){ strcpy(fileName, ""); strcpy(fileName, par->dirStore); strcat(fileName, "EndDocs.test"); ofstream os2 (fileName, ios::binary); os2.write((const char*)par->EndDocs, par->index->nDocs*sizeof(ulong)); // save LbRev[] if(TRACE) cout << " .- EndDocs[] " << par->index->nDocs*sizeof(ulong) << " Bytes" << endl; os2.close(); par->index->sep_rrr = rrr_vector<127>(par->index->sepPhrase_b); par->index->sep_rank = rrr_vector<127>::rank_1_type(&par->index->sep_rrr); std::cout << " sep_rrr uses " << size_in_bytes(par->index->sep_rrr) << " Bytes" << endl; strcpy(fileName, ""); strcpy(fileName, par->dirStore); strcat(fileName, "sep_rrr.test"); store_to_file(par->index->sep_rrr, fileName); strcpy(fileName, ""); strcpy(fileName, par->dirStore); strcat(fileName, "sep_rank.test"); store_to_file(par->index->sep_rank, fileName); strcpy(fileName, ""); strcpy(fileName, par->dirStore); strcat(fileName, "EndDocs.test"); ofstream os3 (fileName, ios::binary); os3.write((const char*)par->EndDocs, par->index->nDocs*sizeof(ulong)); // save LbRev[] if(TRACE) cout << " .- EndDocs[] " << par->index->nDocs*sizeof(ulong) << " Bytes" << endl; os3.close(); // load Sequence... strcpy(fileName, ""); strcpy(fileName, par->dirStore); strcat(fileName, "sequence.test"); ifstream is(fileName, ios::binary); par->seq = new uchar[par->n]; is.read((char*)par->seq, par->n*sizeof(uchar)); is.close(); cout << "Running test searchPattern.." << endl; testSearchPatt_DL_CSA_LZ(par); cout << "Test searchPattern OK !!" << endl; } par->index->~LZDocList64(); cout << "$$$$$$$$$$$$$$$$$$$$$" << endl; return 0; }
int main(int argc, char *argv[]) { // We need all parameters, otherwise the mesh cannot be created correctly if (argc != 14) { std::cout << "usage: " << argv[0] << " type nx ny min_x min_y max_x max_y bcids factor loading ul_lr dead-axis filename\n"; std::cout << "type: Q|q for Quad-4, T|t for Tri-3\n" << "nx: no. elements on primary axis\n" << "ny: no. elements on secondary axis\n" << "min_x: minimum position on the primary axis\n" << "min_y: minimum position on the secondary axis\n" << "max_x: maximum position on the primary axis\n" << "max_y: maximum position on the secondary axis\n" << "bcids: comma-separated list of boundary condition IDs in the ordering top,bottom,left,right border (e.g. 2,0,20,21). If border has no BC, type -1\n" << "factor: global factor multiplied on all force entries in the force file\n" << "loading: 0 for no loading at all, 1 for concentrated load on central node, 2 for uniform load on entire mesh\n" << "ul_lr: 1 if triangle hypotenuse should face the lower right corner of the divided square, 0 for 90° rotated orientation. If type is Quad-4, the value doesn't matter.\n" << "dead-axis: 'x','y' or 'z' to specify which axis should be considered dead. The resulting mesh will lie in the 'yz','xz' or 'xy'-plane, respectively.\n" << "filename: the name of the mesh file to create\n"; return -1; } char type = argv[1][0]; // edit here, for more types in the future: if (type != 'Q' && type != 'q' && type != 'T' && type != 't') { std::cout << "Invalid element type specified: \"" << type << "\". " << "Only 'Q'|'q' and 'T'|'t' are allowed.\n"; return -1; } // bring the type into lower case for better management // in the rest of the program: if (type == 'Q') type = 'q'; else if (type == 'T') type = 't'; int nx = atoi(argv[2]); if (nx <= 0) { std::cout << "Invalid number of elements on primary axis! Only positive integer values allowed.\n"; return -1; } int ny = atoi(argv[3]); if (ny <= 0) { std::cout << "Invalid number of elements on secondary axis! Only positive integer values allowed.\n"; return -1; } double min_x = atof(argv[4]); double min_y = atof(argv[5]); double max_x = atof(argv[6]); double max_y = atof(argv[7]); // get comma-separated list of boundary condition IDs // expected format: int,int,int,int std::string bcids = argv[8]; std::size_t first = bcids.find_first_of(","); int curPos = 0; int t_bcid = -1; if (first != std::string::npos) { std::string str = bcids.substr(0, first); t_bcid = std::stoi(str); } curPos = first+1; first = bcids.find_first_of(",", first+1); int b_bcid = -1; if (first != std::string::npos) { std::string str = bcids.substr(curPos, first-curPos); b_bcid = std::stoi(str); } curPos = first+1; first = bcids.find_first_of(",", first+1); int l_bcid = -1; if (first != std::string::npos) { std::string str = bcids.substr(curPos, first-curPos); l_bcid = std::stoi(str); } curPos = first+1; std::string str = bcids.substr(curPos, bcids.size()); int r_bcid = std::stoi(str); double factor = atof(argv[9]); int loading = atoi(argv[10]); bool ul_lr = atoi(argv[11])==1? true : false; // upper left to lower right diagonal, or ur_ll? char deadAxis = argv[12][0]; char* fname = argv[13]; bool bleft = false; if (l_bcid >= 0) bleft = true; bool bright = false; if (r_bcid >= 0) bright = true; bool btop = false; if (t_bcid >= 0) btop = true; bool bbottom = false; if (b_bcid >= 0) bbottom = true; if (deadAxis != 'x' && deadAxis != 'y' && deadAxis != 'z') { std::cout << "Invalid parameter for dead axis: \"" << deadAxis << "\". Only 'x','y' and 'z' are allowed.\n"; return -1; } int n_elem = nx*ny; if (type == 't') // we split every rectangle into two triangles n_elem *= 2; int n_nodes = (nx+1)*(ny+1); std::vector<std::vector<double> > nodes; std::vector<std::vector<int> > elem; double fracx = (max_x-min_x)/(double)nx; double fracy = (max_y-min_y)/(double)ny; // creates nodes for (int y = 0; y <= ny; y++) { std::vector<double> node(3, 0.0); if (deadAxis == 'z') // secondary axis is y node[1] = min_y + y*fracy; else // deadAxis = y|x -> secondary axis is z node[2] = min_y + y*fracy; for (int x = 0; x <= nx; x++) { if (deadAxis == 'x') // primary axis is y node[1] = (min_x + x*fracx); else // deadAxis = y|z -> primary axis is x node[0] = (min_x + x*fracx); nodes.push_back(node); } } // create elements for (int y = 0; y < ny; y++) { if (type == 'q') { std::vector<int> quad(4); for (int x = 0; x < nx; x++) { /* 3-----2 | | | | 0-----1 */ int n_id = x + y*(nx+1); quad[0] = n_id; quad[1] = n_id + 1; quad[2] = n_id + (nx+1) + 1; quad[3] = n_id + (nx+1); elem.push_back(quad); } } else if (type == 't') { std::vector<int> tri1(3); std::vector<int> tri2(3); for (int x = 0; x < nx; x++) { int n_id = x + y*(nx+1); if (ul_lr) { /* 2|2---1 |\\ | | \\ | | \\ | 0---1|0 */ tri1[0] = n_id; tri1[1] = n_id + 1; tri1[2] = n_id + (nx+1); tri2[0] = n_id + 1; tri2[1] = n_id + (nx+1) + 1; tri2[2] = n_id + (nx+1); } else { /* 2---0|1 | //| | // | | // | 1|0---2 */ tri1[0] = n_id; tri1[1] = n_id + (nx+1) + 1; tri1[2] = n_id + 1; tri2[0] = n_id + (nx+1) + 1; tri2[1] = n_id; tri2[2] = n_id + (nx+1); } elem.push_back(tri1); elem.push_back(tri2); } } } std::string meshname = fname; meshname += ".xda"; std::filebuf fb; fb.open (meshname.c_str(), std::ios::out); std::ostream os(&fb); // The header of the XDA mesh file: os << "libMesh-0.7.0+\n"; os << n_elem << " # number of elements\n"; os << n_nodes << " # number of nodes\n"; os << ". # boundary condition specification file\n"; os << "n/a # subdomain id specification file\n"; os << "n/a # processor id specification file\n"; os << "n/a # p-level specification file\n"; os << n_elem << " # n_elem at level 0, [ type (n0 ... nN-1) ]\n"; // write the elements: char elType = 't'; if (type == 't') elType = '3'; else if (type == 'q') elType = '5'; for (unsigned int i = 0; i < elem.size(); i++) { std::vector<int> cur_elem = elem[i]; os << elType; for (unsigned int k = 0; k < cur_elem.size(); k++) os << " " << cur_elem[k]; os << "\n"; } // write the node coordinates: for (unsigned int i = 0; i < nodes.size(); i++) { std::vector<double> cur_node = nodes[i]; os << cur_node[0] << " " << cur_node[1] << " " << cur_node[2] << "\n"; } // calculate the number of edges with boundary conditions: int noBC = 0; if (bleft) noBC += ny; if (bright) noBC += ny; if (btop) noBC += nx; if (bbottom) noBC += nx; os << noBC << " # number of boundary conditions\n"; // boundary condition format: // x y z // x: ID of element // y: number of edge of element. // edge 0 from vertex 0 to 1, // edge 1 from vertex 1 to 2, etc. // z: BC ID // top and bottom borders: for (int i = 0; i < nx; i++) { if (type == 't') { if (ul_lr) { if (bbottom) os << 2*i << " 0 " << b_bcid << "\n"; // bottom border if (btop) os << 2*nx*ny-2*i-1 << " 1 " << t_bcid << "\n"; // top border } else { if (bbottom) os << 2*i << " 2 " << b_bcid << "\n"; // bottom border if (btop) os << 2*nx*ny-2*i-1 << " 2 " << t_bcid << "\n"; // top border } } else if (type == 'q') { if (bbottom) os << i << " 0 " << b_bcid << "\n"; // bottom border if (btop) os << nx*ny-1-i << " 2 " << t_bcid << "\n"; // top border } } // left and right borders: for (int i = 0; i < ny; i++) { if (type == 't') { if (ul_lr) { if (bleft) os << 2*nx*i << " 2 " << l_bcid << "\n"; // left border if (bright) os << 2*nx*(i+1)-1 << " 0 " << r_bcid << "\n"; // right border } else { if (bleft) os << 2*nx*i+1 << " 1 " << l_bcid << "\n"; // left border if (bright) os << 2*nx*(i+1)-2 << " 1 " << r_bcid << "\n"; // right border } } else if (type == 'q') { if (bleft) os << nx*i << " 3 " << l_bcid << "\n"; // left border if (bright) os << nx*(i+1)-1 << " 1 " << r_bcid << "\n"; // right border } } fb.close(); if (loading <= 0) // if no loading is desired, we are done. return 0; std::string forcename = fname; forcename += "_f"; // convention: force file is named like mesh file with "_f" at the end fb.open (forcename.c_str(), std::ios::out); std::ostream os2(&fb); os2 << n_nodes << "\n"; if (loading == 1) // concentrated loading at central node { os2 << factor << "\n"; for (unsigned int i = 0; i < nodes.size()-1; i++) { // force is applied perpendicular to mesh plane (for plate testing) // direction of force could be also command-line argument (for the future) if (i == (unsigned)n_nodes/2) if (deadAxis == 'x') os2 << "1 0 0 0 0 0\n"; else if (deadAxis == 'y') os2 << "0 1 0 0 0 0\n"; else os2 << "0 0 1 0 0 0\n"; else os2 << "0 0 0 0 0 0\n"; } } else if (loading == 2) // uniformly distributed loading { // convert area force to nodal force: // factor * elem_x_len * elem_y_len / #nodes_of_elem * #neighboring_elements_sharing_this_node // for quad's: f*xlen*ylen / 4 * 4 -> f*xlen*ylen // for tri's: f*xlen*ylen/2 / 3 * 6 -> f*xlen*ylen os2 << (factor*((max_x-min_x)/(double)nx)*((max_y-min_y)/(double)ny)) << "\n"; // force is applied perpendicular to mesh plane (for plate testing) // direction of force could be also command-line argument (for the future) if (deadAxis == 'x') for (unsigned int i = 0; i < nodes.size()-1; i++) os2 << "1 0 0 0 0 0\n"; else if (deadAxis == 'y') for (unsigned int i = 0; i < nodes.size()-1; i++) os2 << "0 1 0 0 0 0\n"; else for (unsigned int i = 0; i < nodes.size()-1; i++) os2 << "0 0 1 0 0 0\n"; } fb.close(); return 0; }
int main() { double data[][5] = { { 0.1, 0.2, 0.3, 0.4, 0.5 }, { 1.1, 1.2, 1.3, 1.4, 1.5 }, { 2.1, 2.2, 2.3, 2.4, 2.5 }, }; std::wstring fileName = L"data.dat"; std::ofstream os(ws2s(fileName)); if (os.is_open()) { for (size_t j = 0; j < ARRAYSIZE(data[0]); ++j) { for (size_t i = 0; i < ARRAYSIZE(data); ++i) os.write(reinterpret_cast<const char *>(&data[i][j]), sizeof data[i][j]); } os.close(); } SharedFileDataSourcePtr dataSource = std::make_shared<SimpleFileDataSource<double>>(3, 1); Verify(dataSource->ReadFile(fileName)); for (int i = 0; i < dataSource->GetChannelCount(); ++i) { for (int j = 0; j < dataSource->GetLength(); ++j) std::cout << dataSource->Read(i, j, 1)[0] << "\t"; std::cout << std::endl; } DIVIDING_LINE_2('-', 79); short data2[][5] = { { short(0.1 * 1000), short(0.2 * 1000), short(0.3 * 1000), short(0.4 * 1000), short(0.5 * 1000) }, { short(1.1 * 1000), short(1.2 * 1000), short(1.3 * 1000), short(1.4 * 1000), short(1.5 * 1000) }, { short(2.1 * 1000), short(2.2 * 1000), short(2.3 * 1000), short(2.4 * 1000), short(2.5 * 1000) }, }; std::ofstream os2(ws2s(fileName)); if (os2.is_open()) { for (size_t j = 0; j < ARRAYSIZE(data2[0]); ++j) { for (size_t i = 0; i < ARRAYSIZE(data2); ++i) os2.write(reinterpret_cast<const char *>(&data2[i][j]), sizeof data2[i][j]); } os2.close(); } dataSource = std::make_shared<SimpleFileDataSource<short>>(3, 1); Verify(dataSource->ReadFile(fileName)); for (int i = 0; i < dataSource->GetChannelCount(); ++i) { for (int j = 0; j < dataSource->GetLength(); ++j) std::cout << dataSource->Read(i, j, 1)[0] << "\t"; std::cout << std::endl; } return 0; }
void MetaDataTable::readStarLoop(std::ifstream& in, std::vector<EMDLabel> *desiredLabels) { setIsList(false); //Read column labels int labelPosition = 0; EMDLabel label; std::string line, token, value; // First read all the column labels while (getline(in, line, '\n')) { line = simplify(line); // TODO: handle comments... if (line[0] == '#' || line[0] == '\0' || line[0] == ';') continue; if (line[0] == '_') // label definition line { //Only take string from "_" until "#" token = line.substr(line.find("_") + 1, line.find("#") - 2); label = EMDL::str2Label(token); //std::cerr << " label= XX" << label << "XX token= XX" << token<<"XX" << std::endl; if (desiredLabels != NULL && !vectorContainsLabel(*desiredLabels, label)) label = EMDL_UNDEFINED; //ignore if not present in desiredLabels if (label == EMDL_UNDEFINED) { //std::cerr << "Warning: ignoring the following (undefined) label:" <<token << std::endl; REPORT_ERROR("ERROR: Unrecognised metadata label: " + token); ignoreLabels.push_back(labelPosition); } else activeLabels.push_back(label); labelPosition++; } else // found first data line { break; } } // Then fill the table (dont read another line until the one from above has been handled) bool is_first= true; while (is_first || getline(in, line, '\n')) { is_first=false; line = simplify(line); // Stop at empty line if (line[0] == '\0') break; // Add a new line to the table addObject(); // Parse data values std::stringstream os2(line); std::string value; labelPosition = 0; int counterIgnored = 0; while (os2 >> value) { // TODO: handle comments here... if (std::find(ignoreLabels.begin(), ignoreLabels.end(), labelPosition) != ignoreLabels.end()) { // Ignore this column counterIgnored++; labelPosition++; continue; } setValueFromString(activeLabels[labelPosition - counterIgnored], value); labelPosition++; } } }
void pop_data::print(std::ostream &out, const std::vector<std::string> &l, size_t cw, size_t prec, size_t off) const { if (m_sets.size() == 0) return; // Check if all data sets have the correct size for (iterator i = m_sets.begin(); i != m_sets.end(); i++) { if (l.size() != i->data.size()) { throw libwfa_exception("pop_data", "print(...) const", __FILE__, __LINE__, "Length of population data."); } } // Compute max width of an index number size_t nw1 = 0, nl = l.size(); while (nl != 0) { nl /= 10; nw1++; } // Compute max width of a label size_t nw2 = 0; for (std::vector<std::string>::const_iterator i = l.begin(); i != l.end(); i++) { nw2 = std::max(nw2, i->size()); } // Compute width of initial columns size_t nw = nw1 + nw2 + 1; nw = std::max(nw, (size_t) 4); std::string os1(off, ' '), os2(nw - (nw1 + nw2 + 1) + off, ' '); // Reduce column width, if total width is wider than 80 characters size_t maxwidth = 80; size_t mincolwidth = prec + 4; size_t width = off + nw + cw * m_sets.size(); while (width > maxwidth && cw > mincolwidth) { width -= m_sets.size(); cw--; } // Print header out << std::right << std::fixed << std::setprecision(prec); out << os1 << std::setw(nw) << "Atom"; for (pop_data::iterator i = m_sets.begin(); i != m_sets.end(); i++) { out << std::setw(cw) << i->name; } out << std::endl; out << os1 << std::string(width - off, '-') << std::endl; arma::vec total(m_sets.size(), arma::fill::zeros); for (size_t i = 0, j = 1; i != l.size(); i++, j++) { out << os2 << std::setw(nw1) << j; out << " " << std::setw(nw2) << l[i]; size_t k = 0; for (iterator kk = m_sets.begin(); kk != m_sets.end(); k++, kk++) { total(k) += kk->data(i); out << std::setw(cw) << kk->data(i); } out << std::endl; } // sum out << os1 << std::string(width - off, '-') << std::endl; out << std::right; out << os1 << std::setw(nw) << "Sum:"; for (size_t i = 0; i < total.size(); i++) out << std::setw(cw) << total(i); out << std::endl; }