boostfs::path findFileNothrow(const std::string& fileName, const std::string& searchPath) { boostfs::path filePath = fileName; if(filePath.empty()) return boostfs::path(); // First check whether the path is complete (often called "absolute") or is // relative to the current directory. if(filePath.is_complete() || *filePath.begin() == "." || *filePath.begin() == "..") { if(isRegularFile(filePath)) return filePath; else return boostfs::path(); } // In other cases, look for fileName in each path of the searchpath. TqPathsTokenizer paths(searchPath); for(TqPathsTokenizer::iterator i = paths.begin(), end = paths.end(); i != end; ++i) { boostfs::path candidate = (*i)/filePath; if(isRegularFile(candidate)) return candidate; } return boostfs::path(); }
void Filename::backup( const char * filename ) { if ( isRegularFile( filename ) > 0 ) { string backup = filename; backup += "~"; rename( filename, backup.c_str() ); } }
KFileMetaInfo KFileItem::metaInfo(bool autoget, int what) const { if ((isRegularFile() || isDir()) && autoget && !d->m_metaInfo.isValid()) { bool isLocalUrl; KUrl url(mostLocalUrl(isLocalUrl)); d->m_metaInfo = KFileMetaInfo(url.toLocalFile(), mimetype(), (KFileMetaInfo::What)what); } return d->m_metaInfo; }
// Open the BEDGRAPH file void BedGraphFile::Open() { if (bedGraphFile == "stdin") { _bedGraphStream = &cin; return; } // unzipped, regular else if ((isGzipFile(bedGraphFile) == false) && (isRegularFile(bedGraphFile) == true)) { _bedGraphStream = new ifstream(bedGraphFile.c_str(), ios::in); // open an ifstream ifstream bedg(bedGraphFile.c_str(), ios::in); // can we open the file? if ( !bedg ) { cerr << "Error: The requested bedgraph file (" << bedGraphFile << ") could not be opened. Exiting!" << endl; exit (1); } else { // if so, close it (this was just a test) bedg.close(); // now set a pointer to the stream so that we _bedGraphStream = new ifstream(bedGraphFile.c_str(), ios::in); } } else if ((isGzipFile(bedGraphFile) == true) && (isRegularFile(bedGraphFile) == true)) { igzstream bedg(bedGraphFile.c_str(), ios::in); if ( !bedg ) { cerr << "Error: The requested bedgraph file (" << bedGraphFile << ") could not be opened. Exiting!" << endl; exit (1); } else { // if so, close it (this was just a test) bedg.close(); // now set a pointer to the stream so that we _bedGraphStream = new igzstream(bedGraphFile.c_str(), ios::in); } } else { cerr << "Error: Unexpected file type (" << bedGraphFile << "). Exiting!" << endl; exit(1); } }
std::string extension() { std::string res; if( !isRegularFile() ) return std::string(""); size_t pointPos = _pathStr.find_last_of( '.' ); if( pointPos == std::string::npos ) return std::string(""); size_t extSize = _pathStr.size() - pointPos; res = _pathStr.substr( pointPos, extSize ); return res; }
size_t size() { if( exists() && isRegularFile() ) { #if defined(__LINUX__) struct stat st; stat(_pathStr.c_str(), &st); return st.st_size; #elif defined(__WINDOWS__) return 0; #endif } else return 0; }
/** * Check if a file located at a given path exists. */ int Exists(const char* path) { int exists = 0; if(isRegularFile(path)) { FILE* fp = fopen(path, "r"); if(fp != NULL) { exists = 1; } fclose(fp); } return exists; }
/** * Coverts a file located with a given path into a string * so that the contents can be worked with without the user needing * to do extra work using standard I/O. */ char* fileToStringSTDIO(const char* path) { char* string = calloc(1, sizeof(char *)); int ch = 0; int index = 0; //Determine whether or not the path is a file if(isRegularFile(path)) { //Attempt to open file FILE* fp = fopen(path, "r"); //If the file is NULL proceed no further if(fp != NULL) { //Define a string to hold contents of a file string = calloc(bufferSize(fp), sizeof(char *)); while((ch = fgetc(fp)) != EOF) { if((char) ch != '\n' || (char) ch != '\t' || (char) ch != '\v' || (char) ch != '\f') { //printf("char %d %c: \n", index, (char) ch); string[index] = (char) ch; index++; } else { string[index] = ' '; index++; } } } //Close the file fclose(fp); } // Return the file contents as a string. return string; }
char* fileToStringSYSTEM(const char* path) { char* string = calloc(1, sizeof(char *)); char* ch = calloc(1, sizeof(char *)); int index = 0; int fd; //Determine whether or not the path is a file if(isRegularFile(path)) { fd = open(path, O_RDONLY); //If file descriptor is that of an error, leave. if(fd != -1) { //Define a string to hold contents of a file string = calloc(bufferSizeSYS(fd), sizeof(char *)); while(read(fd, ch, 1) != 0) { if(ch[0] != '\n' || ch[0] != '\t' || ch[0] != '\v' || ch[0] != '\f') { string[index] = ch[0]; index++; } else { string[index] = ' '; index++; } } } //Close the file close(fd); } //Get rid of buffer free(ch); //Return the file contents as a string return string; }
void collectSourceFile(list * names, list * paths, const char *path) { DIR *dp; struct dirent *ep; char file[MAX_PATH_LEN]; dp = opendir(path); if (dp != NULL) { while (ep = readdir(dp)) { strcpy(file, path); strcat(file, "/"); strcat(file, ep->d_name); if (isSourceFile(ep->d_name) && isRegularFile(file)) { addNode(names, ep->d_name); addNode(paths, file); } } (void) closedir(dp); } else perror("Couldn't open the directory"); }
bool isLink(const bfs::path& path) { if (isSymlink(path)) { return true; } #ifdef MACOSX // aliases appear as regular files if (isRegularFile(path)) { FSRef ref; if (FSPathMakeRef((const UInt8*)path.c_str(), &ref, NULL) == noErr) { Boolean isAlias = false, isFolder = false; if (FSIsAliasFile(&ref, &isAlias, &isFolder) == noErr) { return isAlias; } } } #endif return false; }
void KwayMergeSort::OpenTempFiles() { for (size_t i = 0; i < _vTempFileNames.size(); ++i) { ifstream *file; if (isRegularFile(_vTempFileNames[i]) == true) { file = new ifstream(_vTempFileNames[i].c_str(), ios::in); } // gzipped //else if ((isGzipFile(_vTempFileNames[i]) == true) && (isRegularFile(_vTempFileNames[i]) == true)) { // file = new igzstream(_vTempFileNames[i].c_str(), ios::in); //} if (file->good() == true) { // add a pointer to the opened temp file to the list _vTempFiles.push_back(file); } else { cerr << "Unable to open temp file (" << _vTempFileNames[i] << "). I suspect a limit on number of open file handles. Exiting." << endl; exit(1); } } }
void testObj::test<1>(void) { ensure("normal file not marked regular", isRegularFile("base.t") ); }
bool FileUtils::isRegularFile(const string& path, const string& name) { string fileName = path + "/" + name; return isRegularFile(fileName); }
/** * Recursively visit all files / directories starting at the given directory. */ FileVisitor::VisitResult File::visitMe(const VisitOptions &options, unsigned maxDepth) const { FileVisitor::VisitResult vr = FileVisitor::VisitResult::CONTINUE; if (isSymlink()) { if (!options.followSymLinks) return vr; // visit the symlink location instead char buf[PATH_MAX]; ssize_t s = readlink(getFullname().c_str(), buf, sizeof(buf)); if (s <= 0) { Log::perror(getFullname()); throw std::runtime_error(std::string("Cannot read a symlink ") + getFullname()); } // terminate string buf[s] = 0; if (buf[0] == PATH_SEP_CHAR) { // absolute link location File linked(buf); return linked.visitMe(options, maxDepth); } else { // relative link location File linked(*this, buf); return linked.visitMe(options, maxDepth); } } if (isDirectory()) { if (maxDepth > 0) { vr = options.visitor.visitDirectory(*this); if (vr == FileVisitor::VisitResult::STOP) return vr; if (vr == FileVisitor::VisitResult::STOP_DIR) { options.visitor.afterVisitDirectory(*this); return FileVisitor::VisitResult::CONTINUE; } // recurse with max depth - 1 vr = recurseInto(options, maxDepth - 1); if (vr == FileVisitor::VisitResult::STOP) return vr; // what to do at end of of directory vr = options.visitor.afterVisitDirectory(*this); } } else { if (options.visitNonRegular || isRegularFile()) vr = options.visitor.visitFile(*this); else vr = FileVisitor::VisitResult::CONTINUE; } if (vr == FileVisitor::VisitResult::STOP) return vr; if (vr == FileVisitor::VisitResult::STOP_DIR) return FileVisitor::VisitResult::CONTINUE; return vr; }