BOOL TInifile::WriteIni() { Lock(); BOOL ret = FALSE; HANDLE hFile = ::CreateFileW(iniFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (hFile != INVALID_HANDLE_VALUE) { #define MIN_INI_ALLOC (64 * 1024) #define MAX_INI_ALLOC (10 * 1024 * 1024) #define MIN_LINE_SIZE (2 * 1024) VBuf vbuf(MIN_INI_ALLOC, MAX_INI_ALLOC); char *p = (char *)vbuf.Buf(); for (TIniSection *sec = TopObj(); sec && p; sec = NextObj(sec)) { TIniKey *key = sec->TopObj(); int len = 0; if (key) { if (sec->Name()) { len = sprintf(p, "[%s]\r\n", sec->Name()); p = NextBuf(&vbuf, len, MIN_LINE_SIZE, MIN_INI_ALLOC); } while (key) { if (key->Key()) { len = sprintf(p, "%s=\"%s\"\r\n", key->Key(), key->Val()); p = NextBuf(&vbuf, len, MIN_LINE_SIZE, MIN_INI_ALLOC); } else { len = sprintf(p, "%s\r\n", key->Val()); p = NextBuf(&vbuf, len, MIN_LINE_SIZE, MIN_INI_ALLOC); } key = sec->NextObj(key); } } } DWORD size; ret = ::WriteFile(hFile, vbuf.Buf(), (DWORD)vbuf.UsedSize(), &size, 0); ::CloseHandle(hFile); } UnLock(); return ret; }
void TInifile::InitCore(WCHAR *_ini_file) { iniFile = _ini_file; Lock(); HANDLE hFile = ::CreateFileW(iniFile, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); AddObj(rootSec = new TIniSection()); if (hFile != INVALID_HANDLE_VALUE) { DWORD size = ::GetFileSize(hFile, 0); // I don't care 4GB over :-) VBuf vbuf(size + 1); // already 0 fill if (::ReadFile(hFile, vbuf, size, &size, 0)) { #define MAX_INI_LINE (64 * 1024) DynBuf buf(MAX_INI_LINE); DynBuf val(MAX_INI_LINE); char name[1024], *tok; BOOL is_section; TIniSection *target_sec=rootSec; for (tok=strtok(vbuf, "\r\n"); tok; tok=strtok(NULL, "\r\n")) { BOOL ret = Parse(tok, &is_section, name, val); if (!ret) { target_sec->AddKey(NULL, tok); } else if (is_section) { target_sec = new TIniSection(); target_sec->Set(name); AddObj(target_sec); } else { target_sec->AddKey(name, val); } } } ::CloseHandle(hFile); // GetFileInfo(iniFile, &iniFt, &iniSize); } UnLock(); }
CreateStatus CreateFileBySelf(char *path, char *fname) { HANDLE hSelfFile = INVALID_HANDLE_VALUE; HANDLE hMap = NULL; BYTE *data = NULL; BYTE *target = NULL; char self_name[MAX_PATH] = ""; DWORD selfSize = 0; DWORD size = 0; CreateStatus ret = CS_BROKEN; GetModuleFileName(::GetModuleHandle(NULL), self_name, sizeof(self_name)); hSelfFile = ::CreateFile(self_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); hMap = ::CreateFileMapping(hSelfFile, 0, PAGE_READONLY, 0, 0, 0); data = (BYTE *)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); if (!data) goto END; selfSize = GetFileSize(hSelfFile, 0); if ((target = FindSeparatedData(data, selfSize, fname, &size))) { VBuf vbuf(FILESIZE_MAX); if (DeflateData(target, size, &vbuf)) { ret = CS_ACCESS; HANDLE hDestFile = ::CreateFile(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (hDestFile != INVALID_HANDLE_VALUE) { if (::WriteFile(hDestFile, vbuf.Buf(), (DWORD)vbuf.UsedSize(), &size, 0)) { ret = CS_OK; } ::CloseHandle(hDestFile); } } } END: ::UnmapViewOfFile(data); ::CloseHandle(hMap); ::CloseHandle(hSelfFile); return ret; }
int ingest1(const char *input,const char *output,char *ref,bool exit_on_mismatch=true) { cerr << "Input: " << input << "\tOutput: "<<output<<endl; kstream_t *ks; kstring_t str = {0,0,0}; gzFile fp = gzopen(input, "r"); VarBuffer vbuf(1000); int prev_rid = -1; if(fp==NULL) { fprintf(stderr,"problem opening %s\n",input); exit(1); } char *out_fname = (char *)malloc(strlen(output)+5); strcpy(out_fname,output); strcat(out_fname,".tmp"); if(fileexists(out_fname)) { fprintf(stderr,"%s file already exists. will not overwrite\n",out_fname); exit(1); } printf("depth: %s\n",out_fname); gzFile depth_fp = gzopen(out_fname, "wb1"); strcpy(out_fname,output); strcat(out_fname,".bcf"); if(fileexists(out_fname)) { fprintf(stderr,"%s file already exists. will not overwrite\n",out_fname); exit(1); } printf("variants: %s\n",out_fname); htsFile *variant_fp=hts_open(out_fname,"wb1"); if(variant_fp==NULL) { fprintf(stderr,"problem opening %s\n",input); exit(1); } ks = ks_init(fp); htsFile *hfp=hts_open(input, "r"); bcf_hdr_t *hdr_in = bcf_hdr_read(hfp); hts_close(hfp); //this is a hack to fix gvcfs where AD is incorrectly defined in the header. (vcf4.2 does not technically allow Number=R) bcf_hdr_remove(hdr_in,BCF_HL_FMT,"AD"); assert( bcf_hdr_append(hdr_in,"##FORMAT=<ID=AD,Number=R,Type=Integer,Description=\"Allelic depths for the ref and alt alleles in the order listed. For indels this value only includes reads which confidently support each allele (posterior prob 0.999 or higher that read contains indicated allele vs all other intersecting indel alleles)\">") == 0); //this is a hack to fix broken gvcfs where GQ is incorrectly labelled as float (v4.3 spec says it should be integer) bcf_hdr_remove(hdr_in,BCF_HL_FMT,"GQ"); assert( bcf_hdr_append(hdr_in,"##FORMAT=<ID=GQ,Number=1,Type=Integer,Description=\"Genotype Quality\">") == 0); // bcf_hdr_t *hdr_out=hdr_in; bcf_hdr_t *hdr_out = bcf_hdr_dup(hdr_in); remove_hdr_lines(hdr_out,BCF_HL_INFO); remove_hdr_lines(hdr_out,BCF_HL_FLT); bcf_hdr_sync(hdr_out); //here we add FORMAT/PF. which is the pass filter flag for alts. assert( bcf_hdr_append(hdr_out,"##FORMAT=<ID=PF,Number=A,Type=Integer,Description=\"variant was PASS filter in original sample gvcf\">") == 0); args_t *norm_args = init_vcfnorm(hdr_out,ref); norm_args->check_ref |= CHECK_REF_WARN; bcf1_t *bcf_rec = bcf_init(); bcf_hdr_write(variant_fp, hdr_out); kstring_t work1 = {0,0,0}; int buf[5]; ks_tokaux_t aux; int ndec=0; int ref_len,alt_len; while( ks_getuntil(ks, '\n', &str, 0) >=0) { // fprintf(stderr,"%s\n",str.s); if(str.s[0]!='#') { char *ptr = kstrtok(str.s,"\t",&aux);//chrom ptr = kstrtok(NULL,NULL,&aux);//pos work1.l=0; kputsn(str.s,ptr-str.s-1, &work1); buf[0] = bcf_hdr_name2id(hdr_in, work1.s); assert( buf[0]>=0); buf[1]=atoi(ptr)-1; ptr = kstrtok(NULL,NULL,&aux);//ID ptr = kstrtok(NULL,NULL,&aux);//REF ref_len=0; while(ptr[ref_len]!='\t') ref_len++; ptr = kstrtok(NULL,NULL,&aux);//ALT bool is_variant=false; alt_len=0; while(ptr[alt_len]!='\t') alt_len++; if(ptr[0]!='.') is_variant=true; char * QUAL_ptr = kstrtok(NULL, NULL, &aux); assert (QUAL_ptr != NULL); for(int i=0;i<2;i++) ptr = kstrtok(NULL,NULL,&aux);// gets us to INFO //find END if it is there char *end_ptr=strstr(ptr,"END=") ; if(end_ptr!=NULL) buf[2]=atoi(end_ptr+4)-1; else buf[2]=buf[1]+alt_len-1; ptr = kstrtok(NULL,NULL,&aux);//FORMAT //find index of DP (if present) //if not present, dont output anything (indels ignored) char *DP_ptr = find_format(ptr,"DP"); int GQX = 0; int QUAL = 0; // AH: change code to use the minimum of GQ and QUAL fields if // GQX is not defined. See here: // https://support.basespace.illumina.com/knowledgebase/articles/144844-vcf-file // "GQXGenotype quality. GQX is the minimum of the GQ value // and the QUAL column. In general, these are similar values; // taking the minimum makes GQX the more conservative measure of // genotype quality." if(DP_ptr!=NULL) { buf[3]=atoi(DP_ptr); char *GQX_ptr = find_format(ptr,"GQX"); if (GQX_ptr == NULL) { GQX_ptr = find_format(ptr,"GQ"); GQX = atoi(GQX_ptr); if (QUAL_ptr[0] != '.') { QUAL = atoi(QUAL_ptr); if (QUAL < GQX) GQX = QUAL; } } else { GQX = atoi(GQX_ptr); } //trying to reduce entropy on GQ to get better compression performance. //1. rounds down to nearest 10. //2. sets gq to min(gq,100). buf[4]=GQX/10; buf[4]*=10; if(buf[4]>100) buf[4]=100; // printf("%d\t%d\t%d\t%d\t%d\n",buf[0],buf[1],buf[2],buf[3],buf[4]); if(gzwrite(depth_fp,buf,5*sizeof(int))!=(5*sizeof(int))) die("ERROR: problem writing "+(string)out_fname+".tmp"); } if(is_variant) {//wass this a variant? if so write it out to the bcf norm_args->ntotal++; vcf_parse(&str,hdr_in,bcf_rec); // cerr<<bcf_rec->rid<<":"<<bcf_rec->pos<<endl; if(prev_rid!=bcf_rec->rid) vbuf.flush(variant_fp,hdr_out); else vbuf.flush(bcf_rec->pos,variant_fp,hdr_out); prev_rid=bcf_rec->rid; int32_t pass = bcf_has_filter(hdr_in, bcf_rec, "."); bcf_update_format_int32(hdr_out,bcf_rec,"PF",&pass,1); bcf_update_filter(hdr_out,bcf_rec,NULL,0); if(bcf_rec->n_allele>2) {//split multi-allelics (using vcfnorm.c from bcftools1.3 norm_args->nsplit++; split_multiallelic_to_biallelics(norm_args,bcf_rec ); for(int i=0;i<norm_args->ntmp_lines;i++){ remove_info(norm_args->tmp_lines[i]); if(realign(norm_args,norm_args->tmp_lines[i]) != ERR_REF_MISMATCH) ndec+=decompose(norm_args->tmp_lines[i],hdr_out,vbuf); else if(exit_on_mismatch) die("vcf did not match the reference"); else norm_args->nskipped++; } } else { remove_info(bcf_rec); if( realign(norm_args,bcf_rec) != ERR_REF_MISMATCH) ndec+=decompose(bcf_rec,hdr_out,vbuf); else if(exit_on_mismatch) die("vcf did not match the reference"); else norm_args->nskipped++; } vbuf.flush(bcf_rec->pos,variant_fp,hdr_out); } } } vbuf.flush(variant_fp,hdr_out); bcf_hdr_destroy(hdr_in); bcf_hdr_destroy(hdr_out); bcf_destroy1(bcf_rec); ks_destroy(ks); gzclose(fp); gzclose(depth_fp); free(str.s); free(work1.s); hts_close(variant_fp); destroy_data(norm_args); fprintf(stderr,"Variant lines total/split/realigned/skipped:\t%d/%d/%d/%d\n", norm_args->ntotal,norm_args->nsplit,norm_args->nchanged,norm_args->nskipped); fprintf(stderr,"Decomposed %d MNPs\n", ndec); fprintf(stderr,"Indexing %s\n",out_fname); bcf_index_build(out_fname, BCF_LIDX_SHIFT); free(out_fname); return 0; }
void RBExport::AddMesh( int meshID, int startVByte, int startVert, int nVert, int startFace, int nFaces ) { if (nVert == 0) return; // create mesh JMesh* pMesh = new JMesh(); char nameBuf[_MAX_PATH]; const char* nodeName = m_Nodes[m_Vertices[startVert]->nodeID].m_pNode->GetName(); sprintf( nameBuf, "%s_mesh", nodeName ); // create skin bone list std::vector<int> boneReindex; boneReindex.resize( m_Nodes.size() ); std::fill( boneReindex.begin(), boneReindex.end(), -1 ); int nMeshBones = 0; for (int i = 0; i < nVert; i++) { ExpVertex& v = *m_Vertices[startVert + i]; for (int j = 0; j < v.nBones; j++) { int boneIdx = v.boneIdx[j]; if (boneIdx == -1) continue; if (boneReindex[boneIdx] == -1) { pMesh->AddBone( m_Nodes[boneIdx].m_pNode->GetName() ); boneReindex[boneIdx] = nMeshBones; nMeshBones++; } v.boneIdx[j] = boneReindex[boneIdx]; } } if (pMesh->GetNSkinBones() > c_MaxBonesPerSkin) { Err( "Mesh %s exceeds bone limit. Number of ones is %d, maximum allowed is %d.", nodeName, pMesh->GetNSkinBones(), c_MaxBonesPerSkin ); } // check whether all the references to bones within indices are in correct range for (int i = 0; i < nVert; i++) { const ExpVertex& v = *(m_Vertices[startVert + i]); for (int j = 0; j < v.nBones; j++) { if (v.boneIdx[j] >= 0 && v.boneIdx[j] < pMesh->GetNSkinBones()) { continue; } Err( "Skin of mesh %s has incorrect bone reference: %d.", nodeName, v.boneIdx[j] ); } } // create vertex declaration VertexDeclaration vertexDecl = GetVDecl( startVert, nVert ); int vertexStride = vertexDecl.m_VertexSize; // create vertex buffer int nVertBytes = nVert*vertexStride; Buffer vbuf( nVertBytes ); FillVertexBuffer( vbuf.GetData(), vertexDecl, startVert, nVert ); // add vertices to model m_pModel->AddVertices( vbuf.GetData(), nVertBytes ); int mtlID = m_Vertices[startVert]->mtlID; const char* mtlName = ""; if (mtlID >= 0) mtlName = m_Materials[mtlID]->GetName(); int nodeID = m_Vertices[startVert]->nodeID; JObject* pHostBone = m_Nodes[nodeID].m_pObject; pMesh->SetName ( nameBuf ); pMesh->SetVisible ( true ); pMesh->SetVertexRange( startVByte, nVert ); pMesh->SetIndexRange ( startFace*3, nFaces*3 ); pMesh->SetVDecl ( vertexDecl ); pMesh->SetMaterial ( mtlName ); pMesh->SetHostBone ( pHostBone->GetName() ); // add faces to model std::vector<WORD> idx; int nIdx = 3*nFaces; idx.resize( nIdx ); for (int j = 0; j < nIdx; j += 3) { ExpFace& face = m_Faces[startFace + j/3]; idx[j + 0] = face.pV0->index - startVert; idx[j + 1] = face.pV1->index - startVert; idx[j + 2] = face.pV2->index - startVert; } m_pModel->AddIndices( &idx[0], nIdx ); m_pModel->AddChild( pMesh ); } // RBExport::AddMeshData
std::shared_ptr<gameplay::Model> OBJWriter::readModel(const boost::filesystem::path& path, const std::shared_ptr<gameplay::ShaderProgram>& shaderProgram, const glm::vec3& ambientColor) const { Assimp::Importer importer; const aiScene* scene = importer.ReadFile((m_basePath / path).string(), aiProcess_JoinIdenticalVertices | aiProcess_Triangulate | aiProcess_ValidateDataStructure | aiProcess_FlipUVs); BOOST_ASSERT(scene != nullptr); auto renderModel = std::make_shared<gameplay::Model>(); for( unsigned int mi = 0; mi < scene->mNumMeshes; ++mi ) { BOOST_LOG_TRIVIAL(info) << "Converting mesh " << mi + 1 << " of " << scene->mNumMeshes << " from " << m_basePath / path; const aiMesh* mesh = scene->mMeshes[mi]; if( mesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE ) BOOST_THROW_EXCEPTION(std::runtime_error("Mesh does not consist of triangles only")); if( !mesh->HasTextureCoords(0) ) BOOST_THROW_EXCEPTION(std::runtime_error("Mesh does not have UV coordinates")); if( mesh->mNumUVComponents[0] != 2 ) BOOST_THROW_EXCEPTION(std::runtime_error("Mesh does not have a 2D UV channel")); if( !mesh->HasFaces() ) BOOST_THROW_EXCEPTION(std::runtime_error("Mesh does not have faces")); if( !mesh->HasPositions() ) BOOST_THROW_EXCEPTION(std::runtime_error("Mesh does not have positions")); std::shared_ptr<gameplay::Mesh> renderMesh; if( mesh->HasNormals() ) { std::vector<VDataNormal> vbuf(mesh->mNumVertices); for( unsigned int i = 0; i < mesh->mNumVertices; ++i ) { vbuf[i].position = glm::vec3{mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z} * static_cast<float>(SectorSize); vbuf[i].normal = glm::vec3{mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z}; vbuf[i].uv = glm::vec2{mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y}; if( mesh->HasVertexColors(0) ) vbuf[i].color = glm::vec4(mesh->mColors[0][i].r, mesh->mColors[0][i].g, mesh->mColors[0][i].b, mesh->mColors[0][i].a); else vbuf[i].color = glm::vec4(ambientColor, 1); } renderMesh = std::make_shared<gameplay::Mesh>(VDataNormal::getFormat(), mesh->mNumVertices, false); renderMesh->rebuild(reinterpret_cast<const float*>(vbuf.data()), mesh->mNumVertices); } else { std::vector<VData> vbuf(mesh->mNumVertices); for( unsigned int i = 0; i < mesh->mNumVertices; ++i ) { vbuf[i].position = glm::vec3{mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z} * static_cast<float>(SectorSize); vbuf[i].uv = glm::vec2{mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y}; if( mesh->HasVertexColors(0) ) vbuf[i].color = glm::vec4(mesh->mColors[0][i].r, mesh->mColors[0][i].g, mesh->mColors[0][i].b, mesh->mColors[0][i].a); else vbuf[i].color = glm::vec4(ambientColor, 1); } renderMesh = std::make_shared<gameplay::Mesh>(VData::getFormat(), mesh->mNumVertices, false); renderMesh->rebuild(reinterpret_cast<const float*>(vbuf.data()), mesh->mNumVertices); } std::vector<uint32_t> faces; for( const aiFace& face : gsl::span<aiFace>(mesh->mFaces, mesh->mNumFaces) ) { BOOST_ASSERT(face.mNumIndices == 3); faces.push_back(face.mIndices[0]); faces.push_back(face.mIndices[1]); faces.push_back(face.mIndices[2]); } auto part = renderMesh->addPart(gameplay::Mesh::TRIANGLES, gameplay::Mesh::INDEX32, mesh->mNumFaces * 3, false); part->setIndexData(faces.data(), 0, faces.size()); const aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; aiString textureName; if( material->GetTexture(aiTextureType_DIFFUSE, 0, &textureName) != aiReturn_SUCCESS ) BOOST_THROW_EXCEPTION(std::runtime_error("Failed to get diffuse texture path from mesh")); part->setMaterial(readMaterial(textureName.C_Str(), shaderProgram)); renderModel->addMesh(renderMesh); } return renderModel; }