From 47998865a11eefd5914c6170a296d9ac5e4edc95 Mon Sep 17 00:00:00 2001 From: Sarah Westcott Date: Thu, 16 Feb 2017 19:32:45 -0500 Subject: [PATCH] Adds fileExists function --- source/mothurout.cpp | 38 ++++++++++++++++++++++++++++++-------- source/mothurout.h | 2 +- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/source/mothurout.cpp b/source/mothurout.cpp index 8900ad88..84b74172 100644 --- a/source/mothurout.cpp +++ b/source/mothurout.cpp @@ -205,24 +205,48 @@ void MothurOut::clearCurrentFiles() { exit(1); } } -/***********************************************************************/ + +/*********************************************************************************************/ +bool MothurOut::fileExists(string name) { + try { + bool exists = false; + name = getFullPathName(name); + +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + + ifstream in; + openInputFile(name, in, ""); + + //if this file exists + if (in) { in.close(); exists = true; } + +#else + DWORD attributes = GetFileAttributes(name.c_str()); + exists = (attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY)); +#endif + + return exists; + } + catch(exception& e) { + errorOut(e, "MothurOut", "clearCurrentFiles"); + exit(1); + } +} +/********************************************************************/ string MothurOut::findProgramPath(string programName){ try { string pPath = ""; //look in ./ //is this the programs path? - ifstream in5; string tempIn = "."; #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) tempIn += "/" + programName; #else tempIn += "\\" + programName; #endif - openInputFile(tempIn, in5, ""); - //if this file exists - if (in5) { in5.close(); pPath = getFullPathName(tempIn); if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } return pPath; } + if (fileExists(tempIn)) { pPath = getFullPathName(tempIn); if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } return pPath; } string envPath = getenv("PATH"); @@ -272,17 +296,15 @@ string MothurOut::findProgramPath(string programName){ if (debug) { mothurOut("[DEBUG]: looking in " + dirs[i] + " for " + programName + " \n"); } //is this the programs path? - ifstream in; string tempIn = dirs[i]; #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) tempIn += "/" + programName; #else tempIn += "\\" + programName; #endif - openInputFile(tempIn, in, ""); //if this file exists - if (in) { in.close(); pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; } + if (fileExists(tempIn)) { pPath = tempIn; if (debug) { mothurOut("[DEBUG]: found it, programPath = " + pPath + "\n"); } break; } } } diff --git a/source/mothurout.h b/source/mothurout.h index 902c311a..8e24a464 100644 --- a/source/mothurout.h +++ b/source/mothurout.h @@ -116,7 +116,7 @@ class MothurOut { int openInputFile(string, ifstream&, string); //no error given vector allGZFiles(vector&); vector isGZ(string); //checks existence and format - will fail for either or both. - + bool fileExists(string name); bool checkLocations(string&, string); //filename, inputDir. checks for file in ./, inputdir, default and mothur's exe location. Returns false if cant be found. If found completes name with location string getline(ifstream&); string getline(istringstream&);