Permalink
Browse files

Adds fileExists function

  • Loading branch information...
1 parent eb421b4 commit 47998865a11eefd5914c6170a296d9ac5e4edc95 @mothur-westcott mothur-westcott committed Feb 17, 2017
Showing with 31 additions and 9 deletions.
  1. +30 −8 source/mothurout.cpp
  2. +1 −1 source/mothurout.h
View
@@ -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; }
}
}
View
@@ -116,7 +116,7 @@ class MothurOut {
int openInputFile(string, ifstream&, string); //no error given
vector<bool> allGZFiles(vector<string>&);
vector<bool> 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&);

0 comments on commit 4799886

Please sign in to comment.