bool CDIF_DumpCD(const char *fn) { FILE *fp; if(!(fp = fopen(fn, "wb"))) { ErrnoHolder ene(errno); printf("File open error: %s\n", ene.StrError()); return(0); } for(long long i = 0; i < toc.tracks[100].lba; i++) { uint8 buf[2352 + 96]; CDIF_ReadRawSector(buf, i); if(fwrite(buf, 1, 2352 + 96, fp) != 2352 + 96) { ErrnoHolder ene(errno); printf("File write error: %s\n", ene.StrError()); } } if(fclose(fp)) { ErrnoHolder ene(errno); printf("fclose error: %s\n", ene.StrError()); } return(1); }
uint64 FileStream::size(void) { #ifdef WIN32 LARGE_INTEGER fsz; if(OpenedMode != MODE_READ && fflush(fp) == EOF) { ErrnoHolder ene(errno); throw MDFN_Error(ene.Errno(), _("Error getting the size of opened file \"%s\": %s"), path_save.c_str(), ene.StrError()); } if(!GetFileSizeEx((HANDLE)_get_osfhandle(fileno(fp)), &fsz)) { const uint32 ec = GetLastError(); throw MDFN_Error(0, _("Error getting the size of opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } return (uint64)fsz.QuadPart; #else STRUCT_STAT buf; if((OpenedMode != MODE_READ && fflush(fp) == EOF) || fstat(fileno(fp), &buf) == -1) { ErrnoHolder ene(errno); throw MDFN_Error(ene.Errno(), _("Error getting the size of opened file \"%s\": %s"), path_save.c_str(), ene.StrError()); } return (std::make_unsigned<decltype(buf.st_size)>::type)buf.st_size; #endif }
void FileStream::truncate(uint64 length) { #ifdef WIN32 if(fflush(fp) == EOF) { ErrnoHolder ene(errno); throw MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError()); } LARGE_INTEGER pos_set; LARGE_INTEGER pos_save; HANDLE fhand = (HANDLE)_get_osfhandle(fileno(fp)); pos_set.QuadPart = 0; if(!SetFilePointerEx(fhand, pos_set, &pos_save, FILE_CURRENT)) { const uint32 ec = GetLastError(); throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } pos_set.QuadPart = length; if(!SetFilePointerEx(fhand, pos_set, nullptr, FILE_BEGIN)) { const uint32 ec = GetLastError(); throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } if(!SetEndOfFile(fhand)) { const uint32 ec = GetLastError(); SetFilePointerEx(fhand, pos_save, nullptr, FILE_BEGIN); throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } if(!SetFilePointerEx(fhand, pos_save, nullptr, FILE_BEGIN)) { // Can SetFilePointerEx() fail here? const uint32 ec = GetLastError(); throw MDFN_Error(0, _("Error truncating opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } #else if(fflush(fp) == EOF || ftruncate(fileno(fp), length) != 0) { ErrnoHolder ene(errno); throw MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError()); } #endif }
/*! Precondition: The parent type is PARENT_VERTEX. */ int RFC_Pane_base::get_parent_node( int id) const { const ParentEdge_ID &eid=_subnode_parents[ id-1]; Element_node_enumerator ene(_base, eid.face_id); return ene[eid.edge_id]; }
void Pane_ghost_connectivity:: mark_elems_from_nodes(std::vector<std::vector<bool> > &marked_nodes, std::vector<std::vector<bool> > &marked_elems){ marked_elems.clear(); marked_elems.resize(_npanes); //Loop through panes for(int i=0; i < (int)(_npanes); ++i){ marked_elems[i].clear(); marked_elems[i].resize(_panes[i]->size_of_real_elements(),false); // Loop through real elements. // Mark for quality check if they contain shared nodes. int s_real_elems = _panes[i]->size_of_real_elements(); std::vector<int> nodes; for(int j=1; j<= s_real_elems; ++j){ COM::Element_node_enumerator ene(_panes[i],j); ene.get_nodes(nodes); for(int k=0, nk=nodes.size(); k<nk; ++k){ if (marked_nodes[i][nodes[k]-1]) marked_elems[i][j-1] = true; } } } }
uint64 FileStream::read(void *data, uint64 count, bool error_on_eos) { uint64 read_count; if(prev_was_write == 1) seek(0, SEEK_CUR); clearerr(fp); read_count = fread(data, 1, count, fp); if(read_count != count) { ErrnoHolder ene(errno); if(ferror(fp)) throw(MDFN_Error(ene.Errno(), _("Error reading from opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); if(error_on_eos) throw(MDFN_Error(0, _("Error reading from opened file \"%s\": %s"), path_save.c_str(), _("Unexpected EOF"))); } prev_was_write = 0; return(read_count); }
// Return FALSE on fatal error(IPS file found but couldn't be applied), // or TRUE on success(IPS patching succeeded, or IPS file not found). static bool LoadIPS(MDFNFILE &GameFile, const char *path) { FILE *IPSFile; MDFN_printf(_("Applying IPS file \"%s\"...\n"), path); IPSFile = fopen(path, "rb"); if(!IPSFile) { ErrnoHolder ene(errno); MDFN_printf(_("Failed: %s\n"), ene.StrError()); if(ene.Errno() == ENOENT) return(1); else { MDFN_PrintError(_("Error opening IPS file: %s\n"), ene.StrError()); return(0); } } if(!GameFile.ApplyIPS(IPSFile)) { fclose(IPSFile); return(0); } fclose(IPSFile); return(1); }
void EnemyManager::createTypeOne(sf::Vector2f pos, unsigned int level) { Enemy ene(level); ene.sprite.setTexture(m_resMan.texture("Enemy1")); auto& edAni = m_resMan.frameAnimation("Enemy Default"); m_resMan.addFrame("Enemy Default", sf::seconds(1.f), sf::IntRect(0, 0, m_resMan.texture("Enemy1").getSize().x, m_resMan.texture("Enemy1").getSize().y)); ene.animator.addAnimation("Default", edAni, sf::seconds(1.f)); //m_resMan.addAnimation("Enemy1Ani", "Enemy1"); //m_resMan.addAniFrame("Enemy1Ani", sf::Rect<int>(0, 0, 30, 36)); //ene.sprite.setAnimation(m_resMan.getAnimation("Enemy1Ani")); ene.sprite.setOrigin(ene.sprite.getGlobalBounds().width / 2.f, ene.sprite.getGlobalBounds().height / 2.f); ene.sprite.setPosition(pos); m_enemies.push_back(ene); }
int FileWrapper::scanf(const char *format, ...) { va_list ap; int ret; clearerr(fp); va_start(ap, format); ret = vfscanf(fp, format, ap); va_end(ap); if(ferror(fp)) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error reading from opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } //if(ret < 0 || ret == EOF) // throw(MDFN_Error(0, _("%s error on format string \"%s\""), "trio_vfscanf()", format)); return(ret); }
/// Obtain shortest edge length of incident edges of each element. void Window_manifold_2:: compute_shortest_edgelen_elements( COM::DataItem *lens) { std::vector< COM:: Pane*> panes; lens->window()-> panes( panes); std::vector< COM::Pane*>::const_iterator it = panes.begin(); for (int i=0, local_npanes = panes.size(); i<local_npanes; ++i, ++it){ const COM::Pane &pane = **it; const Point_3<Real> *pnts = reinterpret_cast<const Point_3<Real>*>(pane.coordinates()); Real *lp = (Real *)(pane.dataitem( lens->id())->pointer()); // Loop through elements of the pane Element_node_enumerator ene( &pane, 1); for ( int j=pane.size_of_elements(); j>0; --j, ene.next(),++lp) { Real sqlen = HUGE_VAL; int uindex = ene[0]-1; for (int k = 0, ne=ene.size_of_edges(); k<ne; k++){ int vindex=ene[(k+1)%ne]-1; sqlen = std::min( sqlen, (pnts[ uindex]-pnts[ vindex]).squared_norm()); uindex = vindex; } *lp = std::sqrt(sqlen); } } }
void FileStream::lock(bool nb) { if(locked) return; #ifdef WIN32 OVERLAPPED olp; memset(&olp, 0, sizeof(OVERLAPPED)); olp.Offset = ~(DWORD)0; olp.OffsetHigh = ~(DWORD)0; if(!LockFileEx((HANDLE)_get_osfhandle(fileno(fp)), LOCKFILE_EXCLUSIVE_LOCK | (nb ? LOCKFILE_FAIL_IMMEDIATELY : 0), 0, 1, 0, &olp)) { const uint32 ec = GetLastError(); throw MDFN_Error((ec == ERROR_LOCK_VIOLATION) ? EWOULDBLOCK : 0, _("Error locking opened file \"%s\": %s"), path_save.c_str(), Win32Common::ErrCodeToString(ec).c_str()); } #else if(flock(fileno(fp), LOCK_EX | (nb ? LOCK_NB : 0)) == -1) { ErrnoHolder ene(errno); throw MDFN_Error(ene.Errno(), _("Error locking opened file \"%s\": %s"), path_save.c_str(), ene.StrError()); } #endif locked = true; }
FileWrapper::FileWrapper(const char *path, const int mode, const char *purpose) : OpenedMode(mode) { path_save = std::string(path); if(mode == MODE_READ) fp = fopen(path, "rb"); else if(mode == MODE_WRITE) fp = fopen(path, "wb"); else if(mode == MODE_WRITE_SAFE) // SO ANNOYING { int open_flags = O_WRONLY | O_CREAT | O_EXCL; #ifdef O_BINARY open_flags |= O_BINARY; #elif defined(_O_BINARY) open_flags |= _O_BINARY; #endif #if defined(S_IRGRP) && defined(S_IROTH) int tmpfd = open(path, open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); #else int tmpfd = open(path, open_flags, S_IRUSR | S_IWUSR); #endif if(tmpfd == -1) { ErrnoHolder ene(errno); if(purpose) throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\" for \"%s\": %s"), path_save.c_str(), purpose, ene.StrError())); else throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError())); } fp = fdopen(tmpfd, "wb"); } if(!fp) { ErrnoHolder ene(errno); if(purpose) throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\" for \"%s\": %s"), path_save.c_str(), purpose, ene.StrError())); else throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
void FileWrapper::flush(void) { if(fflush(fp) == EOF) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error flushing to opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
void FileWrapper::write(const void *data, uint64 count) { if(fwrite(data, 1, count, fp) != count) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error writing to opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
void Window_manifold_2::serialize_window( COM::Window *outwin) const { // Clearn up the output window outwin->delete_pane(0); // Build a renumbering of nodes std::vector<std::vector<int> > nodes_gids; assign_global_nodeIDs( nodes_gids); Access_Mode mode = ACROSS_PANE; // Create a pane int nnodes=size_of_nodes( mode); outwin->set_size( "nc", 1, nnodes); double *coors; outwin->resize_array( "nc",1,(void**)&coors); // Mesh vertices int gid=0; // Loop through the panes to write the nodes PM_const_iterator it=pm_begin(), iend=pm_end(); for ( ; it!=iend; ++it) { // const COM::Pane &pn = *it->pane(); const COM::Pane &pn = *(*it)->pane(); const double *p = pn.coordinates(); for ( int i=0, nn=pn.size_of_real_nodes(); i<nn; ++i) { // if ( it->is_primary( i+1, mode)) { if ( (*it)->is_primary( i+1, mode)) { std::copy( &p[3*i], &p[3*i+3], &coors[3*gid]); ++gid; } } } COM_assertion( gid==nnodes); // Mesh triangles. int *tris; outwin->set_size( ":t3:", 1, size_of_faces(REAL_PANE)); outwin->resize_array( ":t3:", 1, (void**)&tris); int i=0, k=0; for ( it=pm_begin(); it!=iend; ++it, ++i) { // int fn=it->size_of_faces(REAL_PANE); if ( fn==0) continue; int fn=(*it)->size_of_faces(REAL_PANE); if ( fn==0) continue; // Element_node_enumerator ene( it->pane(), 1); Element_node_enumerator ene( (*it)->pane(), 1); for ( int f=0; f<fn; ++f, ++k, ene.next()) { for ( int j=0; j<3; ++j) { tris[3*k+j] = nodes_gids[i][ene[j]-1]; } } } outwin->init_done(); }
void FileStream::truncate(uint64 length) { if(fflush(fp) == EOF) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
void FileWrapper::seek(int64 offset, int whence) { if(fseeko(fp, offset, whence) == -1) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error seeking in opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
void FileWrapper::put_char(int c) { if(fputc(c, fp) == EOF) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error writing to opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
FileStream::FileStream(const std::string& path, const int mode) : OpenedMode(mode), mapping(NULL), mapping_size(0) { path_save = path; if(mode == MODE_READ) fp = fopen(path.c_str(), "rb"); else if(mode == MODE_WRITE) fp = fopen(path.c_str(), "wb"); else if(mode == MODE_WRITE_SAFE || mode == MODE_WRITE_INPLACE) // SO ANNOYING { int open_flags = O_WRONLY | O_CREAT; if(mode == MODE_WRITE_SAFE) open_flags |= O_EXCL; #ifdef O_BINARY open_flags |= O_BINARY; #elif defined(_O_BINARY) open_flags |= _O_BINARY; #endif #if defined(S_IRGRP) && defined(S_IROTH) int tmpfd = open(path.c_str(), open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); #else int tmpfd = open(path.c_str(), open_flags, S_IRUSR | S_IWUSR); #endif if(tmpfd == -1) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError())); } fp = fdopen(tmpfd, "wb"); } else abort(); if(!fp) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
void FileStream::truncate(uint64 length) { //not needed by mednadisc //if(fflush(fp) == EOF || ftruncate(fileno(fp), length) != 0) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } }
FileStream::FileStream(const char *path, const int mode) { fp = filestream_open(path, (mode == MODE_WRITE || mode == MODE_WRITE_INPLACE) ? RETRO_VFS_FILE_ACCESS_WRITE : RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE); if (!fp) { ErrnoHolder ene(errno); MDFN_Error(ene.Errno(), "Error opening file:\n%s\n%s", path, ene.StrError()); } }
void Pane_dual_connectivity::construct_connectivity_unstr() { int nnodes, nelems; if ( _with_ghost) { nnodes=_pane.size_of_nodes(); nelems=_pane.size_of_elements(); } else { nnodes=_pane.size_of_real_nodes(); nelems=_pane.size_of_real_elements(); } { // First, count the number of incident elements of each node. std::vector<char> nielems(nnodes); std::fill_n( nielems.begin(), nnodes, 0); Element_node_enumerator_uns ene( &_pane, 1); for ( int i=1; i<=nelems; ++i, ene.next()) for ( int k=0, nn=ene.size_of_nodes(); k<nn; ++k) ++nielems[ ene[k]-1]; // Now construct a vector for storing the offsets _offsets.clear(); _offsets.resize(nnodes+1); _offsets[0] = 0; for ( int i=1; i<=nnodes; ++i) _offsets[i] = _offsets[i-1]+nielems[i-1]; _eids.resize( _offsets[nnodes]); } { // Fill in the connectivity table Element_node_enumerator_uns ene( &_pane, 1); for ( int i=1; i<=nelems; ++i, ene.next()) for ( int k=0, nn=ene.size_of_nodes(); k<nn; ++k) _eids[ _offsets[ene[k]-1]++] = i; // Recover _offsets for ( int i=nnodes; i>0; --i) _offsets[i] = _offsets[i-1]; _offsets[0] = 0; } }
uint64 FileStream::tell(void) { auto offset = ftello(fp); if(offset == -1) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error getting position in opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } return (std::make_unsigned<decltype(offset)>::type)offset; }
uint64 FileStream::size(void) { STRUCT_STAT buf; if((OpenedMode != MODE_READ && fflush(fp) == EOF) || fstat(fileno(fp), &buf) == -1) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error getting the size of opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } return (std::make_unsigned<decltype(buf.st_size)>::type)buf.st_size; }
Point_3 RFC_Pane_base::get_point_of_subnode( int i) const { const int fid = _subnode_parents[i-1].face_id; Element_node_enumerator ene( _base, fid); Point_2 nc; get_nat_coor_in_element( i, ene, nc); Nodal_coor_const coors; Field< const Nodal_coor_const, Element_node_enumerator> ps( coors, coordinates(), ene); return SURF::Generic_element_2( ene.size_of_edges(), ene.size_of_nodes()).interpolate(ps, nc); }
void RFC_Pane_base::comp_nat_coors() { int nsn=size_of_subnodes(); _subnode_normalized_nc.resize( nsn); _subnode_subID.resize( size_of_nodes(), -1); // Initialize _subnode_normalized_nc. for ( int i=0; i<nsn; ++i) { int f=_subnode_parents[i].face_id; Element_node_enumerator ene( _base, f); Point_2 p; get_nat_coor_in_element( i+1, ene, p); _subnode_normalized_nc[i] = Point_2S(p[0], p[1]); // Assign sub-node ID for each vertex in input mesh if (parent_type_of_subnode( i+1)==PARENT_VERTEX) { int vid = ene[_subnode_parents[i].edge_id]; _subnode_subID[ vid-1] = i+1; } } int nsf=size_of_subfaces(); _subface_nat_coors.resize( nsf); // Initialize _subface_nat_coors. Element_node_enumerator ene( base(), 1); for ( int i=0; i<nsf; ++i) { if ( ene.id() != _subface_parents[i]) { ene.next(); if ( ene.pane()==NULL || ene.id() != _subface_parents[i]) get_host_element_of_subface( i+1, ene); } Point_2 p; for ( int j=0; j<3; ++j) { get_nat_coor_in_element( _subfaces[i][j], ene, p); _subface_nat_coors[i][j] = Point_2S(p[0], p[1]); } } }
FileStream::FileStream(const char *path, const int mode): OpenedMode(mode) { if(mode == FileStream::MODE_WRITE) fp = fopen(path, "wb"); else fp = fopen(path, "rb"); if(!fp) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError())); } }
char *FileWrapper::get_line(char *buf_s, int buf_size) { char *ret; clearerr(fp); ret = ::fgets(buf_s, buf_size, fp); if(ferror(fp)) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error reading line in opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } return(ret); }
void FileStream::write(const void *data, uint64 count) { if(prev_was_write == 0) seek(0, SEEK_CUR); if(fwrite(data, 1, count, fp) != count) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error writing to opened file \"%s\": %s"), path_save.c_str(), ene.StrError())); } prev_was_write = 1; }
FileWrapper::FileWrapper(FileWrapper &original) : OpenedMode(original.OpenedMode) { int fd; int duped_fd; path_save = original.path_save; original.flush(); fd = fileno(original.fp); if(-1 == (duped_fd = dup(fd))) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error duping file descriptor: %s"), ene.StrError())); } if(!(fp = fdopen(duped_fd, (OpenedMode == MODE_READ) ? "rb" : "wb"))) { ErrnoHolder ene(errno); throw(MDFN_Error(ene.Errno(), _("Error during fdopen(): %s"), ene.StrError())); } }